Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions app/Metrics/App/WikiMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
218 changes: 157 additions & 61 deletions tests/Metrics/WikiMetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
Expand All @@ -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,
]);

Expand All @@ -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
]);
}
}
Loading