-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
Add math test #32724
Add math test #32724
Conversation
Since Arel is private API, can we test this behavior from the public API? |
I think we can follow existing precedent for Arel tests. While it is private API, it exists to provide a clean layer to build AR on top of, so IMO it makes sense for it to have thorough unit tests. |
I see. So can we more exercise another expressions too ( |
They are all defined rails/activerecord/lib/arel/nodes/function.rb Lines 34 to 40 in 989b1cb
and as far as I can see they don't have any tests: https://github.com/rails/rails/tree/master/activerecord/test/cases/arel/nodes . Would you like me to add some? |
For now few tests exists in the file: rails/activerecord/test/cases/arel/attributes/attribute_test.rb Lines 313 to 389 in ac93e7b
How about adding |
Why is this still open? |
@vedant1811 it is adding some tests for an untested part of the code. I will add more tests as @kamipo suggests but I have not had time at the moment. |
d367872
to
2bf08c8
Compare
@kamipo I've added tests for the math operators. Is that what you were suggesting? |
end | ||
end | ||
|
||
%i[+ - & | ^ << >>].each do |math_operator| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason not to include *
and /
in the list?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they have a slightly different SQL signature https://github.com/rails/rails/pull/32724/files/2bf08c892b1c14d5c4c433e791017dd8d499b21e#diff-e79446cf78f4dc85660b8a7c9bbec429R8
E.g.
(AVG("users"."id") + 2)
vs
AVG("users"."id") * 2
} | ||
end | ||
|
||
it "table node should be compatiable with #{math_operator}" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
table[:id]
is not table node, but attribute node.
rails/activerecord/lib/arel/table.rb
Lines 81 to 83 in df850b8
def [](name) | |
::Arel::Attribute.new self, name | |
end |
rails/activerecord/lib/arel/attributes/attribute.rb
Lines 5 to 10 in df850b8
class Attribute < Struct.new :relation, :name | |
include Arel::Expressions | |
include Arel::Predications | |
include Arel::AliasPredication | |
include Arel::OrderPredications | |
include Arel::Math |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I've updated it 765a04a78c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Can you squash your commits into one?
After rails#449 was merged math can be done on these nodes, adding a test file to unit test all the math operators.
After rails/arel#449 was merged math can be done on these
nodes, adding a spec.
Copied over from rails/arel#524