func (s *ItemSuite) TestUpdateItem_new(c *C) { attrs := []dynamodb.Attribute{ *dynamodb.NewStringAttribute("intval", "1"), } var rk string if s.WithRange { rk = "1" } pk := &dynamodb.Key{HashKey: "UpdateKeyVal", RangeKey: rk} num := func(a, b string) dynamodb.Attribute { return *dynamodb.NewNumericAttribute(a, b) } checkVal := func(i string) { if item, err := s.table.GetItem(pk); err != nil { c.Error(err) } else { c.Check(item["intval"], DeepEquals, dynamodb.NewNumericAttribute("intval", i)) } } if ok, err := s.table.PutItem("UpdateKeyVal", rk, attrs); !ok { c.Error(err) } checkVal("1") // Simple Increment s.table.UpdateItem(pk).UpdateExpression("SET intval = intval + :incr", num(":incr", "5")).Execute() checkVal("6") conditionalUpdate := func(check string) { s.table.UpdateItem(pk). ConditionExpression("intval = :check"). UpdateExpression("SET intval = intval + :incr"). ExpressionAttributes(num(":check", check), num(":incr", "4")). Execute() } // Conditional increment should be a no-op. conditionalUpdate("42") checkVal("6") // conditional increment should succeed this time conditionalUpdate("6") checkVal("10") // Update with new values getting values result, err := s.table.UpdateItem(pk). ReturnValues(dynamodb.UPDATED_NEW). UpdateExpression("SET intval = intval + :incr", num(":incr", "2")). Execute() c.Check(err, IsNil) c.Check(result.Attributes["intval"], DeepEquals, num("intval", "12")) checkVal("12") }
func (s *QueryBuilderSuite) TestUpdateQuery(c *gocheck.C) { primary := dynamodb.NewStringAttribute("domain", "") rangek := dynamodb.NewNumericAttribute("time", "") key := dynamodb.PrimaryKey{primary, rangek} table := s.server.NewTable("sites", key) countAttribute := dynamodb.NewNumericAttribute("count", "4") attributes := []dynamodb.Attribute{*countAttribute} q := dynamodb.NewQuery(table) q.AddKey(table, &dynamodb.Key{HashKey: "test", RangeKey: "1234"}) q.AddUpdates(attributes, "ADD") queryJson, err := simplejson.NewJson([]byte(q.String())) if err != nil { c.Fatal(err) } expectedJson, err := simplejson.NewJson([]byte(` { "AttributeUpdates": { "count": { "Action": "ADD", "Value": { "N": "4" } } }, "Key": { "domain": { "S": "test" }, "time": { "N": "1234" } }, "TableName": "sites" } `)) if err != nil { c.Fatal(err) } c.Check(queryJson, gocheck.DeepEquals, expectedJson) }
func (s *ItemSuite) TestPutGetDeleteItem(c *C) { attrs := []dynamodb.Attribute{ *dynamodb.NewStringAttribute("Attr1", "Attr1Val"), } var rk string if s.WithRange { rk = "1" } // Put if ok, err := s.table.PutItem("NewHashKeyVal", rk, attrs); !ok { c.Fatal(err) } // Get to verify Put operation pk := &dynamodb.Key{HashKey: "NewHashKeyVal", RangeKey: rk} item, err := s.table.GetItem(pk) if err != nil { c.Fatal(err) } if val, ok := item["TestHashKey"]; ok { c.Check(val, DeepEquals, dynamodb.NewStringAttribute("TestHashKey", "NewHashKeyVal")) } else { c.Error("Expect TestHashKey to be found") } if s.WithRange { if val, ok := item["TestRangeKey"]; ok { c.Check(val, DeepEquals, dynamodb.NewNumericAttribute("TestRangeKey", "1")) } else { c.Error("Expect TestRangeKey to be found") } } // Delete if ok, _ := s.table.DeleteItem(pk); !ok { c.Fatal(err) } // Get to verify Delete operation _, err = s.table.GetItem(pk) c.Check(err.Error(), Matches, "Item not found") }
func (s *QueryBuilderSuite) TestAddWriteRequestItems(c *gocheck.C) { primary := dynamodb.NewStringAttribute("WidgetFoo", "") secondary := dynamodb.NewNumericAttribute("Created", "") key := dynamodb.PrimaryKey{primary, secondary} table := s.server.NewTable("FooData", key) primary2 := dynamodb.NewStringAttribute("TestHashKey", "") secondary2 := dynamodb.NewNumericAttribute("TestRangeKey", "") key2 := dynamodb.PrimaryKey{primary2, secondary2} table2 := s.server.NewTable("TestTable", key2) q := dynamodb.NewEmptyQuery() attribute1 := dynamodb.NewNumericAttribute("testing", "4") attribute2 := dynamodb.NewNumericAttribute("testingbatch", "2111") attribute3 := dynamodb.NewStringAttribute("testingstrbatch", "mystr") item1 := []dynamodb.Attribute{*attribute1, *attribute2, *attribute3} attribute4 := dynamodb.NewNumericAttribute("testing", "444") attribute5 := dynamodb.NewNumericAttribute("testingbatch", "93748249272") attribute6 := dynamodb.NewStringAttribute("testingstrbatch", "myotherstr") item2 := []dynamodb.Attribute{*attribute4, *attribute5, *attribute6} attributeDel1 := dynamodb.NewStringAttribute("TestHashKeyDel", "DelKey") attributeDel2 := dynamodb.NewNumericAttribute("TestRangeKeyDel", "7777777") itemDel := []dynamodb.Attribute{*attributeDel1, *attributeDel2} attributeTest1 := dynamodb.NewStringAttribute("TestHashKey", "MyKey") attributeTest2 := dynamodb.NewNumericAttribute("TestRangeKey", "0193820384293") itemTest := []dynamodb.Attribute{*attributeTest1, *attributeTest2} tableItems := map[*dynamodb.Table]map[string][][]dynamodb.Attribute{} actionItems := make(map[string][][]dynamodb.Attribute) actionItems["Put"] = [][]dynamodb.Attribute{item1, item2} actionItems["Delete"] = [][]dynamodb.Attribute{itemDel} tableItems[table] = actionItems actionItems2 := make(map[string][][]dynamodb.Attribute) actionItems2["Put"] = [][]dynamodb.Attribute{itemTest} tableItems[table2] = actionItems2 q.AddWriteRequestItems(tableItems) queryJson, err := simplejson.NewJson([]byte(q.String())) if err != nil { c.Fatal(err) } expectedJson, err := simplejson.NewJson([]byte(` { "RequestItems": { "TestTable": [ { "PutRequest": { "Item": { "TestRangeKey": { "N": "0193820384293" }, "TestHashKey": { "S": "MyKey" } } } } ], "FooData": [ { "PutRequest": { "Item": { "testingstrbatch": { "S": "mystr" }, "testingbatch": { "N": "2111" }, "testing": { "N": "4" } } } }, { "PutRequest": { "Item": { "testingstrbatch": { "S": "myotherstr" }, "testingbatch": { "N": "93748249272" }, "testing": { "N": "444" } } } }, { "DeleteRequest": { "Key": { "TestRangeKeyDel": { "N": "7777777" }, "TestHashKeyDel": { "S": "DelKey" } } } } ] } } `)) if err != nil { c.Fatal(err) } c.Check(queryJson, gocheck.DeepEquals, expectedJson) }
func (s *ItemSuite) TestConditionalPutUpdateDeleteItem(c *C) { if s.WithRange { // No rangekey test required return } attrs := []dynamodb.Attribute{ *dynamodb.NewStringAttribute("Attr1", "Attr1Val"), } pk := &dynamodb.Key{HashKey: "NewHashKeyVal"} // Put if ok, err := s.table.PutItem("NewHashKeyVal", "", attrs); !ok { c.Fatal(err) } { // Put with condition failed expected := []dynamodb.Attribute{ *dynamodb.NewStringAttribute("Attr1", "expectedAttr1Val").SetExists(true), *dynamodb.NewStringAttribute("AttrNotExists", "").SetExists(false), } if ok, err := s.table.ConditionalPutItem("NewHashKeyVal", "", attrs, expected); ok { c.Errorf("Expect condition does not meet.") } else { c.Check(err.Error(), Matches, "ConditionalCheckFailedException.*") } // Add attributes with condition failed if ok, err := s.table.ConditionalAddAttributes(pk, attrs, expected); ok { c.Errorf("Expect condition does not meet.") } else { c.Check(err.Error(), Matches, "ConditionalCheckFailedException.*") } // Update attributes with condition failed if ok, err := s.table.ConditionalUpdateAttributes(pk, attrs, expected); ok { c.Errorf("Expect condition does not meet.") } else { c.Check(err.Error(), Matches, "ConditionalCheckFailedException.*") } // Delete attributes with condition failed if ok, err := s.table.ConditionalDeleteAttributes(pk, attrs, expected); ok { c.Errorf("Expect condition does not meet.") } else { c.Check(err.Error(), Matches, "ConditionalCheckFailedException.*") } } { expected := []dynamodb.Attribute{ *dynamodb.NewStringAttribute("Attr1", "Attr1Val").SetExists(true), } // Add attributes with condition met addNewAttrs := []dynamodb.Attribute{ *dynamodb.NewNumericAttribute("AddNewAttr1", "10"), *dynamodb.NewNumericAttribute("AddNewAttr2", "20"), } if ok, err := s.table.ConditionalAddAttributes(pk, addNewAttrs, nil); !ok { c.Errorf("Expect condition met. %s", err) } // Update attributes with condition met updateAttrs := []dynamodb.Attribute{ *dynamodb.NewNumericAttribute("AddNewAttr1", "100"), } if ok, err := s.table.ConditionalUpdateAttributes(pk, updateAttrs, expected); !ok { c.Errorf("Expect condition met. %s", err) } // Delete attributes with condition met deleteAttrs := []dynamodb.Attribute{ *dynamodb.NewNumericAttribute("AddNewAttr2", ""), } if ok, err := s.table.ConditionalDeleteAttributes(pk, deleteAttrs, expected); !ok { c.Errorf("Expect condition met. %s", err) } // Get to verify operations that condition are met item, err := s.table.GetItem(pk) if err != nil { c.Fatal(err) } if val, ok := item["AddNewAttr1"]; ok { c.Check(val, DeepEquals, dynamodb.NewNumericAttribute("AddNewAttr1", "100")) } else { c.Error("Expect AddNewAttr1 attribute to be added and updated") } if _, ok := item["AddNewAttr2"]; ok { c.Error("Expect AddNewAttr2 attribute to be deleted") } } { // Put with condition met expected := []dynamodb.Attribute{ *dynamodb.NewStringAttribute("Attr1", "Attr1Val").SetExists(true), } newattrs := []dynamodb.Attribute{ *dynamodb.NewStringAttribute("Attr1", "Attr2Val"), } if ok, err := s.table.ConditionalPutItem("NewHashKeyVal", "", newattrs, expected); !ok { c.Errorf("Expect condition met. %s", err) } // Get to verify Put operation that condition are met item, err := s.table.GetItem(pk) if err != nil { c.Fatal(err) } if val, ok := item["Attr1"]; ok { c.Check(val, DeepEquals, dynamodb.NewStringAttribute("Attr1", "Attr2Val")) } else { c.Error("Expect Attr1 attribute to be updated") } } { // Delete with condition failed expected := []dynamodb.Attribute{ *dynamodb.NewStringAttribute("Attr1", "expectedAttr1Val").SetExists(true), } if ok, err := s.table.ConditionalDeleteItem(pk, expected); ok { c.Errorf("Expect condition does not meet.") } else { c.Check(err.Error(), Matches, "ConditionalCheckFailedException.*") } } { // Delete with condition met expected := []dynamodb.Attribute{ *dynamodb.NewStringAttribute("Attr1", "Attr2Val").SetExists(true), } if ok, _ := s.table.ConditionalDeleteItem(pk, expected); !ok { c.Errorf("Expect condition met.") } // Get to verify Delete operation _, err := s.table.GetItem(pk) c.Check(err.Error(), Matches, "Item not found") } }
func (s *ItemSuite) TestUpdateItem(c *C) { attrs := []dynamodb.Attribute{ *dynamodb.NewNumericAttribute("count", "0"), } var rk string if s.WithRange { rk = "1" } if ok, err := s.table.PutItem("NewHashKeyVal", rk, attrs); !ok { c.Fatal(err) } // UpdateItem with Add attrs = []dynamodb.Attribute{ *dynamodb.NewNumericAttribute("count", "10"), } pk := &dynamodb.Key{HashKey: "NewHashKeyVal", RangeKey: rk} if ok, err := s.table.AddAttributes(pk, attrs); !ok { c.Error(err) } // Get to verify Add operation if item, err := s.table.GetItemConsistent(pk, true); err != nil { c.Error(err) } else { if val, ok := item["count"]; ok { c.Check(val, DeepEquals, dynamodb.NewNumericAttribute("count", "10")) } else { c.Error("Expect count to be found") } } // UpdateItem with Put attrs = []dynamodb.Attribute{ *dynamodb.NewNumericAttribute("count", "100"), } if ok, err := s.table.UpdateAttributes(pk, attrs); !ok { c.Error(err) } // Get to verify Put operation if item, err := s.table.GetItem(pk); err != nil { c.Fatal(err) } else { if val, ok := item["count"]; ok { c.Check(val, DeepEquals, dynamodb.NewNumericAttribute("count", "100")) } else { c.Error("Expect count to be found") } } // UpdateItem with Delete attrs = []dynamodb.Attribute{ *dynamodb.NewNumericAttribute("count", ""), } if ok, err := s.table.DeleteAttributes(pk, attrs); !ok { c.Error(err) } // Get to verify Delete operation if item, err := s.table.GetItem(pk); err != nil { c.Error(err) } else { if _, ok := item["count"]; ok { c.Error("Expect count not to be found") } } }