/
db_test.go
117 lines (97 loc) · 1.91 KB
/
db_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
package postgres_backend
import (
"database/sql"
_ "github.com/lib/pq"
"github.com/straumur/straumur"
"testing"
"time"
)
func TestDB(t *testing.T) {
const connection = "dbname=teststream host=localhost sslmode=disable"
db, err := sql.Open("postgres", connection)
if err != nil {
t.Error("Error:", err)
return
}
//truncate table
clear := func() {
_, err := db.Exec(`TRUNCATE TABLE event RESTART IDENTITY;`)
if err != nil {
t.Fatal(err)
}
}
_, err = db.Exec(`drop table if exists migration_info, event;`)
if err != nil {
t.Fatal("Error:", err)
}
// With migrations applied
_, err = NewPostgresDataSource(connection)
if err != nil {
t.Fatal("PostgresDataSource has error:", err)
}
// With no migrations applied
p, err := NewPostgresDataSource(connection)
if err != nil {
t.Fatal("PostgresDataSource has error:", err)
}
straumur.RunDataBackendSuite(t, p, clear)
}
func TestDuplicateInsertion(t *testing.T) {
const connection = "dbname=teststream host=localhost sslmode=disable"
db, err := sql.Open("postgres", connection)
if err != nil {
t.Error("Error:", err)
return
}
//truncate table
clear := func() {
_, err := db.Exec(`TRUNCATE TABLE event RESTART IDENTITY;`)
if err != nil {
t.Fatal(err)
}
}
clear()
now := time.Now()
e := straumur.NewEvent(
"foo.bar",
nil,
nil,
"My event",
3,
"mysystem",
[]string{"ns/foo", "ns/moo"},
nil,
[]string{"someone"},
nil)
e.Created = now
e2 := straumur.NewEvent(
"foo.bar",
nil,
nil,
"My event",
3,
"mysystem",
[]string{"ns/foo", "ns/moo"},
nil,
[]string{"someone"},
nil)
e2.Created = now
e3 := straumur.NewEvent(
"foo.bar",
nil,
nil,
"My event",
3,
"mysystem",
[]string{"ns/foo", "ns/moo"},
nil,
[]string{"someone"},
nil)
p, err := NewPostgresDataSource(connection)
if err != nil {
t.Fatal("PostgresDataSource has error:", err)
}
p.Save(e)
p.Save(e2)
p.Save(e3)
}