2017-07-15 22:39:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-15 09:39:17 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/developer"
|
|
|
|
require "models/comment"
|
|
|
|
require "models/post"
|
|
|
|
require "models/topic"
|
|
|
|
|
|
|
|
class NullRelationTest < ActiveRecord::TestCase
|
|
|
|
fixtures :posts, :comments
|
|
|
|
|
|
|
|
def test_none
|
2018-10-04 15:30:01 -04:00
|
|
|
assert_no_queries do
|
2017-07-15 09:39:17 -04:00
|
|
|
assert_equal [], Developer.none
|
|
|
|
assert_equal [], Developer.all.none
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_none_chainable
|
2020-05-26 19:36:48 -04:00
|
|
|
assert_queries(0) do
|
2017-07-15 09:39:17 -04:00
|
|
|
assert_equal [], Developer.none.where(name: "David")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_none_chainable_to_existing_scope_extension_method
|
2018-10-04 15:30:01 -04:00
|
|
|
assert_no_queries do
|
2017-07-15 09:39:17 -04:00
|
|
|
assert_equal 1, Topic.anonymous_extension.none.one
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-17 16:34:41 -04:00
|
|
|
def test_async_query_on_null_relation
|
|
|
|
assert_no_queries do
|
|
|
|
assert_equal [], Developer.none.load_async.load
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-15 09:39:17 -04:00
|
|
|
def test_none_chained_to_methods_firing_queries_straight_to_db
|
2018-10-04 15:30:01 -04:00
|
|
|
assert_no_queries do
|
2017-07-15 09:39:17 -04:00
|
|
|
assert_equal [], Developer.none.pluck(:id, :name)
|
|
|
|
assert_equal 0, Developer.none.delete_all
|
|
|
|
assert_equal 0, Developer.none.update_all(name: "David")
|
|
|
|
assert_equal 0, Developer.none.delete(1)
|
|
|
|
assert_equal false, Developer.none.exists?(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_null_relation_content_size_methods
|
2018-10-04 15:30:01 -04:00
|
|
|
assert_no_queries do
|
2017-07-15 09:39:17 -04:00
|
|
|
assert_equal 0, Developer.none.size
|
|
|
|
assert_equal 0, Developer.none.count
|
|
|
|
assert_equal true, Developer.none.empty?
|
|
|
|
assert_equal true, Developer.none.none?
|
|
|
|
assert_equal false, Developer.none.any?
|
|
|
|
assert_equal false, Developer.none.one?
|
|
|
|
assert_equal false, Developer.none.many?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_null_relation_metadata_methods
|
2021-05-15 05:52:04 -04:00
|
|
|
assert_includes Developer.none.to_sql, " WHERE (1=0)"
|
2017-07-15 09:39:17 -04:00
|
|
|
assert_equal({}, Developer.none.where_values_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_null_relation_where_values_hash
|
|
|
|
assert_equal({ "salary" => 100_000 }, Developer.none.where(salary: 100_000).where_values_hash)
|
|
|
|
end
|
|
|
|
|
2017-07-15 10:15:49 -04:00
|
|
|
[:count, :sum].each do |method|
|
|
|
|
define_method "test_null_relation_#{method}" do
|
2018-10-04 15:30:01 -04:00
|
|
|
assert_no_queries do
|
2017-07-15 10:15:49 -04:00
|
|
|
assert_equal 0, Comment.none.public_send(method, :id)
|
|
|
|
assert_equal Hash.new, Comment.none.group(:post_id).public_send(method, :id)
|
|
|
|
end
|
|
|
|
end
|
2017-07-15 09:39:17 -04:00
|
|
|
end
|
|
|
|
|
2017-07-15 10:15:49 -04:00
|
|
|
[:average, :minimum, :maximum].each do |method|
|
|
|
|
define_method "test_null_relation_#{method}" do
|
2018-10-04 15:30:01 -04:00
|
|
|
assert_no_queries do
|
2017-07-15 10:15:49 -04:00
|
|
|
assert_nil Comment.none.public_send(method, :id)
|
|
|
|
assert_equal Hash.new, Comment.none.group(:post_id).public_send(method, :id)
|
|
|
|
end
|
|
|
|
end
|
2017-07-15 09:39:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_null_relation_in_where_condition
|
|
|
|
assert_operator Comment.count, :>, 0 # precondition, make sure there are comments.
|
|
|
|
assert_equal 0, Comment.where(post_id: Post.none).count
|
|
|
|
end
|
|
|
|
end
|