forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinishers.go.tpl
114 lines (97 loc) · 4.14 KB
/
finishers.go.tpl
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
{{- $alias := .Aliases.Table .Table.Name}}
func test{{$alias.UpPlural}}Bind(t *testing.T) {
t.Parallel()
seed := randomize.NewSeed()
var err error
o := &{{$alias.UpSingular}}{}
if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
}
{{if not .NoContext}}ctx := context.Background(){{end}}
tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}})
defer func() { _ = tx.Rollback() }()
if err = o.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil {
t.Error(err)
}
if err = {{$alias.UpPlural}}().Bind({{if .NoContext}}nil{{else}}ctx{{end}}, tx, o); err != nil {
t.Error(err)
}
}
func test{{$alias.UpPlural}}One(t *testing.T) {
t.Parallel()
seed := randomize.NewSeed()
var err error
o := &{{$alias.UpSingular}}{}
if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
}
{{if not .NoContext}}ctx := context.Background(){{end}}
tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}})
defer func() { _ = tx.Rollback() }()
if err = o.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil {
t.Error(err)
}
if x, err := {{$alias.UpPlural}}().One({{if not .NoContext}}ctx, {{end -}} tx); err != nil {
t.Error(err)
} else if x == nil {
t.Error("expected to get a non nil record")
}
}
func test{{$alias.UpPlural}}All(t *testing.T) {
t.Parallel()
seed := randomize.NewSeed()
var err error
{{$alias.DownSingular}}One := &{{$alias.UpSingular}}{}
{{$alias.DownSingular}}Two := &{{$alias.UpSingular}}{}
if err = randomize.Struct(seed, {{$alias.DownSingular}}One, {{$alias.DownSingular}}DBTypes, false, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
}
if err = randomize.Struct(seed, {{$alias.DownSingular}}Two, {{$alias.DownSingular}}DBTypes, false, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
}
{{if not .NoContext}}ctx := context.Background(){{end}}
tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}})
defer func() { _ = tx.Rollback() }()
if err = {{$alias.DownSingular}}One.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil {
t.Error(err)
}
if err = {{$alias.DownSingular}}Two.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil {
t.Error(err)
}
slice, err := {{$alias.UpPlural}}().All({{if not .NoContext}}ctx, {{end -}} tx)
if err != nil {
t.Error(err)
}
if len(slice) != 2 {
t.Error("want 2 records, got:", len(slice))
}
}
func test{{$alias.UpPlural}}Count(t *testing.T) {
t.Parallel()
var err error
seed := randomize.NewSeed()
{{$alias.DownSingular}}One := &{{$alias.UpSingular}}{}
{{$alias.DownSingular}}Two := &{{$alias.UpSingular}}{}
if err = randomize.Struct(seed, {{$alias.DownSingular}}One, {{$alias.DownSingular}}DBTypes, false, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
}
if err = randomize.Struct(seed, {{$alias.DownSingular}}Two, {{$alias.DownSingular}}DBTypes, false, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
}
{{if not .NoContext}}ctx := context.Background(){{end}}
tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}})
defer func() { _ = tx.Rollback() }()
if err = {{$alias.DownSingular}}One.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil {
t.Error(err)
}
if err = {{$alias.DownSingular}}Two.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil {
t.Error(err)
}
count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
if err != nil {
t.Error(err)
}
if count != 2 {
t.Error("want 2 records, got:", count)
}
}