Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#29560 closed enhancement (fixed)

Improved coverage and performance for WP_Meta_Query unit tests

Reported by: boonebgorges's profile boonebgorges Owned by: boonebgorges's profile boonebgorges
Milestone: 4.1 Priority: normal
Severity: normal Version:
Component: Options, Meta APIs Keywords:
Focuses: Cc:


Existing unit tests for WP_Meta_Query (and the meta_query parameter for WP_Query) are spotty, inconsistent, and in some cases, imprecise. Not having proper unit tests for this API makes it nearly impossible to do the kinds of refactoring that we all know, deep in our tender hearts, is possible.

The attached patch overhauls the unit tests for WP_Meta_Query and meta_query. A summary of the changes:

  1. In tests/meta/query.php, I've added a bunch of tests that bring us pretty close to complete coverage for the WP_Meta_Query class. There are now tests that describe all possible errors and transformations that can be performed by the class, given a variety of different kinds of inputs. It's tough to write exhaustive tests for get_sql() that aren't too specific to the idiosyncracies of our SQL syntax, but I did try to write as many non-fragile tests as possible before resorting to the more resource-intensive meta_query tests.
  2. In tests/post/query.php, I've added a number of tests that cover basic uses of meta_query (existing tests covered primarily the more complex cases). This includes tests verifying all possible 'compare' values, as well as the fallback values for 'key', 'value', and 'compare' in individual clauses. It's not practical (and maybe not even possible) to write tests for every possible permutation of query arguments, so for the time being I didn't add anything complex. (I do think there are some funky nesting bugs with, eg, multiple NOT EXISTS queries, but for this ticket I wanted to stick to passing tests.)
  3. In tests/post/query.php, I refactored the existing tests to be more focused. They were doing full-fledged WP_Querys, and then doing a bunch of comparisons of WP_Post objects. This is unnecessary. These tests are meant to verify that the 'meta_query' argument limits results to the proper posts, so the only thing we need to check is the post IDs. Passing 'update_post_meta_cache=false', 'update_term_meta_cache=false', and 'fields=ids' to each WP_Query saves a couple of SQL queries in each test, we save many assertions by not testing, eg, whether the results are WP_Post objects.
  4. Added 'public' access modifiers to each test method.

The entire test suite passes with my patch. I've added a couple dozen tests & assertions, but because of refactoring of existing classes, the whole suite runs in about the same total time as before.

Attachments (1)

29560.patch (36.8 KB) - added by boonebgorges 10 years ago.

Download all attachments as: .zip

Change History (4)

10 years ago

This ticket was mentioned in IRC in #wordpress-dev by boonebgorges. View the logs.

10 years ago

#2 @boonebgorges
10 years ago

  • Milestone changed from Awaiting Review to 4.1
  • Owner set to boonebgorges
  • Status changed from new to assigned

#3 @boonebgorges
10 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 29799:

Improve unit tests related to WP_Meta_Query.

  • More complete test coverage for publicly available methods of WP_Meta_Query.
  • Move tests that rely on WP_Query (via the meta_query parameter) to tests/post/query.php.
  • Better coverage for basic use cases of 'meta_query', including all values of 'compare' and default values of 'key', 'value', and 'compare'.
  • Improve performance for tests that run WP_Query, by retrieving only post IDs and not prefetching postmeta and post terms.
  • Add 'public' visibility keywords to test methods.
  • Whitespace cleanup.

Fixes #29560

Note: See TracTickets for help on using tickets.