Skip to content

Commit 85b3162

Browse files
Move randomText function to internal/tests/rand and use internal API in vector_test
xd
1 parent cc7774b commit 85b3162

File tree

3 files changed

+57
-55
lines changed

3 files changed

+57
-55
lines changed

common_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"flag"
2929
"fmt"
3030
"log"
31-
"math/rand"
3231
"net"
3332
"strings"
3433
"sync"
@@ -53,10 +52,6 @@ var (
5352
flagCassVersion cassVersion
5453
)
5554

56-
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
57-
58-
const randCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
59-
6055
func init() {
6156
flag.Var(&flagCassVersion, "gocql.cversion", "the cassandra version being tested against")
6257

@@ -424,11 +419,3 @@ func staticAddressTranslator(newAddr net.IP, newPort int) AddressTranslator {
424419
return newAddr, newPort
425420
})
426421
}
427-
428-
func randomText(size int) string {
429-
result := make([]byte, size)
430-
for i := range result {
431-
result[i] = randCharset[rand.Intn(len(randCharset))]
432-
}
433-
return string(result)
434-
}

internal/tests/rand.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tests
33
import (
44
"math/rand"
55
"sync"
6+
"time"
67
)
78

89
// RandInterface defines the thread-safe random number generator interface.
@@ -162,3 +163,15 @@ func (r *ThreadSafeRand) Read(p []byte) (n int, err error) {
162163
defer r.mux.Unlock()
163164
return r.r.Read(p)
164165
}
166+
167+
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
168+
169+
const randCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
170+
171+
func RandomText(size int) string {
172+
result := make([]byte, size)
173+
for i := range result {
174+
result[i] = randCharset[rand.Intn(len(randCharset))]
175+
}
176+
return string(result)
177+
}

vector_test.go

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636

3737
"github.com/stretchr/testify/require"
3838
"gopkg.in/inf.v0"
39+
40+
"github.com/gocql/gocql/internal/tests"
3941
)
4042

