gocql_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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:8000 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:8000 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 strategy_class = 'SimpleStrategy'
  56. AND strategy_options:replication_factor = 1`); err != nil {
  57. t.Fatal(err)
  58. }
  59. if _, err := db.Exec("USE gocql_wiki"); err != nil {
  60. t.Fatal(err)
  61. }
  62. if _, err := db.Exec(`CREATE TABLE page (
  63. title varchar,
  64. revid timeuuid,
  65. body varchar,
  66. hits int,
  67. protected boolean,
  68. modified timestamp,
  69. attachment blob,
  70. PRIMARY KEY (title, revid)
  71. )`); err != nil {
  72. t.Fatal(err)
  73. }
  74. for _, p := range pages {
  75. if _, err := db.Exec(`INSERT INTO page (title, revid, body, hits,
  76. protected, modified, attachment) VALUES (?, ?, ?, ?, ?, ?, ?);`,
  77. p.Title, p.RevID, p.Body, p.Hits, p.Protected, p.Modified,
  78. p.Attachment); err != nil {
  79. t.Fatal(err)
  80. }
  81. }
  82. row := db.QueryRow(`SELECT count(*) FROM page`)
  83. var count int
  84. if err := row.Scan(&count); err != nil {
  85. t.Error(err)
  86. }
  87. if count != len(pages) {
  88. t.Fatalf("expected %d rows, got %d", len(pages), count)
  89. }
  90. for _, page := range pages {
  91. row := db.QueryRow(`SELECT title, revid, body, hits, protected,
  92. modified, attachment
  93. FROM page WHERE title = ? AND revid = ?`, page.Title, page.RevID)
  94. var p Page
  95. err := row.Scan(&p.Title, &p.RevID, &p.Body, &p.Hits, &p.Protected,
  96. &p.Modified, &p.Attachment)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. p.Modified = p.Modified.In(time.UTC)
  101. if page.Title != p.Title || page.RevID != p.RevID ||
  102. page.Body != p.Body || page.Modified != p.Modified ||
  103. page.Hits != p.Hits || page.Protected != p.Protected ||
  104. !bytes.Equal(page.Attachment, p.Attachment) {
  105. t.Errorf("expected %#v got %#v", *page, p)
  106. }
  107. }
  108. row = db.QueryRow(`SELECT title, revid, body, hits, protected,
  109. modified, attachment
  110. FROM page WHERE title = ? ORDER BY revid DESC`, "Frontpage")
  111. var p Page
  112. if err := row.Scan(&p.Title, &p.RevID, &p.Body, &p.Hits, &p.Protected,
  113. &p.Modified, &p.Attachment); err != nil {
  114. t.Error(err)
  115. }
  116. p.Modified = p.Modified.In(time.UTC)
  117. page := pages[1]
  118. if page.Title != p.Title || page.RevID != p.RevID ||
  119. page.Body != p.Body || page.Modified != p.Modified ||
  120. page.Hits != p.Hits || page.Protected != p.Protected ||
  121. !bytes.Equal(page.Attachment, p.Attachment) {
  122. t.Errorf("expected %#v got %#v", *page, p)
  123. }
  124. }