This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
moresql_test.go
90 lines (79 loc) · 2.46 KB
/
moresql_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
package moresql_test
import (
"testing"
_ "github.com/lib/pq"
m "github.com/zph/moresql"
. "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) TestBuildUpsertStatement(c *C) {
mongo := m.Mongo{"_id", "id"}
p := m.Postgres{"id", "text"}
f := m.Field{mongo, p}
f2 := m.Field{m.Mongo{"count", "text"}, m.Postgres{"count", "text"}}
fields := m.Fields{"_id": f, "count": f2}
collection := m.Collection{
Name: "categories",
PgTable: "categories_in_pg",
Fields: fields}
o := m.Statement{collection}
sql := o.BuildUpsert()
expected := `INSERT INTO "categories_in_pg" ("id", "count")
VALUES (:id, :count)
ON CONFLICT ("id")
DO UPDATE SET "count" = :count;`
c.Check(sql, Equals, expected)
}
func (s *MySuite) TestBuildInsertStatement(c *C) {
mongo := m.Mongo{"_id", "id"}
p := m.Postgres{"id", "text"}
f := m.Field{mongo, p}
f2 := m.Field{m.Mongo{"count", "text"}, m.Postgres{"count", "text"}}
fields := m.Fields{"_id": f, "count": f2}
collection := m.Collection{
Name: "categories",
PgTable: "categories",
Fields: fields}
o := m.Statement{collection}
sql := o.BuildInsert()
expected := `INSERT INTO "categories" ("id", "count")
VALUES (:id, :count)`
c.Check(sql, Equals, expected)
}
func (s *MySuite) TestBuildUpdateStatement(c *C) {
mongo := m.Mongo{"_id", "id"}
p := m.Postgres{"id", "id"}
f := m.Field{mongo, p}
f2 := m.Field{m.Mongo{"count", "text"}, m.Postgres{"count", "text"}}
f3 := m.Field{m.Mongo{"avg", "text"}, m.Postgres{"avg", "text"}}
fields := m.Fields{"_id": f, "count": f2, "avg": f3}
collection := m.Collection{
Name: "categories",
PgTable: "categories",
Fields: fields}
o := m.Statement{collection}
sql := o.BuildUpdate()
expected := `UPDATE "categories"
SET "avg" = :avg, "count" = :count
WHERE "id" = :_id;`
c.Check(sql, Equals, expected)
}
func (s *MySuite) TestBuildDeleteStatement(c *C) {
mongo := m.Mongo{"_id", "id"}
p := m.Postgres{"id", "id"}
f := m.Field{mongo, p}
f2 := m.Field{m.Mongo{"count", "text"}, m.Postgres{"count", "text"}}
f3 := m.Field{m.Mongo{"avg", "text"}, m.Postgres{"avg", "text"}}
fields := m.Fields{"_id": f, "count": f2, "avg": f3}
collection := m.Collection{
Name: "categories",
PgTable: "categories",
Fields: fields}
o := m.Statement{collection}
sql := o.BuildDelete()
expected := `DELETE FROM "categories" WHERE "id" = :_id;`
c.Check(sql, Equals, expected)
}