forked from ligato/cn-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcassa_put_test.go
135 lines (110 loc) · 4.38 KB
/
cassa_put_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
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cassandra_test
import (
"testing"
"github.com/ligato/cn-infra/db/sql"
"github.com/ligato/cn-infra/db/sql/cassandra"
"github.com/onsi/gomega"
)
// TestPut1_convenient is most convenient way of putting one entity to cassandra
func TestPut1_convenient(t *testing.T) {
gomega.RegisterTestingT(t)
session := mockSession()
defer session.Close()
db := cassandra.NewBrokerUsingSession(session)
sqlStr, _, _ := cassandra.PutExpToString(sql.FieldEQ(&JamesBond.ID), JamesBond)
gomega.Expect(sqlStr).Should(gomega.BeEquivalentTo(
"UPDATE User SET first_name = ?, last_name = ? WHERE id = ?"))
mockExec(session, sqlStr, []interface{}{
"James Bond", //set ID
"James", //set first_name
"Bond", //set last_name
"James Bond", //where
})
err := db.Put(sql.FieldEQ(&JamesBond.ID), JamesBond)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
}
// TestPut2_EQ is most convenient way of putting one entity to cassandra
func TestPut2_EQ(t *testing.T) {
gomega.RegisterTestingT(t)
session := mockSession()
defer session.Close()
db := cassandra.NewBrokerUsingSession(session)
sqlStr, _, _ := cassandra.PutExpToString(sql.FieldEQ(&JamesBond.ID), JamesBond)
gomega.Expect(sqlStr).Should(gomega.BeEquivalentTo(
"UPDATE User SET first_name = ?, last_name = ? WHERE id = ?"))
mockExec(session, sqlStr, []interface{}{
"James Bond", //set ID
"James", //set first_name
"Bond", //set last_name
"James Bond", //where
})
err := db.Put(sql.Field(&JamesBond.ID, sql.EQ(JamesBond.ID)), JamesBond)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
}
// TestPut3_customTableSchema checks that generated SQL statements
// contain customized table name & schema (see interfaces sql.TableName, sql.SchemaName)
func TestPut3_customTableSchema(t *testing.T) {
gomega.RegisterTestingT(t)
session := mockSession()
defer session.Close()
db := cassandra.NewBrokerUsingSession(session)
entity := &CustomizedTablenameAndSchema{ID: "id", LastName: "Bond"}
sqlStr, _, _ := cassandra.PutExpToString(sql.FieldEQ(&entity.ID), entity)
gomega.Expect(sqlStr).Should(gomega.BeEquivalentTo(
"UPDATE my_custom_schema.my_custom_name SET last_name = ? WHERE id = ?"))
mockExec(session, sqlStr, []interface{}{
"James Bond", //set ID
"James", //set first_name
"Bond", //set last_name
"James Bond", //where
})
err := db.Put(sql.FieldEQ(&entity.ID), entity)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
}
// TestPut4_uuid_EQ used to verify inserting entity having a field of type gocql.UUID and using FieldEQ in where condition
func TestPut4_uuid_FieldEQ(t *testing.T) {
gomega.RegisterTestingT(t)
session := mockSession()
defer session.Close()
db := cassandra.NewBrokerUsingSession(session)
sqlStr, _, _ := cassandra.PutExpToString(sql.FieldEQ(&MyTweet.ID), MyTweet)
gomega.Expect(sqlStr).Should(gomega.BeEquivalentTo(
"UPDATE Tweet SET text = ? WHERE id = ?"))
mockExec(session, sqlStr, []interface{}{
myID, //set ID
"hello world", //set Text
myID, //where
})
err := db.Put(sql.FieldEQ(&MyTweet.ID), MyTweet)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
}
// TestPut5_EQ used to verify inserting entity having a field of type gocql.UUID and using EQ in where condition
func TestPut5_uuid_EQ(t *testing.T) {
gomega.RegisterTestingT(t)
session := mockSession()
defer session.Close()
db := cassandra.NewBrokerUsingSession(session)
sqlStr, _, _ := cassandra.PutExpToString(sql.FieldEQ(&MyTweet.ID), MyTweet)
gomega.Expect(sqlStr).Should(gomega.BeEquivalentTo(
"UPDATE Tweet SET text = ? WHERE id = ?"))
mockExec(session, sqlStr, []interface{}{
myID, //set ID
"hello world", //set Text
myID, //where
})
err := db.Put(sql.Field(&MyTweet.ID, sql.EQ(MyTweet.ID)), MyTweet)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
}