Skip to content

Commit

Permalink
test for aggregate on soft delete
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Apr 30, 2020
1 parent 4ac0e5a commit 0d272bb
Showing 1 changed file with 68 additions and 25 deletions.
93 changes: 68 additions & 25 deletions tests/system/Database/Live/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 0d272bb

Please sign in to comment.