Skip to content

Commit

Permalink
Fix remove only nested attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
baopham committed Oct 19, 2017
1 parent ebf0d16 commit 6b91a9a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/DynamoDbQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,12 @@ public function removeAttribute(...$attributes)
$this->resetExpressions();

try {
$this->client->updateItem([
$this->client->updateItem(array_filter([
'TableName' => $this->model->getTable(),
'Key' => $key,
'UpdateExpression' => $this->updateExpression->remove($attributes),
'ExpressionAttributeNames' => $this->expressionAttributeNames->all(),
]);
]));

return true;
} catch (Exception $e) {
Expand Down
24 changes: 20 additions & 4 deletions tests/DynamoDbCompositeModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,23 @@ public function testConditionsNotContainingAllCompositeKeys()
$this->assertEquals($expectedItem, $foundItems->first()->toArray());
}

public function testRemoveAttributeOnQuery()
public function testRemoveNestedAttribute()
{
$this->seed([
'id' => ['S' => 'foo'],
'id2' => ['S' => 'bar']
]);

$this->testModel
->where('id', 'foo')
->where('id2', 'bar')
->removeAttribute('nested.foo');

$item = $this->testModel->find(['id' => 'foo', 'id2' => 'bar']);
$this->assertArrayNotHasKey('foo', $item->nested);
}

public function testRemoveAttributesOnQuery()
{
$this->seed([
'id' => ['S' => 'foo'],
Expand All @@ -337,10 +353,10 @@ public function testRemoveAttributeOnQuery()

$item = $this->testModel->find(['id' => 'foo', 'id2' => 'bar']);

$this->assertRemoveAttribute($item);
$this->assertRemoveAttributes($item);
}

public function testRemoveAttributeOnModel()
public function testRemoveAttributesOnModel()
{
$this->seed([
'id' => ['S' => 'foo'],
Expand All @@ -351,7 +367,7 @@ public function testRemoveAttributeOnModel()
$item->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');
$item = $this->testModel->first();

$this->assertRemoveAttribute($item);
$this->assertRemoveAttributes($item);
}

protected function seed($attributes = [], $exclude = [])
Expand Down
22 changes: 17 additions & 5 deletions tests/DynamoDbModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,19 @@ public function testLimit()
$this->assertEquals(4, $items->count());
}

public function testRemoveAttributeOnQuery()
public function testRemoveNestedAttribute()
{
$this->seed(['id' => ['S' => 'foo']]);

$this->testModel
->where('id', 'foo')
->removeAttribute('nested.foo');

$item = $this->testModel->find('foo');
$this->assertArrayNotHasKey('foo', $item->nested);
}

public function testRemoveAttributesOnQuery()
{
$this->seed(['id' => ['S' => 'foo']]);

Expand All @@ -667,21 +679,21 @@ public function testRemoveAttributeOnQuery()
->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');

$item = $this->testModel->find('foo');
$this->assertRemoveAttribute($item);
$this->assertRemoveAttributes($item);
}

public function testRemoveAttributeOnModel()
public function testRemoveAttributesOnModel()
{
$this->seed(['id' => ['S' => 'foo']]);

$item = $this->testModel->first();
$item->removeAttribute('description', 'name', 'nested.foo', 'nested.nestedArray[0]', 'nestedArray[0]');
$item = $this->testModel->first();

$this->assertRemoveAttribute($item);
$this->assertRemoveAttributes($item);
}

protected function assertRemoveAttribute($item)
protected function assertRemoveAttributes($item)
{
$this->assertNull($item->name);
$this->assertNull($item->description);
Expand Down

0 comments on commit 6b91a9a

Please sign in to comment.