-
Notifications
You must be signed in to change notification settings - Fork 3
/
config_test.go
133 lines (102 loc) · 3.66 KB
/
config_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
127
128
129
130
131
132
133
package mysqldumper
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var sampleConfig = `
{
"tables": {
"users": "WHERE id = 1",
"carts": "WHERE user_id=1 AND item_id=2"
},
"columns": {
"user_id": "WHERE user_id = 1",
"cart_id": "WHERE cart_id = 3"
},
"size": {
"gt": "10 MiB",
"filters": {
"*.id": "ORDER BY id DESC LIMIT 30",
"*.created_at": "ORDER BY created_at DESC LIMIT 30",
"items": "LIMIT 10"
}
},
"replacements": {
"users.password": "MD5('123456')"
},
"filters": {
"table1": "onlydata",
"table2": "nodata",
"table3": "ignore",
"*": "onlydata"
}
}
`
func TestDumFilter(t *testing.T) {
Convey("Parse config file", t, func() {
_, err := ParseConfig([]byte(sampleConfig))
So(err, ShouldBeNil)
})
Convey("Check config parameters", t, func() {
dumper, err := ParseConfig([]byte(sampleConfig))
So(err, ShouldBeNil)
filter := dumper.GetDumpFilter(Table{Name: "users"})
So(filter, ShouldEqual, "WHERE id = 1")
filter = dumper.GetDumpFilter(Table{Name: "carts"})
So(filter, ShouldEqual, "WHERE user_id=1 AND item_id=2")
filter = dumper.GetDumpFilter(Table{Name: "items", Columns: &Columns{"id", "cart_id", "name", "value"}})
So(filter, ShouldEqual, "WHERE cart_id = 3")
size := Size(10485760) // ==10 MiB
filter = dumper.GetDumpFilter(Table{Name: "logs", Size: &size})
So(filter, ShouldEqual, "")
size = Size(10485760 + 1) // >10 MiB
filter = dumper.GetDumpFilter(Table{Name: "logs", Size: &size})
So(filter, ShouldEqual, "ORDER BY 1 DESC LIMIT 30")
filter = dumper.GetDumpFilter(Table{Name: "items", Size: &size})
So(filter, ShouldEqual, "LIMIT 10")
filter = dumper.GetDumpFilter(Table{Name: "table1"})
So(filter, ShouldEqual, "")
filter = dumper.GetDumpFilter(Table{Name: "table2"})
So(filter, ShouldEqual, "LIMIT 0")
filter = dumper.GetDumpFilter(Table{Name: "table3"})
So(filter, ShouldEqual, "LIMIT 0")
filter = dumper.GetDumpFilter(Table{Name: "table4"})
So(filter, ShouldEqual, "")
canDump := dumper.CanDumpDefinition("table1")
So(canDump, ShouldBeFalse)
canDump = dumper.CanDumpDefinition("table2")
So(canDump, ShouldBeTrue)
canDump = dumper.CanDumpDefinition("table3")
So(canDump, ShouldBeFalse)
canDump = dumper.CanDumpDefinition("table4")
So(canDump, ShouldBeFalse)
columns := dumper.GetDumpColumns(Table{Name: "users", Columns: &Columns{"id", "name", "password"}})
So(columns, ShouldEqual, "`id`,`name`,MD5('123456') AS `password`")
})
Convey("Check empty config", t, func() {
dumper, err := ParseConfig(nil)
So(err, ShouldBeNil)
filter := dumper.GetDumpFilter(Table{Name: "users"})
So(filter, ShouldEqual, "")
filter = dumper.GetDumpFilter(Table{Name: "carts"})
So(filter, ShouldEqual, "")
filter = dumper.GetDumpFilter(Table{Name: "items", Columns: &Columns{"id", "cart_id", "name", "value"}})
So(filter, ShouldEqual, "")
size := Size(10485760) // ==10 MiB
filter = dumper.GetDumpFilter(Table{Name: "logs", Size: &size})
So(filter, ShouldEqual, "")
size = Size(10485760 + 1) // >10 MiB
filter = dumper.GetDumpFilter(Table{Name: "logs", Size: &size})
So(filter, ShouldEqual, "")
filter = dumper.GetDumpFilter(Table{Name: "table1"})
So(filter, ShouldEqual, "")
filter = dumper.GetDumpFilter(Table{Name: "table2"})
So(filter, ShouldEqual, "")
filter = dumper.GetDumpFilter(Table{Name: "table3"})
So(filter, ShouldEqual, "")
filter = dumper.GetDumpFilter(Table{Name: "table4"})
So(filter, ShouldEqual, "")
columns := dumper.GetDumpColumns(Table{Name: "users", Columns: &Columns{"id", "name", "password"}})
So(columns, ShouldEqual, "`id`,`name`,`password`")
})
}