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