func TestDocumentMarshalJSONDoesNotIncludeEmptyValues(t *testing.T) { doc := dynago.Document{"key1": "shows up", "key2": 9, "fields": dynago.StringSet([]string{"is", "present"}), "id": "", "name": nil, "tags": []string{}} jsonDoc, _ := doc.MarshalJSON() assert.Contains(t, string(jsonDoc), `"fields":{"SS":["is","present"]}`) assert.Contains(t, string(jsonDoc), `"key1":{"S":"shows up"}`) assert.Contains(t, string(jsonDoc), `"key2":{"N":"9"}`) }
func TestDocumentGetNumberPanicsIfTheUnderlyingTypeIsNotANumber(t *testing.T) { doc := dynago.Document{"id": "not-a-dynago-number"} assert.Panics(t, func() { doc.GetNumber("id") }) }
func TestDocumentGetNumberReturnsAnEmptyNumberWhenTheKeyIsNotPresent(t *testing.T) { doc := dynago.Document{} assert.Equal(t, dynago.Number(""), doc.GetNumber("id")) }
func TestDocumentGetNumberReturnsTheDynagoNumberWrappingTheValue(t *testing.T) { doc := dynago.Document{"id": dynago.Number("12")} assert.Equal(t, dynago.Number("12"), doc.GetNumber("id")) }
func TestDocumentGetStringReturnsAnEmptyStringWhenTheKeyIsNotPresent(t *testing.T) { doc := dynago.Document{} assert.Equal(t, "", doc.GetString("name")) }
func TestDocumentGetStringReturnsTheUnderlyingValueAsAString(t *testing.T) { doc := dynago.Document{"name": "Timmy Testerson"} assert.Equal(t, "Timmy Testerson", doc.GetString("name")) }
func TestDocumentGetList(t *testing.T) { doc := dynago.Document{"vals": dynago.List{"val1", "val2"}, "wrongtype": 4} assert.Equal(t, dynago.List{"val1", "val2"}, doc.GetList("vals")) assert.Equal(t, dynago.List(nil), doc.GetList("notarealkey")) assert.Panics(t, func() { doc.GetList("wrongtype") }) }
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") }) }
func TestDocumentGetTimePanicsWhenFormatIsNotIso8601(t *testing.T) { doc := dynago.Document{"time": "Foo"} assert.Panics(t, func() { doc.GetTime("time") }) }
func TestDocumentGetTimeReturnsNilWhenTheKeyDoesNotExist(t *testing.T) { doc := dynago.Document{} assert.Nil(t, doc.GetTime("time")) }
func TestDocumentGetTimeReturnsTheTimeValueFromISO8601(t *testing.T) { doc := dynago.Document{"time": "1990-04-16T00:00:00Z"} val, _ := time.Parse("2006-01-02T15:04:05Z", "1990-04-16T00:00:00Z") assert.Equal(t, &val, doc.GetTime("time")) }
func TestDocumentGetStringSetPanic(t *testing.T) { doc := dynago.Document{"vals": "not-a-string-slice"} assert.Panics(t, func() { doc.GetStringSet("vals") }) }
func TestDocumentGetStringSetReturnsAnEmptyStringSetWhenTheKeyDoesNotExist(t *testing.T) { doc := dynago.Document{} assert.Equal(t, dynago.StringSet{}, doc.GetStringSet("vals")) }
func TestDocumentGetStringSetReturnsTheStringSetValue(t *testing.T) { doc := dynago.Document{"vals": dynago.StringSet{"val1", "val2"}} assert.Equal(t, dynago.StringSet{"val1", "val2"}, doc.GetStringSet("vals")) }