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; } } diff --git a/tests/Metrics/WikiMetricsTest.php b/tests/Metrics/WikiMetricsTest.php index 9a6b0f11f..a3df4e7b8 100644 --- a/tests/Metrics/WikiMetricsTest.php +++ b/tests/Metrics/WikiMetricsTest.php @@ -168,7 +168,157 @@ public function testSaveNullForFailedRequestOfTriplesCount() { ]); } - public function testSavesEntityCountsCorrectly() { + public static 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 +341,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 +358,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 ]); } }