Skip to content

Commit

Permalink
Add tests on errors in query
Browse files Browse the repository at this point in the history
  • Loading branch information
rnixik committed Jul 17, 2018
1 parent 678d88a commit d0d595f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ type dbConnector func(conset *ConnectionSettings) (db *sqlx.DB, err error)

// QueryResult returns rows of data from DB
func (q *Query) QueryResult(dbConnect dbConnector, conset *ConnectionSettings, writer DataWriter) (err error) {
if len(q.primaryInterval) != 2 {
return fmt.Errorf("primaryInterval should contain two values")
}

db, err := dbConnect(conset)
if err != nil {
return err
Expand Down
36 changes: 34 additions & 2 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ func TestQueryResult(t *testing.T) {
}
}

func TestQueryResultIntervalError(t *testing.T) {
dbConnect := func(conset *ConnectionSettings) (db *sqlx.DB, err error) {
return nil, nil
}

var simpleQuery = &Query{
tables: []*QueryTable{
&QueryTable{"some_table", []string{"id"}},
},
relations: []*QueryRelation{},
primaryInterval: []int64{},
}

err := simpleQuery.QueryResult(dbConnect, &ConnectionSettings{}, &SimpleWriter{})
if err == nil {
t.Errorf("Expected error, but got nil")
return
}
}

func TestQueryResultConnectionError(t *testing.T) {
dbConnect := func(conset *ConnectionSettings) (db *sqlx.DB, err error) {
return nil, fmt.Errorf("Some DB error")
Expand Down Expand Up @@ -121,10 +141,22 @@ func TestQueryResultDDLError(t *testing.T) {
return sqlxDB, nil
}

mock.ExpectQuery("DESCRIBE `routes`").
var simpleQuery = &Query{
tables: []*QueryTable{
&QueryTable{"some_table", []string{"id"}},
},
relations: []*QueryRelation{},
primaryInterval: []int64{1, 2},
}

mock.ExpectQuery("SELECT (.+) FROM `some_table`").
WithArgs(1, 2).
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))

mock.ExpectQuery("DESCRIBE `some_table`").
WillReturnError(fmt.Errorf("Some error"))

err = typicalQuery.QueryResult(dbConnectMock, &ConnectionSettings{}, &EmptyWriter{})
err = simpleQuery.QueryResult(dbConnectMock, &ConnectionSettings{}, &EmptyWriter{})
if err == nil {
t.Errorf("Expected error, but got nil")
return
Expand Down

0 comments on commit d0d595f

Please sign in to comment.