gocql_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2012 by Christoph Hack <christoph@tux21b.org>
  2. // All rights reserved. Distributed under the Simplified BSD License.
  3. package gocql
  4. import (
  5. "bytes"
  6. "database/sql"
  7. "github.com/tux21b/gocql/uuid"
  8. "testing"
  9. "time"
  10. )
  11. func TestSimple(t *testing.T) {
  12. db, err := sql.Open("gocql", "localhost:8000 keyspace=system")
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. rows, err := db.Query("SELECT keyspace_name FROM schema_keyspaces")
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. for rows.Next() {
  21. var keyspace string
  22. if err := rows.Scan(&keyspace); err != nil {
  23. t.Fatal(err)
  24. }
  25. }
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. }
  30. type Page struct {
  31. Title string
  32. RevID uuid.UUID
  33. Body string
  34. Hits int
  35. Protected bool
  36. Modified time.Time
  37. Attachment []byte
  38. }
  39. var pages = []*Page{
  40. &Page{"Frontpage", uuid.TimeUUID(), "Hello world!", 0, false,
  41. time.Date(2012, 8, 20, 10, 0, 0, 0, time.UTC), nil},
  42. &Page{"Frontpage", uuid.TimeUUID(), "Hello modified world!", 0, false,
  43. time.Date(2012, 8, 22, 10, 0, 0, 0, time.UTC), []byte("img data\x00")},
  44. &Page{"LoremIpsum", uuid.TimeUUID(), "Lorem ipsum dolor sit amet", 12,
  45. true, time.Date(2012, 8, 22, 10, 0, 8, 0, time.UTC), nil},
  46. }
  47. func TestWiki(t *testing.T) {
  48. db, err := sql.Open("gocql", "localhost:8000")
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. db.Exec("DROP KEYSPACE gocql_wiki")
  53. if _, err := db.Exec(`CREATE KEYSPACE gocql_wiki
  54. WITH strategy_class = 'SimpleStrategy'
  55. AND strategy_options: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. }