Exemplo n.º 1
0
func TestQueryPagination(t *testing.T) {
	assert, client := funcTest.setUp(t)
	assert.NoError(batchAddPosts(client, 42, 100, 118))

	// Paginate the posts
	q := client.Query("Posts").
		KeyConditionExpression("UserId = :uid", dynago.Param{":uid", 42}).
		FilterExpression("Dated <> :d", dynago.Param{":d", 101}).
		Limit(10)
	results, err := q.Execute()
	assert.NoError(err)
	assert.Equal(9, len(results.Items))
	assert.Equal(dynago.Number("42"), results.Items[0]["UserId"])
	assert.Equal(dynago.Number("100"), results.Items[0]["Dated"])

	// Check that we skipped something.
	assert.Equal(10, results.ScannedCount)
	assert.Equal(9, results.Count)

	assert.NotNil(results.LastEvaluatedKey)
	assert.Equal(2, len(results.LastEvaluatedKey))
	assert.NotNil(results.Next())

	// page 2, also use ProjectionExpression
	results, err = results.Next().ProjectionExpression("Dated").Execute()
	assert.NoError(err)
	assert.Equal(8, len(results.Items))
	assert.Equal(1, len(results.Items[0]))
	assert.Nil(results.LastEvaluatedKey)
	assert.Nil(results.Next())
}
Exemplo n.º 2
0
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)
}
Exemplo n.º 3
0
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"])
}
Exemplo n.º 4
0
func TestDocumentGetBool(t *testing.T) {
	doc := dynago.Document{"val": dynago.Number("1")}
	assert.Equal(t, true, doc.GetBool("val"))

	doc = dynago.Document{}
	assert.Equal(t, false, doc.GetBool("val"))

	doc = dynago.Document{"val": nil}
	assert.Equal(t, false, doc.GetBool("val"))

	doc = dynago.Document{"val": dynago.Number("b")}
	assert.Panics(t, func() {
		doc.GetBool("val")
	})

	doc = dynago.Document{"val": "hello"}
	assert.Panics(t, func() {
		doc.GetBool("val")
	})
}
Exemplo n.º 5
0
func TestDocumentGetNumberReturnsAnEmptyNumberWhenTheKeyIsNotPresent(t *testing.T) {
	doc := dynago.Document{}
	assert.Equal(t, dynago.Number(""), doc.GetNumber("id"))
}
Exemplo n.º 6
0
func TestDocumentGetNumberReturnsTheDynagoNumberWrappingTheValue(t *testing.T) {
	doc := dynago.Document{"id": dynago.Number("12")}
	assert.Equal(t, dynago.Number("12"), doc.GetNumber("id"))
}
Exemplo n.º 7
0
func TestNumberFloatValReturnsAnErrorIfItCannotParseTheValue(t *testing.T) {
	num := dynago.Number("nope")
	floatVal, err := num.FloatVal()
	assert.Equal(t, float64(0), floatVal)
	assert.Error(t, err)
}
Exemplo n.º 8
0
func TestNumberFloatValReturnsTheValueAsAnfloat(t *testing.T) {
	num := dynago.Number("18.12")
	floatVal, err := num.FloatVal()
	assert.Equal(t, float64(18.12), floatVal)
	assert.Nil(t, err)
}
Exemplo n.º 9
0
func TestNumberUint64ValReturnsTheValueAsAnInt(t *testing.T) {
	num := dynago.Number("123456789012")
	intVal, err := num.Uint64Val()
	assert.Equal(t, uint64(123456789012), intVal)
	assert.Nil(t, err)
}
Exemplo n.º 10
0
func TestNumberInt64ValReturnsTheValueAsAnInt(t *testing.T) {
	num := dynago.Number("18")
	intVal, err := num.Int64Val()
	assert.Equal(t, int64(18), intVal)
	assert.Nil(t, err)
}
Exemplo n.º 11
0
func TestNumberIntValReturnsAnErrorIfItCannotParseTheValue(t *testing.T) {
	num := dynago.Number("nope")
	intVal, err := num.IntVal()
	assert.Equal(t, 0, intVal)
	assert.Error(t, err)
}