func TestMockExecutorPutItem(t *testing.T) { assert, client, executor := mockSetup(t) client.PutItem("table2", dynago.HashKey("Id", 5)). ConditionExpression("Foo = :bar").Param(":bar", "45"). ReturnValues(dynago.ReturnUpdatedNew). Execute() assert.Equal(true, executor.PutItemCalled) assert.Equal("PutItem", executor.PutItemCall.Method) assert.Equal("table2", executor.PutItemCall.Table) assert.Equal(dynago.HashKey("Id", 5), executor.PutItemCall.Item) assert.Equal(dynago.ReturnUpdatedNew, executor.PutItemCall.ReturnValues) assert.Equal(executor.Calls[0], *executor.PutItemCall) }
func TestDeleteItem_functional(t *testing.T) { assert, client := funcTest.setUp(t) _, err := client.PutItem("Person", person(47, "Mary")).Execute() assert.NoError(err) key := dynago.HashKey("Id", 47) di := client.DeleteItem("Person", key). ConditionExpression("#n <> :name", dynago.Param{"#n", "Name"}, dynago.Param{":name", "Mary"}). ReturnValues(dynago.ReturnAllOld) result, err := di.Execute() assert.Nil(result) assert.NotNil(err) e := err.(*dynago.Error) assert.Equal(dynago.ErrorConditionFailed, e.Type) result, err = di.ConditionExpression("#n <> :name", dynago.Param{":name", "Albert"}).Execute() assert.NoError(err) assert.NotNil(result) doc := dynago.Document{"Name": "Mary", "IncVal": dynago.Number("1"), "Id": dynago.Number("47")} assert.Equal(doc, result.Attributes) result, err = di.ReturnValues(dynago.ReturnNone).Execute() assert.Nil(err) assert.Nil(result) }
func TestMockExecutorDeleteItem(t *testing.T) { assert, client, executor := mockSetup(t) client.DeleteItem("table1", dynago.HashKey("Id", 51)). ConditionExpression("expr1", dynago.Param{":foo", 4}, dynago.Param{"#f", "f"}). ReturnValues(dynago.ReturnAllOld). Execute() assert.Equal(true, executor.DeleteItemCalled) assert.Equal(executor.Calls[0], *executor.DeleteItemCall) call := executor.DeleteItemCall assert.Equal("DeleteItem", call.Method) assert.Equal(dynago.HashKey("Id", 51), call.Key) assert.Equal("table1", call.Table) assert.Equal("expr1", call.ConditionExpression) assert.Equal(dynago.ReturnAllOld, call.ReturnValues) assert.Equal(dynago.Document{":foo": 4}, call.ExpressionAttributeValues) assert.Equal(map[string]string{"#f": "f"}, call.ExpressionAttributeNames) }
func TestBatchWrite(t *testing.T) { assert, client := funcTest.setUp(t) _, err := client.PutItem("Person", person(4, "ToDelete")).Execute() assert.NoError(err) p1 := person(1, "Joe") p2 := person(2, "Mary") p3 := person(3, "Amy") _, err = client.BatchWrite(). Put("Person", p1, p2, p3). Delete("Person", dynago.HashKey("Id", 4)). Execute() assert.NoError(err) response, err := client.GetItem("Person", dynago.HashKey("Id", 2)).Execute() assert.Equal("Mary", response.Item["Name"]) }
func TestMockExecutorBatchWriteItem(t *testing.T) { assert, client, executor := mockSetup(t) doc1 := dynago.Document{"Id": 1, "Name": "1"} doc2 := dynago.Document{"Id": 2, "Name": "2"} key3 := dynago.HashKey("Id", 3) key4 := dynago.HashKey("Id", 4) client.BatchWrite(). Put("table1", doc1, doc2). Delete("table1", key3). Delete("table2", key4). Execute() assert.Equal(true, executor.BatchWriteItemCalled) assert.Equal([]dynago.Document{doc2, doc1}, executor.BatchWriteItemCall.BatchWrites.GetPuts("table1")) assert.Equal(0, len(executor.BatchWriteItemCall.BatchWrites.GetPuts("table2"))) assert.Equal([]dynago.Document{key3}, executor.BatchWriteItemCall.BatchWrites.GetDeleteKeys("table1")) assert.Equal([]dynago.Document{key4}, executor.BatchWriteItemCall.BatchWrites.GetDeleteKeys("table2")) assert.Equal(executor.Calls[0], *executor.BatchWriteItemCall) }
func ExampleClient_UpdateItem(client *dynago.Client) { _, err := client.UpdateItem("Person", dynago.HashKey("Id", 42)). UpdateExpression("SET Name=:name"). Param(":name", "Joe"). Execute() if err != nil { fmt.Printf("UpdateItem failed: %v", err) } }
func TestGet(t *testing.T) { assert, client := funcTest.setUp(t) putResp, err := client.PutItem("Person", person(42, "Bob")).Execute() assert.NoError(err) assert.Nil(putResp) response, err := client.GetItem("Person", dynago.HashKey("Id", 42)).Execute() assert.Equal("Bob", response.Item["Name"]) assert.IsType(dynago.Number("1"), response.Item["Id"]) assert.Equal(dynago.Number("42"), response.Item["Id"]) }
func TestMockExecutorGetItem(t *testing.T) { assert, client, executor := mockSetup(t) client.GetItem("table1", dynago.HashKey("Id", 5)).ConsistentRead(true).Execute() assert.Equal(true, executor.GetItemCalled) assert.Equal("GetItem", executor.GetItemCall.Method) assert.Equal("table1", executor.GetItemCall.Table) assert.Equal(true, executor.GetItemCall.ConsistentRead) assert.Equal(dynago.Document{"Id": 5}, executor.GetItemCall.Key) assert.Equal(1, len(executor.Calls)) assert.Equal(executor.Calls[0], *executor.GetItemCall) }
func ExampleClient_BatchWrite(client *dynago.Client) { record1 := dynago.Document{"Id": 1, "Name": "Person1"} record2 := dynago.Document{"Id": 2, "Name": "Person2"} // We can put and delete at the same time to multiple tables. client.BatchWrite(). Put("Table1", record1, record2). Put("Table2", dynago.Document{"Name": "Other"}). Delete("Table2", dynago.HashKey("Id", 42)). Execute() }
func TestUpdateItemConditional(t *testing.T) { assert, client := funcTest.setUp(t) _, err := client.PutItem("Person", person(5, "ToUpdate")).Execute() assert.NoError(err) result, err := client.UpdateItem("Person", dynago.HashKey("Id", 5)). UpdateExpression("SET #n = :name"). Param("#n", "Name").Param(":name", "Bob"). ReturnValues(dynago.ReturnUpdatedNew).Execute() assert.NoError(err) assert.NotNil(result) assert.NotNil(result.Attributes) assert.Equal("Bob", result.Attributes["Name"]) }
func TestMockExecutorUpdateItem(t *testing.T) { assert, client, executor := mockSetup(t) client.UpdateItem("table4", dynago.HashKey("Id", 50)). UpdateExpression("Foo = :param1").Param(":param1", 90). ConditionExpression("#foo > :param2").Param("#foo", "Foo"). Execute() assert.Equal(true, executor.UpdateItemCalled) assert.Equal("UpdateItem", executor.UpdateItemCall.Method) assert.Equal("table4", executor.UpdateItemCall.Table) assert.Equal("Foo = :param1", executor.UpdateItemCall.UpdateExpression) assert.Equal("#foo > :param2", executor.UpdateItemCall.ConditionExpression) assert.Equal(map[string]string{"#foo": "Foo"}, executor.UpdateItemCall.ExpressionAttributeNames) assert.Equal(dynago.Document{":param1": 90}, executor.UpdateItemCall.ExpressionAttributeValues) }
func TestMockExecutorScan(t *testing.T) { assert, client, executor := mockSetup(t) scan := client.Scan("table5"). ExclusiveStartKey(dynago.HashKey("Id", 2)). FilterExpression("Foo = :bar", dynago.Param{":bar", 10}). ProjectionExpression("Foo, Bar, #baz", dynago.Param{"#baz", "Baz"}). IndexName("index5") scan.Execute() assert.Equal(true, executor.ScanCalled) assert.NotNil(executor.ScanCall) assert.Equal("Scan", executor.ScanCall.Method) assert.Equal("table5", executor.ScanCall.Table) assert.Equal("Foo = :bar", executor.ScanCall.FilterExpression) assert.Equal(dynago.Document{":bar": 10}, executor.ScanCall.ExpressionAttributeValues) assert.Equal(map[string]string{"#baz": "Baz"}, executor.ScanCall.ExpressionAttributeNames) assert.Equal("Foo, Bar, #baz", executor.ScanCall.ProjectionExpression) assert.Equal("index5", executor.ScanCall.IndexName) assert.Nil(executor.ScanCall.Segment) scan.Segment(5, 10).Execute() assert.Equal(2, len(executor.Calls)) assert.NotNil(executor.ScanCall.Segment) assert.Equal(5, *executor.ScanCall.Segment) assert.Equal(10, *executor.ScanCall.TotalSegments) }