-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecord_reader_test.go
209 lines (178 loc) · 5.23 KB
/
record_reader_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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package marlow
import "io"
import "sync"
import "bytes"
import "go/ast"
import "testing"
import "strings"
import "go/token"
import "go/parser"
import "github.com/franela/goblin"
type recordReaderTestScaffold struct {
source io.Reader
imports chan string
output *bytes.Buffer
waiter *sync.WaitGroup
closed bool
}
func (s *recordReaderTestScaffold) root() ast.Decl {
tree, e := parser.ParseFile(token.NewFileSet(), "", s.source, parser.AllErrors)
if e != nil {
panic(e)
}
if len(tree.Decls) < 1 {
panic("not enough declarations in provided source")
}
return tree.Decls[0]
}
func (s *recordReaderTestScaffold) close() {
if s.closed {
return
}
s.closed = true
close(s.imports)
}
func (s *recordReaderTestScaffold) error() error {
reader, _ := newRecordReader(s.root(), s.imports)
_, e := io.Copy(s.output, reader)
return e
}
func (s *recordReaderTestScaffold) reset() {
s.closed = false
s.imports = make(chan string)
s.output = new(bytes.Buffer)
s.waiter = &sync.WaitGroup{}
}
func Test_RecordReader(t *testing.T) {
g := goblin.Goblin(t)
var scaffold *recordReaderTestScaffold
g.Describe("recordReader", func() {
g.BeforeEach(func() {
scaffold = &recordReaderTestScaffold{}
scaffold.reset()
})
g.BeforeEach(func() {
scaffold.waiter.Add(1)
go func() {
received := make(map[string]bool)
for importName := range scaffold.imports {
received[importName] = true
}
scaffold.waiter.Done()
}()
})
g.AfterEach(func() {
scaffold.close()
scaffold.waiter.Wait()
})
g.It("with a valid source struct", func() {
scaffold.source = strings.NewReader(`
package marlowt
type Author struct {
Title string
}`)
reader, ok := newRecordReader(scaffold.root(), scaffold.imports)
g.Assert(ok).Equal(true)
_, e := io.Copy(scaffold.output, reader)
scaffold.close()
g.Assert(e).Equal(nil)
})
g.It("with a valid source struct including explicit column names", func() {
scaffold.source = strings.NewReader(`
package marlowt
type Author struct {
Title string ` + "`marlow:\"column=title\"`" + `
}`)
reader, ok := newRecordReader(scaffold.root(), scaffold.imports)
g.Assert(ok).Equal(true)
_, e := io.Copy(scaffold.output, reader)
scaffold.close()
g.Assert(e).Equal(nil)
})
g.It("with a valid source struct including explicit table name from field", func() {
scaffold.source = strings.NewReader(`
package marlowt
type Author struct {
table string ` + "`marlow:\"tableName=authors\"`" + `
Title string
}`)
reader, ok := newRecordReader(scaffold.root(), scaffold.imports)
g.Assert(ok).Equal(true)
_, e := io.Copy(scaffold.output, reader)
scaffold.close()
g.Assert(e).Equal(nil)
})
g.It("with a valid source struct with empty marlow field column config", func() {
scaffold.source = strings.NewReader(`
package marlowt
type Author struct {
Title string
IgnoredColumn string ` + "`marlow:\"\"`" + `
}`)
g.Assert(scaffold.error()).Equal(nil)
})
g.It("with a valid source struct with explicit exclusions of certain columns", func() {
scaffold.source = strings.NewReader(`
package marlowt
type Author struct {
Title string
IgnoredColumn string ` + "`marlow:\"column=-\"`" + `
}`)
g.Assert(scaffold.error()).Equal(nil)
})
g.It("errors during copy if duplicate column names", func() {
scaffold.source = strings.NewReader(`
package marlowt
type Author struct {
Title string
IgnoredColumn string ` + "`marlow:\"column=dupe\"`" + `
OtherColumn string ` + "`marlow:\"column=dupe\"`" + `
}`)
g.Assert(scaffold.error() == nil).Equal(false)
})
g.It("errors during copy if invalid column name characters", func() {
scaffold.source = strings.NewReader(`
package marlowt
type Author struct {
Title string
MiddleName string ` + "`marlow:\"column=b@dName\"`" + `
}`)
g.Assert(scaffold.error() == nil).Equal(false)
})
g.It("errors during copy if slice field type", func() {
scaffold.source = strings.NewReader(`
package marlowt
type Author struct {
Title string
MiddleName string
SliceColumn []string ` + "`marlow:\"column=dupe\"`" + `
}`)
g.Assert(scaffold.error() == nil).Equal(false)
})
g.It("does not produce anything if all features are disabled", func() {
scaffold.source = strings.NewReader(`
package main
type Author struct {
table bool ` + "`marlow:\"updateable=false&createable=false&deletable=false&queryable=false\"`" + `
Name string
}`)
g.Assert(scaffold.error()).Equal(nil)
r := io.MultiReader(strings.NewReader("package main\n\n"), scaffold.output)
f, e := parser.ParseFile(token.NewFileSet(), "no-op.go", r, parser.DeclarationErrors)
t.Logf("%s", scaffold.output.String())
g.Assert(e).Equal(nil)
g.Assert(len(f.Decls)).Equal(0)
})
g.It("errors during copy if duplicate column names (with other valid fields)", func() {
scaffold.source = strings.NewReader(`
package marlowt
type Author struct {
Title string
MiddleName string
IgnoredColumn string ` + "`marlow:\"column=dupe\"`" + `
OtherColumn string ` + "`marlow:\"column=dupe\"`" + `
}`)
g.Assert(scaffold.error() == nil).Equal(false)
})
})
}