Skip to content

Commit 831ec42

Browse files
committed
add dataProvider for test
1 parent b0f9b4c commit 831ec42

1 file changed

Lines changed: 158 additions & 61 deletions

File tree

tests/Metrics/WikiMetricsTest.php

Lines changed: 158 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,158 @@ public function testSaveNullForFailedRequestOfTriplesCount() {
168168
]);
169169
}
170170

171-
public function testSavesEntityCountsCorrectly() {
171+
static public function dummyDataProvider()
172+
{
173+
$item1 = [
174+
'page_namespace' => 120,
175+
'page_is_redirect' => 0,
176+
'page_title' => 'foo',
177+
'page_random' => 0,
178+
'page_touched' => random_bytes(10),
179+
'page_latest' => 1,
180+
'page_len' => 2,
181+
];
182+
183+
$item2 = [
184+
'page_namespace' => 120,
185+
'page_is_redirect' => 0,
186+
'page_title' => 'bar',
187+
'page_random' => 0,
188+
'page_touched' => random_bytes(10),
189+
'page_latest' => 0,
190+
'page_len' => 2,
191+
];
192+
193+
$property = [
194+
'page_namespace' => 122,
195+
'page_is_redirect' => 0,
196+
'page_title' => 'foo',
197+
'page_random' => 0,
198+
'page_touched' => random_bytes(10),
199+
'page_latest' => 1,
200+
'page_len' => 2
201+
];
202+
203+
$entitySchema = [
204+
'page_namespace' => 640,
205+
'page_is_redirect' => 0,
206+
'page_title' => 'bar',
207+
'page_random' => 0,
208+
'page_touched' => random_bytes(10),
209+
'page_latest' => 1,
210+
'page_len' => 2,
211+
];
212+
213+
$entitySchemaRedirect = [
214+
'page_namespace' => 640,
215+
'page_is_redirect' => 1,
216+
'page_title' => 'foo',
217+
'page_random' => 0,
218+
'page_touched' => random_bytes(10),
219+
'page_latest' => 1,
220+
'page_len' => 2,
221+
];
222+
223+
$lexeme = [
224+
'page_namespace' => 146,
225+
'page_is_redirect' => 0,
226+
'page_title' => 'foo',
227+
'page_random' => 0,
228+
'page_touched' => random_bytes(10),
229+
'page_latest' => 1,
230+
'page_len' => 2,
231+
];
232+
233+
// all relevant data types
234+
yield [
235+
'expectedItemCount' => 2,
236+
'expectedPropertyCount' => 1,
237+
'expectedLexemeCount' => 1,
238+
'expectedEntitySchemaCount' => 1,
239+
240+
'pageData' => [
241+
$item1,
242+
$item2,
243+
$property,
244+
$lexeme,
245+
$entitySchema,
246+
$entitySchemaRedirect,
247+
]
248+
];
249+
250+
// zero items
251+
yield [
252+
'expectedItemCount' => 0,
253+
'expectedPropertyCount' => 1,
254+
'expectedLexemeCount' => 1,
255+
'expectedEntitySchemaCount' => 1,
256+
257+
'pageData' => [
258+
// $item1,
259+
// $item2,
260+
$property,
261+
$lexeme,
262+
$entitySchema,
263+
$entitySchemaRedirect,
264+
]
265+
];
266+
267+
// zero properties
268+
yield [
269+
'expectedItemCount' => 2,
270+
'expectedPropertyCount' => 0,
271+
'expectedLexemeCount' => 1,
272+
'expectedEntitySchemaCount' => 1,
273+
274+
'pageData' => [
275+
$item1,
276+
$item2,
277+
// $property,
278+
$lexeme,
279+
$entitySchema,
280+
$entitySchemaRedirect,
281+
]
282+
];
283+
284+
// zero Lexemes
285+
yield [
286+
'expectedItemCount' => 2,
287+
'expectedPropertyCount' => 1,
288+
'expectedLexemeCount' => 0,
289+
'expectedEntitySchemaCount' => 1,
290+
291+
'pageData' => [
292+
$item1,
293+
$item2,
294+
$property,
295+
// $lexeme,
296+
$entitySchema,
297+
$entitySchemaRedirect,
298+
]
299+
];
300+
301+
// zero EntitySchemas
302+
yield [
303+
'expectedItemCount' => 2,
304+
'expectedPropertyCount' => 1,
305+
'expectedLexemeCount' => 1,
306+
'expectedEntitySchemaCount' => 0,
307+
308+
'pageData' => [
309+
$item1,
310+
$item2,
311+
$property,
312+
$lexeme,
313+
// $entitySchema,
314+
$entitySchemaRedirect, // should not count
315+
]
316+
];
317+
}
318+
319+
/**
320+
* @dataProvider dummyDataProvider
321+
*/
322+
public function testSavesEntityCountsCorrectly($expectedItemCount, $expectedPropertyCount, $expectedLexemeCount, $expectedEntitySchemaCount, $pageData) {
172323
$wiki = Wiki::factory()->create([
173324
'domain' => 'entitycounttest.wikibase.cloud',
174325
]);
@@ -191,66 +342,12 @@ public function testSavesEntityCountsCorrectly() {
191342
});
192343

193344
// Insert dummy data
194-
DB::table($tablePage)->insert([
195-
[
196-
'page_namespace' => 120,
197-
'page_is_redirect' => 0,
198-
'page_title' => 'foo',
199-
'page_random' => 0,
200-
'page_touched' => random_bytes(10),
201-
'page_latest' => 1,
202-
'page_len' => 2,
203-
], // item
204-
[
205-
'page_namespace' => 120,
206-
'page_is_redirect' => 0,
207-
'page_title' => 'bar',
208-
'page_random' => 0,
209-
'page_touched' => random_bytes(10),
210-
'page_latest' => 0,
211-
'page_len' => 2,
212-
], // item
213-
[
214-
'page_namespace' => 122,
215-
'page_is_redirect' => 0,
216-
'page_title' => 'foo',
217-
'page_random' => 0,
218-
'page_touched' => random_bytes(10),
219-
'page_latest' => 1,
220-
'page_len' => 2], // property
221-
[
222-
'page_namespace' => 640,
223-
'page_is_redirect' => 0,
224-
'page_title' => 'bar',
225-
'page_random' => 0,
226-
'page_touched' => random_bytes(10),
227-
'page_latest' => 1,
228-
'page_len' => 2,
229-
], // entity schema
230-
[
231-
'page_namespace' => 146,
232-
'page_is_redirect' => 0,
233-
'page_title' => 'foo',
234-
'page_random' => 0,
235-
'page_touched' => random_bytes(10),
236-
'page_latest' => 1,
237-
'page_len' => 2,
238-
], // lexeme
239-
[
240-
'page_namespace' => 640,
241-
'page_is_redirect' => 1,
242-
'page_title' => 'foo',
243-
'page_random' => 0,
244-
'page_touched' => random_bytes(10),
245-
'page_latest' => 1,
246-
'page_len' => 2,
247-
], // entity schema
248-
]);
345+
DB::table($tablePage)->insert($pageData);
249346
WikiDailyMetrics::create([
250347
'id' => $wiki->id . '_' . now()->subDay()->toDateString(),
251348
'wiki_id' => $wiki->id,
252349
'date' => now()->subDay()->toDateString(),
253-
'pages' => 6,
350+
'pages' => count($pageData),
254351
'is_deleted' => 0,
255352
]);
256353

@@ -262,10 +359,10 @@ public function testSavesEntityCountsCorrectly() {
262359

263360
$this->assertDatabaseHas('wiki_daily_metrics', [
264361
'wiki_id' => $wiki->id,
265-
'item_count' => 2,
266-
'property_count' => 1,
267-
'lexeme_count' => 1,
268-
'entity_schema_count' => 1, // the redirect should be ignored
362+
'item_count' => $expectedItemCount,
363+
'property_count' => $expectedPropertyCount,
364+
'lexeme_count' => $expectedLexemeCount,
365+
'entity_schema_count' => $expectedEntitySchemaCount, // redirects should be ignored
269366
]);
270367
}
271368
}

0 commit comments

Comments
 (0)