From 0d272bb6f28a5d6bc70e7a5aa9e62c4348124e52 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 1 May 2020 03:38:08 +0700 Subject: [PATCH] test for aggregate on soft delete --- tests/system/Database/Live/ModelTest.php | 93 +++++++++++++++++------- 1 file changed, 68 insertions(+), 25 deletions(-) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 0fccf161778d..32cab9d5021f 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -285,40 +285,72 @@ public function testFirstAggregate($groupBy) //-------------------------------------------------------------------- + public function provideAggregateAndGroupBy() + { + return [ + [ + true, + true, + ], + [ + false, + false, + ], + [ + true, + false, + ], + [ + false, + true, + ], + ]; + } + /** - * @dataProvider provideGroupBy + * @dataProvider provideAggregateAndGroupBy */ - public function testFirstRespectsSoftDeletes($groupBy) + public function testFirstRespectsSoftDeletes($aggregate, $groupBy) { $this->db->table('user') ->where('id', 1) ->update(['deleted_at' => date('Y-m-d H:i:s')]); $model = new UserModel(); - if ($groupBy) + if ($aggregate) { - $user = $model->groupBy('id')->first(); + $model->select('SUM(id) as id'); } - else + + if ($groupBy) { - $user = $model->first(); + $model->groupBy('id'); } - // fix for PHP7.2 - $count = is_array($user) ? count($user) : 1; - $this->assertEquals(1, $count); - $this->assertEquals(2, $user->id); + $user = $model->first(); - $user = $model->withDeleted(); - if ($groupBy) + if (! $aggregate) { - $user = $model->groupBy('id')->first(); + // fix for PHP7.2 + $count = is_array($user) ? count($user) : 1; + $this->assertEquals(1, $count); + $this->assertEquals(2, $user->id); } else { - $user = $model->first(); + if ($groupBy) + { + $this->assertEquals(2, $user->id); + } + else + { + $this->assertEquals(9, $user->id); + } } + $user = $model->withDeleted(); + $user = $model->first(); + $this->assertEquals(1, $user->id); } @@ -1926,30 +1958,41 @@ public function testUndefinedMethodInBuilder() } /** - * @dataProvider provideGroupBy + * @dataProvider provideAggregateAndGroupBy */ - public function testFirstRecoverTempUseSoftDeletes($groupBy) + public function testFirstRecoverTempUseSoftDeletes($aggregate, $groupBy) { $model = new UserModel($this->db); $model->delete(1); - if ($groupBy) + if ($aggregate) { - $user = $model->groupBy('id')->withDeleted()->first(); + $model->select('sum(id) as id'); } - else + + if ($groupBy) { - $user = $model->withDeleted()->first(); + $model->groupBy('id'); } - $this->assertEquals(1, $user->id); - $user2 = $model->first(); - if ($groupBy) + + $user = $model->withDeleted()->first(); + + if (! $aggregate) { - $user2 = $model->groupBy('id')->first(); + $this->assertEquals(1, $user->id); } else { - $user2 = $model->first(); + if ($groupBy) + { + $this->assertEquals(1, $user->id); + } + else + { + $this->assertEquals(10, $user->id); + } } + + $user2 = $model->first(); $this->assertEquals(2, $user2->id); }