-
Notifications
You must be signed in to change notification settings - Fork 0
/
bqt_test.go
106 lines (93 loc) · 3.41 KB
/
bqt_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
package bqt
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestReplaceNoPreviousAlias(t *testing.T) {
replacement := Replacement{
TableFullName: "`one`.`two`.`three`",
ReplaceSql: "select * from x",
TableShortName: "new_table_name",
}
replaced := Replace("from `one`.`two`.`three` do something", replacement)
assert.Equal(t, replaced, "from (select * from x) AS new_table_name do something")
}
func TestReplacePreviousAlias(t *testing.T) {
replacement := Replacement{
TableFullName: "`one`.`two`.`three`",
ReplaceSql: "select * from x",
TableShortName: "new_table_name",
}
replaced := Replace("from `one`.`two`.`three` AS ALIAS1 do something", replacement)
assert.Equal(t, replaced, "from (select * from x) AS ALIAS1 do something")
}
func TestMockToSql(t *testing.T) {
m := Mock{Filepath: "tests_data/sample.csv"}
mockAsSQl, err := mockToSql(m)
assert.Nil(t, err)
expectedSQL1 := "SELECT \"something\" AS column1, \"1.0\" AS column2, \"100\" AS column3"
expectedSQL2 := "SELECT \"something2\" AS column1, \"2.0\" AS column2, \"200\" AS column3"
expectedSQL3 := "SELECT \"something3\" AS column1, \"3.0\" AS column2, \"300\" AS column3"
assert.True(t, strings.Contains(mockAsSQl.Sql, expectedSQL1))
assert.True(t, strings.Contains(mockAsSQl.Sql, expectedSQL2))
assert.True(t, strings.Contains(mockAsSQl.Sql, expectedSQL3))
}
func TestMockToSqlWithTypes(t *testing.T) {
m := Mock{Filepath: "tests_data/sample.csv", Types: map[string]string{"column2": "INT64"}}
mockAsSQl, err := mockToSql(m)
assert.Nil(t, err)
expectedSQL1 := "SELECT \"something\" AS column1, CAST(\"1.0\" AS INT64) AS column2, \"100\" AS column3"
expectedSQL2 := "SELECT \"something2\" AS column1, CAST(\"2.0\" AS INT64) AS column2, \"200\" AS column3"
expectedSQL3 := "SELECT \"something3\" AS column1, CAST(\"3.0\" AS INT64) AS column2, \"300\" AS column3"
assert.True(t, strings.Contains(mockAsSQl.Sql, expectedSQL1))
assert.True(t, strings.Contains(mockAsSQl.Sql, expectedSQL2))
assert.True(t, strings.Contains(mockAsSQl.Sql, expectedSQL3))
}
func TestParseJson(t *testing.T) {
test, err := ParseTest("tests_data/test1.json")
assert.Nil(t, err)
expectedTest := Test{
SourceFile: "tests_data/test1.json",
Name: "simple_test",
Output: Mock{
Filepath: "tests_data/out.csv",
Types: map[string]string{
"column1": "string",
},
},
File: "tests_data/test1.sql",
FileContent: "select column1 from `dataset`.`table`",
Mocks: map[string]Mock{
"`dataset`.`table`": Mock{
Filepath: "tests_data/test1_in1.csv",
Types: map[string]string{
"c1": "int64",
},
},
},
}
assert.Equal(t, test, expectedTest)
}
func TestRunBQT(t *testing.T) {
// Simple test
test, err := ParseTest("tests_data/test1.json")
err = RunTests("local", []Test{test})
assert.Nil(t, err)
// Simple test with aliases and various tables
test2, err := ParseTest("tests_data/test2.json")
err = RunTests("local", []Test{test2})
assert.Nil(t, err)
// failing test with missing data
test3, err := ParseTest("tests_data/test3.json")
err = RunTests("local", []Test{test3})
assert.NotNil(t, err)
// failing test with extra data
test4, err := ParseTest("tests_data/test4.json")
err = RunTests("local", []Test{test4})
assert.NotNil(t, err)
// failing test with extra data and missing data
test5, err := ParseTest("tests_data/test4.json")
err = RunTests("local", []Test{test5})
assert.NotNil(t, err)
}