forked from ligato/cn-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcassa_data_test.go
164 lines (139 loc) · 4.74 KB
/
cassa_data_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
// 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 (
"errors"
"github.com/gocql/gocql"
"github.com/ligato/cn-infra/db/sql"
"github.com/ligato/cn-infra/db/sql/cassandra"
"github.com/maraino/go-mock"
"github.com/willfaught/gockle"
)
// test data
var JamesBond = &User{ID: "James Bond", FirstName: "James", LastName: "Bond"}
var PeterBond = &User{ID: "Peter Bond", FirstName: "Peter", LastName: "Bond"}
var myID gocql.UUID = gocql.TimeUUID()
var MyTweet = &Tweet{ID: myID.String(), Text: "hello"}
// instance that represents users table (used in queries to define columns)
var UserTable = &User{}
// instance that represents tweets table (used in queries to define columns)
var TweetTable = &Tweet{}
//var UsersTypeInfo = map[string /*FieldName*/ ]gocql.TypeInfo{
//runtimeutils.GetFunctionName(UserTable.GetLastName): gocql.NewNativeType(0x03, gocql.TypeVarchar, ""),
//"LastName": gocql.NewNativeType(0x03, gocql.TypeVarchar, ""),
//}
// User is simple structure for testing purposes
type User struct {
ID string `cql:"id" pk:"id"`
FirstName string `cql:"first_name"`
LastName string `cql:"last_name"`
ExportedButNotCql string `cql:"-"`
notExported string
}
// Tweet structure using uuid for testing purposes
type Tweet struct {
ID string `cql:"id" pk:"id"`
Text string `cql:"text"`
}
// CustomizedTablenameAndSchema implements sql.TableName, sql.SchemaName interfaces
type CustomizedTablenameAndSchema struct {
ID string `cql:"id" pk:"id"`
LastName string `cql:"last_name"`
}
// TableName implements sql.TableName interface
func (entity *CustomizedTablenameAndSchema) TableName() string {
return "my_custom_name"
}
// SchemaName implements sql.SchemaName interface
func (entity *CustomizedTablenameAndSchema) SchemaName() string {
return "my_custom_schema"
}
// simple structure that holds values of one row for mock iterator
type row struct {
values []interface{}
fields []string
}
// mockQuery is a helper for testing. It setups mock iterator
func mockQuery(sessionMock *gockle.SessionMock, query sql.Expression, rows ...*row) {
sqlStr, _ /*binding*/, err := cassandra.SelectExpToString(query)
if err != nil {
panic(err.Error())
}
sessionMock.When("ScanIterator", sqlStr, mock.Any).Return(&IteratorMock{rows: rows})
sessionMock.When("Close").Return()
}
// mockExec is a helper for testing. It setups mock iterator with any parameters/arguments
func mockExec(sessionMock *gockle.SessionMock, query string, binding []interface{}) {
sessionMock.When("Exec", query, mock.Any).Return(nil)
sessionMock.When("Close").Return()
}
// cells is a helper that harvests all exported fields values
func cells(entity interface{}) (cellsInRow *row) {
fields, values := cassandra.SliceOfFieldsWithValPtrs(entity)
return &row{values, fields}
}
// IteratorMock is a mock Iterator. See github.com/maraino/go-mock.
type IteratorMock struct {
index int
rows []*row
closed bool
lastErr error
}
// Close implements Iterator.
func (m IteratorMock) Close() error {
if m.closed {
return errors.New("already closed")
}
return m.lastErr
}
// Scan implements Iterator.
func (m *IteratorMock) Scan(results ...interface{}) bool {
if len(m.rows) > m.index {
for i := 0; i < len(results) && i < len(m.rows[m.index].values); i++ {
// TODO !!! types of fields
typeInfo := gocql.NewNativeType(0x03, gocql.TypeVarchar, "")
bytes, err := gocql.Marshal(typeInfo, m.rows[m.index].values[i])
if err != nil {
m.lastErr = err
return false
}
err = gocql.Unmarshal(typeInfo, bytes, results[i])
if err != nil {
m.lastErr = err
return false
}
}
m.index++
return true
}
return false
}
// ScanMap implements Iterator.
func (m *IteratorMock) ScanMap(results map[string]interface{}) bool {
if len(m.rows) > m.index {
for i := 0; i < len(m.rows[m.index].values) && i < len(m.rows[m.index].fields); i++ {
key := m.rows[m.index].fields[i]
value := m.rows[m.index].values[i]
results[key] = value
}
m.index++
return true
}
return false
}
func mockSession() (sessionMock *gockle.SessionMock) {
sessionMock = &gockle.SessionMock{}
sessionMock.When("Close").Return(nil)
return sessionMock
}