Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExpectBatch() bug with unordered queries expectations #207

Closed
FarHowl opened this issue Jun 10, 2024 · 0 comments
Closed

ExpectBatch() bug with unordered queries expectations #207

FarHowl opened this issue Jun 10, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@FarHowl
Copy link

FarHowl commented Jun 10, 2024

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:

  • OS: Windows 11
  • IDE: VS Code
@pashagolub pashagolub self-assigned this Jun 10, 2024
@pashagolub pashagolub added the bug Something isn't working label Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants