-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathspqrql_test.go
126 lines (114 loc) · 3.75 KB
/
spqrql_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
package decode
import (
"testing"
"github.com/pg-sharding/spqr/pkg/models/kr"
protos "github.com/pg-sharding/spqr/pkg/protos"
"github.com/pg-sharding/spqr/qdb"
"github.com/stretchr/testify/assert"
)
// TestKeyRange is a unit test function for the KeyRange function.
//
// It tests the KeyRange function by creating a key range with the given parameters and
// asserting that the returned string matches the expected value.
//
// Parameters:
// - t (*testing.T): The testing object used for assertions.
//
// Returns:
// - None.
func TestKeyRange(t *testing.T) {
assert := assert.New(t)
assert.Equal("CREATE KEY RANGE kr1 FROM 10 ROUTE TO sh1 FOR DISTRIBUTION ds1;",
KeyRange(&kr.KeyRange{
ID: "kr1",
ShardID: "sh1",
Distribution: "ds1",
LowerBound: []interface{}{10},
ColumnTypes: []string{qdb.ColumnTypeInteger},
}))
// UpperBound is ignored
assert.Equal("CREATE KEY RANGE kr1 FROM 10 ROUTE TO sh1 FOR DISTRIBUTION ds1;",
KeyRange(
&kr.KeyRange{
ID: "kr1",
ShardID: "sh1",
Distribution: "ds1",
LowerBound: []interface{}{10},
ColumnTypes: []string{qdb.ColumnTypeInteger},
}))
}
// TestDistribution is a unit test function for the Distribution function.
//
// It tests the Distribution function by creating a distribution with the given parameters and
// asserting that the returned string matches the expected value.
//
// Parameters:
// - t (*testing.T): The testing object used for assertions.
//
// Returns:
// - None.
func TestDistribution(t *testing.T) {
assert := assert.New(t)
assert.Equal("CREATE DISTRIBUTION ds1 COLUMN TYPES integer;",
Distribution(&protos.Distribution{
Id: "ds1",
ColumnTypes: []string{"integer"},
}))
// relations ignored
assert.Equal("CREATE DISTRIBUTION ds1 COLUMN TYPES integer;",
Distribution(&protos.Distribution{
Id: "ds1",
ColumnTypes: []string{"integer"},
Relations: []*protos.DistributedRelation{
{Name: "rel", DistributionKey: []*protos.DistributionKeyEntry{{Column: "id", HashFunction: "identity"}}},
},
}))
// multiple types
assert.Equal("CREATE DISTRIBUTION ds1 COLUMN TYPES integer, varchar;",
Distribution(&protos.Distribution{
Id: "ds1",
ColumnTypes: []string{"integer", "varchar"},
}))
// order is preserved
assert.Equal("CREATE DISTRIBUTION ds1 COLUMN TYPES varchar, integer;",
Distribution(&protos.Distribution{
Id: "ds1",
ColumnTypes: []string{"varchar", "integer"},
}))
}
// TestDistributedRelation is a unit test function for the DistributedRelation function.
//
// It tests the DistributedRelation function by asserting that the returned string matches the expected string.
//
// Parameters:
// - t (*testing.T): The testing object used for assertions.
//
// Returns:
// - None.
func TestDistributedRelation(t *testing.T) {
assert := assert.New(t)
assert.Equal("ALTER DISTRIBUTION ds1 ATTACH RELATION rel DISTRIBUTION KEY id HASH FUNCTION identity;",
DistributedRelation(&protos.DistributedRelation{
Name: "rel",
DistributionKey: []*protos.DistributionKeyEntry{{Column: "id", HashFunction: "identity"}},
}, "ds1"),
)
// missing hash func
assert.Equal("ALTER DISTRIBUTION ds1 ATTACH RELATION rel DISTRIBUTION KEY id;",
DistributedRelation(&protos.DistributedRelation{
Name: "rel",
DistributionKey: []*protos.DistributionKeyEntry{{Column: "id"}},
}, "ds1"),
)
// multiple columns
assert.Equal("ALTER DISTRIBUTION ds1 ATTACH RELATION rel DISTRIBUTION KEY id HASH FUNCTION identity, "+
"id2 HASH FUNCTION city;",
DistributedRelation(&protos.DistributedRelation{
Name: "rel",
DistributionKey: []*protos.DistributionKeyEntry{
{Column: "id", HashFunction: "identity"},
{Column: "id2", HashFunction: "city"},
},
}, "ds1"),
)
}