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

Match a multi argument query against an expectation with an unique argument(s) #26

Closed
nicwest opened this issue Oct 9, 2015 · 5 comments

Comments

@nicwest
Copy link

nicwest commented Oct 9, 2015

I'm hitting an issue where I want to mock a query but I don't know about (or in my particular case; don't care about) some of the arguments that the query was made with. I Also don't know what order the queries are made in

consider this somewhat convoluted example:

https://gist.github.com/nicwest/f902199d47f5c4a7902a

I would like to do something along the lines of

    mock.ExpectQuery("SELECT word FROM things").WithUniqueArgs("Fred").WillReturnRows(
        sqlmock.NewRows(columns).AddRow("Fish"),
    )

    mock.ExpectQuery("SELECT word FROM things").WithUniqueArgs("Bat").WillReturnRows(
        sqlmock.NewRows(columns).AddRow("Bat"),
    )

    mock.ExpectQuery("SELECT word FROM things").WithUniqueArgs("Harry").WillReturnRows(
        sqlmock.NewRows(columns).AddRow("Horse"),
    )

Any suggestions?

@l3pp4rd
Copy link
Member

l3pp4rd commented Oct 12, 2015

Hi, there is an example with unordered matching. I think that will manage your use case.

@nicwest
Copy link
Author

nicwest commented Oct 12, 2015

Thanks!

That I think solves part of my problem, the other bit is how I differentiate between two queries without specifying all of the arguments?

@l3pp4rd
Copy link
Member

l3pp4rd commented Oct 12, 2015

You may catch it by regular expression on SQL without providing any argument, in that case you need to know how many of these queries will be run and just add expectations in a loop only on SQL statement. If you provide at least one argument, only in that case arguments are being matched. Nothing prevents you from expecting SQL query like: SELECT (.*) FROM things or ^INSERT INTO things

@nicwest
Copy link
Author

nicwest commented Oct 13, 2015

Sure, but in the case that my query is SELECT * FROM things WHERE thing=? AND foobar=? and I know what thing will be but not foobar? I can think of some examples even under test conditions of where this could happen, for example a random number, a relation id, the current time, the process id, an OS specific file path, etc.

@l3pp4rd
Copy link
Member

l3pp4rd commented Oct 13, 2015

Ah, in that case you should use custom argument matcher for one which differs and you do not care about.

@l3pp4rd l3pp4rd closed this as completed Oct 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants