Skip to content

How can I make sqlmock not care about the order of arguments? #322

@felipe-cg

Description

@felipe-cg

Question

Hello, I have a test for an app using both sqlmock and GORM:

db.ExpectPrepare(regexp.QuoteMeta(`INSERT INTO "table_name").
  ExpectQuery().
  WithArgs(testId, anotherTestId).
  WillReturnRows(sqlmock.NewRows([]string{"test_id", "another_test_id"}).
  AddRow(testId, anotherTestId))

However, when I run my tests, sometimes the query I receive is like this:
INSERT INTO "table_name" ("test_id","another_test_id") VALUES ('example','example2') ON CONFLICT DO NOTHING RETURNING "test_id","another_test_id"

And other times, the arguments are swapped like this:
INSERT INTO "table_name" ("another_test_id", "test_id") VALUES ('example','example2') ON CONFLICT DO NOTHING RETURNING "test_id","another_test_id"

This inconsistency in the order of arguments causes some tests to fail because sqlmock expects the query arguments in a specific order. I would like to know if there is a way to make sqlmock not care about the order of arguments. Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions