Skip to content

Commit

Permalink
Minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Jun 21, 2018
1 parent 182eaf8 commit d4039f4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 35 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ test-db:
internal/test/sql/$(REFORM_DATABASE)_drop.sql
reform-db -db-driver="$(REFORM_DRIVER)" -db-source="$(REFORM_ROOT_SOURCE)" exec \
internal/test/sql/$(REFORM_DATABASE)_create.sql


# TODO remove that hack in reform 1.4
# https://github.com/go-reform/reform/issues/151
# https://github.com/go-reform/reform/issues/157
cat \
internal/test/sql/$(REFORM_DATABASE)_init.sql \
internal/test/sql/data.sql \
Expand All @@ -53,7 +56,7 @@ test-db:
> internal/test/sql/$(REFORM_DATABASE)_combined.tmp.sql
reform-db -db-driver="$(REFORM_DRIVER)" -db-source="$(REFORM_INIT_SOURCE)" exec \
internal/test/sql/$(REFORM_DATABASE)_combined.tmp.sql

go test $(REFORM_TEST_FLAGS) -covermode=count -coverprofile=reform-db.cover gopkg.in/reform.v1/reform-db
go test $(REFORM_TEST_FLAGS) -covermode=count -coverprofile=reform.cover
gocoverutil -coverprofile=coverage.txt merge *.cover
Expand Down
21 changes: 10 additions & 11 deletions base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,26 @@ func checkForeignKeys(t *testing.T, q *reform.Querier) {
}

