gocql_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "bytes"
  7. "database/sql"
  8. "github.com/tux21b/gocql/uuid"
  9. "testing"
  10. "time"
  11. )
  12. func TestSimple(t *testing.T) {
  13. db, err := sql.Open("gocql", "localhost:9042 keyspace=system")
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. rows, err := db.Query("SELECT keyspace_name FROM schema_keyspaces")
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. for rows.Next() {
  22. var keyspace string
  23. if err := rows.Scan(&keyspace); err != nil {
  24. t.Fatal(err)
  25. }
  26. }
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. }
  31. type Page struct {
  32. Title string
  33. RevID uuid.UUID
  34. Body string
  35. Hits int
  36. Protected bool
  37. Modified time.Time
  38. Attachment []byte
  39. }
  40. var pages = []*Page{
  41. &Page{"Frontpage", uuid.TimeUUID(), "Hello world!", 0, false,
  42. time.Date(2012, 8, 20, 10, 0, 0, 0, time.UTC), nil},
  43. &Page{"Frontpage", uuid.TimeUUID(), "Hello modified world!", 0, false,
  44. time.Date(2012, 8, 22, 10, 0, 0, 0, time.UTC), []byte("img data\x00")},
  45. &Page{"LoremIpsum", uuid.TimeUUID(), "Lorem ipsum dolor sit amet", 12,
  46. true, time.Date(2012, 8, 22, 10, 0, 8, 0, time.UTC), nil},
  47. }
  48. func TestWiki(t *testing.T) {
  49. db, err := sql.Open("gocql", "localhost:9042 compression=snappy")
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. db.Exec("DROP KEYSPACE gocql_wiki")
  54. if _, err := db.Exec(`CREATE KEYSPACE gocql_wiki
  55. WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }`); err != nil {
  56. t.Fatal(err)
  57. }
  58. if _, err := db.Exec("USE gocql_wiki"); err != nil {
  59. t.Fatal(err)
  60. }
  61. if _, err := db.Exec(`CREATE TABLE page (
  62. title varchar,
  63. revid timeuuid,
  64. body varchar,
  65. hits int,
  66. protected boolean,
  67. modified timestamp,
  68. attachment blob,
  69. PRIMARY KEY (title, revid)
  70. )`); err != nil {
  71. t.Fatal(err)
  72. }
  73. for _, p := range pages {
  74. if _, err := db.Exec(`INSERT INTO page (title, revid, body, hits,
  75. protected, modified, attachment) VALUES (?, ?, ?, ?, ?, ?, ?);`,
  76. p.Title, p.RevID, p.Body, p.Hits, p.Protected, p.Modified,
  77. p.Attachment); err != nil {
  78. t.Fatal(err)
  79. }
  80. }
  81. row := db.QueryRow(`SELECT count(*) FROM page`)
  82. var count int
  83. if err := row.Scan(&count); err != nil {
  84. t.Error(err)
  85. }
  86. if count != len(pages) {
  87. t.Fatalf("expected %d rows, got %d", len(pages), count)
  88. }
  89. for _, page := range pages {
  90. row := db.QueryRow(`SELECT title, revid, body, hits, protected,
  91. modified, attachment
  92. FROM page WHERE title = ? AND revid = ?`, page.Title, page.RevID)
  93. var p Page
  94. err := row.Scan(&p.Title, &p.RevID, &p.Body, &p.Hits, &p.Protected,
  95. &p.Modified, &p.Attachment)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. p.Modified = p.Modified.In(time.UTC)
  100. if page.Title != p.Title || page.RevID != p.RevID ||
  101. page.Body != p.Body || page.Modified != p.Modified ||
  102. page.Hits != p.Hits || page.Protected != p.Protected ||
  103. !bytes.Equal(page.Attachment, p.Attachment) {
  104. t.Errorf("expected %#v got %#v", *page, p)
  105. }
  106. }
  107. row = db.QueryRow(`SELECT title, revid, body, hits, protected,
  108. modified, attachment
  109. FROM page WHERE title = ? ORDER BY revid DESC`, "Frontpage")
  110. var p Page
  111. if err := row.Scan(&p.Title, &p.RevID, &p.Body, &p.Hits, &p.Protected,
  112. &p.Modified, &p.Attachment); err != nil {
  113. t.Error(err)
  114. }
  115. p.Modified = p.Modified.In(time.UTC)
  116. page := pages[1]
  117. if page.Title != p.Title || page.RevID != p.RevID ||
  118. page.Body != p.Body || page.Modified != p.Modified ||
  119. page.Hits != p.Hits || page.Protected != p.Protected ||
  120. !bytes.Equal(page.Attachment, p.Attachment) {
  121. t.Errorf("expected %#v got %#v", *page, p)
  122. }
  123. }
  124. func TestTypes(t *testing.T) {
  125. db, err := sql.Open("gocql", "localhost:9042 compression=snappy")
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. db.Exec("DROP KEYSPACE gocql_types")
  130. if _, err := db.Exec(`CREATE KEYSPACE gocql_types
  131. WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }`); err != nil {
  132. t.Fatal(err)
  133. }
  134. if _, err := db.Exec("USE gocql_types"); err != nil {
  135. t.Fatal(err)
  136. }
  137. if _, err := db.Exec(`CREATE TABLE stuff (
  138. id bigint,
  139. foo text,
  140. PRIMARY KEY (id)
  141. )`); err != nil {
  142. t.Fatal(err)
  143. }
  144. id := int64(-1 << 63)
  145. if _, err := db.Exec(`INSERT INTO stuff (id, foo) VALUES (?, ?);`, &id, "test"); err != nil {
  146. t.Fatal(err)
  147. }
  148. var rid int64
  149. row := db.QueryRow(`SELECT id FROM stuff WHERE id = ?`, id)
  150. if err := row.Scan(&rid); err != nil {
  151. t.Error(err)
  152. }
  153. if id != rid {
  154. t.Errorf("expected %v got %v", id, rid)
  155. }
  156. }
  157. func TestNullColumnValues(t *testing.T) {
  158. db, err := sql.Open("gocql", "localhost:9042 compression=snappy")
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. db.Exec("DROP KEYSPACE gocql_nullvalues")
  163. if _, err := db.Exec(`CREATE KEYSPACE gocql_nullvalues
  164. WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };`); err != nil {
  165. t.Fatal(err)
  166. }
  167. if _, err := db.Exec("USE gocql_nullvalues"); err != nil {
  168. t.Fatal(err)
  169. }
  170. if _, err := db.Exec(`CREATE TABLE stuff (
  171. id bigint,
  172. subid bigint,
  173. foo text,
  174. bar text,
  175. PRIMARY KEY (id, subid)
  176. )`); err != nil {
  177. t.Fatal(err)
  178. }
  179. id := int64(-1 << 63)
  180. if _, err := db.Exec(`INSERT INTO stuff (id, subid, foo) VALUES (?, ?, ?);`, id, int64(4), "test"); err != nil {
  181. t.Fatal(err)
  182. }
  183. if _, err := db.Exec(`INSERT INTO stuff (id, subid, bar) VALUES (?, ?, ?);`, id, int64(6), "test2"); err != nil {
  184. t.Fatal(err)
  185. }
  186. var rid int64
  187. var sid int64
  188. var data1 []byte
  189. var data2 []byte
  190. if rows, err := db.Query(`SELECT id, subid, foo, bar FROM stuff`); err == nil {
  191. for rows.Next() {
  192. if err := rows.Scan(&rid, &sid, &data1, &data2); err != nil {
  193. t.Error(err)
  194. }
  195. }
  196. } else {
  197. t.Fatal(err)
  198. }
  199. }