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 | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 4.1 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Options, Meta APIs | Keywords: | |
Focuses: | Cc: |
Description
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:
- 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-intensivemeta_query
tests. - 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.) - 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.
- 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)
Change History (4)
This ticket was mentioned in IRC in #wordpress-dev by boonebgorges. View the logs.
10 years ago
Note: See
TracTickets for help on using
tickets.
In 29799: