Skip to content

Commit 7d8b798

Browse files
committed
Add some updateBatch() tests
These are some tests I had developed but forgot to include them earlier for updateBatch. They exercise some of the same methods used with upsert.
1 parent af452d8 commit 7d8b798

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

tests/system/Database/Live/UpdateTest.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,162 @@ public function testUpdateBatchConstraintsRawSqlAndAlias()
324324
'country' => 'UK',
325325
]);
326326
}
327+
328+
public function testUpdateBatchUpdateFieldsAndAlias()
329+
{
330+
if ($this->db->DBDriver === 'SQLite3' && ! (version_compare($this->db->getVersion(), '3.33.0') >= 0)) {
331+
$this->markTestSkipped('Only SQLite 3.33 and newer can complete this test.');
332+
}
333+
334+
$data = [
335+
[
336+
'email' => '[email protected]',
337+
'name' => 'Derek Jones Does Not Change',
338+
'country' => 'Greece',
339+
],
340+
[
341+
342+
'email' => '[email protected]',
343+
'name' => 'Ahmadinejad No change',
344+
'country' => 'Greece',
345+
],
346+
];
347+
348+
$rawSql = new RawSql('CURRENT_TIMESTAMP');
349+
350+
$updateFields = ['country', 'updated_at' => $rawSql];
351+
352+
$this->db->table('user')->updateFields($updateFields)->onConstraint('email')->updateBatch($data);
353+
354+
// check to see if update_at was updated
355+
$result = $this->db->table('user')
356+
->where("email IN ('[email protected]','[email protected]')")
357+
->get()
358+
->getResultArray();
359+
360+
foreach ($result as $row) {
361+
$this->assertNotNull($row['updated_at']);
362+
}
363+
364+
// only country and update_at should have changed
365+
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'country' => 'Greece']);
366+
$this->seeInDatabase('user', ['name' => 'Ahmadinejad', 'country' => 'Greece']);
367+
368+
// Original dataset from seeder
369+
$data = [
370+
[
371+
'name' => 'Derek Should Change',
372+
'email' => '[email protected]',
373+
'country' => 'Greece', // will update
374+
],
375+
[
376+
'name' => 'Ahmadinejad', // did't change above and will not change
377+
'email' => '[email protected]',
378+
'country' => 'Iran', // will not update
379+
],
380+
[
381+
'name' => 'Should Not Change',
382+
'email' => '[email protected]',
383+
'country' => 'Greece', // will not update
384+
],
385+
[
386+
'name' => 'Should Change',
387+
'email' => '[email protected]',
388+
'country' => 'UK', // will update
389+
],
390+
];
391+
392+
$updateFields = ['name', 'updated_at' => new RawSql('NULL')];
393+
394+
$esc = $this->db->escapeChar;
395+
396+
// contraint is email and if the updated country = the source country
397+
// setting alias allows us to reference it in RawSql
398+
$this->db->table('user')
399+
->updateFields($updateFields)
400+
->onConstraint(['email', new RawSql("{$esc}db_user{$esc}.{$esc}country{$esc} = {$esc}_update{$esc}.{$esc}country{$esc}")])
401+
->setAlias('_update')
402+
->updateBatch($data);
403+
404+
$result = $this->db->table('user')->get()->getResultArray();
405+
406+
foreach ($result as $row) {
407+
if ($row['email'] === '[email protected]') {
408+
$this->assertNotNull($row['updated_at']);
409+
} else {
410+
$this->assertNull($row['updated_at']);
411+
}
412+
}
413+
414+
$this->seeInDatabase('user', ['name' => 'Derek Should Change', 'country' => 'Greece']);
415+
$this->seeInDatabase('user', ['name' => 'Ahmadinejad', 'country' => 'Greece']);
416+
$this->seeInDatabase('user', ['name' => 'Richard A Causey', 'country' => 'US']);
417+
$this->seeInDatabase('user', ['name' => 'Should Change', 'country' => 'UK']);
418+
}
419+
420+
public function testUpdateBatchWithoutOnConstraint()
421+
{
422+
if ($this->db->DBDriver === 'SQLite3' && ! (version_compare($this->db->getVersion(), '3.33.0') >= 0)) {
423+
$this->markTestSkipped('Only SQLite 3.33 and newer can complete this test.');
424+
}
425+
426+
$data = [
427+
[
428+
'name' => 'Derek Nothing', // won't update
429+
'email' => '[email protected]',
430+
'country' => 'Canada',
431+
],
432+
[
433+
'name' => 'Ahmadinejad',
434+
'email' => '[email protected]',
435+
'country' => 'Canada',
436+
],
437+
[
438+
'name' => 'Richard A Causey',
439+
'email' => '[email protected]',
440+
'country' => 'Canada',
441+
],
442+
[
443+
'name' => 'Chris Martin',
444+
'email' => '[email protected]',
445+
'country' => 'Canada',
446+
],
447+
];
448+
449+
$this->db->table('user')->updateBatch($data, 'email, name', 2);
450+
451+
$result = $this->db->table('user')->get()->getResultArray();
452+
453+
foreach ($result as $row) {
454+
if ($row['email'] === '[email protected]') {
455+
$this->assertSame('US', $row['country']);
456+
} else {
457+
$this->assertSame('Canada', $row['country']);
458+
}
459+
}
460+
}
461+
462+
public function testRawSqlConstraint()
463+
{
464+
if ($this->db->DBDriver === 'SQLite3' && ! (version_compare($this->db->getVersion(), '3.33.0') >= 0)) {
465+
$this->markTestSkipped('Only SQLite 3.33 and newer can complete this test.');
466+
}
467+
468+
$data = [
469+
[
470+
'name' => 'Derek Jones',
471+
'email' => '[email protected]',
472+
'country' => 'Germany',
473+
],
474+
];
475+
476+
$builder = $this->db->table('user');
477+
478+
$builder->setData($data, true, 'db_myalias')
479+
->updateFields('name, country')
480+
->onConstraint(new RawSql($this->db->protectIdentifiers('user.email') . ' = ' . $this->db->protectIdentifiers('myalias.email')))
481+
->updateBatch();
482+
483+
$this->seeInDatabase('user', ['email' => '[email protected]', 'country' => 'Germany']);
484+
}
327485
}

0 commit comments

Comments
 (0)