Skip to content

Add test cases about MySQL ORDER BY FIELD() #23751

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

Merged
merged 1 commit into from
Feb 22, 2016
Merged

Add test cases about MySQL ORDER BY FIELD() #23751

merged 1 commit into from
Feb 22, 2016

Conversation

chezou
Copy link
Contributor

@chezou chezou commented Feb 18, 2016

These assertions examine the sanitization of ORDER BY FIELD() with empty data on MySQL appropriately.

  Tag.order(['field(id, ?)', []]).to_sql
  # => SELECT "tags".* FROM "tags" ORDER BY field(id, NULL)

  Tag.order(['field(id, ?)', nil]).to_sql
  # => SELECT "tags".* FROM "tags" ORDER BY field(id, NULL)

Add assertions to MySQL `ORDER BY FIELD()` with empty data.
These tests examine to sanitize `ORDER BY FIELD()` with empty data
appropriately.

```ruby
  Tag.order(['field(id, ?)', []]).to_sql
  # => SELECT "tags".* FROM "tags" ORDER BY field(id, NULL)

  Tag.order(['field(id, ?)', nil]).to_sql
  # => SELECT "tags".* FROM "tags" ORDER BY field(id, NULL)
```
@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @pixeltrix (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@chezou chezou changed the title Add assertions order by field with empty data Add test cases about MySQL ORDER BY FIELD() Feb 18, 2016
@senny
Copy link
Member

senny commented Feb 18, 2016

@chezou what was the reason that prompted you to write this test-case?

@chezou
Copy link
Contributor Author

chezou commented Feb 22, 2016

These are special cases of ORDER BY FIELD() which return records same as without ORDER BY clause in current implementation of MySQL. In order to clarify what AR returns, I added the tests.

@senny
Copy link
Member

senny commented Feb 22, 2016

@chezou does AR do anything else than returning the records exactly as they are returned from MySQL? I don't understand what behavior of AR we are testing here. If it's something thats dependent on the MySQL implementation then we just pass on whatever we receive.

@pixeltrix
Copy link
Contributor

@senny I think it's more testing the sanitisation of order clauses - I guess a couple of extra tests for empty and nil values wouldn't be harmful, though I hope we cover sanitisation of [] and nil elsewhere as well.

pixeltrix added a commit that referenced this pull request Feb 22, 2016
Add test cases about MySQL ORDER BY FIELD()
@pixeltrix pixeltrix merged commit 3591a00 into rails:master Feb 22, 2016
@chezou chezou deleted the add-test-case-order-by-field branch February 23, 2016 02:01
@chezou
Copy link
Contributor Author

chezou commented Feb 23, 2016

Thank you for merge it. The second and the subsequent arguments are important for FIELDS() function. So this spec makes clear that AR sanitize [] and nil properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants