Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use get_num_queries in tests. #4438

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 6 additions & 12 deletions tests/phpunit/tests/bookmark/getBookmarks.php
Expand Up @@ -5,8 +5,6 @@
*/
class Tests_Bookmark_GetBookmarks extends WP_UnitTestCase {
public function test_should_hit_cache() {
global $wpdb;

$bookmarks = self::factory()->bookmark->create_many( 2 );

$found1 = get_bookmarks(
Expand All @@ -15,7 +13,7 @@ public function test_should_hit_cache() {
)
);

$num_queries = $wpdb->num_queries;
$num_queries = get_num_queries();

$found2 = get_bookmarks(
array(
Expand All @@ -24,12 +22,10 @@ public function test_should_hit_cache() {
);

$this->assertSameSets( $found1, $found2 );
$this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertSame( $num_queries, get_num_queries() );
}

public function test_adding_bookmark_should_bust_get_bookmarks_cache() {
global $wpdb;

$bookmarks = self::factory()->bookmark->create_many( 2 );

// Prime cache.
Expand All @@ -39,7 +35,7 @@ public function test_adding_bookmark_should_bust_get_bookmarks_cache() {
)
);

$num_queries = $wpdb->num_queries;
$num_queries = get_num_queries();

$bookmarks[] = wp_insert_link(
array(
Expand All @@ -55,15 +51,13 @@ public function test_adding_bookmark_should_bust_get_bookmarks_cache() {
);

$this->assertEqualSets( $bookmarks, wp_list_pluck( $found2, 'link_id' ) );
$this->assertGreaterThan( $num_queries, $wpdb->num_queries );
$this->assertGreaterThan( $num_queries, get_num_queries() );
}

/**
* @ticket 18356
*/
public function test_orderby_rand_should_not_be_cached() {
global $wpdb;

$bookmarks = self::factory()->bookmark->create_many( 2 );

$found1 = get_bookmarks(
Expand All @@ -72,7 +66,7 @@ public function test_orderby_rand_should_not_be_cached() {
)
);

$num_queries = $wpdb->num_queries;
$num_queries = get_num_queries();

$found2 = get_bookmarks(
array(
Expand All @@ -82,7 +76,7 @@ public function test_orderby_rand_should_not_be_cached() {

// Equal sets != same order.
$this->assertEqualSets( $found1, $found2 );
$this->assertGreaterThan( $num_queries, $wpdb->num_queries );
$this->assertGreaterThan( $num_queries, get_num_queries() );
}

public function test_exclude_param_gets_properly_parsed_as_list() {
Expand Down
12 changes: 4 additions & 8 deletions tests/phpunit/tests/comment/getPageOfComment.php
Expand Up @@ -100,27 +100,23 @@ public function test_type_pings() {
* @ticket 11334
*/
public function test_subsequent_calls_should_hit_cache() {
global $wpdb;

$p = self::factory()->post->create();
$c = self::factory()->comment->create( array( 'comment_post_ID' => $p ) );

// Prime cache.
$page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );

$num_queries = $wpdb->num_queries;
$num_queries = get_num_queries();
$page_2 = get_page_of_comment( $c, array( 'per_page' => 3 ) );

$this->assertSame( $page_1, $page_2 );
$this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertSame( $num_queries, get_num_queries() );
}

/**
* @ticket 11334
*/
public function test_cache_hits_should_be_sensitive_to_comment_type() {
global $wpdb;

$p = self::factory()->post->create();
$comment = self::factory()->comment->create(
array(
Expand Down Expand Up @@ -151,7 +147,7 @@ public function test_cache_hits_should_be_sensitive_to_comment_type() {
);
$this->assertSame( 2, $page_trackbacks );

$num_queries = $wpdb->num_queries;
$num_queries = get_num_queries();
$page_comments = get_page_of_comment(
$comment,
array(
Expand All @@ -161,7 +157,7 @@ public function test_cache_hits_should_be_sensitive_to_comment_type() {
);
$this->assertSame( 1, $page_comments );

$this->assertNotEquals( $num_queries, $wpdb->num_queries );
$this->assertNotEquals( $num_queries, get_num_queries() );
}

/**
Expand Down
46 changes: 17 additions & 29 deletions tests/phpunit/tests/comment/metaCache.php
Expand Up @@ -12,8 +12,6 @@ class Tests_Comment_MetaCache extends WP_UnitTestCase {
* @covers ::update_comment_meta
*/
public function test_update_comment_meta_cache_should_default_to_true() {
global $wpdb;

$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$comment_ids = self::factory()->comment->create_post_comments( $p, 3 );

Expand All @@ -30,12 +28,12 @@ public function test_update_comment_meta_cache_should_default_to_true() {
)
);

$num_queries = $wpdb->num_queries;
$num_queries = get_num_queries();
foreach ( $comment_ids as $cid ) {
get_comment_meta( $cid, 'foo', 'bar' );
}

$this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertSame( $num_queries, get_num_queries() );
}

/**
Expand All @@ -44,8 +42,6 @@ public function test_update_comment_meta_cache_should_default_to_true() {
* @covers ::update_comment_meta
*/
public function test_update_comment_meta_cache_true() {
global $wpdb;

$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$comment_ids = self::factory()->comment->create_post_comments( $p, 3 );

Expand All @@ -63,12 +59,12 @@ public function test_update_comment_meta_cache_true() {
)
);

$num_queries = $wpdb->num_queries;
$num_queries = get_num_queries();
foreach ( $comment_ids as $cid ) {
get_comment_meta( $cid, 'foo', 'bar' );
}

$this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertSame( $num_queries, get_num_queries() );
}

/**
Expand All @@ -77,8 +73,6 @@ public function test_update_comment_meta_cache_true() {
* @covers ::update_comment_meta
*/
public function test_update_comment_meta_cache_false() {
global $wpdb;

$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$comment_ids = self::factory()->comment->create_post_comments( $p, 3 );

Expand All @@ -93,12 +87,12 @@ public function test_update_comment_meta_cache_false() {
)
);

$num_queries = $wpdb->num_queries;
$num_queries = get_num_queries();
foreach ( $comment_ids as $cid ) {
get_comment_meta( $cid, 'foo', 'bar' );
}

$this->assertSame( $num_queries + 3, $wpdb->num_queries );
$this->assertSame( $num_queries + 3, get_num_queries() );
}

/**
Expand All @@ -107,8 +101,6 @@ public function test_update_comment_meta_cache_false() {
* @covers ::get_comment_meta
*/
public function test_comment_meta_should_be_lazy_loaded_for_all_comments_in_comments_template() {
global $wpdb;

$p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$comment_ids = self::factory()->comment->create_post_comments( $p, 3 );

Expand All @@ -126,14 +118,14 @@ public function test_comment_meta_should_be_lazy_loaded_for_all_comments_in_comm
$cform = get_echo( 'comments_template' );

// First request will hit the database.
$num_queries = $wpdb->num_queries;
$num_queries = get_num_queries();
get_comment_meta( $comment_ids[0], 'sauce' );
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
$this->assertSame( $num_queries + 1, get_num_queries() );

// Second and third requests should be in cache.
get_comment_meta( $comment_ids[1], 'sauce' );
get_comment_meta( $comment_ids[2], 'sauce' );
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
$this->assertSame( $num_queries + 1, get_num_queries() );
}
}
}
Expand All @@ -144,8 +136,6 @@ public function test_comment_meta_should_be_lazy_loaded_for_all_comments_in_comm
* @covers ::get_comment_meta
*/
public function test_comment_meta_should_be_lazy_loaded_in_comment_feed_queries() {
global $wpdb;

$posts = self::factory()->post->create_many( 2, array( 'post_status' => 'publish' ) );

$now = time();
Expand Down Expand Up @@ -173,19 +163,19 @@ public function test_comment_meta_should_be_lazy_loaded_in_comment_feed_queries(
);

// First comment will cause the cache to be primed.
$num_queries = $wpdb->num_queries;
$num_queries = get_num_queries();
$this->assertSame( 'bar', get_comment_meta( $comments[0], 'foo', 'bar' ) );
$num_queries++;
$this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertSame( $num_queries, get_num_queries() );

// Second comment from the results should not cause more queries.
$this->assertSame( 'bar', get_comment_meta( $comments[1], 'foo', 'bar' ) );
$this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertSame( $num_queries, get_num_queries() );

// A comment from outside the results will not be primed.
$this->assertSame( 'bar', get_comment_meta( $comments[4], 'foo', 'bar' ) );
$num_queries++;
$this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertSame( $num_queries, get_num_queries() );
}

/**
Expand All @@ -194,8 +184,6 @@ public function test_comment_meta_should_be_lazy_loaded_in_comment_feed_queries(
* @covers ::get_comment_meta
*/
public function test_comment_meta_should_be_lazy_loaded_in_single_post_comment_feed_queries() {
global $wpdb;

$posts = self::factory()->post->create_many( 2, array( 'post_status' => 'publish' ) );

$now = time();
Expand Down Expand Up @@ -224,19 +212,19 @@ public function test_comment_meta_should_be_lazy_loaded_in_single_post_comment_f
);

// First comment will cause the cache to be primed.
$num_queries = $wpdb->num_queries;
$num_queries = get_num_queries();
$this->assertSame( 'bar', get_comment_meta( $comments[0], 'foo', 'bar' ) );
$num_queries++;
$this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertSame( $num_queries, get_num_queries() );

// Second comment from the results should not cause more queries.
$this->assertSame( 'bar', get_comment_meta( $comments[1], 'foo', 'bar' ) );
$this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertSame( $num_queries, get_num_queries() );

// A comment from outside the results will not be primed.
$this->assertSame( 'bar', get_comment_meta( $comments[4], 'foo', 'bar' ) );
$num_queries++;
$this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertSame( $num_queries, get_num_queries() );
}

/**
Expand Down