Skip to content

Commit

Permalink
Failing tests due to expected queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliocn authored Sep 19, 2024
1 parent efee7a3 commit 4a4e1d9
Showing 1 changed file with 62 additions and 18 deletions.
80 changes: 62 additions & 18 deletions examples/batch/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,93 @@ package main
import (
"testing"

pgx "github.com/jackc/pgx/v5"
"github.com/pashagolub/pgxmock/v4"
pgxmock "github.com/pashagolub/pgxmock/v4"
)

// a successful test case
func TestExpectBatch(t *testing.T) {
func TestNewExample(t *testing.T) {
mock, err := pgxmock.NewPool()
if err != nil {
t.Fatal(err)
}
defer mock.Close()

// Expect a call to Exec
mock.ExpectExec(`^CREATE TABLE IF NOT EXISTS (.+)`).
WillReturnResult(pgxmock.NewResult("CREATE TABLE", 0))

_, err = NewExample(mock)
if err != nil {
t.Errorf("creating new example error: %s", err)
}

// We make sure that all expectations were met
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}

func TestSendCustomBatch(t *testing.T) {
mock, err := pgxmock.NewPool()
if err != nil {
t.Fatal(err)
}
defer mock.Close()

// Expect a call to Exec and pgx.Batch
mock.ExpectExec(`^CREATE TABLE IF NOT EXISTS (.+)`).
WillReturnResult(pgxmock.NewResult("CREATE TABLE", 0))
mock.ExpectBatch()

// Setup the example
var example = ExampleBatch{db: mock, batch: &pgx.Batch{}}
example, err := NewExample(mock)
if err != nil {
t.Errorf("creating new example error: %s", err)
}

// now we execute our method
example.requestBatch()
err = example.SendCustomBatch([]string{
"SELECT title FROM metadata",
"SELECT authors FROM metadata",
"SELECT subject, description FROM metadata",
})
if err != nil {
t.Errorf("SendCustomBatch error: %s", err)
}

err = example.TestCustomResults()
if err != nil {
t.Errorf("TestCustomResults error: %s", err)
}

// we make sure that all expectations were met
// We make sure that all expectations were met
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}

// a failing test case
func TestExpectBegin(t *testing.T) {
func TestBulkInsert(t *testing.T) {
mock, err := pgxmock.NewPool()
if err != nil {
t.Fatal(err)
}
defer mock.Close()

mock.ExpectBegin()
// Expect a call to Exec and pgx.Batch
mock.ExpectExec(`^CREATE TABLE IF NOT EXISTS (.+)`).
WillReturnResult(pgxmock.NewResult("CREATE TABLE", 0))
mock.ExpectBatch()

// Setup the example
var example = ExampleBatch{db: mock, batch: &pgx.Batch{}}
example, err := NewExample(mock)
if err != nil {
t.Errorf("creating new example error: %s", err)
}

// now we execute our method
example.requestBatch()
// Insert multiple rows into the database
err = example.BulkInsertMetadata([]metadata{
{`title`, `author`, `subject`, `description`},
})
if err != nil {
t.Errorf("bulk insert error: %s", err)
}

// we make sure that all expectations were met
// We make sure that all expectations were met
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
Expand Down

0 comments on commit 4a4e1d9

Please sign in to comment.