Skip to content

Commit 4df7849

Browse files
authored
Replace ginkgo with onpar (#367)
We've been running in to more and more problems with ginkgo, and frankly, I prefer the simplicity of onpar. Time to switch.
1 parent 2925510 commit 4df7849

3 files changed

Lines changed: 220 additions & 296 deletions

File tree

dialect_mysql_test.go

Lines changed: 126 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -13,199 +13,178 @@ package gorp_test
1313

1414
import (
1515
"database/sql"
16+
"errors"
17+
"fmt"
1618
"reflect"
19+
"testing"
1720
"time"
1821

19-
// ginkgo/gomega functions read better as dot-imports.
20-
. "github.com/onsi/ginkgo"
21-
. "github.com/onsi/ginkgo/extensions/table"
22-
. "github.com/onsi/gomega"
23-
22+
"github.com/apoydence/onpar"
23+
"github.com/apoydence/onpar/expect"
24+
"github.com/apoydence/onpar/matchers"
2425
"github.com/go-gorp/gorp"
2526
)
2627

27-
var _ = Describe("MySQLDialect", func() {
28-
var (
29-
engine, encoding string
30-
dialect gorp.MySQLDialect
31-
)
28+
func TestMySQLDialect(t *testing.T) {
29+
o := onpar.New()
30+
defer o.Run(t)
3231

33-
JustBeforeEach(func() {
34-
dialect = gorp.MySQLDialect{
35-
Engine: engine,
36-
Encoding: encoding,
32+
o.BeforeEach(func(t *testing.T) (expect.Expectation, gorp.MySQLDialect) {
33+
return expect.New(t), gorp.MySQLDialect{
34+
Engine: "foo",
35+
Encoding: "bar",
3736
}
3837
})
3938

40-
DescribeTable("ToSqlType",
41-
func(value interface{}, maxsize int, autoIncr bool, expected string) {
42-
typ := reflect.TypeOf(value)
43-
sqlType := dialect.ToSqlType(typ, maxsize, autoIncr)
44-
Expect(sqlType).To(Equal(expected))
45-
},
46-
Entry("bool", true, 0, false, "boolean"),
47-
Entry("int8", int8(1), 0, false, "tinyint"),
48-
Entry("uint8", uint8(1), 0, false, "tinyint unsigned"),
49-
Entry("int16", int16(1), 0, false, "smallint"),
50-
Entry("uint16", uint16(1), 0, false, "smallint unsigned"),
51-
Entry("int32", int32(1), 0, false, "int"),
52-
Entry("int (treated as int32)", int(1), 0, false, "int"),
53-
Entry("uint32", uint32(1), 0, false, "int unsigned"),
54-
Entry("uint (treated as uint32)", uint(1), 0, false, "int unsigned"),
55-
Entry("int64", int64(1), 0, false, "bigint"),
56-
Entry("uint64", uint64(1), 0, false, "bigint unsigned"),
57-
Entry("float32", float32(1), 0, false, "double"),
58-
Entry("float64", float64(1), 0, false, "double"),
59-
Entry("[]uint8", []uint8{1}, 0, false, "mediumblob"),
60-
Entry("NullInt64", sql.NullInt64{}, 0, false, "bigint"),
61-
Entry("NullFloat64", sql.NullFloat64{}, 0, false, "double"),
62-
Entry("NullBool", sql.NullBool{}, 0, false, "tinyint"),
63-
Entry("Time", time.Time{}, 0, false, "datetime"),
64-
Entry("default-size string", "", 0, false, "varchar(255)"),
65-
Entry("sized string", "", 50, false, "varchar(50)"),
66-
Entry("large string", "", 1024, false, "text"),
67-
)
68-
69-
Describe("AutoIncrStr", func() {
70-
It("returns the auto increment string", func() {
71-
Expect(dialect.AutoIncrStr()).To(Equal("auto_increment"))
72-
})
39+
o.Group("ToSqlType", func() {
40+
tests := []struct {
41+
name string
42+
value interface{}
43+
maxSize int
44+
autoIncr bool
45+
expected string
46+
}{
47+
{"bool", true, 0, false, "boolean"},
48+
{"int8", int8(1), 0, false, "tinyint"},
49+
{"uint8", uint8(1), 0, false, "tinyint unsigned"},
50+
{"int16", int16(1), 0, false, "smallint"},
51+
{"uint16", uint16(1), 0, false, "smallint unsigned"},
52+
{"int32", int32(1), 0, false, "int"},
53+
{"int (treated as int32)", int(1), 0, false, "int"},
54+
{"uint32", uint32(1), 0, false, "int unsigned"},
55+
{"uint (treated as uint32)", uint(1), 0, false, "int unsigned"},
56+
{"int64", int64(1), 0, false, "bigint"},
57+
{"uint64", uint64(1), 0, false, "bigint unsigned"},
58+
{"float32", float32(1), 0, false, "double"},
59+
{"float64", float64(1), 0, false, "double"},
60+
{"[]uint8", []uint8{1}, 0, false, "mediumblob"},
61+
{"NullInt64", sql.NullInt64{}, 0, false, "bigint"},
62+
{"NullFloat64", sql.NullFloat64{}, 0, false, "double"},
63+
{"NullBool", sql.NullBool{}, 0, false, "tinyint"},
64+
{"Time", time.Time{}, 0, false, "datetime"},
65+
{"default-size string", "", 0, false, "varchar(255)"},
66+
{"sized string", "", 50, false, "varchar(50)"},
67+
{"large string", "", 1024, false, "text"},
68+
}
69+
for _, t := range tests {
70+
o.Spec(t.name, func(expect expect.Expectation, dialect gorp.MySQLDialect) {
71+
typ := reflect.TypeOf(t.value)
72+
sqlType := dialect.ToSqlType(typ, t.maxSize, t.autoIncr)
73+
expect(sqlType).To(matchers.Equal(t.expected))
74+
})
75+
}
7376
})
7477

75-
Describe("AutoIncrBindValue", func() {
76-
It("returns the value used to bind the auto-increment value", func() {
77-
Expect(dialect.AutoIncrBindValue()).To(Equal("null"))
78-
})
78+
o.Spec("AutoIncrStr", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
79+
expect(dialect.AutoIncrStr()).To(matchers.Equal("auto_increment"))
7980
})
8081

81-
Describe("AutoIncrInsertSuffix", func() {
82-
It("returns the suffix needed for auto-incrementing", func() {
83-
Expect(dialect.AutoIncrInsertSuffix(nil)).To(BeEmpty())
84-
})
82+
o.Spec("AutoIncrBindValue", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
83+
expect(dialect.AutoIncrBindValue()).To(matchers.Equal("null"))
8584
})
8685

87-
Describe("CreateTableSuffix", func() {
88-
Context("with an empty engine", func() {
89-
BeforeEach(func() {
90-
engine = ""
91-
encoding = "foo"
92-
})
93-
It("panics", func() {
94-
Expect(func() {
95-
dialect.CreateTableSuffix()
96-
}).To(Panic())
97-
})
98-
})
86+
o.Spec("AutoIncrInsertSuffix", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
87+
expect(dialect.AutoIncrInsertSuffix(nil)).To(matchers.Equal(""))
88+
})
9989

100-
Context("with an empty encoding", func() {
101-
BeforeEach(func() {
102-
engine = "foo"
103-
encoding = ""
90+
o.Group("CreateTableSuffix", func() {
91+
o.Group("with an empty engine", func() {
92+
o.BeforeEach(func(expect expect.Expectation, dialect gorp.MySQLDialect) (expect.Expectation, gorp.MySQLDialect) {
93+
dialect.Engine = ""
94+
return expect, dialect
10495
})
105-
It("panics", func() {
106-
Expect(func() {
107-
dialect.CreateTableSuffix()
108-
}).To(Panic())
96+
o.Spec("panics", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
97+
expect(func() { dialect.CreateTableSuffix() }).To(Panic())
10998
})
11099
})
111100

112-
Context("with an engine and an encoding", func() {
113-
BeforeEach(func() {
114-
engine = "foo"
115-
encoding = "bar"
101+
o.Group("with an empty encoding", func() {
102+
o.BeforeEach(func(expect expect.Expectation, dialect gorp.MySQLDialect) (expect.Expectation, gorp.MySQLDialect) {
103+
dialect.Encoding = ""
104+
return expect, dialect
116105
})
117-
It("returns a valid suffix", func() {
118-
Expect(dialect.CreateTableSuffix()).To(Equal(" engine=foo charset=bar"))
106+
o.Spec("panics", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
107+
expect(func() { dialect.CreateTableSuffix() }).To(Panic())
119108
})
120109
})
121-
})
122110

123-
Describe("CreateIndexSuffix", func() {
124-
It("returns the suffix for creating indexes", func() {
125-
Expect(dialect.CreateIndexSuffix()).To(Equal("using"))
111+
o.Spec("with an engine and an encoding", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
112+
expect(dialect.CreateTableSuffix()).To(matchers.Equal(" engine=foo charset=bar"))
126113
})
127114
})
128115

129-
Describe("DropIndexSuffix", func() {
130-
It("returns the suffix for deleting indexes", func() {
131-
Expect(dialect.DropIndexSuffix()).To(Equal("on"))
132-
})
116+
o.Spec("CreateIndexSuffix", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
117+
expect(dialect.CreateIndexSuffix()).To(matchers.Equal("using"))
133118
})
134119

135-
Describe("TruncateClause", func() {
136-
It("returns the clause for truncating a table", func() {
137-
Expect(dialect.TruncateClause()).To(Equal("truncate"))
138-
})
120+
o.Spec("DropIndexSuffix", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
121+
expect(dialect.DropIndexSuffix()).To(matchers.Equal("on"))
139122
})
140123

141-
Describe("SleepClause", func() {
142-
It("returns the clause for sleeping", func() {
143-
Expect(dialect.SleepClause(1 * time.Second)).To(Equal("sleep(1.000000)"))
144-
Expect(dialect.SleepClause(100 * time.Millisecond)).To(Equal("sleep(0.100000)"))
145-
})
124+
o.Spec("TruncateClause", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
125+
expect(dialect.TruncateClause()).To(matchers.Equal("truncate"))
146126
})
147127

148-
Describe("BindVar", func() {
149-
It("returns the variable binding sequence", func() {
150-
Expect(dialect.BindVar(0)).To(Equal("?"))
151-
})
128+
o.Spec("SleepClause", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
129+
expect(dialect.SleepClause(1 * time.Second)).To(matchers.Equal("sleep(1.000000)"))
130+
expect(dialect.SleepClause(100 * time.Millisecond)).To(matchers.Equal("sleep(0.100000)"))
152131
})
153132

154-
PDescribe("InsertAutoIncr", func() {})
155-
156-
Describe("QuoteField", func() {
157-
It("returns the argument quoted as a field", func() {
158-
Expect(dialect.QuoteField("foo")).To(Equal("`foo`"))
159-
})
133+
o.Spec("BindVar", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
134+
expect(dialect.BindVar(0)).To(matchers.Equal("?"))
160135
})
161136

162-
Describe("QuotedTableForQuery", func() {
163-
var (
164-
schema, table string
165-
166-
quotedTable string
167-
)
137+
o.Spec("QuoteField", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
138+
expect(dialect.QuoteField("foo")).To(matchers.Equal("`foo`"))
139+
})
168140

169-
JustBeforeEach(func() {
170-
quotedTable = dialect.QuotedTableForQuery(schema, table)
141+
o.Group("QuotedTableForQuery", func() {
142+
o.Spec("using the default schema", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
143+
expect(dialect.QuotedTableForQuery("", "foo")).To(matchers.Equal("`foo`"))
171144
})
172145

173-
Context("using the default schema", func() {
174-
BeforeEach(func() {
175-
schema = ""
176-
table = "foo"
177-
})
178-
It("returns just the table", func() {
179-
Expect(quotedTable).To(Equal("`foo`"))
180-
})
181-
})
182-
183-
Context("with a supplied schema", func() {
184-
BeforeEach(func() {
185-
schema = "foo"
186-
table = "bar"
187-
})
188-
It("returns the schema and table", func() {
189-
Expect(quotedTable).To(Equal("foo.`bar`"))
190-
})
146+
o.Spec("with a supplied schema", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
147+
expect(dialect.QuotedTableForQuery("foo", "bar")).To(matchers.Equal("foo.`bar`"))
191148
})
192149
})
193150

194-
Describe("IfSchemaNotExists", func() {
195-
It("appends 'if not exists' to the command", func() {
196-
Expect(dialect.IfSchemaNotExists("foo", "bar")).To(Equal("foo if not exists"))
197-
})
151+
o.Spec("IfSchemaNotExists", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
152+
expect(dialect.IfSchemaNotExists("foo", "bar")).To(matchers.Equal("foo if not exists"))
198153
})
199154

200-
Describe("IfTableExists", func() {
201-
It("appends 'if exists' to the command", func() {
202-
Expect(dialect.IfTableExists("foo", "bar", "baz")).To(Equal("foo if exists"))
203-
})
155+
o.Spec("IfTableExists", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
156+
expect(dialect.IfTableExists("foo", "bar", "baz")).To(matchers.Equal("foo if exists"))
204157
})
205158

206-
Describe("IfTableNotExists", func() {
207-
It("appends 'if not exists' to the command", func() {
208-
Expect(dialect.IfTableNotExists("foo", "bar", "baz")).To(Equal("foo if not exists"))
209-
})
159+
o.Spec("IfTableNotExists", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
160+
expect(dialect.IfTableNotExists("foo", "bar", "baz")).To(matchers.Equal("foo if not exists"))
210161
})
211-
})
162+
}
163+
164+
type panicMatcher struct {
165+
}
166+
167+
func Panic() panicMatcher {
168+
return panicMatcher{}
169+
}
170+
171+
func (m panicMatcher) Match(actual interface{}) (resultValue interface{}, err error) {
172+
switch f := actual.(type) {
173+
case func():
174+
panicked := false
175+
func() {
176+
defer func() {
177+
if r := recover(); r != nil {
178+
panicked = true
179+
}
180+
}()
181+
f()
182+
}()
183+
if panicked {
184+
return f, nil
185+
}
186+
return f, errors.New("function did not panic")
187+
default:
188+
return f, fmt.Errorf("%T is not func()", f)
189+
}
190+
}

0 commit comments

Comments
 (0)