Skip to content

Commit 06be149

Browse files
authored
Merge pull request #145 from aacanakin/master
Organize test structure, integrate go modules
2 parents bf3a132 + f23e140 commit 06be149

23 files changed

+626
-372
lines changed

.travis.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ addons:
1212
go:
1313
- tip
1414

15+
before_install:
16+
- go get github.com/mattn/goveralls
17+
1518
install:
16-
- go get -u -v -t github.com/aacanakin/qb
19+
- go get -v github.com/lib/pq
20+
- go get -v github.com/go-sql-driver/mysql
21+
- go get -v github.com/mattn/go-sqlite3
22+
- go get -t -v ./...
1723

1824
script:
1925
- go test -v -covermode=count -coverprofile=coverage.out
@@ -23,7 +29,7 @@ script:
2329
- tail --lines +2 sqlite.out >> coverage.out
2430
- tail --lines +2 postgres.out >> coverage.out
2531
- tail --lines +2 mysql.out >> coverage.out
26-
- $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken 0yIEy3NVX2lXn3KxYzHjkla7EWGjvmLAp
32+
- $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken 0yIEy3NVX2lXn3KxYzHjkla7EWGjvmLAp
2733

2834
before_script:
2935
- mysql -e 'create database IF NOT EXISTS qb_test;'

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
# qb - the database toolkit for go
44

