Skip to content

Commit

Permalink
Fix syntax errors in tests, closes pashagolub#6
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Feb 11, 2021
1 parent 77a13a3 commit 2b07579
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 425 deletions.
69 changes: 0 additions & 69 deletions driver_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pgxmock

import (
"context"
"database/sql/driver"
"errors"
"testing"
Expand All @@ -17,55 +16,6 @@ func (c *converter) ConvertValue(v interface{}) (driver.Value, error) {
return nil, errors.New("converter disabled")
}

func TestShouldOpenConnectionIssue15(t *testing.T) {
mock, err := NewConn()
if err != nil {
t.Errorf("expected no error, but got: %s", err)
}
if len(pool.conns) != 1 {
t.Errorf("expected 1 connection in pool, but there is: %d", len(pool.conns))
}

smock, _ := mock.(*pgxmock)
if smock.opened != 1 {
t.Errorf("expected 1 connection on mock to be opened, but there is: %d", smock.opened)
}

// defer so the rows gets closed first
defer func() {
if smock.opened != 0 {
t.Errorf("expected no connections on mock to be opened, but there is: %d", smock.opened)
}
}()

mock.ExpectQuery("SELECT").WillReturnRows(NewRows([]string{"one", "two"}).AddRow("val1", "val2"))
rows, err := mock.Query(context.Background(), "SELECT")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
defer rows.Close()

mock.ExpectExec("UPDATE").WillReturnResult(NewResult("UPDATE", 1))
if _, err = mock.Exec(context.Background(), "UPDATE"); err != nil {
t.Errorf("unexpected error: %s", err)
}

// now there should be two connections open
if smock.opened != 2 {
t.Errorf("expected 2 connection on mock to be opened, but there is: %d", smock.opened)
}

mock.ExpectClose()
if err = mock.Close(); err != nil {
t.Errorf("expected no error on close, but got: %s", err)
}

// one is still reserved for rows
if smock.opened != 1 {
t.Errorf("expected 1 connection on mock to be still reserved for rows, but there is: %d", smock.opened)
}
}

func TestTwoOpenConnectionsOnTheSameDSN(t *testing.T) {
mock, err := NewConn()
if err != nil {
Expand All @@ -75,26 +25,7 @@ func TestTwoOpenConnectionsOnTheSameDSN(t *testing.T) {
if err != nil {
t.Errorf("expected no error, but got: %s", err)
}
if len(pool.conns) != 2 {
t.Errorf("expected 2 connection in pool, but there is: %d", len(pool.conns))
}

if mock == mock2 {
t.Errorf("expected not the same database instance, but it is the same")
}
if mock == mock2 {
t.Errorf("expected not the same mock instance, but it is the same")
}
}

// func TestWithOptions(t *testing.T) {
// c := &converter{}
// _, mock, err := New(ValueConverterOption(c))
// if err != nil {
// t.Errorf("expected no error, but got: %s", err)
// }
// smock, _ := mock.(*pgxmock)
// if smock.converter.(*converter) != c {
// t.Errorf("expected a custom converter to be set")
// }
// }
2 changes: 1 addition & 1 deletion expectations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestBuildQuery(t *testing.T) {

mock.ExpectQuery(query)
mock.ExpectExec(query)
mock.ExpectPrepare(query)
mock.ExpectPrepare("foo", query)

mock.QueryRow(context.Background(), query)
mock.Exec(context.Background(), query)
Expand Down
Loading

0 comments on commit 2b07579

Please sign in to comment.