func (s *QueryBuilderSuite) TestGetItemQuery(c *gocheck.C) { primary := dynamodb.NewStringAttribute("domain", "") key := dynamodb.PrimaryKey{primary, nil} table := s.server.NewTable("sites", key) q := dynamodb.NewQuery(table) q.AddKey(table, &dynamodb.Key{HashKey: "test"}) queryJson, err := simplejson.NewJson([]byte(q.String())) if err != nil { c.Fatal(err) } expectedJson, err := simplejson.NewJson([]byte(` { "Key": { "domain": { "S": "test" } }, "TableName": "sites" } `)) if err != nil { c.Fatal(err) } c.Check(queryJson, gocheck.DeepEquals, expectedJson) }
func TestAddUpdates(t *testing.T) { auth := &aws.Auth{AccessKey: "", SecretKey: "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"} server := dynamodb.Server{*auth, aws.USEast} primary := dynamodb.NewStringAttribute("domain", "") key := dynamodb.PrimaryKey{primary, nil} table := server.NewTable("sites", key) q := dynamodb.NewQuery(table) q.AddKey(table, &dynamodb.Key{HashKey: "test"}) attr := dynamodb.NewStringSetAttribute("StringSet", []string{"str", "str2"}) q.AddUpdates([]dynamodb.Attribute{*attr}, "ADD") queryString := []byte(q.String()) json, err := simplejson.NewJson(queryString) if err != nil { t.Logf("JSON err : %s\n", err) t.Fatalf("Invalid JSON : %s\n", queryString) } attributeUpdates := json.Get("AttributeUpdates") if _, err := attributeUpdates.Map(); err != nil { t.Fatalf("Expected a AttributeUpdates found") } attributesModified := attributeUpdates.Get("StringSet") if _, err := attributesModified.Map(); err != nil { t.Fatalf("Expected a StringSet found : %s", err) } action := attributesModified.Get("Action") if v, err := action.String(); err != nil { t.Fatalf("Expected a action to be string : %s", err) } else if v != "ADD" { t.Fatalf("Expected a action to be ADD : %s", v) } value := attributesModified.Get("Value") if _, err := value.Map(); err != nil { t.Fatalf("Expected a Value found : %s", err) } string_set := value.Get("SS") string_set_ary, err := string_set.StringArray() if err != nil { t.Fatalf("Expected a string set found : %s", err) } if len(string_set_ary) != 2 { t.Fatalf("Expected a string set length to be 2 was : %d", len(string_set_ary)) } for _, v := range string_set_ary { if v != "str" && v != "str2" { t.Fatalf("Expected a string to be str OR str2 was : %s", v) } } }
func TestAddKeyConditions(t *testing.T) { auth := &aws.Auth{AccessKey: "", SecretKey: "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"} server := dynamodb.Server{*auth, aws.USEast} primary := dynamodb.NewStringAttribute("domain", "") key := dynamodb.PrimaryKey{primary, nil} table := server.NewTable("sites", key) q := dynamodb.NewQuery(table) acs := []dynamodb.AttributeComparison{ *dynamodb.NewStringAttributeComparison("domain", "EQ", "example.com"), *dynamodb.NewStringAttributeComparison("path", "EQ", "/"), } q.AddKeyConditions(acs) queryString := []byte(q.String()) json, err := simplejson.NewJson(queryString) if err != nil { t.Logf("JSON err : %s\n", err) t.Fatalf("Invalid JSON : %s\n", queryString) } expected_json, err := simplejson.NewJson([]byte(` { "KeyConditions": { "domain": { "AttributeValueList": [ { "S": "example.com" } ], "ComparisonOperator": "EQ" }, "path": { "AttributeValueList": [ { "S": "/" } ], "ComparisonOperator": "EQ" } }, "TableName": "sites" } `)) if err != nil { t.Logf("JSON err : %s\n", err) t.Fatalf("Invalid JSON : %s\n", expected_json) } if !reflect.DeepEqual(json, expected_json) { t.Fatalf("Unexpected KeyConditions structure") } }
func TestUpdateQuery(t *testing.T) { auth := &aws.Auth{"", "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"} server := dynamodb.Server{*auth, aws.USEast} primary := dynamodb.NewStringAttribute("domain", "") rangek := dynamodb.NewNumericAttribute("time", "") key := dynamodb.PrimaryKey{primary, rangek} table := server.NewTable("sites", key) countAttribute := dynamodb.NewNumericAttribute("count", "4") attributes := []dynamodb.Attribute{*countAttribute} q := dynamodb.NewQuery(table) q.AddKey(table, "test", "1234") q.AddUpdates(attributes, "ADD") queryString := []byte(q.String()) json, err := simplejson.NewJson(queryString) if err != nil { t.Logf("JSON err : %s\n", err) t.Fatalf("Invalid JSON : %s\n", queryString) } tableName := json.Get("TableName").MustString() if tableName != "sites" { t.Fatalf("Expected tableName to be sites was : %s", tableName) } keyMap, err := json.Get("Key").Map() if err != nil { t.Fatalf("Expected a Key") } hashRangeKey := keyMap["HashKeyElement"] if hashRangeKey == nil { t.Fatalf("Expected a HashKeyElement found : %s", keyMap) } rangeKey := keyMap["RangeKeyElement"] if rangeKey == nil { t.Fatalf("Expected a RangeKeyElement found : %s", keyMap) } }
func (s *QueryBuilderSuite) TestAddQueryFilterConditions(c *check.C) { primary := dynamodb.NewStringAttribute("domain", "") key := dynamodb.PrimaryKey{primary, nil} table := s.server.NewTable("sites", key) q := dynamodb.NewQuery(table) acs := []dynamodb.AttributeComparison{ *dynamodb.NewStringAttributeComparison("domain", "EQ", "example.com"), } qf := []dynamodb.AttributeComparison{ *dynamodb.NewNumericAttributeComparison("count", dynamodb.COMPARISON_GREATER_THAN, 5), } q.AddKeyConditions(acs) q.AddQueryFilter(qf) queryJson, err := simplejson.NewJson([]byte(q.String())) if err != nil { c.Fatal(err) } expectedJson, err := simplejson.NewJson([]byte(` { "KeyConditions": { "domain": { "AttributeValueList": [ { "S": "example.com" } ], "ComparisonOperator": "EQ" } }, "QueryFilter": { "count": { "AttributeValueList": [ { "N": "5" } ], "ComparisonOperator": "GT" } }, "TableName": "sites" } `)) if err != nil { c.Fatal(err) } c.Check(queryJson, check.DeepEquals, expectedJson) }
func TestGetItemQuery(t *testing.T) { auth := &aws.Auth{"", "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"} server := dynamodb.Server{*auth, aws.USEast} primary := dynamodb.NewStringAttribute("domain", "") key := dynamodb.PrimaryKey{primary, nil} table := server.NewTable("sites", key) q := dynamodb.NewQuery(table) q.AddKey(table, "test", "") queryString := []byte(q.String()) json, err := simplejson.NewJson(queryString) if err != nil { t.Logf("JSON err : %s\n", err) t.Fatalf("Invalid JSON : %s\n", queryString) } tableName := json.Get("TableName").MustString() if tableName != "sites" { t.Fatalf("Expected tableName to be sites was : %s", tableName) } keyMap, err := json.Get("Key").Map() if err != nil { t.Fatalf("Expected a Key") } hashRangeKey := keyMap["HashKeyElement"] if hashRangeKey == nil { t.Fatalf("Expected a HashKeyElement found : %s", keyMap) } if v, ok := hashRangeKey.(map[string]interface{}); ok { if val, ok := v["S"].(string); ok { if val != "test" { t.Fatalf("Expected HashKeyElement to have the value 'test' found : %s", val) } } } else { t.Fatalf("HashRangeKeyt had the wrong type found : %s", hashRangeKey) } }
func (s *QueryBuilderSuite) TestAddKeyConditions(c *gocheck.C) { primary := dynamodb.NewStringAttribute("domain", "") key := dynamodb.PrimaryKey{primary, nil} table := s.server.NewTable("sites", key) q := dynamodb.NewQuery(table) acs := []dynamodb.AttributeComparison{ *dynamodb.NewStringAttributeComparison("domain", "EQ", "example.com"), *dynamodb.NewStringAttributeComparison("path", "EQ", "/"), } q.AddKeyConditions(acs) queryJson, err := simplejson.NewJson([]byte(q.String())) if err != nil { c.Fatal(err) } expectedJson, err := simplejson.NewJson([]byte(` { "KeyConditions": { "domain": { "AttributeValueList": [ { "S": "example.com" } ], "ComparisonOperator": "EQ" }, "path": { "AttributeValueList": [ { "S": "/" } ], "ComparisonOperator": "EQ" } }, "TableName": "sites" } `)) if err != nil { c.Fatal(err) } c.Check(queryJson, gocheck.DeepEquals, expectedJson) }
func (s *QueryBuilderSuite) TestAddExpectedQuery(c *gocheck.C) { primary := dynamodb.NewStringAttribute("domain", "") key := dynamodb.PrimaryKey{primary, nil} table := s.server.NewTable("sites", key) q := dynamodb.NewQuery(table) q.AddKey(table, &dynamodb.Key{HashKey: "test"}) expected := []dynamodb.Attribute{ *dynamodb.NewStringAttribute("domain", "expectedTest").SetExists(true), *dynamodb.NewStringAttribute("testKey", "").SetExists(false), } q.AddExpected(expected) queryJson, err := simplejson.NewJson([]byte(q.String())) if err != nil { c.Fatal(err) } expectedJson, err := simplejson.NewJson([]byte(` { "Expected": { "domain": { "Exists": "true", "Value": { "S": "expectedTest" } }, "testKey": { "Exists": "false" } }, "Key": { "domain": { "S": "test" } }, "TableName": "sites" } `)) if err != nil { c.Fatal(err) } c.Check(queryJson, gocheck.DeepEquals, expectedJson) }
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 *QueryBuilderSuite) TestAddUpdates(c *gocheck.C) { primary := dynamodb.NewStringAttribute("domain", "") key := dynamodb.PrimaryKey{primary, nil} table := s.server.NewTable("sites", key) q := dynamodb.NewQuery(table) q.AddKey(table, &dynamodb.Key{HashKey: "test"}) attr := dynamodb.NewStringSetAttribute("StringSet", []string{"str", "str2"}) q.AddUpdates([]dynamodb.Attribute{*attr}, "ADD") queryJson, err := simplejson.NewJson([]byte(q.String())) if err != nil { c.Fatal(err) } expectedJson, err := simplejson.NewJson([]byte(` { "AttributeUpdates": { "StringSet": { "Action": "ADD", "Value": { "SS": ["str", "str2"] } } }, "Key": { "domain": { "S": "test" } }, "TableName": "sites" } `)) if err != nil { c.Fatal(err) } c.Check(queryJson, gocheck.DeepEquals, expectedJson) }