5-
[![Join the chat at https://gitter.im/aacanakin/qb](https://badges.gitter.im/aacanakin/qb.svg)](https://gitter.im/aacanakin/qb?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
6-
[![Build Status](https://travis-ci.org/aacanakin/qb.svg?branch=master)](https://travis-ci.org/aacanakin/qb)
5+
[![Build Status](https://travis-ci.org/slicebit/qb.svg?branch=master)](https://travis-ci.org/slicebit/qb)
76
[![Coverage Status](https://coveralls.io/repos/github/slicebit/qb/badge.svg?branch=master)](https://coveralls.io/github/slicebit/qb?branch=master)
87
[![License (LGPL version 2.1)](https://img.shields.io/badge/license-GNU%20LGPL%20version%202.1-brightgreen.svg?style=flat)](http://opensource.org/licenses/LGPL-2.1)
98
[![Go Report Card](https://goreportcard.com/badge/github.com/slicebit/qb)](https://goreportcard.com/report/github.com/slicebit/qb)

column_test.go

Lines changed: 97 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,137 @@
11
package qb
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
6-
)
75

8-
func TestColumn(t *testing.T) {
9-
dialect := NewDialect("default")
6+
"github.com/stretchr/testify/suite"
107

11-
col := Column("id", Varchar().Size(40))
12-
assert.Equal(t, "id", col.Name)
13-
assert.Equal(t, Varchar().Size(40), col.Type)
8+
"github.com/stretchr/testify/assert"
9+
)
1410

15-
assert.Equal(t, "id VARCHAR(40)", col.String(dialect))
11+
type ColumnTestSuite struct {
12+
suite.Suite
13+
dialect Dialect
14+
ctx *CompilerContext
15+
}
1616

17-
col = Column("s", Varchar().Size(255)).Unique().NotNull().Default("hello")
18-
assert.Equal(t, "s VARCHAR(255) UNIQUE NOT NULL DEFAULT 'hello'", col.String(dialect))
17+
func (suite *ColumnTestSuite) SetupTest() {
18+
suite.dialect = NewDefaultDialect()
19+
suite.ctx = NewCompilerContext(suite.dialect)
20+
}
1921

20-
precisionCol := Column("f", Type("FLOAT").Precision(2, 5)).Null()
21-
assert.Equal(t, "f FLOAT(2, 5) NULL", precisionCol.String(dialect))
22+
func (suite *ColumnTestSuite) TestColumnVarcharSpecificSize() {
23+
col := Column("id", Varchar().Size(40))
24+
assert.Equal(suite.T(), "id", col.Name)
25+
assert.Equal(suite.T(), Varchar().Size(40), col.Type)
26+
assert.Equal(suite.T(), "id VARCHAR(40)", col.String(suite.dialect))
27+
}
2228

23-
col = Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
29+
func (suite *ColumnTestSuite) TestColumnVarcharUniqueNotNullDefault() {
30+
col := Column("s", Varchar().Size(255)).Unique().NotNull().Default("hello")
31+
assert.Equal(suite.T(), "s VARCHAR(255) UNIQUE NOT NULL DEFAULT 'hello'", col.String(suite.dialect))
32+
}
2433

25-
assert.Equal(t, "id INT PRIMARY KEY AUTO INCREMENT", col.String(dialect))
34+
func (suite *ColumnTestSuite) TestColumnFloatPrecision() {
35+
col := Column("f", Type("FLOAT").Precision(2, 5)).Null()
36+
assert.Equal(suite.T(), "f FLOAT(2, 5) NULL", col.String(suite.dialect))
37+
}
2638

27-
assert.Equal(t, "c INT TEST", Column("c", Int()).Constraint("TEST").String(dialect))
39+
func (suite *ColumnTestSuite) TestColumnIntInlinePrimaryKeyAutoIncrement() {
40+
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
41+
assert.Equal(suite.T(), "id INT PRIMARY KEY AUTO INCREMENT", col.String(suite.dialect))
42+
assert.Equal(suite.T(), "c INT TEST", Column("c", Int()).Constraint("TEST").String(suite.dialect))
43+
}
2844

29-
// like
45+
func (suite *ColumnTestSuite) TestColumnLike() {
46+
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
3047
like := col.Like("s%")
31-
sql, binds := asDefSQLBinds(like)
3248

33-
assert.Equal(t, "id LIKE ?", sql)
34-
assert.Equal(t, []interface{}{"s%"}, binds)
49+
sql := like.Accept(suite.ctx)
50+
binds := suite.ctx.Binds
3551

36-
// not in
37-
notIn := col.NotIn("id1", "id2")
38-
sql, binds = asDefSQLBinds(notIn)
52+
assert.Equal(suite.T(), "id LIKE ?", sql)
53+
assert.Equal(suite.T(), []interface{}{"s%"}, binds)
54+
}
3955

40-
assert.Equal(t, "id NOT IN (?, ?)", sql)
41-
assert.Equal(t, []interface{}{"id1", "id2"}, binds)
56+
func (suite *ColumnTestSuite) TestColumnNotIn() {
57+
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
58+
notIn := col.NotIn("id1", "id2")
59+
sql := notIn.Accept(suite.ctx)
60+
binds := suite.ctx.Binds
61+
assert.Equal(suite.T(), "id NOT IN (?, ?)", sql)
62+
assert.Equal(suite.T(), []interface{}{"id1", "id2"}, binds)
63+
}
4264

43-
// in
65+
func (suite *ColumnTestSuite) TestColumnIn() {
66+
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
4467
in := col.In("id1", "id2")
45-
sql, binds = asDefSQLBinds(in)
68+
sql := in.Accept(suite.ctx)
69+
binds := suite.ctx.Binds
4670

47-
assert.Equal(t, "id IN (?, ?)", sql)
48-
assert.Equal(t, []interface{}{"id1", "id2"}, binds)
71+
assert.Equal(suite.T(), "id IN (?, ?)", sql)
72+
assert.Equal(suite.T(), []interface{}{"id1", "id2"}, binds)
73+
}
4974

50-
// not eq
75+
func (suite *ColumnTestSuite) TestColumnNotEq() {
76+
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
5177
notEq := col.NotEq("id1")
52-
sql, binds = asDefSQLBinds(notEq)
78+
sql := notEq.Accept(suite.ctx)
79+
binds := suite.ctx.Binds
5380

54-
assert.Equal(t, "id != ?", sql)
55-
assert.Equal(t, []interface{}{"id1"}, binds)
81+
assert.Equal(suite.T(), "id != ?", sql)
82+
assert.Equal(suite.T(), []interface{}{"id1"}, binds)
83+
}
5684

57-
// eq
85+
func (suite *ColumnTestSuite) TestColumnEq() {
86+
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
5887
eq := col.Eq("id1")
59-
sql, binds = asDefSQLBinds(eq)
88+
sql := eq.Accept(suite.ctx)
89+
binds := suite.ctx.Binds
6090

61-
assert.Equal(t, "id = ?", sql)
62-
assert.Equal(t, []interface{}{"id1"}, binds)
91+
assert.Equal(suite.T(), "id = ?", sql)
92+
assert.Equal(suite.T(), []interface{}{"id1"}, binds)
93+
}
6394

64-
// gt
95+
func (suite *ColumnTestSuite) TestColumnGt() {
96+
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
6597
gt := col.Gt("id1")
66-
sql, binds = asDefSQLBinds(gt)
98+
sql := gt.Accept(suite.ctx)
99+
binds := suite.ctx.Binds
67100

68-
assert.Equal(t, "id > ?", sql)
69-
assert.Equal(t, []interface{}{"id1"}, binds)
101+
assert.Equal(suite.T(), "id > ?", sql)
102+
assert.Equal(suite.T(), []interface{}{"id1"}, binds)
103+
}
70104

71-
// lt
105+
func (suite *ColumnTestSuite) TestColumnLt() {
106+
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
72107
lt := col.Lt("id1")
73-
sql, binds = asDefSQLBinds(lt)
108+
sql := lt.Accept(suite.ctx)
109+
binds := suite.ctx.Binds
74110

75-
assert.Equal(t, "id < ?", sql)
76-
assert.Equal(t, []interface{}{"id1"}, binds)
111+
assert.Equal(suite.T(), "id < ?", sql)
112+
assert.Equal(suite.T(), []interface{}{"id1"}, binds)
113+
}
77114

78-
// gte
115+
func (suite *ColumnTestSuite) TestcolumnGte() {
116+
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
79117
gte := col.Gte("id1")
80-
sql, binds = asDefSQLBinds(gte)
118+
sql := gte.Accept(suite.ctx)
119+
binds := suite.ctx.Binds
81120

82-
assert.Equal(t, "id >= ?", sql)
83-
assert.Equal(t, []interface{}{"id1"}, binds)
121+
assert.Equal(suite.T(), "id >= ?", sql)
122+
assert.Equal(suite.T(), []interface{}{"id1"}, binds)
123+
}
84124

85-
// lte
125+
func (suite *ColumnTestSuite) TestColumnLte() {
126+
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
86127
lte := col.Lte("id1")
87-
sql, binds = asDefSQLBinds(lte)
128+
sql := lte.Accept(suite.ctx)
129+
binds := suite.ctx.Binds
88130

89-
assert.Equal(t, "id <= ?", sql)
90-
assert.Equal(t, []interface{}{"id1"}, binds)
131+
assert.Equal(suite.T(), "id <= ?", sql)
132+
assert.Equal(suite.T(), []interface{}{"id1"}, binds)
133+
}
91134

92-
sql = col.Accept(NewCompilerContext(dialect))
93-
assert.Equal(t, "id", sql)
135+
func TestColumnTestSuite(t *testing.T) {
136+
suite.Run(t, new(ColumnTestSuite))
94137
}

combiner_test.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
11
package qb
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/suite"
7+
8+
"github.com/stretchr/testify/assert"
69
)
710

8-
func TestCombiners(t *testing.T) {
11+
type CombinerTestSuite struct {
12+
suite.Suite
13+
dialect Dialect
14+
ctx *CompilerContext
15+
}
16+
17+
func (suite *CombinerTestSuite) SetupTest() {
18+
suite.dialect = NewDefaultDialect()
19+
suite.ctx = NewCompilerContext(suite.dialect)
20+
}
21+
22+
func (suite *CombinerTestSuite) TestCombinerAnd() {
923
email := Column("email", Varchar()).NotNull().Unique()
1024
id := Column("id", Int()).NotNull()
1125

1226
and := And(Eq(email, "[email protected]"), NotEq(id, 1))
13-
or := Or(Eq(email, "[email protected]"), NotEq(id, 1))
27+
sql := and.Accept(suite.ctx)
28+
binds := suite.ctx.Binds
1429

15-
sql, binds := asDefSQLBinds(and)
30+
assert.Equal(suite.T(), "(email = ? AND id != ?)", sql)
31+
assert.Equal(suite.T(), []interface{}{"[email protected]", 1}, binds)
32+
}
1633

17-
assert.Equal(t, "(email = ? AND id != ?)", sql)
18-
assert.Equal(t, []interface{}{"[email protected]", 1}, binds)
34+
func (suite *CombinerTestSuite) TestCombinerOr() {
35+
email := Column("email", Varchar()).NotNull().Unique()
36+
id := Column("id", Int()).NotNull()
1937

20-
sql, binds = asDefSQLBinds(or)
38+
or := Or(Eq(email, "[email protected]"), NotEq(id, 1))
39+
sql := or.Accept(suite.ctx)
40+
binds := suite.ctx.Binds
41+
42+
assert.Equal(suite.T(), "(email = ? OR id != ?)", sql)
43+
assert.Equal(suite.T(), []interface{}{"[email protected]", 1}, binds)
44+
}
2145

22-
assert.Equal(t, "(email = ? OR id != ?)", sql)
23-
assert.Equal(t, []interface{}{"[email protected]", 1}, binds)
46+
func TestCombinerTestSuite(t *testing.T) {
47+
suite.Run(t, new(CombinerTestSuite))
2448
}

0 commit comments

Comments
 (0)