github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/endpoints/get_item/get_item_test.go (about) 1 package get_item 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 ) 8 9 func TestNil(t *testing.T) { 10 g := NewGetItem() 11 _,_,err := g.EndpointReqWithConf(nil) 12 if err == nil { 13 t.Errorf("nil conf should result in error") 14 } 15 } 16 17 func TestRequestMarshal(t *testing.T) { 18 s := []string{ 19 `{"TableName":"Thread","Key":{"ForumName":{"S":"AmazonDynamoDB"},"Subject":{"S":"HowdoIupdatemultipleitems?"}},"AttributesToGet":["LastPostDateTime","Message","Tags"],"ConsistentRead":true,"ReturnConsumedCapacity":"TOTAL"}`, `{"TableName":"Thread","Key":{"ForumName":{"S":"AmazonDynamoDB"},"Subject":{"S":"HowdoIupdatemultipleitems?"}},"ProjectionExpression":"LastPostDateTime,Message,Tags","ConsistentRead":true,"ReturnConsumedCapacity":"TOTAL"}`, 20 } 21 for i, v := range s { 22 var g GetItem 23 um_err := json.Unmarshal([]byte(v), &g) 24 if um_err != nil { 25 t.Errorf("cannot unmarshal to create:\n" + v + "\n") 26 } 27 json, jerr := json.Marshal(g) 28 if jerr != nil { 29 t.Errorf("cannot marshal\n") 30 } 31 if len(s[i]) != len(string(json)) { 32 e := fmt.Sprintf("\n%s\n%s\nshould be same",s[i],string(json)) 33 t.Errorf(e) 34 } 35 } 36 } 37 38 func TestResponseMarshal(t *testing.T) { 39 s := []string{`{"ConsumedCapacity":{"CapacityUnits":1,"TableName":"Thread"},"Item":{"Tags":{"SS":["Update","MultipleItems","HelpMe"]},"LastPostDateTime":{"S":"201303190436"},"Message":{"S":"IwanttoupdatemultipleitemsinasingleAPIcall.What'sthebestwaytodothat?"}}}`} 40 j := []string{`{"Item":{"LastPostDateTime":"201303190436","Message":"IwanttoupdatemultipleitemsinasingleAPIcall.What'sthebestwaytodothat?","Tags":["MultipleItems","HelpMe","Update"]},"ConsumedCapacity":{"CapacityU\nits":1,"TableName":"Thread"}`} 41 for i, v := range s { 42 var g Response 43 um_err := json.Unmarshal([]byte(v), &g) 44 if um_err != nil { 45 t.Errorf("cannot unmarshal to create:\n" + v + "\n") 46 } 47 json1, jerr1 := json.Marshal(g) 48 if jerr1 != nil { 49 t.Errorf("cannot marshal\n") 50 } 51 if len(s[i]) != len(string(json1)) { 52 e := fmt.Sprintf("\n%s\n%s\nshould be same",s[i],string(json1)) 53 t.Errorf(e) 54 } 55 c, cerr := g.ToResponseItemJSON() 56 if cerr != nil { 57 e := fmt.Sprintf("cannot convert %v\n", cerr) 58 t.Errorf(e) 59 } 60 json2, jerr2 := json.Marshal(c) 61 if jerr2 != nil { 62 t.Errorf("cannot marshal\n") 63 } 64 if len(j[i]) != len(string(json2)) { 65 e := fmt.Sprintf("\n%s\n%s\nshould be same",s[i],string(json2)) 66 t.Errorf(e) 67 } 68 } 69 }