|
8 | 8 | use App\Models\ImportMeta; |
9 | 9 | use App\Jobs\ImportCSV; |
10 | 10 | use App\Models\User; |
| 11 | +use App\Models\Mismatch; |
11 | 12 | use Throwable; |
12 | 13 |
|
13 | 14 | class ImportCSVTest extends TestCase |
14 | 15 | { |
15 | 16 | use RefreshDatabase; |
16 | 17 | /** |
17 | | - * Ensure import persists mismatches to database |
| 18 | + * Ensure import persists mismatches to empty database |
18 | 19 | */ |
19 | | - public function test_creates_mismatches(): void |
| 20 | + public function test_creates_mismatches_in_empty_db(): void |
20 | 21 | { |
21 | 22 | $filename = 'creates-mismatches.csv'; |
22 | 23 | $user = User::factory()->uploader()->create(); |
@@ -58,6 +59,193 @@ public function test_creates_mismatches(): void |
58 | 59 | } |
59 | 60 | } |
60 | 61 |
|
| 62 | + /** |
| 63 | + * Ensure reupload doesn't import duplicated row with reviewed state |
| 64 | + */ |
| 65 | + public function test_doesnt_import_mismatches_when_row_exists_and_is_reviewed(): void |
| 66 | + { |
| 67 | + $filename = 'creates-mismatches.csv'; |
| 68 | + $user = User::factory()->uploader()->create(); |
| 69 | + $reupload_import = ImportMeta::factory()->for($user)->create([ |
| 70 | + 'filename' => $filename |
| 71 | + ]); |
| 72 | + |
| 73 | + $already_in_db_import = ImportMeta::factory()->for($user); |
| 74 | + |
| 75 | + Mismatch::factory() |
| 76 | + ->for($already_in_db_import)->create([ |
| 77 | + 'statement_guid' => 'Q184746$7814880A-A6EF-40EC-885E-F46DD58C8DC5', |
| 78 | + 'item_id' => 'Q184746', |
| 79 | + 'property_id' => 'P569', |
| 80 | + 'meta_wikidata_value' => 'Q12138', |
| 81 | + 'wikidata_value' => '3 April 1934', |
| 82 | + 'external_value' => '1934-04-03', |
| 83 | + 'external_url' => 'https://d-nb.info/gnd/119004453', |
| 84 | + 'review_status' => 'both', |
| 85 | + 'type' => 'statement' |
| 86 | + ]); |
| 87 | + |
| 88 | + $header = config('imports.upload.column_keys'); |
| 89 | + $lines = [ |
| 90 | + ["Q184746","Q184746$7814880A-A6EF-40EC-885E-F46DD58C8DC5","P569","3 April 1934" |
| 91 | + ,"Q12138","1934-04-03","https://d-nb.info/gnd/119004453","statement"], |
| 92 | + ["Q184746","Q184746$7200D1AD-E4E8-401B-8D57-8C823810F11F","P21","Q6581072" |
| 93 | + ,"","nonbinary","https://www.imdb.com/name/nm0328762/","statement"] |
| 94 | + ]; |
| 95 | + |
| 96 | + $content = join("\n", array_map(function (array $line) { |
| 97 | + return join(',', $line); |
| 98 | + }, array_merge([$header], $lines))); |
| 99 | + |
| 100 | + Storage::fake('local'); |
| 101 | + Storage::put( |
| 102 | + 'mismatch-files/' . $filename, |
| 103 | + $content |
| 104 | + ); |
| 105 | + |
| 106 | + $expected = array_map(function ($row) use ($header) { |
| 107 | + return array_combine($header, $row); |
| 108 | + }, $lines); |
| 109 | + |
| 110 | + ImportCSV::dispatch($reupload_import); |
| 111 | + |
| 112 | + $this->assertDatabaseCount('mismatches', count($lines)); |
| 113 | + $this->assertDatabaseHas('mismatches', [ |
| 114 | + 'import_id' => $reupload_import->id |
| 115 | + ]); |
| 116 | + |
| 117 | + foreach ($expected as $mismatch) { |
| 118 | + $this->assertDatabaseHas('mismatches', $mismatch); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Ensure reupload imports duplicated row if db row has review status = 'pending' |
| 124 | + */ |
| 125 | + public function test_imports_mismatches_when_row_exists_and_review_status_is_pending(): void |
| 126 | + { |
| 127 | + $filename = 'creates-mismatches.csv'; |
| 128 | + $user = User::factory()->uploader()->create(); |
| 129 | + $reupload_import = ImportMeta::factory()->for($user)->create([ |
| 130 | + 'filename' => $filename |
| 131 | + ]); |
| 132 | + |
| 133 | + $already_in_db_import = ImportMeta::factory()->for($user)->create(); |
| 134 | + |
| 135 | + Mismatch::factory() |
| 136 | + ->for($already_in_db_import)->create([ |
| 137 | + 'statement_guid' => 'Q184746$7814880A-A6EF-40EC-885E-F46DD58C8DC5', |
| 138 | + 'item_id' => 'Q184746', |
| 139 | + 'property_id' => 'P569', |
| 140 | + 'meta_wikidata_value' => 'Q12138', |
| 141 | + 'wikidata_value' => '3 April 1934', |
| 142 | + 'external_value' => '1934-04-03', |
| 143 | + 'external_url' => 'https://d-nb.info/gnd/119004453', |
| 144 | + 'review_status' => 'pending', |
| 145 | + 'type' => 'statement' |
| 146 | + ]); |
| 147 | + |
| 148 | + $header = config('imports.upload.column_keys'); |
| 149 | + $lines = [ |
| 150 | + ["Q184746","Q184746$7814880A-A6EF-40EC-885E-F46DD58C8DC5","P569","3 April 1934" |
| 151 | + ,"Q12138","1934-04-03","https://d-nb.info/gnd/119004453","statement"], |
| 152 | + ["Q184746","Q184746$7200D1AD-E4E8-401B-8D57-8C823810F11F","P21","Q6581072" |
| 153 | + ,"","nonbinary","https://www.imdb.com/name/nm0328762/","statement"] |
| 154 | + ]; |
| 155 | + |
| 156 | + $content = join("\n", array_map(function (array $line) { |
| 157 | + return join(',', $line); |
| 158 | + }, array_merge([$header], $lines))); |
| 159 | + |
| 160 | + Storage::fake('local'); |
| 161 | + Storage::put( |
| 162 | + 'mismatch-files/' . $filename, |
| 163 | + $content |
| 164 | + ); |
| 165 | + |
| 166 | + $expected = array_map(function ($row) use ($header) { |
| 167 | + return array_combine($header, $row); |
| 168 | + }, $lines); |
| 169 | + |
| 170 | + ImportCSV::dispatch($reupload_import); |
| 171 | + |
| 172 | + $this->assertDatabaseCount('mismatches', count($lines) + 1); |
| 173 | + $this->assertDatabaseHas('mismatches', [ |
| 174 | + 'import_id' => $already_in_db_import->id |
| 175 | + ]); |
| 176 | + |
| 177 | + foreach ($expected as $mismatch) { |
| 178 | + $this->assertDatabaseHas('mismatches', $mismatch); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Ensure reupload imports duplicated rows if uploaded by different users |
| 184 | + */ |
| 185 | + public function test_imports_mismatch_row_from_different_users_only_when_not_pending_in_db(): void |
| 186 | + { |
| 187 | + $filename = 'creates-mismatches.csv'; |
| 188 | + $user1 = User::factory()->uploader()->create(); |
| 189 | + $user2 = User::factory()->uploader()->create(); |
| 190 | + $reupload_import1 = ImportMeta::factory()->for($user1)->create([ |
| 191 | + 'filename' => $filename |
| 192 | + ]); |
| 193 | + $reupload_import2 = ImportMeta::factory()->for($user2)->create([ |
| 194 | + 'filename' => $filename |
| 195 | + ]); |
| 196 | + |
| 197 | + $already_in_db_import = ImportMeta::factory()->for($user1)->create(); |
| 198 | + |
| 199 | + Mismatch::factory() |
| 200 | + ->for($already_in_db_import)->create([ |
| 201 | + 'statement_guid' => 'Q184746$7814880A-A6EF-40EC-885E-F46DD58C8DC5', |
| 202 | + 'item_id' => 'Q184746', |
| 203 | + 'property_id' => 'P569', |
| 204 | + 'meta_wikidata_value' => 'Q12138', |
| 205 | + 'wikidata_value' => '3 April 1934', |
| 206 | + 'external_value' => '1934-04-03', |
| 207 | + 'external_url' => 'https://d-nb.info/gnd/119004453', |
| 208 | + 'review_status' => 'both', |
| 209 | + 'type' => 'statement' |
| 210 | + ]); |
| 211 | + |
| 212 | + $header = config('imports.upload.column_keys'); |
| 213 | + $lines = [ |
| 214 | + ["Q184746","Q184746$7814880A-A6EF-40EC-885E-F46DD58C8DC5","P569","3 April 1934" |
| 215 | + ,"Q12138","1934-04-03","https://d-nb.info/gnd/119004453","statement"], |
| 216 | + ["Q184746","Q184746$7200D1AD-E4E8-401B-8D57-8C823810F11F","P21","Q6581072" |
| 217 | + ,"","nonbinary","https://www.imdb.com/name/nm0328762/","statement"] |
| 218 | + ]; |
| 219 | + |
| 220 | + $content = join("\n", array_map(function (array $line) { |
| 221 | + return join(',', $line); |
| 222 | + }, array_merge([$header], $lines))); |
| 223 | + |
| 224 | + Storage::fake('local'); |
| 225 | + Storage::put( |
| 226 | + 'mismatch-files/' . $filename, |
| 227 | + $content |
| 228 | + ); |
| 229 | + |
| 230 | + $expected = array_map(function ($row) use ($header) { |
| 231 | + return array_combine($header, $row); |
| 232 | + }, $lines); |
| 233 | + |
| 234 | + ImportCSV::dispatch($reupload_import1); |
| 235 | + ImportCSV::dispatch($reupload_import2); |
| 236 | + |
| 237 | + // total would be 5 but one is not imported because it's already reviewed in db |
| 238 | + $this->assertDatabaseCount('mismatches', count($lines) * 2); |
| 239 | + $this->assertDatabaseHas('mismatches', [ |
| 240 | + 'import_id' => $reupload_import1->id, |
| 241 | + 'import_id' => $reupload_import2->id, |
| 242 | + ]); |
| 243 | + |
| 244 | + foreach ($expected as $mismatch) { |
| 245 | + $this->assertDatabaseHas('mismatches', $mismatch); |
| 246 | + } |
| 247 | + } |
| 248 | + |
61 | 249 | /** |
62 | 250 | * Ensure no change is commited to Database in case of error |
63 | 251 | */ |
@@ -103,4 +291,73 @@ public function test_rolls_back_on_failure(): void |
103 | 291 | ]); |
104 | 292 | } |
105 | 293 | } |
| 294 | + |
| 295 | + /** |
| 296 | + * Ensure reupload doesn't import duplicated row with reviewed state and ignoring type column if it's empty |
| 297 | + */ |
| 298 | + public function test_doesnt_import_mismatches_when_row_exists_and_is_reviewed_and_type_is_empty_in_csv(): void |
| 299 | + { |
| 300 | + $filename = 'creates-mismatches.csv'; |
| 301 | + $user1 = User::factory()->uploader()->create(); |
| 302 | + $user2 = User::factory()->uploader()->create(); |
| 303 | + $reupload_import1 = ImportMeta::factory()->for($user1)->create([ |
| 304 | + 'filename' => $filename |
| 305 | + ]); |
| 306 | + $reupload_import2 = ImportMeta::factory()->for($user2)->create([ |
| 307 | + 'filename' => $filename |
| 308 | + ]); |
| 309 | + |
| 310 | + $already_in_db_import = ImportMeta::factory()->for($user1)->create(); |
| 311 | + |
| 312 | + Mismatch::factory() |
| 313 | + ->for($already_in_db_import)->create([ |
| 314 | + 'statement_guid' => 'Q184746$7814880A-A6EF-40EC-885E-F46DD58C8DC5', |
| 315 | + 'item_id' => 'Q184746', |
| 316 | + 'property_id' => 'P569', |
| 317 | + 'meta_wikidata_value' => 'Q12138', |
| 318 | + 'wikidata_value' => '3 April 1934', |
| 319 | + 'external_value' => '1934-04-03', |
| 320 | + 'external_url' => 'https://d-nb.info/gnd/119004453', |
| 321 | + 'review_status' => 'both', |
| 322 | + 'type' => 'statement' |
| 323 | + ]); |
| 324 | + |
| 325 | + $header = config('imports.upload.column_keys'); |
| 326 | + $lines = [ |
| 327 | + ["Q184746","Q184746$7814880A-A6EF-40EC-885E-F46DD58C8DC5","P569","3 April 1934" |
| 328 | + ,"Q12138","1934-04-03","https://d-nb.info/gnd/119004453",""], |
| 329 | + ["Q184746","Q184746$7200D1AD-E4E8-401B-8D57-8C823810F11F","P21","Q6581072" |
| 330 | + ,"","nonbinary","https://www.imdb.com/name/nm0328762/",""] |
| 331 | + ]; |
| 332 | + |
| 333 | + $content = join("\n", array_map(function (array $line) { |
| 334 | + return join(',', $line); |
| 335 | + }, array_merge([$header], $lines))); |
| 336 | + |
| 337 | + Storage::fake('local'); |
| 338 | + Storage::put( |
| 339 | + 'mismatch-files/' . $filename, |
| 340 | + $content |
| 341 | + ); |
| 342 | + |
| 343 | + $expected = array_map(function ($row) use ($header) { |
| 344 | + $expected_line = array_combine($header, $row); |
| 345 | + $expected_line['type'] = 'statement'; |
| 346 | + return $expected_line; |
| 347 | + }, $lines); |
| 348 | + |
| 349 | + ImportCSV::dispatch($reupload_import1); |
| 350 | + ImportCSV::dispatch($reupload_import2); |
| 351 | + |
| 352 | + // total would be 5 but one is not imported because it's already reviewed in db |
| 353 | + $this->assertDatabaseCount('mismatches', count($lines) * 2); |
| 354 | + $this->assertDatabaseHas('mismatches', [ |
| 355 | + 'import_id' => $reupload_import1->id, |
| 356 | + 'import_id' => $reupload_import2->id, |
| 357 | + ]); |
| 358 | + |
| 359 | + foreach ($expected as $mismatch) { |
| 360 | + $this->assertDatabaseHas('mismatches', $mismatch); |
| 361 | + } |
| 362 | + } |
106 | 363 | } |
0 commit comments