-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_test.go
More file actions
133 lines (120 loc) · 3.5 KB
/
sql_test.go
File metadata and controls
133 lines (120 loc) · 3.5 KB
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
package dalgo2sql
import (
"testing"
"time"
"github.com/dal-go/dalgo/dal"
)
func TestProcessPrimaryKey(t *testing.T) {
t.Run("single_key", func(t *testing.T) {
key := dal.NewKeyWithID("users", "u1")
processPrimaryKey([]string{"ID"}, key, func(i int, name string, v any) {
if i != 0 || name != "ID" || v != "u1" {
t.Errorf("unexpected values: i=%d, name=%s, v=%v", i, name, v)
}
})
})
t.Run("composite_string_key", func(t *testing.T) {
id := []string{"u1", "p1"}
key := &dal.Key{ID: id}
processPrimaryKey([]string{"ID", "ParentID"}, key, func(i int, name string, v any) {
switch i {
case 0:
if name != "ID" || v != "u1" {
t.Errorf("unexpected values at 0: name=%s, v=%v", name, v)
}
case 1:
if name != "ParentID" || v != "p1" {
t.Errorf("unexpected values at 1: name=%s, v=%v", name, v)
}
}
})
})
t.Run("composite_int_key", func(t *testing.T) {
id := []int{1, 2}
key := &dal.Key{ID: id}
processPrimaryKey([]string{"K1", "K2"}, key, func(i int, name string, v any) {
if v != i+1 {
t.Errorf("expected %d, got %v", i+1, v)
}
})
})
t.Run("composite_int8_key", func(t *testing.T) {
id := []int8{1, 2}
key := &dal.Key{ID: id}
processPrimaryKey([]string{"K1", "K2"}, key, func(i int, name string, v any) {
if v != int8(i+1) {
t.Errorf("expected %d, got %v", i+1, v)
}
})
})
t.Run("composite_int16_key", func(t *testing.T) {
id := []int16{1, 2}
key := &dal.Key{ID: id}
processPrimaryKey([]string{"K1", "K2"}, key, func(i int, name string, v any) {
if v != int16(i+1) {
t.Errorf("expected %d, got %v", i+1, v)
}
})
})
t.Run("composite_int32_key", func(t *testing.T) {
id := []int32{1, 2}
key := &dal.Key{ID: id}
processPrimaryKey([]string{"K1", "K2"}, key, func(i int, name string, v any) {
if v != int32(i+1) {
t.Errorf("expected %d, got %v", i+1, v)
}
})
})
t.Run("composite_int64_key", func(t *testing.T) {
id := []int64{1, 2}
key := &dal.Key{ID: id}
processPrimaryKey([]string{"K1", "K2"}, key, func(i int, name string, v any) {
if v != int64(i+1) {
t.Errorf("expected %d, got %v", i+1, v)
}
})
})
t.Run("composite_time_key", func(t *testing.T) {
now := time.Now()
key := &dal.Key{ID: []time.Time{now, now}}
processPrimaryKey([]string{"T1", "T2"}, key, func(i int, name string, v any) {
if v != now {
t.Errorf("expected %v, got %v", now, v)
}
})
})
t.Run("unsupported_type", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic")
}
}()
key := dal.NewKeyWithID("users", 1.23)
processPrimaryKey([]string{"K1", "K2"}, key, func(i int, name string, v any) {})
})
}
func TestBuildSingleRecordQuery_Panics(t *testing.T) {
t.Run("insert_no_pk_defined", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic")
}
}()
record := dal.NewRecordWithData(dal.NewKeyWithID("users", "u1"), &user2{Name: "John"})
buildSingleRecordQuery(insertOperation, DbOptions{}, record)
})
t.Run("update_no_fields", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic")
}
}()
// If we mark "Name" as part of PK, there will be no fields to update
record := dal.NewRecordWithData(dal.NewKeyWithID("users", "u1"), &user2{Name: "John"})
buildSingleRecordQuery(updateOperation, DbOptions{
Recordsets: map[string]*Recordset{
"users": NewRecordset("users", Table, []dal.FieldRef{dal.Field("ID"), dal.Field("Name")}),
},
}, record)
})
}