forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.go.tpl
125 lines (101 loc) · 3.57 KB
/
delete.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
115
116
117
118
119
120
121
122
123
124
125
{{- $alias := .Aliases.Table .Table.Name}}
func test{{$alias.UpPlural}}Delete(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 .NoRowsAffected -}}
if err = o.Delete({{if not .NoContext}}ctx, {{end -}} tx); err != nil {
t.Error(err)
}
{{else -}}
if rowsAff, err := o.Delete({{if not .NoContext}}ctx, {{end -}} tx); err != nil {
t.Error(err)
} else if rowsAff != 1 {
t.Error("should only have deleted one row, but affected:", rowsAff)
}
{{end -}}
count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
if err != nil {
t.Error(err)
}
if count != 0 {
t.Error("want zero records, got:", count)
}
}
func test{{$alias.UpPlural}}QueryDeleteAll(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 .NoRowsAffected -}}
if err = {{$alias.UpPlural}}().DeleteAll({{if not .NoContext}}ctx, {{end -}} tx); err != nil {
t.Error(err)
}
{{else -}}
if rowsAff, err := {{$alias.UpPlural}}().DeleteAll({{if not .NoContext}}ctx, {{end -}} tx); err != nil {
t.Error(err)
} else if rowsAff != 1 {
t.Error("should only have deleted one row, but affected:", rowsAff)
}
{{end -}}
count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
if err != nil {
t.Error(err)
}
if count != 0 {
t.Error("want zero records, got:", count)
}
}
func test{{$alias.UpPlural}}SliceDeleteAll(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)
}
slice := {{$alias.UpSingular}}Slice{{"{"}}o{{"}"}}
{{if .NoRowsAffected -}}
if err = slice.DeleteAll({{if not .NoContext}}ctx, {{end -}} tx); err != nil {
t.Error(err)
}
{{else -}}
if rowsAff, err := slice.DeleteAll({{if not .NoContext}}ctx, {{end -}} tx); err != nil {
t.Error(err)
} else if rowsAff != 1 {
t.Error("should only have deleted one row, but affected:", rowsAff)
}
{{end -}}
count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
if err != nil {
t.Error(err)
}
if count != 0 {
t.Error("want zero records, got:", count)
}
}