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

Add math test #32724

Merged
merged 1 commit into from
May 14, 2018
Merged

Add math test #32724

merged 1 commit into from
May 14, 2018

Conversation

nikolai-b
Copy link
Contributor

After rails/arel#449 was merged math can be done on these
nodes, adding a spec.

Copied over from rails/arel#524

@kamipo
Copy link
Member

kamipo commented Apr 26, 2018

Since Arel is private API, can we test this behavior from the public API?

@matthewd
Copy link
Member

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.

@kamipo
Copy link
Member

kamipo commented Apr 26, 2018

I see. So can we more exercise another expressions too (maximum, minimum, and average)?

@nikolai-b
Copy link
Contributor Author

They are all defined

%w{
Sum
Exists
Max
Min
Avg
}.each do |name|

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?

@kamipo
Copy link
Member

kamipo commented Apr 26, 2018

For now few tests exists in the file:

describe "#average" do
it "should create a AVG node" do
relation = Table.new(:users)
relation[:id].average.must_be_kind_of Nodes::Avg
end
it "should generate the proper SQL" do
relation = Table.new(:users)
mgr = relation.project relation[:id].average
mgr.to_sql.must_be_like %{
SELECT AVG("users"."id")
FROM "users"
}
end
end
describe "#maximum" do
it "should create a MAX node" do
relation = Table.new(:users)
relation[:id].maximum.must_be_kind_of Nodes::Max
end
it "should generate proper SQL" do
relation = Table.new(:users)
mgr = relation.project relation[:id].maximum
mgr.to_sql.must_be_like %{
SELECT MAX("users"."id")
FROM "users"
}
end
end
describe "#minimum" do
it "should create a Min node" do
relation = Table.new(:users)
relation[:id].minimum.must_be_kind_of Nodes::Min
end
it "should generate proper SQL" do
relation = Table.new(:users)
mgr = relation.project relation[:id].minimum
mgr.to_sql.must_be_like %{
SELECT MIN("users"."id")
FROM "users"
}
end
end
describe "#sum" do
it "should create a SUM node" do
relation = Table.new(:users)
relation[:id].sum.must_be_kind_of Nodes::Sum
end
it "should generate the proper SQL" do
relation = Table.new(:users)
mgr = relation.project relation[:id].sum
mgr.to_sql.must_be_like %{
SELECT SUM("users"."id")
FROM "users"
}
end
end
describe "#count" do
it "should return a count node" do
relation = Table.new(:users)
relation[:id].count.must_be_kind_of Nodes::Count
end
it "should take a distinct param" do
relation = Table.new(:users)
count = relation[:id].count(nil)
count.must_be_kind_of Nodes::Count
count.distinct.must_be_nil
end
end

How about adding math_test.rb instead of adding one .sum - 2 test case?
Not only sum and count but also attributes (e.g. table[:id]) can be mathmatically operated, but not tested.

@vedant1811
Copy link

Why is this still open?

@nikolai-b
Copy link
Contributor Author

@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.

@nikolai-b nikolai-b force-pushed the add_math_test branch 2 times, most recently from d367872 to 2bf08c8 Compare May 14, 2018 08:12
@nikolai-b
Copy link
Contributor Author

@kamipo I've added tests for the math operators. Is that what you were suggesting?

end
end

%i[+ - & | ^ << >>].each do |math_operator|
Copy link
Member

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?

Copy link
Contributor Author

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
Copy link
Member

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.

def [](name)
::Arel::Attribute.new self, name
end

class Attribute < Struct.new :relation, :name
include Arel::Expressions
include Arel::Predications
include Arel::AliasPredication
include Arel::OrderPredications
include Arel::Math

Copy link
Contributor Author

@nikolai-b nikolai-b May 14, 2018

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

Copy link
Member

@kamipo kamipo left a 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.
@kamipo kamipo merged commit ff083ee into rails:master May 14, 2018
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.

None yet

4 participants