/
insert_builder_test.go
101 lines (78 loc) · 2.12 KB
/
insert_builder_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
package postgres_backend
import (
"github.com/straumur/straumur"
"testing"
)
type QueryBuilder func(e *straumur.Event) (string, []interface{}, error)
func queryTest(t *testing.T, expected string, e *straumur.Event, qb QueryBuilder) {
expectedArgs := []interface{}{
e.Key,
"{}",
"{}",
e.Created,
e.Updated,
e.Description,
e.Importance,
e.Origin,
}
if e.ID > 0 {
expectedArgs = []interface{}{
e.Key,
"{}",
"{}",
e.Description,
e.Importance,
e.Origin,
}
}
for _, i := range [][]string{e.Entities, e.OtherReferences, e.Actors, e.Tags} {
for _, s := range i {
expectedArgs = append(expectedArgs, s)
}
}
if e.ID > 0 {
expectedArgs = append(expectedArgs, e.ID)
}
query, args, err := qb(e)
if err != nil {
t.Fatal(err)
}
//Need DeepEqual method for these cases
if len(args) != len(expectedArgs) {
t.Fatalf("Expected %+v, got %+v", expectedArgs, args)
}
if query != expected {
t.Fatalf("Expected:\n\t%s\nGot:\n\t%s", expected, query)
}
}
func TestBuildInsertQuery(t *testing.T) {
const expected = `insert into "event" ("key", "key_params", "created", "updated", "payload", "description", "importance", "origin", "entities", "other_references", "actors", "tags") values ($1, $2, $3, $4, $5, $6, $7, $8,ARRAY[$9, $10]::text[], ARRAY[]::text[], ARRAY[$11]::text[], ARRAY[]::text[]) returning "id", "created", "updated";`
e := straumur.NewEvent(
"foo.bar",
nil,
nil,
"My event",
3,
"mysystem",
[]string{"ns/foo", "ns/moo"},
nil,
[]string{"someone"},
nil)
queryTest(t, expected, e, buildInsertQuery)
}
func TestBuildUpdateQuery(t *testing.T) {
const expected = `update "event" set "key" = $1, "key_params" = $2, "payload" = $3, "description" = $4, "importance" = $5, "origin" = $6, "entities" = ARRAY[$7, $8]::text[], "other_references" = ARRAY[]::text[], "actors" = ARRAY[$9]::text[], "tags" = ARRAY[]::text[] where "id" = $10 returning "updated";`
e := straumur.NewEvent(
"foo.bar",
nil,
nil,
"My event",
3,
"mysystem",
[]string{"ns/foo", "ns/moo"},
nil,
[]string{"someone"},
nil)
e.ID = 123
queryTest(t, expected, e, buildUpdateQuery)
}