From 831ec429e2b00b7f7182b79a0f0efa61a6044db5 Mon Sep 17 00:00:00 2001 From: dena Date: Thu, 4 Sep 2025 16:11:20 +0200 Subject: [PATCH 1/6] add dataProvider for test --- tests/Metrics/WikiMetricsTest.php | 219 +++++++++++++++++++++--------- 1 file changed, 158 insertions(+), 61 deletions(-) diff --git a/tests/Metrics/WikiMetricsTest.php b/tests/Metrics/WikiMetricsTest.php index 9a6b0f11f..d50f5ca20 100644 --- a/tests/Metrics/WikiMetricsTest.php +++ b/tests/Metrics/WikiMetricsTest.php @@ -168,7 +168,158 @@ public function testSaveNullForFailedRequestOfTriplesCount() { ]); } - public function testSavesEntityCountsCorrectly() { + static public function dummyDataProvider() + { + $item1 = [ + 'page_namespace' => 120, + 'page_is_redirect' => 0, + 'page_title' => 'foo', + 'page_random' => 0, + 'page_touched' => random_bytes(10), + 'page_latest' => 1, + 'page_len' => 2, + ]; + + $item2 = [ + 'page_namespace' => 120, + 'page_is_redirect' => 0, + 'page_title' => 'bar', + 'page_random' => 0, + 'page_touched' => random_bytes(10), + 'page_latest' => 0, + 'page_len' => 2, + ]; + + $property = [ + 'page_namespace' => 122, + 'page_is_redirect' => 0, + 'page_title' => 'foo', + 'page_random' => 0, + 'page_touched' => random_bytes(10), + 'page_latest' => 1, + 'page_len' => 2 + ]; + + $entitySchema = [ + 'page_namespace' => 640, + 'page_is_redirect' => 0, + 'page_title' => 'bar', + 'page_random' => 0, + 'page_touched' => random_bytes(10), + 'page_latest' => 1, + 'page_len' => 2, + ]; + + $entitySchemaRedirect = [ + 'page_namespace' => 640, + 'page_is_redirect' => 1, + 'page_title' => 'foo', + 'page_random' => 0, + 'page_touched' => random_bytes(10), + 'page_latest' => 1, + 'page_len' => 2, + ]; + + $lexeme = [ + 'page_namespace' => 146, + 'page_is_redirect' => 0, + 'page_title' => 'foo', + 'page_random' => 0, + 'page_touched' => random_bytes(10), + 'page_latest' => 1, + 'page_len' => 2, + ]; + + // all relevant data types + yield [ + 'expectedItemCount' => 2, + 'expectedPropertyCount' => 1, + 'expectedLexemeCount' => 1, + 'expectedEntitySchemaCount' => 1, + + 'pageData' => [ + $item1, + $item2, + $property, + $lexeme, + $entitySchema, + $entitySchemaRedirect, + ] + ]; + + // zero items + yield [ + 'expectedItemCount' => 0, + 'expectedPropertyCount' => 1, + 'expectedLexemeCount' => 1, + 'expectedEntitySchemaCount' => 1, + + 'pageData' => [ + // $item1, + // $item2, + $property, + $lexeme, + $entitySchema, + $entitySchemaRedirect, + ] + ]; + + // zero properties + yield [ + 'expectedItemCount' => 2, + 'expectedPropertyCount' => 0, + 'expectedLexemeCount' => 1, + 'expectedEntitySchemaCount' => 1, + + 'pageData' => [ + $item1, + $item2, + // $property, + $lexeme, + $entitySchema, + $entitySchemaRedirect, + ] + ]; + + // zero Lexemes + yield [ + 'expectedItemCount' => 2, + 'expectedPropertyCount' => 1, + 'expectedLexemeCount' => 0, + 'expectedEntitySchemaCount' => 1, + + 'pageData' => [ + $item1, + $item2, + $property, + // $lexeme, + $entitySchema, + $entitySchemaRedirect, + ] + ]; + + // zero EntitySchemas + yield [ + 'expectedItemCount' => 2, + 'expectedPropertyCount' => 1, + 'expectedLexemeCount' => 1, + 'expectedEntitySchemaCount' => 0, + + 'pageData' => [ + $item1, + $item2, + $property, + $lexeme, + // $entitySchema, + $entitySchemaRedirect, // should not count + ] + ]; + } + + /** + * @dataProvider dummyDataProvider + */ + public function testSavesEntityCountsCorrectly($expectedItemCount, $expectedPropertyCount, $expectedLexemeCount, $expectedEntitySchemaCount, $pageData) { $wiki = Wiki::factory()->create([ 'domain' => 'entitycounttest.wikibase.cloud', ]); @@ -191,66 +342,12 @@ public function testSavesEntityCountsCorrectly() { }); // Insert dummy data - DB::table($tablePage)->insert([ - [ - 'page_namespace' => 120, - 'page_is_redirect' => 0, - 'page_title' => 'foo', - 'page_random' => 0, - 'page_touched' => random_bytes(10), - 'page_latest' => 1, - 'page_len' => 2, - ], // item - [ - 'page_namespace' => 120, - 'page_is_redirect' => 0, - 'page_title' => 'bar', - 'page_random' => 0, - 'page_touched' => random_bytes(10), - 'page_latest' => 0, - 'page_len' => 2, - ], // item - [ - 'page_namespace' => 122, - 'page_is_redirect' => 0, - 'page_title' => 'foo', - 'page_random' => 0, - 'page_touched' => random_bytes(10), - 'page_latest' => 1, - 'page_len' => 2], // property - [ - 'page_namespace' => 640, - 'page_is_redirect' => 0, - 'page_title' => 'bar', - 'page_random' => 0, - 'page_touched' => random_bytes(10), - 'page_latest' => 1, - 'page_len' => 2, - ], // entity schema - [ - 'page_namespace' => 146, - 'page_is_redirect' => 0, - 'page_title' => 'foo', - 'page_random' => 0, - 'page_touched' => random_bytes(10), - 'page_latest' => 1, - 'page_len' => 2, - ], // lexeme - [ - 'page_namespace' => 640, - 'page_is_redirect' => 1, - 'page_title' => 'foo', - 'page_random' => 0, - 'page_touched' => random_bytes(10), - 'page_latest' => 1, - 'page_len' => 2, - ], // entity schema - ]); + DB::table($tablePage)->insert($pageData); WikiDailyMetrics::create([ 'id' => $wiki->id . '_' . now()->subDay()->toDateString(), 'wiki_id' => $wiki->id, 'date' => now()->subDay()->toDateString(), - 'pages' => 6, + 'pages' => count($pageData), 'is_deleted' => 0, ]); @@ -262,10 +359,10 @@ public function testSavesEntityCountsCorrectly() { $this->assertDatabaseHas('wiki_daily_metrics', [ 'wiki_id' => $wiki->id, - 'item_count' => 2, - 'property_count' => 1, - 'lexeme_count' => 1, - 'entity_schema_count' => 1, // the redirect should be ignored + 'item_count' => $expectedItemCount, + 'property_count' => $expectedPropertyCount, + 'lexeme_count' => $expectedLexemeCount, + 'entity_schema_count' => $expectedEntitySchemaCount, // redirects should be ignored ]); } } From f05d6870926d9d549c2359a819e95a9b2480a1ad Mon Sep 17 00:00:00 2001 From: dena Date: Thu, 4 Sep 2025 16:53:17 +0200 Subject: [PATCH 2/6] add the fix --- app/Metrics/App/WikiMetrics.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/Metrics/App/WikiMetrics.php b/app/Metrics/App/WikiMetrics.php index 7df603e93..8dc6661e0 100644 --- a/app/Metrics/App/WikiMetrics.php +++ b/app/Metrics/App/WikiMetrics.php @@ -199,10 +199,15 @@ private function getNumberOfEntities(): array { $conn = $manager->connection('mw'); $pdo = $conn->getPdo(); $result = $pdo->query($query)->fetchAll(PDO::FETCH_ASSOC); - if (count($result) === 0) { - return [120 => 0, 122 => 0, 146 => 0, 640 => 0]; - } - return array_column($result, 'count', 'namespace'); + $result = array_column($result, 'count', 'namespace'); + + // make sure the array keys are set + $result[120] ??= 0; + $result[122] ??= 0; + $result[146] ??= 0; + $result[640] ??= 0; + + return $result; } } From 903fbe0095b39716e44bcb2dd5de05c57a684bb6 Mon Sep 17 00:00:00 2001 From: dena Date: Thu, 4 Sep 2025 16:59:19 +0200 Subject: [PATCH 3/6] lintin --- tests/Metrics/WikiMetricsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Metrics/WikiMetricsTest.php b/tests/Metrics/WikiMetricsTest.php index d50f5ca20..91fbbbb4c 100644 --- a/tests/Metrics/WikiMetricsTest.php +++ b/tests/Metrics/WikiMetricsTest.php @@ -168,7 +168,7 @@ public function testSaveNullForFailedRequestOfTriplesCount() { ]); } - static public function dummyDataProvider() + public static function dummyDataProvider() { $item1 = [ 'page_namespace' => 120, From 186d157a58118eb8d1b90ac3aa697310eadebe60 Mon Sep 17 00:00:00 2001 From: dena Date: Thu, 4 Sep 2025 17:05:15 +0200 Subject: [PATCH 4/6] more linting --- tests/Metrics/WikiMetricsTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/Metrics/WikiMetricsTest.php b/tests/Metrics/WikiMetricsTest.php index 91fbbbb4c..e4360ff3e 100644 --- a/tests/Metrics/WikiMetricsTest.php +++ b/tests/Metrics/WikiMetricsTest.php @@ -168,8 +168,7 @@ public function testSaveNullForFailedRequestOfTriplesCount() { ]); } - public static function dummyDataProvider() - { + public static function dummyDataProvider() { $item1 = [ 'page_namespace' => 120, 'page_is_redirect' => 0, @@ -197,7 +196,7 @@ public static function dummyDataProvider() 'page_random' => 0, 'page_touched' => random_bytes(10), 'page_latest' => 1, - 'page_len' => 2 + 'page_len' => 2, ]; $entitySchema = [ From 718ce143d88cc84cced53c5ae10a8c43b83b5294 Mon Sep 17 00:00:00 2001 From: dena Date: Thu, 4 Sep 2025 17:10:58 +0200 Subject: [PATCH 5/6] even more linting --- tests/Metrics/WikiMetricsTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Metrics/WikiMetricsTest.php b/tests/Metrics/WikiMetricsTest.php index e4360ff3e..03f548ce4 100644 --- a/tests/Metrics/WikiMetricsTest.php +++ b/tests/Metrics/WikiMetricsTest.php @@ -243,7 +243,7 @@ public static function dummyDataProvider() { $lexeme, $entitySchema, $entitySchemaRedirect, - ] + ], ]; // zero items @@ -260,7 +260,7 @@ public static function dummyDataProvider() { $lexeme, $entitySchema, $entitySchemaRedirect, - ] + ], ]; // zero properties @@ -277,7 +277,7 @@ public static function dummyDataProvider() { $lexeme, $entitySchema, $entitySchemaRedirect, - ] + ], ]; // zero Lexemes @@ -294,7 +294,7 @@ public static function dummyDataProvider() { // $lexeme, $entitySchema, $entitySchemaRedirect, - ] + ], ]; // zero EntitySchemas @@ -311,7 +311,7 @@ public static function dummyDataProvider() { $lexeme, // $entitySchema, $entitySchemaRedirect, // should not count - ] + ], ]; } From e163d9e52a5397468614f187561d1bd00c9045dc Mon Sep 17 00:00:00 2001 From: dena Date: Thu, 4 Sep 2025 17:19:54 +0200 Subject: [PATCH 6/6] linting --- tests/Metrics/WikiMetricsTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Metrics/WikiMetricsTest.php b/tests/Metrics/WikiMetricsTest.php index 03f548ce4..a3df4e7b8 100644 --- a/tests/Metrics/WikiMetricsTest.php +++ b/tests/Metrics/WikiMetricsTest.php @@ -188,7 +188,7 @@ public static function dummyDataProvider() { 'page_latest' => 0, 'page_len' => 2, ]; - + $property = [ 'page_namespace' => 122, 'page_is_redirect' => 0, @@ -245,14 +245,14 @@ public static function dummyDataProvider() { $entitySchemaRedirect, ], ]; - + // zero items yield [ 'expectedItemCount' => 0, 'expectedPropertyCount' => 1, 'expectedLexemeCount' => 1, 'expectedEntitySchemaCount' => 1, - + 'pageData' => [ // $item1, // $item2, @@ -269,7 +269,7 @@ public static function dummyDataProvider() { 'expectedPropertyCount' => 0, 'expectedLexemeCount' => 1, 'expectedEntitySchemaCount' => 1, - + 'pageData' => [ $item1, $item2, @@ -286,7 +286,7 @@ public static function dummyDataProvider() { 'expectedPropertyCount' => 1, 'expectedLexemeCount' => 0, 'expectedEntitySchemaCount' => 1, - + 'pageData' => [ $item1, $item2, @@ -303,7 +303,7 @@ public static function dummyDataProvider() { 'expectedPropertyCount' => 1, 'expectedLexemeCount' => 1, 'expectedEntitySchemaCount' => 0, - + 'pageData' => [ $item1, $item2,