-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnegronicql_test.go
127 lines (78 loc) · 1.96 KB
/
negronicql_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
package negronicql_test
import (
"flag"
"fmt"
"testing"
"github.com/Dal-Papa/negronicql"
"github.com/gocql/gocql"
)
var (
keyspace = "mikebthun_negronicql_middleware_tests"
ips []string
columnFamily = "go_db_package_test"
email = "test@test.com"
ip *string
session *gocql.Session
)
func TestIndex(t *testing.T) {
conn := negronicql.New()
err := conn.Connect()
if err != nil {
t.Fatalf("Is cassandra running?: %s", err)
}
session = conn.Session
}
func TestSetup(t *testing.T) {
ip = flag.String("ip", "127.0.0.1", "Cassandra Ip")
ips = []string{*ip}
flag.Parse()
//create the keyspace if does not exist
cql := fmt.Sprintf(`
CREATE KEYSPACE %s WITH REPLICATION = {
'class' : 'SimpleStrategy',
'replication_factor' : 1
}`, keyspace)
session.Query(cql).Exec()
cql = fmt.Sprintf("DROP TABLE %s.%s", keyspace, columnFamily)
session.Query(cql).Exec()
cql = fmt.Sprintf(`
CREATE TABLE %s.%s
(
email text,
first text,
last text,
PRIMARY KEY ( email )
)
`, keyspace, columnFamily)
if err := session.Query(cql).Exec(); err != nil {
t.Errorf("%s", err)
}
}
func TestInsertWithParams(t *testing.T) {
cql := fmt.Sprintf(`
INSERT INTO %s.%s
(email, first, last)
VALUES ( ?, ?, ? )
`, keyspace, columnFamily)
session.Query(cql, email, "Mike", "B").Exec()
}
func TestSelectWithParams(t *testing.T) {
cql := fmt.Sprintf(`
SELECT email
FROM %s.%s
WHERE email = ?
LIMIT 1
`, keyspace, columnFamily)
var check_email string
if err := session.Query(cql, email).Consistency(gocql.One).Scan(&check_email); err != nil {
t.Fatalf("Query failed %s", err.Error())
}
if check_email != email {
t.Errorf("Email should be %s but is %s.", email, check_email)
}
}
func TestCleanup(t *testing.T) {
cql := fmt.Sprintf("DROP KEYSPACE %s", keyspace)
session.Query(cql).Exec()
defer session.Close()
}