// withIdentityInsert executes an action with MS SQL IDENTITY_INSERT enabled for a table
func withIdentityInsert(suite *ReformSuite, q *reform.Querier, table string, action func()) {
func withIdentityInsert(t *testing.T, q *reform.Querier, table string, action func()) {
if q.Dialect != mssql.Dialect && q.Dialect != sqlserver.Dialect {
action()
return
}

t := suite.T()
sqlTemplate := fmt.Sprintf("SET IDENTITY_INSERT %s %%s", q.QuoteIdentifier(table))
query := fmt.Sprintf("SET IDENTITY_INSERT %s %%s", q.QuoteIdentifier(table))

_, onErr := q.Exec(fmt.Sprintf(sqlTemplate, "ON"))
require.NoError(t, onErr)
_, err := q.Exec(fmt.Sprintf(query, "ON"))
require.NoError(t, err)

action()

_, offErr := q.Exec(fmt.Sprintf(sqlTemplate, "OFF"))
require.NoError(t, offErr)
_, err = q.Exec(fmt.Sprintf(query, "OFF"))
require.NoError(t, err)
}

func insertPersonWithID(suite *ReformSuite, q *reform.Querier, str reform.Struct) error {
func insertPersonWithID(t *testing.T, q *reform.Querier, str reform.Struct) error {
var err error
withIdentityInsert(suite, q, "people", func() { err = q.Insert(str) })
withIdentityInsert(t, q, "people", func() { err = q.Insert(str) })
return err
}

Expand Down Expand Up @@ -183,7 +182,7 @@ func (s *ReformSuite) TestTimezones() {
`(11, '11', %s), (12, '12', %s), (13, '13', %s), (14, '14', %s)`,
s.q.Placeholder(1), s.q.Placeholder(2), s.q.Placeholder(3), s.q.Placeholder(4))

withIdentityInsert(s, s.q, "people", func() {
withIdentityInsert(s.T(), s.q, "people", func() {
_, err := s.q.Exec(q, t1, t2, tVLAT, tHST)
s.NoError(err)
})
Expand All @@ -210,7 +209,7 @@ func (s *ReformSuite) TestTimezones() {
`('11', '11', %s), ('12', '12', %s), ('13', '13', %s), ('14', '14', %s)`,
s.q.Placeholder(1), s.q.Placeholder(2), s.q.Placeholder(3), s.q.Placeholder(4))

withIdentityInsert(s, s.q, "people", func() {
withIdentityInsert(s.T(), s.q, "people", func() {
_, err := s.q.Exec(q, t1, t2, tVLAT, tHST)
s.NoError(err)
})
Expand Down
38 changes: 19 additions & 19 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (s *ReformSuite) TestBeginCommit() {

tx, err := DB.Begin()
s.Require().NoError(err)
s.NoError(insertPersonWithID(s, tx.Querier, person))
s.NoError(insertPersonWithID(s.T(), tx.Querier, person))
s.NoError(tx.Commit())
s.Equal(tx.Commit(), reform.ErrTxDone)
s.Equal(tx.Rollback(), reform.ErrTxDone)
Expand All @@ -35,7 +35,7 @@ func (s *ReformSuite) TestBeginRollback() {

tx, err := DB.Begin()
s.Require().NoError(err)
s.NoError(insertPersonWithID(s, tx.Querier, person))
s.NoError(insertPersonWithID(s.T(), tx.Querier, person))
s.NoError(tx.Rollback())
s.Equal(tx.Commit(), reform.ErrTxDone)
s.Equal(tx.Rollback(), reform.ErrTxDone)
Expand All @@ -57,9 +57,9 @@ func (s *ReformSuite) TestErrorInTransaction() {
// commit works
tx, err := DB.Begin()
s.Require().NoError(err)
s.NoError(insertPersonWithID(s, tx.Querier, person1))
s.Error(insertPersonWithID(s, tx.Querier, person1)) // duplicate PK
s.NoError(insertPersonWithID(s, tx.Querier, person2)) // INSERT works
s.NoError(insertPersonWithID(s.T(), tx.Querier, person1))
s.Error(insertPersonWithID(s.T(), tx.Querier, person1)) // duplicate PK
s.NoError(insertPersonWithID(s.T(), tx.Querier, person2)) // INSERT works
s.NoError(tx.Commit())
s.Equal(tx.Commit(), reform.ErrTxDone)
s.Equal(tx.Rollback(), reform.ErrTxDone)
Expand All @@ -71,9 +71,9 @@ func (s *ReformSuite) TestErrorInTransaction() {
// rollback works
tx, err = DB.Begin()
s.Require().NoError(err)
s.NoError(insertPersonWithID(s, tx.Querier, person1))
s.Error(insertPersonWithID(s, tx.Querier, person1)) // duplicate PK
s.NoError(insertPersonWithID(s, tx.Querier, person2)) // INSERT works
s.NoError(insertPersonWithID(s.T(), tx.Querier, person1))
s.Error(insertPersonWithID(s.T(), tx.Querier, person1)) // duplicate PK
s.NoError(insertPersonWithID(s.T(), tx.Querier, person2)) // INSERT works
s.NoError(tx.Rollback())
s.Equal(tx.Commit(), reform.ErrTxDone)
s.Equal(tx.Rollback(), reform.ErrTxDone)
Expand All @@ -97,9 +97,9 @@ func (s *ReformSuite) TestAbortedTransaction() {
// commit fails
tx, err := DB.Begin()
s.Require().NoError(err)
s.NoError(insertPersonWithID(s, tx.Querier, person1))
s.EqualError(insertPersonWithID(s, tx.Querier, person1), `pq: duplicate key value violates unique constraint "people_pkey"`)
s.EqualError(insertPersonWithID(s, tx.Querier, person2), `pq: current transaction is aborted, commands ignored until end of transaction block`)
s.NoError(insertPersonWithID(s.T(), tx.Querier, person1))
s.EqualError(insertPersonWithID(s.T(), tx.Querier, person1), `pq: duplicate key value violates unique constraint "people_pkey"`)
s.EqualError(insertPersonWithID(s.T(), tx.Querier, person2), `pq: current transaction is aborted, commands ignored until end of transaction block`)
s.EqualError(tx.Commit(), `pq: Could not complete operation in a failed transaction`)
s.Equal(tx.Rollback(), reform.ErrTxDone)
s.EqualError(DB.Reload(person1), reform.ErrNoRows.Error())
Expand All @@ -108,9 +108,9 @@ func (s *ReformSuite) TestAbortedTransaction() {
// rollback works
tx, err = DB.Begin()
s.Require().NoError(err)
s.NoError(insertPersonWithID(s, tx.Querier, person1))
s.EqualError(insertPersonWithID(s, tx.Querier, person1), `pq: duplicate key value violates unique constraint "people_pkey"`)
s.EqualError(insertPersonWithID(s, tx.Querier, person2), `pq: current transaction is aborted, commands ignored until end of transaction block`)
s.NoError(insertPersonWithID(s.T(), tx.Querier, person1))
s.EqualError(insertPersonWithID(s.T(), tx.Querier, person1), `pq: duplicate key value violates unique constraint "people_pkey"`)
s.EqualError(insertPersonWithID(s.T(), tx.Querier, person2), `pq: current transaction is aborted, commands ignored until end of transaction block`)
s.NoError(tx.Rollback())
s.Equal(tx.Commit(), reform.ErrTxDone)
s.Equal(tx.Rollback(), reform.ErrTxDone)
Expand All @@ -126,7 +126,7 @@ func (s *ReformSuite) TestInTransaction() {

// error in closure
err := DB.InTransaction(func(tx *reform.TX) error {
s.NoError(insertPersonWithID(s, tx.Querier, person))
s.NoError(insertPersonWithID(s.T(), tx.Querier, person))
return errors.New("epic error")
})
s.EqualError(err, "epic error")
Expand All @@ -135,16 +135,16 @@ func (s *ReformSuite) TestInTransaction() {
// panic in closure
s.Panics(func() {
err = DB.InTransaction(func(tx *reform.TX) error {
s.NoError(insertPersonWithID(s, tx.Querier, person))
s.NoError(insertPersonWithID(s.T(), tx.Querier, person))
panic("epic panic!")
})
})
s.Equal(DB.Reload(person), reform.ErrNoRows)

// duplicate PK in closure
err = DB.InTransaction(func(tx *reform.TX) error {
s.NoError(insertPersonWithID(s, tx.Querier, person))
err := insertPersonWithID(s, tx.Querier, person)
s.NoError(insertPersonWithID(s.T(), tx.Querier, person))
err := insertPersonWithID(s.T(), tx.Querier, person)
s.Error(err)
return err
})
Expand All @@ -153,7 +153,7 @@ func (s *ReformSuite) TestInTransaction() {

// no error
err = DB.InTransaction(func(tx *reform.TX) error {
s.NoError(insertPersonWithID(s, tx.Querier, person))
s.NoError(insertPersonWithID(s.T(), tx.Querier, person))
return nil
})
s.NoError(err)
Expand Down
6 changes: 3 additions & 3 deletions querier_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *ReformSuite) TestInsertWithValues() {
func (s *ReformSuite) TestInsertWithPrimaryKey() {
newEmail := faker.Internet().Email()
person := &Person{ID: 50, Email: &newEmail}
err := insertPersonWithID(s, s.q, person)
err := insertPersonWithID(s.T(), s.q, person)
s.NoError(err)
s.Equal(int32(50), person.ID)
s.Equal("", person.Name)
Expand Down Expand Up @@ -165,7 +165,7 @@ func (s *ReformSuite) TestInsertMultiWithPrimaryKeys() {
newName := faker.Name().Name()
person1, person2 := &Person{ID: 50, Email: &newEmail}, &Person{ID: 51, Name: newName}

withIdentityInsert(s, s.q, "people", func() {
withIdentityInsert(s.T(), s.q, "people", func() {
err := s.q.InsertMulti(person1, person2)
s.NoError(err)
})
Expand Down Expand Up @@ -367,7 +367,7 @@ func (s *ReformSuite) TestSaveWithPrimaryKey() {
newName := faker.Name().Name()
person := &Person{ID: 99, Name: newName}

withIdentityInsert(s, s.q, "people", func() {
withIdentityInsert(s.T(), s.q, "people", func() {
err := s.q.Save(person)
s.NoError(err)
})
Expand Down

0 comments on commit d4039f4

Please sign in to comment.