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.
 
 
 

381 lines
8.6 KiB

  1. package dynamodb_test
  2. import (
  3. simplejson "github.com/bitly/go-simplejson"
  4. "github.com/goamz/goamz/aws"
  5. "github.com/goamz/goamz/dynamodb"
  6. . "gopkg.in/check.v1"
  7. )
  8. type QueryBuilderSuite struct {
  9. server *dynamodb.Server
  10. }
  11. var _ = Suite(&QueryBuilderSuite{})
  12. func (s *QueryBuilderSuite) SetUpSuite(c *C) {
  13. auth := &aws.Auth{AccessKey: "", SecretKey: "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"}
  14. s.server = &dynamodb.Server{*auth, aws.USEast}
  15. }
  16. func (s *QueryBuilderSuite) TestEmptyQuery(c *C) {
  17. q := dynamodb.NewEmptyQuery()
  18. queryString := q.String()
  19. expectedString := "{}"
  20. c.Check(queryString, Equals, expectedString)
  21. if expectedString != queryString {
  22. c.Fatalf("Unexpected Query String : %s\n", queryString)
  23. }
  24. }
  25. func (s *QueryBuilderSuite) TestAddWriteRequestItems(c *C) {
  26. primary := dynamodb.NewStringAttribute("WidgetFoo", "")
  27. secondary := dynamodb.NewNumericAttribute("Created", "")
  28. key := dynamodb.PrimaryKey{primary, secondary}
  29. table := s.server.NewTable("FooData", key)
  30. primary2 := dynamodb.NewStringAttribute("TestHashKey", "")
  31. secondary2 := dynamodb.NewNumericAttribute("TestRangeKey", "")
  32. key2 := dynamodb.PrimaryKey{primary2, secondary2}
  33. table2 := s.server.NewTable("TestTable", key2)
  34. q := dynamodb.NewEmptyQuery()
  35. attribute1 := dynamodb.NewNumericAttribute("testing", "4")
  36. attribute2 := dynamodb.NewNumericAttribute("testingbatch", "2111")
  37. attribute3 := dynamodb.NewStringAttribute("testingstrbatch", "mystr")
  38. item1 := []dynamodb.Attribute{*attribute1, *attribute2, *attribute3}
  39. attribute4 := dynamodb.NewNumericAttribute("testing", "444")
  40. attribute5 := dynamodb.NewNumericAttribute("testingbatch", "93748249272")
  41. attribute6 := dynamodb.NewStringAttribute("testingstrbatch", "myotherstr")
  42. item2 := []dynamodb.Attribute{*attribute4, *attribute5, *attribute6}
  43. attributeDel1 := dynamodb.NewStringAttribute("TestHashKeyDel", "DelKey")
  44. attributeDel2 := dynamodb.NewNumericAttribute("TestRangeKeyDel", "7777777")
  45. itemDel := []dynamodb.Attribute{*attributeDel1, *attributeDel2}
  46. attributeTest1 := dynamodb.NewStringAttribute("TestHashKey", "MyKey")
  47. attributeTest2 := dynamodb.NewNumericAttribute("TestRangeKey", "0193820384293")
  48. itemTest := []dynamodb.Attribute{*attributeTest1, *attributeTest2}
  49. tableItems := map[*dynamodb.Table]map[string][][]dynamodb.Attribute{}
  50. actionItems := make(map[string][][]dynamodb.Attribute)
  51. actionItems["Put"] = [][]dynamodb.Attribute{item1, item2}
  52. actionItems["Delete"] = [][]dynamodb.Attribute{itemDel}
  53. tableItems[table] = actionItems
  54. actionItems2 := make(map[string][][]dynamodb.Attribute)
  55. actionItems2["Put"] = [][]dynamodb.Attribute{itemTest}
  56. tableItems[table2] = actionItems2
  57. q.AddWriteRequestItems(tableItems)
  58. queryJson, err := simplejson.NewJson([]byte(q.String()))
  59. if err != nil {
  60. c.Fatal(err)
  61. }
  62. expectedJson, err := simplejson.NewJson([]byte(`
  63. {
  64. "RequestItems": {
  65. "TestTable": [
  66. {
  67. "PutRequest": {
  68. "Item": {
  69. "TestRangeKey": {
  70. "N": "0193820384293"
  71. },
  72. "TestHashKey": {
  73. "S": "MyKey"
  74. }
  75. }
  76. }
  77. }
  78. ],
  79. "FooData": [
  80. {
  81. "DeleteRequest": {
  82. "Key": {
  83. "TestRangeKeyDel": {
  84. "N": "7777777"
  85. },
  86. "TestHashKeyDel": {
  87. "S": "DelKey"
  88. }
  89. }
  90. }
  91. },
  92. {
  93. "PutRequest": {
  94. "Item": {
  95. "testingstrbatch": {
  96. "S": "mystr"
  97. },
  98. "testingbatch": {
  99. "N": "2111"
  100. },
  101. "testing": {
  102. "N": "4"
  103. }
  104. }
  105. }
  106. },
  107. {
  108. "PutRequest": {
  109. "Item": {
  110. "testingstrbatch": {
  111. "S": "myotherstr"
  112. },
  113. "testingbatch": {
  114. "N": "93748249272"
  115. },
  116. "testing": {
  117. "N": "444"
  118. }
  119. }
  120. }
  121. }
  122. ]
  123. }
  124. }
  125. `))
  126. if err != nil {
  127. c.Fatal(err)
  128. }
  129. c.Check(queryJson, DeepEquals, expectedJson)
  130. }
  131. func (s *QueryBuilderSuite) TestAddExpectedQuery(c *C) {
  132. primary := dynamodb.NewStringAttribute("domain", "")
  133. key := dynamodb.PrimaryKey{primary, nil}
  134. table := s.server.NewTable("sites", key)
  135. q := dynamodb.NewQuery(table)
  136. q.AddKey(table, &dynamodb.Key{HashKey: "test"})
  137. expected := []dynamodb.Attribute{
  138. *dynamodb.NewStringAttribute("domain", "expectedTest").SetExists(true),
  139. *dynamodb.NewStringAttribute("testKey", "").SetExists(false),
  140. }
  141. q.AddExpected(expected)
  142. queryJson, err := simplejson.NewJson([]byte(q.String()))
  143. if err != nil {
  144. c.Fatal(err)
  145. }
  146. expectedJson, err := simplejson.NewJson([]byte(`
  147. {
  148. "Expected": {
  149. "domain": {
  150. "Exists": "true",
  151. "Value": {
  152. "S": "expectedTest"
  153. }
  154. },
  155. "testKey": {
  156. "Exists": "false"
  157. }
  158. },
  159. "Key": {
  160. "domain": {
  161. "S": "test"
  162. }
  163. },
  164. "TableName": "sites"
  165. }
  166. `))
  167. if err != nil {
  168. c.Fatal(err)
  169. }
  170. c.Check(queryJson, DeepEquals, expectedJson)
  171. }
  172. func (s *QueryBuilderSuite) TestGetItemQuery(c *C) {
  173. primary := dynamodb.NewStringAttribute("domain", "")
  174. key := dynamodb.PrimaryKey{primary, nil}
  175. table := s.server.NewTable("sites", key)
  176. q := dynamodb.NewQuery(table)
  177. q.AddKey(table, &dynamodb.Key{HashKey: "test"})
  178. {
  179. queryJson, err := simplejson.NewJson([]byte(q.String()))
  180. if err != nil {
  181. c.Fatal(err)
  182. }
  183. expectedJson, err := simplejson.NewJson([]byte(`
  184. {
  185. "Key": {
  186. "domain": {
  187. "S": "test"
  188. }
  189. },
  190. "TableName": "sites"
  191. }
  192. `))
  193. if err != nil {
  194. c.Fatal(err)
  195. }
  196. c.Check(queryJson, DeepEquals, expectedJson)
  197. }
  198. // Use ConsistentRead
  199. {
  200. q.ConsistentRead(true)
  201. queryJson, err := simplejson.NewJson([]byte(q.String()))
  202. if err != nil {
  203. c.Fatal(err)
  204. }
  205. expectedJson, err := simplejson.NewJson([]byte(`
  206. {
  207. "ConsistentRead": "true",
  208. "Key": {
  209. "domain": {
  210. "S": "test"
  211. }
  212. },
  213. "TableName": "sites"
  214. }
  215. `))
  216. if err != nil {
  217. c.Fatal(err)
  218. }
  219. c.Check(queryJson, DeepEquals, expectedJson)
  220. }
  221. }
  222. func (s *QueryBuilderSuite) TestUpdateQuery(c *C) {
  223. primary := dynamodb.NewStringAttribute("domain", "")
  224. rangek := dynamodb.NewNumericAttribute("time", "")
  225. key := dynamodb.PrimaryKey{primary, rangek}
  226. table := s.server.NewTable("sites", key)
  227. countAttribute := dynamodb.NewNumericAttribute("count", "4")
  228. attributes := []dynamodb.Attribute{*countAttribute}
  229. q := dynamodb.NewQuery(table)
  230. q.AddKey(table, &dynamodb.Key{HashKey: "test", RangeKey: "1234"})
  231. q.AddUpdates(attributes, "ADD")
  232. queryJson, err := simplejson.NewJson([]byte(q.String()))
  233. if err != nil {
  234. c.Fatal(err)
  235. }
  236. expectedJson, err := simplejson.NewJson([]byte(`
  237. {
  238. "AttributeUpdates": {
  239. "count": {
  240. "Action": "ADD",
  241. "Value": {
  242. "N": "4"
  243. }
  244. }
  245. },
  246. "Key": {
  247. "domain": {
  248. "S": "test"
  249. },
  250. "time": {
  251. "N": "1234"
  252. }
  253. },
  254. "TableName": "sites"
  255. }
  256. `))
  257. if err != nil {
  258. c.Fatal(err)
  259. }
  260. c.Check(queryJson, DeepEquals, expectedJson)
  261. }
  262. func (s *QueryBuilderSuite) TestAddUpdates(c *C) {
  263. primary := dynamodb.NewStringAttribute("domain", "")
  264. key := dynamodb.PrimaryKey{primary, nil}
  265. table := s.server.NewTable("sites", key)
  266. q := dynamodb.NewQuery(table)
  267. q.AddKey(table, &dynamodb.Key{HashKey: "test"})
  268. attr := dynamodb.NewStringSetAttribute("StringSet", []string{"str", "str2"})
  269. q.AddUpdates([]dynamodb.Attribute{*attr}, "ADD")
  270. queryJson, err := simplejson.NewJson([]byte(q.String()))
  271. if err != nil {
  272. c.Fatal(err)
  273. }
  274. expectedJson, err := simplejson.NewJson([]byte(`
  275. {
  276. "AttributeUpdates": {
  277. "StringSet": {
  278. "Action": "ADD",
  279. "Value": {
  280. "SS": ["str", "str2"]
  281. }
  282. }
  283. },
  284. "Key": {
  285. "domain": {
  286. "S": "test"
  287. }
  288. },
  289. "TableName": "sites"
  290. }
  291. `))
  292. if err != nil {
  293. c.Fatal(err)
  294. }
  295. c.Check(queryJson, DeepEquals, expectedJson)
  296. }
  297. func (s *QueryBuilderSuite) TestAddKeyConditions(c *C) {
  298. primary := dynamodb.NewStringAttribute("domain", "")
  299. key := dynamodb.PrimaryKey{primary, nil}
  300. table := s.server.NewTable("sites", key)
  301. q := dynamodb.NewQuery(table)
  302. acs := []dynamodb.AttributeComparison{
  303. *dynamodb.NewStringAttributeComparison("domain", "EQ", "example.com"),
  304. *dynamodb.NewStringAttributeComparison("path", "EQ", "/"),
  305. }
  306. q.AddKeyConditions(acs)
  307. queryJson, err := simplejson.NewJson([]byte(q.String()))
  308. if err != nil {
  309. c.Fatal(err)
  310. }
  311. expectedJson, err := simplejson.NewJson([]byte(`
  312. {
  313. "KeyConditions": {
  314. "domain": {
  315. "AttributeValueList": [
  316. {
  317. "S": "example.com"
  318. }
  319. ],
  320. "ComparisonOperator": "EQ"
  321. },
  322. "path": {
  323. "AttributeValueList": [
  324. {
  325. "S": "/"
  326. }
  327. ],
  328. "ComparisonOperator": "EQ"
  329. }
  330. },
  331. "TableName": "sites"
  332. }
  333. `))
  334. if err != nil {
  335. c.Fatal(err)
  336. }
  337. c.Check(queryJson, DeepEquals, expectedJson)
  338. }