-
Notifications
You must be signed in to change notification settings - Fork 27
/
godb_test.go
144 lines (124 loc) · 4.89 KB
/
godb_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package godb
import (
"testing"
"github.com/samonzeweb/godb/tablenamer"
. "github.com/smartystreets/goconvey/convey"
)
func TestClone(t *testing.T) {
Convey("Given an existing DB", t, func() {
db := createInMemoryConnection(t)
defer db.Close()
Convey("Clone create a DB copy of an existing one", func() {
clone := db.Clone()
So(clone.adapter, ShouldHaveSameTypeAs, db.adapter)
So(clone.sqlDB, ShouldEqual, db.sqlDB)
So(clone.logger, ShouldEqual, db.logger)
So(clone.defaultTableNamer, ShouldEqual, db.defaultTableNamer)
})
Convey("Clone don't copy existing transaction", func() {
db.Begin()
clone := db.Clone()
defer clone.Clear()
So(clone.sqlTx, ShouldBeNil)
})
})
}
func TestTableNamer(t *testing.T) {
db := createInMemoryConnection(t)
defer db.Close()
// Same
Convey("Given a record descriptor, same name", t, func() {
db.SetDefaultTableNamer(tablenamer.Same())
instancePtr := &typeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("getTableName returns by default the struct name a table name", func() {
tableName := db.defaultTableNamer(recordDesc.getTableName())
So(tableName, ShouldEqual, "typeToDescribe")
})
})
Convey("Given a record descriptor of type implementing tableNamer interface, same name", t, func() {
db.SetDefaultTableNamer(tablenamer.Same())
instancePtr := &otherTypeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("getTableName returns the string given by TableName()", func() {
tableName := db.defaultTableNamer(recordDesc.getTableName())
So(tableName, ShouldEqual, "others")
})
})
// Plural
Convey("Given a record descriptor, plural name", t, func() {
db.SetDefaultTableNamer(tablenamer.Plural())
instancePtr := &typeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("getTableName returns by default the struct name a table name in plural form", func() {
tableName := db.defaultTableNamer(recordDesc.getTableName())
So(tableName, ShouldEqual, "typeToDescribes")
})
})
Convey("Given a record descriptor of type implementing tableNamer interface, in plural form", t, func() {
db.SetDefaultTableNamer(tablenamer.Plural())
instancePtr := &otherTypeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("getTableName returns the string given by TableName() - plural", func() {
tableName := db.defaultTableNamer(recordDesc.getTableName())
So(tableName, ShouldEqual, "others")
})
})
// Snake
Convey("Given a record descriptor, snake case name", t, func() {
db.SetDefaultTableNamer(tablenamer.Snake())
instancePtr := &typeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("getTableName returns by default the struct name a table name in snake form", func() {
tableName := db.defaultTableNamer(recordDesc.getTableName())
So(tableName, ShouldEqual, "type_to_describe")
})
})
Convey("Given a record descriptor of type implementing tableNamer interface, snake name", t, func() {
db.SetDefaultTableNamer(tablenamer.Snake())
instancePtr := &otherTypeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("getTableName returns the string given by TableName() - snake", func() {
tableName := db.defaultTableNamer(recordDesc.getTableName())
So(tableName, ShouldEqual, "others")
})
})
// Snake Plural
Convey("Given a record descriptor, snake case name in plural", t, func() {
db.SetDefaultTableNamer(tablenamer.SnakePlural())
instancePtr := &typeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("getTableName returns by default the struct name a table name in plural snake form", func() {
tableName := db.defaultTableNamer(recordDesc.getTableName())
So(tableName, ShouldEqual, "type_to_describes")
})
})
Convey("Given a record descriptor of type implementing tableNamer interface, plural snake name", t, func() {
db.SetDefaultTableNamer(tablenamer.SnakePlural())
instancePtr := &otherTypeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("getTableName returns the string given by TableName() - plural snake", func() {
tableName := db.defaultTableNamer(recordDesc.getTableName())
So(tableName, ShouldEqual, "others")
})
})
}
func TestQuote(t *testing.T) {
Convey("Given an existing DB", t, func() {
db := createInMemoryConnection(t)
defer db.Close()
Convey("quote act like adapter Quote with a simple identified", func() {
identifier := "foo"
quotedIdentifier := db.quote(identifier)
So(quotedIdentifier, ShouldEqual, db.adapter.Quote(identifier))
})
Convey("quote quotes all parts of an identifier", func() {
identifier := "foo.bar.baz"
quotedIdentifier := db.quote(identifier)
expectedQuotedIdentified := db.adapter.Quote("foo") + "." +
db.adapter.Quote("bar") + "." +
db.adapter.Quote("baz")
So(quotedIdentifier, ShouldEqual, expectedQuotedIdentified)
})
})
}