|
9 | 9 | use App\WikiDailyMetrics; |
10 | 10 | use App\WikiDb; |
11 | 11 | use Carbon\Carbon; |
| 12 | +use Illuminate\Database\Schema\Blueprint; |
12 | 13 | use Illuminate\Foundation\Testing\RefreshDatabase; |
13 | 14 | use Illuminate\Support\Facades\DB; |
14 | 15 | use Illuminate\Support\Facades\Http; |
| 16 | +use Illuminate\Support\Facades\Schema; |
15 | 17 | use Tests\TestCase; |
16 | 18 |
|
17 | 19 | class WikiMetricsTest extends TestCase { |
@@ -165,4 +167,107 @@ public function testSaveNullForFailedRequestOfTriplesCount() { |
165 | 167 | 'number_of_triples' => null, |
166 | 168 | ]); |
167 | 169 | } |
| 170 | + |
| 171 | + public function testSavesEntityCountsCorrectly() |
| 172 | + { |
| 173 | + $wiki = Wiki::factory()->create([ |
| 174 | + 'domain' => 'entitycounttest.wikibase.cloud' |
| 175 | + ]); |
| 176 | + |
| 177 | + $wikiDb = WikiDb::first(); |
| 178 | + $wikiDb->update(['wiki_id' => $wiki->id]); |
| 179 | + |
| 180 | + $tablePage = $wikiDb->name . '.' . $wikiDb->prefix . '_page'; |
| 181 | + |
| 182 | + Schema::dropIfExists($tablePage); |
| 183 | + Schema::create($tablePage, function (Blueprint $table) { |
| 184 | + $table->increments('page_id'); |
| 185 | + $table->integer('page_namespace'); |
| 186 | + $table->boolean('page_is_redirect')->default(0); |
| 187 | + $table->string('page_title', 255); |
| 188 | + $table->double('page_random'); |
| 189 | + $table->binary('page_touched'); |
| 190 | + $table->integer('page_latest'); |
| 191 | + $table->integer('page_len'); |
| 192 | + }); |
| 193 | + |
| 194 | + // Insert dummy data |
| 195 | + DB::table($tablePage)->insert([ |
| 196 | + [ |
| 197 | + 'page_namespace' => 120, |
| 198 | + 'page_is_redirect' => 0, |
| 199 | + 'page_title' => 'foo', |
| 200 | + 'page_random' => 0, |
| 201 | + 'page_touched' => random_bytes(10), |
| 202 | + 'page_latest' => 1, |
| 203 | + 'page_len' => 2 |
| 204 | + ], // item |
| 205 | + [ |
| 206 | + 'page_namespace' => 120, |
| 207 | + 'page_is_redirect' => 0, |
| 208 | + 'page_title' => 'bar', |
| 209 | + 'page_random' => 0, |
| 210 | + 'page_touched' => random_bytes(10), |
| 211 | + 'page_latest' => 0, |
| 212 | + 'page_len' => 2 |
| 213 | + ], // item |
| 214 | + [ |
| 215 | + 'page_namespace' => 122, |
| 216 | + 'page_is_redirect' => 0, |
| 217 | + 'page_title' => 'foo', |
| 218 | + 'page_random' => 0, |
| 219 | + 'page_touched' => random_bytes(10), |
| 220 | + 'page_latest' => 1, |
| 221 | + 'page_len' => 2] |
| 222 | + , // property |
| 223 | + [ |
| 224 | + 'page_namespace' => 640, |
| 225 | + 'page_is_redirect' => 0, |
| 226 | + 'page_title' => 'bar', |
| 227 | + 'page_random' => 0, |
| 228 | + 'page_touched' => random_bytes(10), |
| 229 | + 'page_latest' => 1, |
| 230 | + 'page_len' => 2 |
| 231 | + ], // entity schema |
| 232 | + [ |
| 233 | + 'page_namespace' => 146, |
| 234 | + 'page_is_redirect' => 0, |
| 235 | + 'page_title' => 'foo', |
| 236 | + 'page_random' => 0, |
| 237 | + 'page_touched' => random_bytes(10), |
| 238 | + 'page_latest' => 1, |
| 239 | + 'page_len' => 2 |
| 240 | + ], // lexeme |
| 241 | + [ |
| 242 | + 'page_namespace' => 640, |
| 243 | + 'page_is_redirect' => 1, |
| 244 | + 'page_title' => 'foo', |
| 245 | + 'page_random' => 0, |
| 246 | + 'page_touched' => random_bytes(10), |
| 247 | + 'page_latest' => 1, |
| 248 | + 'page_len' => 2 |
| 249 | + ], // entity schema |
| 250 | + ]); |
| 251 | + WikiDailyMetrics::create([ |
| 252 | + 'id' => $wiki->id . '_' . now()->subDay()->toDateString(), |
| 253 | + 'wiki_id' => $wiki->id, |
| 254 | + 'date' => now()->subDay()->toDateString(), |
| 255 | + 'pages' => 6, |
| 256 | + 'is_deleted' => 0 |
| 257 | + ]); |
| 258 | + |
| 259 | + (new WikiMetrics())->saveMetrics($wiki); |
| 260 | + |
| 261 | + //clean up after the test |
| 262 | + $wiki->forceDelete(); |
| 263 | + Schema::dropIfExists($tablePage); |
| 264 | + |
| 265 | + $this->assertDatabaseHas('wiki_daily_metrics', [ |
| 266 | + 'wiki_id' => $wiki->id, |
| 267 | + 'item_count' => 2, |
| 268 | + 'property_count' => 1, |
| 269 | + 'lexeme_count' => 1, |
| 270 | + 'entity_schema_count' => 1 // the redirect should be ignored |
| 271 | + ]); |
| 272 | + } |
168 | 273 | } |
0 commit comments