From 6b91a9a91858a8525740dca289f0b30767a88afb Mon Sep 17 00:00:00 2001 From: Bao Pham Date: Fri, 20 Oct 2017 01:18:57 +0800 Subject: [PATCH] Fix remove only nested attributes --- src/DynamoDbQueryBuilder.php | 4 ++-- tests/DynamoDbCompositeModelTest.php | 24 ++++++++++++++++++++---- tests/DynamoDbModelTest.php | 22 +++++++++++++++++----- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/DynamoDbQueryBuilder.php b/src/DynamoDbQueryBuilder.php index fa8990e..b95a6cc 100644 --- a/src/DynamoDbQueryBuilder.php +++ b/src/DynamoDbQueryBuilder.php @@ -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) { diff --git a/tests/DynamoDbCompositeModelTest.php b/tests/DynamoDbCompositeModelTest.php index 0144ea1..a1ade68 100644 --- a/tests/DynamoDbCompositeModelTest.php +++ b/tests/DynamoDbCompositeModelTest.php @@ -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'], @@ -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'], @@ -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 = []) diff --git a/tests/DynamoDbModelTest.php b/tests/DynamoDbModelTest.php index 7abed1d..417a9da 100644 --- a/tests/DynamoDbModelTest.php +++ b/tests/DynamoDbModelTest.php @@ -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']]); @@ -667,10 +679,10 @@ 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']]); @@ -678,10 +690,10 @@ 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 assertRemoveAttribute($item) + protected function assertRemoveAttributes($item) { $this->assertNull($item->name); $this->assertNull($item->description);