-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator_test.go
More file actions
158 lines (138 loc) · 5.23 KB
/
Copy pathgenerator_test.go
File metadata and controls
158 lines (138 loc) · 5.23 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
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
package sqlseeder
import (
"fmt"
"sort"
"testing"
"github.com/stretchr/testify/require"
)
func TestGenerator_IsLastIndex(t *testing.T) {
// Test cases
testCases := []struct {
index int
data interface{}
expected bool
}{
{0, []string{"a"}, true},
{1, []string{"a", "b"}, true},
{0, []string{"a", "b"}, false},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("Index %d", tc.index), func(t *testing.T) {
isLast := generator.IsLastIndex(tc.index, tc.data)
if isLast != tc.expected {
t.Errorf("Expected IsLastIndex to be %v, but got %v", tc.expected, isLast)
}
})
}
}
func TestGenerator_GetColumnName(t *testing.T) {
// Test one-to-many column
columnName := generator.GetColumnName("category_id**categories**category_name")
expected := "category_id"
if columnName != expected {
t.Errorf("Expected column name to be '%s', but got '%s'", expected, columnName)
}
// Test regular column
columnName = generator.GetColumnName("name")
expected = "name"
if columnName != expected {
t.Errorf("Expected column name to be '%s', but got '%s'", expected, columnName)
}
}
func TestGenerator_GenerateRootTableDataRow(t *testing.T) {
row := map[string]interface{}{
"id": "1",
"name": "Product 1",
"category_id**categories**category_name": "Electronics",
}
rootColumns := []string{"id", "name", "category_id**categories**category_name"}
result, err := generator.GenerateRootTableDataRow(rootColumns, row, "products")
if err != nil {
t.Error(err)
}
expected := map[string]interface{}{
"id": "'1'",
"name": "'Product 1'",
"category_id**categories**category_name": "(SELECT category_id FROM categories WHERE category_name = 'Electronics')",
}
require.Equal(t, result, expected)
}
func TestGenerator_GenerateTableData(t *testing.T) {
// Sample data
data := []map[string]interface{}{
{
"id": "1",
"product_name": "Product 1",
"category_id**categories**category_name": "Electronics",
"tag_id***product_tags***tags***tag_name***product_name": "tag1|tag2",
},
{
"id": "2",
"product_name": "Product 2",
"category_id**categories**category_name": "Books",
"tag_id***product_tags***tags***tag_name***product_name": "tag3",
},
}
// Expected SQLData
expected := &SQLData{
Statements: []SQLStatement{
{
Table: "products",
Schema: "public",
Columns: []string{"id", "product_name", "category_id**categories**category_name"},
Rows: []map[string]interface{}{
{
"id": "'1'",
"product_name": "'Product 1'",
"category_id**categories**category_name": "(SELECT category_id FROM categories WHERE category_name = 'Electronics')",
},
{
"id": "'2'",
"product_name": "'Product 2'",
"category_id**categories**category_name": "(SELECT category_id FROM categories WHERE category_name = 'Books')",
},
},
},
{
Table: "product_tags",
Schema: "",
Columns: []string{"product_id**public.products**product_name", "tag_id**tags**tag_name"},
Rows: []map[string]interface{}{
{
"product_id**public.products**product_name": "(SELECT product_id FROM public.products WHERE product_name = 'Product 1')",
"tag_id**tags**tag_name": "(SELECT tag_id FROM tags WHERE tag_name = 'tag1')",
},
{
"product_id**public.products**product_name": "(SELECT product_id FROM public.products WHERE product_name = 'Product 1')",
"tag_id**tags**tag_name": "(SELECT tag_id FROM tags WHERE tag_name = 'tag2')",
},
{
"product_id**public.products**product_name": "(SELECT product_id FROM public.products WHERE product_name = 'Product 2')",
"tag_id**tags**tag_name": "(SELECT tag_id FROM tags WHERE tag_name = 'tag3')",
},
},
},
},
}
// Generate SQLData
result, err := generator.GenerateTableData(data, "public", "products")
require.NoError(t, err)
// Compare results
require.Equal(t, len(expected.Statements), len(result.Statements))
for i := range expected.Statements {
// Sort the Rows slice to ignore order when comparing
sort.Slice(expected.Statements[i].Rows, func(j, k int) bool {
return fmt.Sprint(expected.Statements[i].Rows[j]) < fmt.Sprint(expected.Statements[i].Rows[k])
})
sort.Slice(result.Statements[i].Rows, func(j, k int) bool {
return fmt.Sprint(result.Statements[i].Rows[j]) < fmt.Sprint(result.Statements[i].Rows[k])
})
require.Equal(t, expected.Statements[i].Table, result.Statements[i].Table)
require.Equal(t, expected.Statements[i].Schema, result.Statements[i].Schema)
require.Equal(t, expected.Statements[i].Columns, result.Statements[i].Columns)
require.Equal(t, expected.Statements[i].Rows, result.Statements[i].Rows)
}
}
func TestGenerator_Generate(t *testing.T) {
// ... (This test requires reading from a template file and generating SQL, so it's also best to implement based on your specific template and logic) ...
}