Skip to content

Commit 3eaaee2

Browse files
committed
Added LIMIT / OFFSET tests
1 parent 8754bb4 commit 3eaaee2

3 files changed

Lines changed: 137 additions & 1 deletion

File tree

tests/PdoDbMySQLTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,42 @@ public function testUpdateLimit(): void
488488
$this->assertCount(5, $inactive);
489489
}
490490

491+
public function testLimitAndOffset(): void
492+
{
493+
$db = self::$db;
494+
495+
$names = ['Alice', 'Bob', 'Charlie', 'Dana', 'Eve'];
496+
foreach ($names as $i => $name) {
497+
$db->find()->table('users')->insert([
498+
'name' => $name,
499+
'company' => 'TestCorp',
500+
'age' => 20 + $i
501+
]);
502+
}
503+
504+
// first two records (limit = 2)
505+
$firstTwo = $db->find()
506+
->from('users')
507+
->orderBy('id', 'ASC')
508+
->limit(2)
509+
->get();
510+
511+
$this->assertCount(2, $firstTwo);
512+
$this->assertEquals('Alice', $firstTwo[0]['name']);
513+
$this->assertEquals('Bob', $firstTwo[1]['name']);
514+
515+
// (offset = 2)
516+
$nextTwo = $db->find()
517+
->from('users')
518+
->orderBy('id' ,'ASC')
519+
->limit(2)
520+
->offset(2)
521+
->get();
522+
523+
$this->assertCount(2, $nextTwo);
524+
$this->assertEquals('Charlie', $nextTwo[0]['name']);
525+
$this->assertEquals('Dana', $nextTwo[1]['name']);
526+
}
491527

492528
public function testUpdateWithRawValue(): void
493529
{

tests/PdoDbPostgreSQLTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,71 @@ public function testUpdate(): void
401401

402402
}
403403

404+
public function testUpdateLimit(): void
405+
{
406+
// @todo Implement update limit logic for PostgreSQL
407+
$this->markTestSkipped('Logic not implemented for PostgreSQL dialect.');
408+
$db = self::$db;
409+
410+
411+
for ($i = 1; $i <= 7; $i++) {
412+
$db->find()->table('users')->insert([
413+
'name' => "Cnt{$i}",
414+
'company' => 'C',
415+
'age' => 20 + $i,
416+
'status' => 'active',
417+
]);
418+
}
419+
420+
// Update with limit 5
421+
$db->find()->table('users')
422+
->limit(5)
423+
->update(['status' => 'inactive']);
424+
425+
$inactive = $db->find()
426+
->from('users')
427+
->where('status', 'inactive')
428+
->get();
429+
430+
$this->assertCount(5, $inactive);
431+
}
432+
433+
public function testLimitAndOffset(): void
434+
{
435+
$db = self::$db;
436+
437+
$names = ['Alice', 'Bob', 'Charlie', 'Dana', 'Eve'];
438+
foreach ($names as $i => $name) {
439+
$db->find()->table('users')->insert([
440+
'name' => $name,
441+
'company' => 'TestCorp',
442+
'age' => 20 + $i
443+
]);
444+
}
445+
446+
// first two records (limit = 2)
447+
$firstTwo = $db->find()
448+
->from('users')
449+
->orderBy('id', 'ASC')
450+
->limit(2)
451+
->get();
452+
453+
$this->assertCount(2, $firstTwo);
454+
$this->assertEquals('Alice', $firstTwo[0]['name']);
455+
$this->assertEquals('Bob', $firstTwo[1]['name']);
456+
457+
// (offset = 2)
458+
$nextTwo = $db->find()
459+
->from('users')
460+
->orderBy('id' ,'ASC')
461+
->limit(2)
462+
->offset(2)
463+
->get();
464+
465+
$this->assertCount(2, $nextTwo);
466+
$this->assertEquals('Charlie', $nextTwo[0]['name']);
467+
$this->assertEquals('Dana', $nextTwo[1]['name']);
468+
}
404469

405470
public function testUpdateWithRawValue(): void
406471
{

tests/PdoDbSqliteTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ public function testUpdate(): void
349349

350350
}
351351

352-
353352
public function testUpdateLimit(): void
354353
{
355354
$db = self::$db;
@@ -377,6 +376,42 @@ public function testUpdateLimit(): void
377376
$this->assertCount(5, $inactive);
378377
}
379378

379+
public function testLimitAndOffset(): void
380+
{
381+
$db = self::$db;
382+
383+
$names = ['Alice', 'Bob', 'Charlie', 'Dana', 'Eve'];
384+
foreach ($names as $i => $name) {
385+
$db->find()->table('users')->insert([
386+
'name' => $name,
387+
'company' => 'TestCorp',
388+
'age' => 20 + $i
389+
]);
390+
}
391+
392+
// first two records (limit = 2)
393+
$firstTwo = $db->find()
394+
->from('users')
395+
->orderBy('id', 'ASC')
396+
->limit(2)
397+
->get();
398+
399+
$this->assertCount(2, $firstTwo);
400+
$this->assertEquals('Alice', $firstTwo[0]['name']);
401+
$this->assertEquals('Bob', $firstTwo[1]['name']);
402+
403+
// (offset = 2)
404+
$nextTwo = $db->find()
405+
->from('users')
406+
->orderBy('id' ,'ASC')
407+
->limit(2)
408+
->offset(2)
409+
->get();
410+
411+
$this->assertCount(2, $nextTwo);
412+
$this->assertEquals('Charlie', $nextTwo[0]['name']);
413+
$this->assertEquals('Dana', $nextTwo[1]['name']);
414+
}
380415

381416
public function testUpdateWithRawValue(): void
382417
{

0 commit comments

Comments
 (0)