2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/post"
|
2013-09-04 00:03:26 -04:00
|
|
|
|
|
|
|
module ActiveRecord
|
2016-08-11 00:16:07 -04:00
|
|
|
class RelationMutationTest < ActiveRecord::TestCase
|
2017-05-22 11:22:47 -04:00
|
|
|
(Relation::MULTI_VALUE_METHODS - [:references, :extending, :order, :unscope, :select]).each do |method|
|
2013-09-04 00:03:26 -04:00
|
|
|
test "##{method}!" do
|
|
|
|
assert relation.public_send("#{method}!", :foo).equal?(relation)
|
|
|
|
assert_equal [:foo], relation.public_send("#{method}_values")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-16 00:43:24 -04:00
|
|
|
test "#_select!" do
|
2016-07-17 08:14:01 -04:00
|
|
|
assert relation._select!(:foo).equal?(relation)
|
|
|
|
assert_equal [:foo], relation.select_values
|
2014-04-16 00:43:24 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "#order!" do
|
|
|
|
assert relation.order!("name ASC").equal?(relation)
|
|
|
|
assert_equal ["name ASC"], relation.order_values
|
2013-09-04 00:03:26 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "#order! with symbol prepends the table name" do
|
2013-09-04 00:03:26 -04:00
|
|
|
assert relation.order!(:name).equal?(relation)
|
|
|
|
node = relation.order_values.first
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate node, :ascending?
|
2019-02-16 11:56:18 -05:00
|
|
|
assert_equal "name", node.expr.name
|
2013-09-04 00:03:26 -04:00
|
|
|
assert_equal "posts", node.expr.relation.name
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "#order! on non-string does not attempt regexp match for references" do
|
2013-09-04 00:03:26 -04:00
|
|
|
obj = Object.new
|
2015-09-09 15:00:40 -04:00
|
|
|
assert_not_called(obj, :=~) do
|
|
|
|
assert relation.order!(obj)
|
|
|
|
assert_equal [obj], relation.order_values
|
|
|
|
end
|
2013-09-04 00:03:26 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "#references!" do
|
2013-09-04 00:03:26 -04:00
|
|
|
assert relation.references!(:foo).equal?(relation)
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes relation.references_values, "foo"
|
2013-09-04 00:03:26 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "extending!" do
|
2013-09-04 00:03:26 -04:00
|
|
|
mod, mod2 = Module.new, Module.new
|
|
|
|
|
|
|
|
assert relation.extending!(mod).equal?(relation)
|
|
|
|
assert_equal [mod], relation.extending_values
|
|
|
|
assert relation.is_a?(mod)
|
|
|
|
|
|
|
|
relation.extending!(mod2)
|
|
|
|
assert_equal [mod, mod2], relation.extending_values
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "extending! with empty args" do
|
2013-09-04 00:03:26 -04:00
|
|
|
relation.extending!
|
|
|
|
assert_equal [], relation.extending_values
|
|
|
|
end
|
|
|
|
|
2018-04-11 20:08:13 -04:00
|
|
|
(Relation::SINGLE_VALUE_METHODS - [:lock, :reordering, :reverse_order, :create_with, :skip_query_cache]).each do |method|
|
2013-09-04 00:03:26 -04:00
|
|
|
test "##{method}!" do
|
|
|
|
assert relation.public_send("#{method}!", :foo).equal?(relation)
|
|
|
|
assert_equal :foo, relation.public_send("#{method}_value")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "#from!" do
|
|
|
|
assert relation.from!("foo").equal?(relation)
|
|
|
|
assert_equal "foo", relation.from_clause.value
|
2013-09-04 00:03:26 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "#lock!" do
|
|
|
|
assert relation.lock!("foo").equal?(relation)
|
|
|
|
assert_equal "foo", relation.lock_value
|
2013-09-04 00:03:26 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "#reorder!" do
|
2017-01-05 03:20:57 -05:00
|
|
|
@relation = relation.order("foo")
|
2013-09-04 00:03:26 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert relation.reorder!("bar").equal?(relation)
|
|
|
|
assert_equal ["bar"], relation.order_values
|
2013-09-04 00:03:26 -04:00
|
|
|
assert relation.reordering_value
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "#reorder! with symbol prepends the table name" do
|
2013-09-12 20:36:00 -04:00
|
|
|
assert relation.reorder!(:name).equal?(relation)
|
|
|
|
node = relation.order_values.first
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate node, :ascending?
|
2019-02-16 11:56:18 -05:00
|
|
|
assert_equal "name", node.expr.name
|
2013-09-12 20:36:00 -04:00
|
|
|
assert_equal "posts", node.expr.relation.name
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "reverse_order!" do
|
|
|
|
@relation = Post.order("title ASC, comments_count DESC")
|
2014-04-07 15:01:17 -04:00
|
|
|
|
|
|
|
relation.reverse_order!
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "title DESC", relation.order_values.first
|
|
|
|
assert_equal "comments_count ASC", relation.order_values.last
|
2014-04-07 15:01:17 -04:00
|
|
|
|
2013-09-04 00:03:26 -04:00
|
|
|
relation.reverse_order!
|
2014-04-07 15:01:17 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "title ASC", relation.order_values.first
|
|
|
|
assert_equal "comments_count DESC", relation.order_values.last
|
2013-09-04 00:03:26 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "create_with!" do
|
|
|
|
assert relation.create_with!(foo: "bar").equal?(relation)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ foo: "bar" }, relation.create_with_value)
|
2013-09-04 00:03:26 -04:00
|
|
|
end
|
|
|
|
|
2017-04-06 19:40:52 -04:00
|
|
|
test "merge!" do
|
2015-01-25 19:35:46 -05:00
|
|
|
assert relation.merge!(select: :foo).equal?(relation)
|
|
|
|
assert_equal [:foo], relation.select_values
|
2013-09-04 00:03:26 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "merge with a proc" do
|
2015-01-25 19:35:46 -05:00
|
|
|
assert_equal [:foo], relation.merge(-> { select(:foo) }).select_values
|
2013-09-04 00:03:26 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "none!" do
|
2013-09-04 00:03:26 -04:00
|
|
|
assert relation.none!.equal?(relation)
|
|
|
|
assert_equal [NullRelation], relation.extending_values
|
|
|
|
assert relation.is_a?(NullRelation)
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
test "distinct!" do
|
2013-09-04 00:03:26 -04:00
|
|
|
relation.distinct! :foo
|
|
|
|
assert_equal :foo, relation.distinct_value
|
|
|
|
end
|
2017-04-24 16:10:47 -04:00
|
|
|
|
|
|
|
test "skip_query_cache!" do
|
|
|
|
relation.skip_query_cache!
|
|
|
|
assert relation.skip_query_cache_value
|
|
|
|
end
|
2017-07-10 18:40:26 -04:00
|
|
|
|
2017-11-20 23:38:21 -05:00
|
|
|
test "skip_preloading!" do
|
|
|
|
relation.skip_preloading!
|
|
|
|
assert relation.skip_preloading_value
|
|
|
|
end
|
|
|
|
|
2017-07-10 18:40:26 -04:00
|
|
|
private
|
|
|
|
def relation
|
2018-01-24 14:06:30 -05:00
|
|
|
@relation ||= Relation.new(FakeKlass)
|
2017-07-10 18:40:26 -04:00
|
|
|
end
|
2013-09-04 00:03:26 -04:00
|
|
|
end
|
|
|
|
end
|