pashagolub / pgxmock

pgx mock driver for golang to test database interactions
Other
386 stars 49 forks source link

ExpectBatch() bug with unordered queries expectations #207

Closed FarHowl closed 4 months ago

FarHowl commented 4 months ago

Describe the bug A new funcionality with ExpectBatch() function doesn`t match Batch() expectation, when we are trying to follow expectations without straight order using pgxmock.MatchExpectationsInOrder(false).

To Reproduce

func processBatch(db pgxmock.PgxPoolIface) error {
    batch := &pgx.Batch{}
        // Random order
    batch.Queue("SELECT id FROM normalized_queries WHERE query = $1", "some query")
    batch.Queue("INSERT INTO normalized_queries (query) VALUES ($1) RETURNING id", "some query")

    results := db.SendBatch(context.Background(), batch)
    defer results.Close()

    for i := 0; i < batch.Len(); i++ {
        var id int
        err := results.QueryRow().Scan(&id)
        if err != nil {
            return err
        }
    }

    return nil
}
func TestProcessBatch(t *testing.T) {
    mock, err := pgxmock.NewPool()
    assert.NoError(t, err)
    defer mock.Close()

    mock.MatchExpectationsInOrder(false)

    expectedBatch := mock.ExpectBatch()
    expectedBatch.ExpectQuery(regexp.QuoteMeta("INSERT INTO normalized_queries (query) VALUES ($1) RETURNING id")).WithArgs("some query").
        WillReturnRows(pgxmock.NewRows([]string{"id"}).AddRow(10))
    expectedBatch.ExpectQuery(regexp.QuoteMeta("SELECT id FROM normalized_queries WHERE query = $1")).WithArgs("some query").
        WillReturnRows(pgxmock.NewRows([]string{"id"}).AddRow(20))

    err = processBatch(mock)
    assert.NoError(t, err)
    assert.NoError(t, mock.ExpectationsWereMet())
}

Screenshot of the issue image

Expected behavior

func processBatch(db pgxmock.PgxPoolIface) error {
    batch := &pgx.Batch{}
        // Straight order
    batch.Queue("INSERT INTO normalized_queries (query) VALUES ($1) RETURNING id", "some query")
    batch.Queue("SELECT id FROM normalized_queries WHERE query = $1", "some query")

    results := db.SendBatch(context.Background(), batch)
    defer results.Close()

    for i := 0; i < batch.Len(); i++ {
        var id int
        err := results.QueryRow().Scan(&id)
        if err != nil {
            return err
        }
    }

    return nil
}
func TestProcessBatch(t *testing.T) {
    mock, err := pgxmock.NewPool()
    assert.NoError(t, err)
    defer mock.Close()

    mock.MatchExpectationsInOrder(false)

    expectedBatch := mock.ExpectBatch()
    expectedBatch.ExpectQuery(regexp.QuoteMeta("INSERT INTO normalized_queries (query) VALUES ($1) RETURNING id")).WithArgs("some query").
        WillReturnRows(pgxmock.NewRows([]string{"id"}).AddRow(10))
    expectedBatch.ExpectQuery(regexp.QuoteMeta("SELECT id FROM normalized_queries WHERE query = $1")).WithArgs("some query").
        WillReturnRows(pgxmock.NewRows([]string{"id"}).AddRow(20))

    err = processBatch(mock)
    assert.NoError(t, err)
    assert.NoError(t, mock.ExpectationsWereMet())
}

Screenshot of the expected behaviour image

Desktop: