You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

284 lines
9.2 KiB

  1. package dynamodb_test
  2. import (
  3. "time"
  4. "github.com/goamz/goamz/dynamodb"
  5. . "gopkg.in/check.v1"
  6. )
  7. type TestSubStruct struct {
  8. SubBool bool
  9. SubInt int
  10. SubString string
  11. SubStringArray []string
  12. }
  13. type TestStruct struct {
  14. TestBool bool
  15. TestInt int
  16. TestInt32 int32
  17. TestInt64 int64
  18. TestUint uint
  19. TestFloat32 float32
  20. TestFloat64 float64
  21. TestString string
  22. TestByteArray []byte
  23. TestStringArray []string
  24. TestIntArray []int
  25. TestInt8Array []int8
  26. TestFloatArray []float64
  27. TestSub TestSubStruct
  28. }
  29. type TestStructTime struct {
  30. TestTime time.Time
  31. }
  32. func testObject() *TestStruct {
  33. return &TestStruct{
  34. TestBool: true,
  35. TestInt: -99,
  36. TestInt32: 999,
  37. TestInt64: 9999,
  38. TestUint: 99,
  39. TestFloat32: 9.9999,
  40. TestFloat64: 99.999999,
  41. TestString: "test",
  42. TestByteArray: []byte("bytes"),
  43. TestStringArray: []string{"test1", "test2", "test3", "test4"},
  44. TestIntArray: []int{0, 1, 12, 123, 1234, 12345},
  45. TestInt8Array: []int8{0, 1, 12, 123},
  46. TestFloatArray: []float64{0.1, 1.1, 1.2, 1.23, 1.234, 1.2345},
  47. TestSub: TestSubStruct{
  48. SubBool: true,
  49. SubInt: 2,
  50. SubString: "subtest",
  51. SubStringArray: []string{"sub1", "sub2", "sub3"},
  52. },
  53. }
  54. }
  55. func testObjectTime() *TestStructTime {
  56. t, _ := time.Parse("Jan 2, 2006 at 3:04pm", "Mar 3, 2003 at 5:03pm")
  57. return &TestStructTime{
  58. TestTime: t,
  59. }
  60. }
  61. func testObjectWithZeroValues() *TestStruct {
  62. return &TestStruct{}
  63. }
  64. func testObjectWithNilSets() *TestStruct {
  65. return &TestStruct{
  66. TestBool: true,
  67. TestInt: -99,
  68. TestInt32: 999,
  69. TestInt64: 9999,
  70. TestUint: 99,
  71. TestFloat32: 9.9999,
  72. TestFloat64: 99.999999,
  73. TestString: "test",
  74. TestByteArray: []byte("bytes"),
  75. TestStringArray: []string(nil),
  76. TestIntArray: []int(nil),
  77. TestFloatArray: []float64(nil),
  78. TestSub: TestSubStruct{
  79. SubBool: true,
  80. SubInt: 2,
  81. SubString: "subtest",
  82. SubStringArray: []string{"sub1", "sub2", "sub3"},
  83. },
  84. }
  85. }
  86. func testObjectWithEmptySets() *TestStruct {
  87. return &TestStruct{
  88. TestBool: true,
  89. TestInt: -99,
  90. TestInt32: 999,
  91. TestInt64: 9999,
  92. TestUint: 99,
  93. TestFloat32: 9.9999,
  94. TestFloat64: 99.999999,
  95. TestString: "test",
  96. TestByteArray: []byte("bytes"),
  97. TestStringArray: []string{},
  98. TestIntArray: []int{},
  99. TestFloatArray: []float64{},
  100. TestSub: TestSubStruct{
  101. SubBool: true,
  102. SubInt: 2,
  103. SubString: "subtest",
  104. SubStringArray: []string{"sub1", "sub2", "sub3"},
  105. },
  106. }
  107. }
  108. func testAttrs() []dynamodb.Attribute {
  109. return []dynamodb.Attribute{
  110. dynamodb.Attribute{Type: "N", Name: "TestBool", Value: "1", SetValues: []string(nil)},
  111. dynamodb.Attribute{Type: "N", Name: "TestInt", Value: "-99", SetValues: []string(nil)},
  112. dynamodb.Attribute{Type: "N", Name: "TestInt32", Value: "999", SetValues: []string(nil)},
  113. dynamodb.Attribute{Type: "N", Name: "TestInt64", Value: "9999", SetValues: []string(nil)},
  114. dynamodb.Attribute{Type: "N", Name: "TestUint", Value: "99", SetValues: []string(nil)},
  115. dynamodb.Attribute{Type: "N", Name: "TestFloat32", Value: "9.9999", SetValues: []string(nil)},
  116. dynamodb.Attribute{Type: "N", Name: "TestFloat64", Value: "99.999999", SetValues: []string(nil)},
  117. dynamodb.Attribute{Type: "S", Name: "TestString", Value: "test", SetValues: []string(nil)},
  118. dynamodb.Attribute{Type: "S", Name: "TestByteArray", Value: "Ynl0ZXM=", SetValues: []string(nil)},
  119. dynamodb.Attribute{Type: "SS", Name: "TestStringArray", Value: "", SetValues: []string{"test1", "test2", "test3", "test4"}},
  120. dynamodb.Attribute{Type: "NS", Name: "TestIntArray", Value: "", SetValues: []string{"0", "1", "12", "123", "1234", "12345"}},
  121. dynamodb.Attribute{Type: "NS", Name: "TestInt8Array", Value: "", SetValues: []string{"0", "1", "12", "123"}},
  122. dynamodb.Attribute{Type: "NS", Name: "TestFloatArray", Value: "", SetValues: []string{"0.1", "1.1", "1.2", "1.23", "1.234", "1.2345"}},
  123. dynamodb.Attribute{Type: "S", Name: "TestSub", Value: `{"SubBool":true,"SubInt":2,"SubString":"subtest","SubStringArray":["sub1","sub2","sub3"]}`, SetValues: []string(nil)},
  124. }
  125. }
  126. func testAttrsTime() []dynamodb.Attribute {
  127. return []dynamodb.Attribute{
  128. dynamodb.Attribute{Type: "S", Name: "TestTime", Value: "\"2003-03-03T17:03:00Z\"", SetValues: []string(nil)},
  129. }
  130. }
  131. func testAttrsWithZeroValues() []dynamodb.Attribute {
  132. return []dynamodb.Attribute{
  133. dynamodb.Attribute{Type: "N", Name: "TestBool", Value: "0", SetValues: []string(nil)},
  134. dynamodb.Attribute{Type: "N", Name: "TestInt", Value: "0", SetValues: []string(nil)},
  135. dynamodb.Attribute{Type: "N", Name: "TestInt32", Value: "0", SetValues: []string(nil)},
  136. dynamodb.Attribute{Type: "N", Name: "TestInt64", Value: "0", SetValues: []string(nil)},
  137. dynamodb.Attribute{Type: "N", Name: "TestUint", Value: "0", SetValues: []string(nil)},
  138. dynamodb.Attribute{Type: "N", Name: "TestFloat32", Value: "0", SetValues: []string(nil)},
  139. dynamodb.Attribute{Type: "N", Name: "TestFloat64", Value: "0", SetValues: []string(nil)},
  140. dynamodb.Attribute{Type: "S", Name: "TestSub", Value: `{"SubBool":false,"SubInt":0,"SubString":"","SubStringArray":null}`, SetValues: []string(nil)},
  141. }
  142. }
  143. func testAttrsWithNilSets() []dynamodb.Attribute {
  144. return []dynamodb.Attribute{
  145. dynamodb.Attribute{Type: "N", Name: "TestBool", Value: "1", SetValues: []string(nil)},
  146. dynamodb.Attribute{Type: "N", Name: "TestInt", Value: "-99", SetValues: []string(nil)},
  147. dynamodb.Attribute{Type: "N", Name: "TestInt32", Value: "999", SetValues: []string(nil)},
  148. dynamodb.Attribute{Type: "N", Name: "TestInt64", Value: "9999", SetValues: []string(nil)},
  149. dynamodb.Attribute{Type: "N", Name: "TestUint", Value: "99", SetValues: []string(nil)},
  150. dynamodb.Attribute{Type: "N", Name: "TestFloat32", Value: "9.9999", SetValues: []string(nil)},
  151. dynamodb.Attribute{Type: "N", Name: "TestFloat64", Value: "99.999999", SetValues: []string(nil)},
  152. dynamodb.Attribute{Type: "S", Name: "TestString", Value: "test", SetValues: []string(nil)},
  153. dynamodb.Attribute{Type: "S", Name: "TestByteArray", Value: "Ynl0ZXM=", SetValues: []string(nil)},
  154. dynamodb.Attribute{Type: "S", Name: "TestSub", Value: `{"SubBool":true,"SubInt":2,"SubString":"subtest","SubStringArray":["sub1","sub2","sub3"]}`, SetValues: []string(nil)},
  155. }
  156. }
  157. type MarshallerSuite struct {
  158. }
  159. var _ = Suite(&MarshallerSuite{})
  160. func (s *MarshallerSuite) TestMarshal(c *C) {
  161. testObj := testObject()
  162. attrs, err := dynamodb.MarshalAttributes(testObj)
  163. if err != nil {
  164. c.Errorf("Error from dynamodb.MarshalAttributes: %#v", err)
  165. }
  166. expected := testAttrs()
  167. c.Check(attrs, DeepEquals, expected)
  168. }
  169. func (s *MarshallerSuite) TestUnmarshal(c *C) {
  170. testObj := &TestStruct{}
  171. attrMap := map[string]*dynamodb.Attribute{}
  172. attrs := testAttrs()
  173. for i, _ := range attrs {
  174. attrMap[attrs[i].Name] = &attrs[i]
  175. }
  176. err := dynamodb.UnmarshalAttributes(&attrMap, testObj)
  177. if err != nil {
  178. c.Fatalf("Error from dynamodb.UnmarshalAttributes: %#v (Built: %#v)", err, testObj)
  179. }
  180. expected := testObject()
  181. c.Check(testObj, DeepEquals, expected)
  182. }
  183. func (s *MarshallerSuite) TestMarshalTime(c *C) {
  184. testObj := testObjectTime()
  185. attrs, err := dynamodb.MarshalAttributes(testObj)
  186. if err != nil {
  187. c.Errorf("Error from dynamodb.MarshalAttributes: %#v", err)
  188. }
  189. expected := testAttrsTime()
  190. c.Check(attrs, DeepEquals, expected)
  191. }
  192. func (s *MarshallerSuite) TestUnmarshalTime(c *C) {
  193. testObj := &TestStructTime{}
  194. attrMap := map[string]*dynamodb.Attribute{}
  195. attrs := testAttrsTime()
  196. for i, _ := range attrs {
  197. attrMap[attrs[i].Name] = &attrs[i]
  198. }
  199. err := dynamodb.UnmarshalAttributes(&attrMap, testObj)
  200. if err != nil {
  201. c.Fatalf("Error from dynamodb.UnmarshalAttributes: %#v (Built: %#v)", err, testObj)
  202. }
  203. expected := testObjectTime()
  204. c.Check(testObj, DeepEquals, expected)
  205. }
  206. func (s *MarshallerSuite) TestMarshalNilSets(c *C) {
  207. testObj := testObjectWithNilSets()
  208. attrs, err := dynamodb.MarshalAttributes(testObj)
  209. if err != nil {
  210. c.Errorf("Error from dynamodb.MarshalAttributes: %#v", err)
  211. }
  212. expected := testAttrsWithNilSets()
  213. c.Check(attrs, DeepEquals, expected)
  214. }
  215. func (s *MarshallerSuite) TestMarshalZeroValues(c *C) {
  216. testObj := testObjectWithZeroValues()
  217. attrs, err := dynamodb.MarshalAttributes(testObj)
  218. if err != nil {
  219. c.Errorf("Error from dynamodb.MarshalAttributes: %#v", err)
  220. }
  221. expected := testAttrsWithZeroValues()
  222. c.Check(attrs, DeepEquals, expected)
  223. }
  224. func (s *MarshallerSuite) TestMarshalEmptySets(c *C) {
  225. testObj := testObjectWithEmptySets()
  226. attrs, err := dynamodb.MarshalAttributes(testObj)
  227. if err != nil {
  228. c.Errorf("Error from dynamodb.MarshalAttributes: %#v", err)
  229. }
  230. expected := testAttrsWithNilSets()
  231. c.Check(attrs, DeepEquals, expected)
  232. }
  233. func (s *MarshallerSuite) TestUnmarshalEmptySets(c *C) {
  234. testObj := &TestStruct{}
  235. attrMap := map[string]*dynamodb.Attribute{}
  236. attrs := testAttrsWithNilSets()
  237. for i, _ := range attrs {
  238. attrMap[attrs[i].Name] = &attrs[i]
  239. }
  240. err := dynamodb.UnmarshalAttributes(&attrMap, testObj)
  241. if err != nil {
  242. c.Fatalf("Error from dynamodb.UnmarshalAttributes: %#v (Built: %#v)", err, testObj)
  243. }
  244. expected := testObjectWithNilSets()
  245. c.Check(testObj, DeepEquals, expected)
  246. }