4143
type person struct {
@@ -76,9 +78,9 @@ func TestVector_Marshaler(t *testing.T) {
7678
if err != nil {
7779
t.Fatal(err)
7880
}
79-
assertDeepEqual(t, "fixed size element vector", insertFixVec, selectFixVec)
81+
tests.AssertDeepEqual(t, "fixed size element vector", insertFixVec, selectFixVec)
8082

81-
longText := randomText(500)
83+
longText := tests.RandomText(500)
8284
insertVarVec := []string{"apache", "cassandra", longText, "gocql"}
8385
err = session.Query("INSERT INTO vector_variable(id, vec) VALUES(?, ?)", 1, insertVarVec).Exec()
8486
if err != nil {
@@ -89,7 +91,7 @@ func TestVector_Marshaler(t *testing.T) {
8991
if err != nil {
9092
t.Fatal(err)
9193
}
92-
assertDeepEqual(t, "variable size element vector", insertVarVec, selectVarVec)
94+
tests.AssertDeepEqual(t, "variable size element vector", insertVarVec, selectVarVec)
9395
}
9496

9597
func TestVector_Types(t *testing.T) {
@@ -124,7 +126,7 @@ func TestVector_Types(t *testing.T) {
124126
map2["abc"] = 123
125127
map3 := make(map[string]int)
126128

127-
tests := []struct {
129+
testCases := []struct {
128130
name string
129131
cqlType string
130132
value interface{}
@@ -152,9 +154,9 @@ func TestVector_Types(t *testing.T) {
152154
comparator: func(e interface{}, a interface{}) {
153155
expected := e.([]net.IP)
154156
actual := a.([]net.IP)
155-
assertEqual(t, "vector size", len(expected), len(actual))
157+
tests.AssertEqual(t, "vector size", len(expected), len(actual))
156158
for i, _ := range expected {
157-
assertTrue(t, "vector", expected[i].Equal(actual[i]))
159+
tests.AssertTrue(t, "vector", expected[i].Equal(actual[i]))
158160
}
159161
},
160162
},
@@ -176,7 +178,7 @@ func TestVector_Types(t *testing.T) {
176178
{name: "vector_map_text_int", cqlType: "map<text, int>", value: []map[string]int{map1, map2, map3}},
177179
}
178180

179-
for _, test := range tests {
181+
for _, test := range testCases {
180182
t.Run(test.name, func(t *testing.T) {
181183
tableName := fmt.Sprintf("vector_%s", test.name)
182184
err := createTable(session, fmt.Sprintf(`CREATE TABLE IF NOT EXISTS gocql_test.%s(id int primary key, vec vector<%s, 3>);`, tableName, test.cqlType))
@@ -197,7 +199,7 @@ func TestVector_Types(t *testing.T) {
197199
if test.comparator != nil {
198200
test.comparator(test.value, v.Elem().Interface())
199201
} else {
200-
assertDeepEqual(t, "vector", test.value, v.Elem().Interface())
202+
tests.AssertDeepEqual(t, "vector", test.value, v.Elem().Interface())
201203
}
202204
})
203205
}
@@ -244,7 +246,7 @@ func TestVector_MarshalerUDT(t *testing.T) {
244246
t.Fatal(err)
245247
}
246248

247-
assertDeepEqual(t, "udt", &insVec, &selVec)
249+
tests.AssertDeepEqual(t, "udt", &insVec, &selVec)
248250
}
249251

250252
func TestVector_Empty(t *testing.T) {
@@ -274,7 +276,7 @@ func TestVector_Empty(t *testing.T) {
274276
if err != nil {
275277
t.Fatal(err)
276278
}
277-
assertTrue(t, "fixed size element vector is empty", selectFixVec == nil)
279+
tests.AssertTrue(t, "fixed size element vector is empty", selectFixVec == nil)
278280

279281
err = session.Query("INSERT INTO vector_variable_null(id) VALUES(?)", 1).Exec()
280282
if err != nil {
@@ -285,7 +287,7 @@ func TestVector_Empty(t *testing.T) {
285287
if err != nil {
286288
t.Fatal(err)
287289
}
288-
assertTrue(t, "variable size element vector is empty", selectVarVec == nil)
290+
tests.AssertTrue(t, "variable size element vector is empty", selectVarVec == nil)
289291
}
290292

291293
func TestVector_MissingDimension(t *testing.T) {
@@ -309,7 +311,7 @@ func TestVector_MissingDimension(t *testing.T) {
309311
}
310312

311313
func TestVector_SubTypeParsing(t *testing.T) {
312-
tests := []struct {
314+
testCases := []struct {
313315
name string
314316
custom string
315317
expected TypeInfo
@@ -320,22 +322,22 @@ func TestVector_SubTypeParsing(t *testing.T) {
320322
name: "udt",
321323
custom: "org.apache.cassandra.db.marshal.UserType(gocql_test,706572736f6e,66697273745f6e616d65:org.apache.cassandra.db.marshal.UTF8Type,6c6173745f6e616d65:org.apache.cassandra.db.marshal.UTF8Type,616765:org.apache.cassandra.db.marshal.Int32Type)",
322324
expected: UDTTypeInfo{
323-
NativeType{typ: TypeUDT},
324-
"gocql_test",
325-
"person",
326-
[]UDTField{
327-
UDTField{"first_name", NativeType{typ: TypeVarchar}},
328-
UDTField{"last_name", NativeType{typ: TypeVarchar}},
329-
UDTField{"age", NativeType{typ: TypeInt}},
325+
NativeType: NativeType{typ: TypeUDT},
326+
KeySpace: "gocql_test",
327+
Name: "person",
328+
Elements: []UDTField{
329+
UDTField{Name: "first_name", Type: NativeType{typ: TypeVarchar}},
330+
UDTField{Name: "last_name", Type: NativeType{typ: TypeVarchar}},
331+
UDTField{Name: "age", Type: NativeType{typ: TypeInt}},
330332
},
331333
},
332334
},
333335
{
334336
name: "tuple",
335337
custom: "org.apache.cassandra.db.marshal.TupleType(org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.Int32Type,org.apache.cassandra.db.marshal.UTF8Type)",
336338
expected: TupleTypeInfo{
337-
NativeType{typ: TypeTuple},
338-
[]TypeInfo{
339+
NativeType: NativeType{typ: TypeTuple},
340+
Elems: []TypeInfo{
339341
NativeType{typ: TypeVarchar},
340342
NativeType{typ: TypeInt},
341343
NativeType{typ: TypeVarchar},
@@ -346,57 +348,57 @@ func TestVector_SubTypeParsing(t *testing.T) {
346348
name: "vector_vector_inet",
347349
custom: "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.InetAddressType, 2), 3)",
348350
expected: VectorType{
349-
NativeType{typ: TypeCustom, custom: "org.apache.cassandra.db.marshal.VectorType"},
350-
VectorType{
351-
NativeType{typ: TypeCustom, custom: "org.apache.cassandra.db.marshal.VectorType"},
352-
NativeType{typ: TypeInet},
353-
2,
351+
NativeType: NativeType{typ: TypeCustom, custom: "org.apache.cassandra.db.marshal.VectorType"},
352+
SubType: VectorType{
353+
NativeType: NativeType{typ: TypeCustom, custom: "org.apache.cassandra.db.marshal.VectorType"},
354+
SubType: NativeType{typ: TypeInet},
355+
Dimensions: 2,
354356
},
355-
3,
357+
Dimensions: 3,
356358
},
357359
},
358360
{
359361
name: "map_int_vector_text",
360362
custom: "org.apache.cassandra.db.marshal.MapType(org.apache.cassandra.db.marshal.Int32Type,org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.UTF8Type, 10))",
361363
expected: CollectionType{
362-
NativeType{typ: TypeMap},
363-
NativeType{typ: TypeInt},
364-
VectorType{
365-
NativeType{typ: TypeCustom, custom: "org.apache.cassandra.db.marshal.VectorType"},
366-
NativeType{typ: TypeVarchar},
367-
10,
364+
NativeType: NativeType{typ: TypeMap},
365+
Key: NativeType{typ: TypeInt},
366+
Elem: VectorType{
367+
NativeType: NativeType{typ: TypeCustom, custom: "org.apache.cassandra.db.marshal.VectorType"},
368+
SubType: NativeType{typ: TypeVarchar},
369+
Dimensions: 10,
368370
},
369371
},
370372
},
371373
{
372374
name: "set_map_vector_text_text",
373375
custom: "org.apache.cassandra.db.marshal.SetType(org.apache.cassandra.db.marshal.MapType(org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.Int32Type, 10),org.apache.cassandra.db.marshal.UTF8Type))",
374376
expected: CollectionType{
375-
NativeType{typ: TypeSet},
376-
nil,
377-
CollectionType{
377+
NativeType: NativeType{typ: TypeSet},
378+
Key: nil,
379+
Elem: CollectionType{
378380
NativeType{typ: TypeMap},
379381
VectorType{
380-
NativeType{typ: TypeCustom, custom: "org.apache.cassandra.db.marshal.VectorType"},
381-
NativeType{typ: TypeInt},
382-
10,
382+
NativeType: NativeType{typ: TypeCustom, custom: "org.apache.cassandra.db.marshal.VectorType"},
383+
SubType: NativeType{typ: TypeInt},
384+
Dimensions: 10,
383385
},
384386
NativeType{typ: TypeVarchar},
385387
},
386388
},
387389
},
388390
}
389391

390-
for _, test := range tests {
392+
for _, test := range testCases {
391393
t.Run(test.name, func(t *testing.T) {
392394
f := newFramer(nil, 0)
393395
f.writeShort(0)
394396
f.writeString(fmt.Sprintf("org.apache.cassandra.db.marshal.VectorType(%s, 2)", test.custom))
395397
parsedType := f.readTypeInfo()
396398
require.IsType(t, parsedType, VectorType{})
397399
vectorType := parsedType.(VectorType)
398-
assertEqual(t, "dimensions", 2, vectorType.Dimensions)
399-
assertDeepEqual(t, "vector", test.expected, vectorType.SubType)
400+
tests.AssertEqual(t, "dimensions", 2, vectorType.Dimensions)
401+
tests.AssertDeepEqual(t, "vector", test.expected, vectorType.SubType)
400402
})
401403
}
402404
}

0 commit comments

Comments
 (0)