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

Fix InheritanceTest#test_scope_inherited_properly implementation bugs #21994

Closed
wants to merge 3 commits into from

Conversation

mtodd
Copy link
Contributor

@mtodd mtodd commented Oct 19, 2015

I've updated the InheritanceTest#test_scope_inherited_properly test to trigger a reflection on the associations (which ActiveRecord::Relation does lazily). This causes the following error to be raised, indicating the test is not properly testing scope inheritance:

ActiveRecord::ConfigurationError: Association named 'account' was not found on Company; perhaps you misspelled it?

This PR fixes #21993 by fixing the pluralization of the joins clause on the Company.of_first_firm scope.

Inheritance is indeed properly working, just improperly tested. 😄

This triggers the JoinDependency work to reflect on the associations
and trigger an error as follows:

  ActiveRecord::ConfigurationError: Association named 'account' was
  not found on Company; perhaps you misspelled it?
Should be `Company.joins(:accounts)` not `Company.joins(:account)`.
@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 @carlosantoniodasilva (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.

@mtodd
Copy link
Contributor Author

mtodd commented Oct 19, 2015

Hmm, would like to see mtodd@b780f95 built and fail as expected.

@@ -13,7 +13,7 @@ class Company < AbstractCompany
has_many :accounts

scope :of_first_firm, lambda {
joins(:account => :firm).
joins(:accounts => :firm).
where('firms.id' => 1)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this was the original intent and a simple typo, or if the association for the has_one :account is failing to be inherited.

assert_nothing_raised { Company.of_first_firm }
assert_nothing_raised { Client.of_first_firm }
assert_nothing_raised { Company.of_first_firm.to_sql }
assert_nothing_raised { Client.of_first_firm.to_sql }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#21994 (comment) still applies (RE: JoinDependency)

arthurnn added a commit that referenced this pull request Oct 27, 2015
Fix InheritanceTest#test_scope_inherited_properly implementation bugs
@arthurnn
Copy link
Member

thanks , merged via 60c9701

@arthurnn arthurnn closed this Oct 27, 2015
@rafaelfranca
Copy link
Member

That broke the build so I reverted it.

rafaelfranca added a commit that referenced this pull request Oct 27, 2015
This reverts commit 60c9701, reversing
changes made to 6a25202.

Reason: Broken build
@mtodd
Copy link
Contributor Author

mtodd commented Oct 27, 2015

For the record: the failing build referenced includes https://travis-ci.org/rails/rails/jobs/87585808#L1309-L1309

@rafaelfranca @arthurnn looks like Company's accounts association was removed in @rafaelsales' commit rafaelsales@4f21d42 (merged in 4f21d42) very recently because it was missing an ID in the schema. So that would be the simple answer why it started failing (though reading through the history I'm surprised my branch didn't have that change).

@rafaelfranca @arthurnn who would know best about the current implementation of scope inheritance and this test in particular? @sikachu backported @Ben-M's patch for this test a while back, though I'm not sure how much the implementation has changed or whether the test was failing correctly in the first place (before the fix was applied), if the association in Company.of_first_firm is supposed to be plural or singular, or what...

In fact, I'm noticing that the Company.of_first_firm definition provides a where('firms.id' => 1) but there's no firms table since Firm is an STI class of Company. Does it matter for the test? Clearly not, since we're just testing that the scope is inherited, not whether the scope is correct.

Following that line of reasoning, it would seem that it doesn't matter if the association actually exists at all, just that both Company, the class that defines of_first_firm, and Client, the class that inherits of_first_firm, both respond to that method without exploding. This means that the test is fine as is, we don't need to actually materialize the SQL validating the associations in the scope, just that the subclass responds to the method.

Am I understanding the situation correctly? Is this kind of test or test environment acceptable?

If this is the case, I don't mind adding a comment in to explain as much for anybody who stumbles upon the test and wonders what the intent was, and offers a warning that the actual scope itself doesn't work.

@mtodd
Copy link
Contributor Author

mtodd commented Oct 27, 2015

Opened #22084 with a simple documentation change, assuming I'm understanding the nature of the test correctly.

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

Successfully merging this pull request may close these issues.

Improper assertion for ActiveRecord test for proper scope inheritance
5 participants