[wp-trac] [WordPress Trac] #29560: Improved coverage and performance for WP_Meta_Query unit tests

WordPress Trac noreply at wordpress.org
Sat Sep 6 18:44:12 UTC 2014


#29560: Improved coverage and performance for WP_Meta_Query unit tests
--------------------------------+-----------------------------
 Reporter:  boonebgorges        |      Owner:
     Type:  enhancement         |     Status:  new
 Priority:  normal              |  Milestone:  Awaiting Review
Component:  Options, Meta APIs  |    Version:
 Severity:  normal              |   Keywords:
  Focuses:                      |
--------------------------------+-----------------------------
 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.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/29560>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list