Skip to content

Commit

Permalink
Replace ginkgo with onpar (#367)
Browse files Browse the repository at this point in the history
We've been running in to more and more problems with ginkgo, and frankly, I prefer the simplicity of onpar.  Time to switch.
  • Loading branch information
nelsam authored Feb 26, 2018
1 parent 2925510 commit 4df7849
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 296 deletions.
273 changes: 126 additions & 147 deletions dialect_mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,199 +13,178 @@ package gorp_test

import (
"database/sql"
"errors"
"fmt"
"reflect"
"testing"
"time"

// ginkgo/gomega functions read better as dot-imports.
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"

"github.com/apoydence/onpar"
"github.com/apoydence/onpar/expect"
"github.com/apoydence/onpar/matchers"
"github.com/go-gorp/gorp"
)

var _ = Describe("MySQLDialect", func() {
var (
engine, encoding string
dialect gorp.MySQLDialect
)
func TestMySQLDialect(t *testing.T) {
o := onpar.New()
defer o.Run(t)

JustBeforeEach(func() {
dialect = gorp.MySQLDialect{
Engine: engine,
Encoding: encoding,
o.BeforeEach(func(t *testing.T) (expect.Expectation, gorp.MySQLDialect) {
return expect.New(t), gorp.MySQLDialect{
Engine: "foo",
Encoding: "bar",
}
})

DescribeTable("ToSqlType",
func(value interface{}, maxsize int, autoIncr bool, expected string) {
typ := reflect.TypeOf(value)
sqlType := dialect.ToSqlType(typ, maxsize, autoIncr)
Expect(sqlType).To(Equal(expected))
},
Entry("bool", true, 0, false, "boolean"),
Entry("int8", int8(1), 0, false, "tinyint"),
Entry("uint8", uint8(1), 0, false, "tinyint unsigned"),
Entry("int16", int16(1), 0, false, "smallint"),
Entry("uint16", uint16(1), 0, false, "smallint unsigned"),
Entry("int32", int32(1), 0, false, "int"),
Entry("int (treated as int32)", int(1), 0, false, "int"),
Entry("uint32", uint32(1), 0, false, "int unsigned"),
Entry("uint (treated as uint32)", uint(1), 0, false, "int unsigned"),
Entry("int64", int64(1), 0, false, "bigint"),
Entry("uint64", uint64(1), 0, false, "bigint unsigned"),
Entry("float32", float32(1), 0, false, "double"),
Entry("float64", float64(1), 0, false, "double"),
Entry("[]uint8", []uint8{1}, 0, false, "mediumblob"),
Entry("NullInt64", sql.NullInt64{}, 0, false, "bigint"),
Entry("NullFloat64", sql.NullFloat64{}, 0, false, "double"),
Entry("NullBool", sql.NullBool{}, 0, false, "tinyint"),
Entry("Time", time.Time{}, 0, false, "datetime"),
Entry("default-size string", "", 0, false, "varchar(255)"),
Entry("sized string", "", 50, false, "varchar(50)"),
Entry("large string", "", 1024, false, "text"),
)

Describe("AutoIncrStr", func() {
It("returns the auto increment string", func() {
Expect(dialect.AutoIncrStr()).To(Equal("auto_increment"))
})
o.Group("ToSqlType", func() {
tests := []struct {
name string
value interface{}
maxSize int
autoIncr bool
expected string
}{
{"bool", true, 0, false, "boolean"},
{"int8", int8(1), 0, false, "tinyint"},
{"uint8", uint8(1), 0, false, "tinyint unsigned"},
{"int16", int16(1), 0, false, "smallint"},
{"uint16", uint16(1), 0, false, "smallint unsigned"},
{"int32", int32(1), 0, false, "int"},
{"int (treated as int32)", int(1), 0, false, "int"},
{"uint32", uint32(1), 0, false, "int unsigned"},
{"uint (treated as uint32)", uint(1), 0, false, "int unsigned"},
{"int64", int64(1), 0, false, "bigint"},
{"uint64", uint64(1), 0, false, "bigint unsigned"},
{"float32", float32(1), 0, false, "double"},
{"float64", float64(1), 0, false, "double"},
{"[]uint8", []uint8{1}, 0, false, "mediumblob"},
{"NullInt64", sql.NullInt64{}, 0, false, "bigint"},
{"NullFloat64", sql.NullFloat64{}, 0, false, "double"},
{"NullBool", sql.NullBool{}, 0, false, "tinyint"},
{"Time", time.Time{}, 0, false, "datetime"},
{"default-size string", "", 0, false, "varchar(255)"},
{"sized string", "", 50, false, "varchar(50)"},
{"large string", "", 1024, false, "text"},
}
for _, t := range tests {
o.Spec(t.name, func(expect expect.Expectation, dialect gorp.MySQLDialect) {
typ := reflect.TypeOf(t.value)
sqlType := dialect.ToSqlType(typ, t.maxSize, t.autoIncr)
expect(sqlType).To(matchers.Equal(t.expected))
})
}
})

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

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

Describe("CreateTableSuffix", func() {
Context("with an empty engine", func() {
BeforeEach(func() {
engine = ""
encoding = "foo"
})
It("panics", func() {
Expect(func() {
dialect.CreateTableSuffix()
}).To(Panic())
})
})
o.Spec("AutoIncrInsertSuffix", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(dialect.AutoIncrInsertSuffix(nil)).To(matchers.Equal(""))
})

Context("with an empty encoding", func() {
BeforeEach(func() {
engine = "foo"
encoding = ""
o.Group("CreateTableSuffix", func() {
o.Group("with an empty engine", func() {
o.BeforeEach(func(expect expect.Expectation, dialect gorp.MySQLDialect) (expect.Expectation, gorp.MySQLDialect) {
dialect.Engine = ""
return expect, dialect
})
It("panics", func() {
Expect(func() {
dialect.CreateTableSuffix()
}).To(Panic())
o.Spec("panics", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(func() { dialect.CreateTableSuffix() }).To(Panic())
})
})

Context("with an engine and an encoding", func() {
BeforeEach(func() {
engine = "foo"
encoding = "bar"
o.Group("with an empty encoding", func() {
o.BeforeEach(func(expect expect.Expectation, dialect gorp.MySQLDialect) (expect.Expectation, gorp.MySQLDialect) {
dialect.Encoding = ""
return expect, dialect
})
It("returns a valid suffix", func() {
Expect(dialect.CreateTableSuffix()).To(Equal(" engine=foo charset=bar"))
o.Spec("panics", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(func() { dialect.CreateTableSuffix() }).To(Panic())
})
})
})

Describe("CreateIndexSuffix", func() {
It("returns the suffix for creating indexes", func() {
Expect(dialect.CreateIndexSuffix()).To(Equal("using"))
o.Spec("with an engine and an encoding", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(dialect.CreateTableSuffix()).To(matchers.Equal(" engine=foo charset=bar"))
})
})

Describe("DropIndexSuffix", func() {
It("returns the suffix for deleting indexes", func() {
Expect(dialect.DropIndexSuffix()).To(Equal("on"))
})
o.Spec("CreateIndexSuffix", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(dialect.CreateIndexSuffix()).To(matchers.Equal("using"))
})

Describe("TruncateClause", func() {
It("returns the clause for truncating a table", func() {
Expect(dialect.TruncateClause()).To(Equal("truncate"))
})
o.Spec("DropIndexSuffix", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(dialect.DropIndexSuffix()).To(matchers.Equal("on"))
})

Describe("SleepClause", func() {
It("returns the clause for sleeping", func() {
Expect(dialect.SleepClause(1 * time.Second)).To(Equal("sleep(1.000000)"))
Expect(dialect.SleepClause(100 * time.Millisecond)).To(Equal("sleep(0.100000)"))
})
o.Spec("TruncateClause", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(dialect.TruncateClause()).To(matchers.Equal("truncate"))
})

Describe("BindVar", func() {
It("returns the variable binding sequence", func() {
Expect(dialect.BindVar(0)).To(Equal("?"))
})
o.Spec("SleepClause", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(dialect.SleepClause(1 * time.Second)).To(matchers.Equal("sleep(1.000000)"))
expect(dialect.SleepClause(100 * time.Millisecond)).To(matchers.Equal("sleep(0.100000)"))
})

PDescribe("InsertAutoIncr", func() {})

Describe("QuoteField", func() {
It("returns the argument quoted as a field", func() {
Expect(dialect.QuoteField("foo")).To(Equal("`foo`"))
})
o.Spec("BindVar", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(dialect.BindVar(0)).To(matchers.Equal("?"))
})

Describe("QuotedTableForQuery", func() {
var (
schema, table string

quotedTable string
)
o.Spec("QuoteField", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(dialect.QuoteField("foo")).To(matchers.Equal("`foo`"))
})

JustBeforeEach(func() {
quotedTable = dialect.QuotedTableForQuery(schema, table)
o.Group("QuotedTableForQuery", func() {
o.Spec("using the default schema", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(dialect.QuotedTableForQuery("", "foo")).To(matchers.Equal("`foo`"))
})

Context("using the default schema", func() {
BeforeEach(func() {
schema = ""
table = "foo"
})
It("returns just the table", func() {
Expect(quotedTable).To(Equal("`foo`"))
})
})

Context("with a supplied schema", func() {
BeforeEach(func() {
schema = "foo"
table = "bar"
})
It("returns the schema and table", func() {
Expect(quotedTable).To(Equal("foo.`bar`"))
})
o.Spec("with a supplied schema", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(dialect.QuotedTableForQuery("foo", "bar")).To(matchers.Equal("foo.`bar`"))
})
})

Describe("IfSchemaNotExists", func() {
It("appends 'if not exists' to the command", func() {
Expect(dialect.IfSchemaNotExists("foo", "bar")).To(Equal("foo if not exists"))
})
o.Spec("IfSchemaNotExists", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(dialect.IfSchemaNotExists("foo", "bar")).To(matchers.Equal("foo if not exists"))
})

Describe("IfTableExists", func() {
It("appends 'if exists' to the command", func() {
Expect(dialect.IfTableExists("foo", "bar", "baz")).To(Equal("foo if exists"))
})
o.Spec("IfTableExists", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(dialect.IfTableExists("foo", "bar", "baz")).To(matchers.Equal("foo if exists"))
})

Describe("IfTableNotExists", func() {
It("appends 'if not exists' to the command", func() {
Expect(dialect.IfTableNotExists("foo", "bar", "baz")).To(Equal("foo if not exists"))
})
o.Spec("IfTableNotExists", func(expect expect.Expectation, dialect gorp.MySQLDialect) {
expect(dialect.IfTableNotExists("foo", "bar", "baz")).To(matchers.Equal("foo if not exists"))
})
})
}

type panicMatcher struct {
}

func Panic() panicMatcher {
return panicMatcher{}
}

func (m panicMatcher) Match(actual interface{}) (resultValue interface{}, err error) {
switch f := actual.(type) {
case func():
panicked := false
func() {
defer func() {
if r := recover(); r != nil {
panicked = true
}
}()
f()
}()
if panicked {
return f, nil
}
return f, errors.New("function did not panic")
default:
return f, fmt.Errorf("%T is not func()", f)
}
}
Loading

0 comments on commit 4df7849

Please sign in to comment.