2021-02-14 03:41:49 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "cases/helper"
|
|
|
|
require "models/post"
|
|
|
|
require "models/comment"
|
|
|
|
|
|
|
|
class ExcludingTest < ActiveRecord::TestCase
|
|
|
|
fixtures :posts, :comments
|
|
|
|
|
2021-03-03 10:03:21 -05:00
|
|
|
setup { @post = posts(:welcome) }
|
|
|
|
|
2021-02-14 03:41:49 -05:00
|
|
|
def test_result_set_does_not_include_single_excluded_record
|
2021-03-03 10:05:45 -05:00
|
|
|
assert_not_includes Post.excluding(@post), @post
|
2021-03-03 10:03:21 -05:00
|
|
|
assert_not_includes Post.excluding(@post).to_a, @post
|
2021-03-03 10:05:45 -05:00
|
|
|
|
|
|
|
assert_not_includes Post.without(@post), @post
|
2021-02-14 03:41:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_result_set_does_not_include_collection_of_excluded_records
|
2021-03-03 10:03:21 -05:00
|
|
|
relation = Post.excluding(@post, posts(:thinking))
|
2021-03-03 10:05:45 -05:00
|
|
|
assert_not_includes relation, @post
|
|
|
|
assert_not_includes relation, posts(:thinking)
|
2021-02-14 03:41:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_result_set_through_association_does_not_include_single_excluded_record
|
2021-03-03 10:02:00 -05:00
|
|
|
comment_greetings, comment_more_greetings = comments(:greetings, :more_greetings)
|
2021-02-14 03:41:49 -05:00
|
|
|
|
2021-03-03 10:03:21 -05:00
|
|
|
relation = @post.comments.excluding(comment_greetings)
|
2021-03-03 10:05:45 -05:00
|
|
|
assert_not_includes relation, comment_greetings
|
|
|
|
assert_includes relation, comment_more_greetings
|
2021-02-14 03:41:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_result_set_through_association_does_not_include_collection_of_excluded_records
|
2021-03-03 10:02:00 -05:00
|
|
|
comment_greetings, comment_more_greetings = comments(:greetings, :more_greetings)
|
2021-02-14 03:41:49 -05:00
|
|
|
|
2021-03-03 10:03:21 -05:00
|
|
|
relation = @post.comments.excluding([ comment_greetings, comment_more_greetings ])
|
2021-03-03 10:05:45 -05:00
|
|
|
assert_not_includes relation, comment_greetings
|
|
|
|
assert_not_includes relation, comment_more_greetings
|
2021-02-14 03:41:49 -05:00
|
|
|
end
|
|
|
|
|
2021-03-03 07:48:57 -05:00
|
|
|
def test_does_not_exclude_records_when_no_arguments
|
2021-03-03 09:58:27 -05:00
|
|
|
assert_no_excludes Post.excluding
|
|
|
|
assert_no_excludes Post.excluding(nil)
|
|
|
|
assert_no_excludes Post.excluding([])
|
|
|
|
assert_no_excludes Post.excluding([ nil ])
|
2021-02-14 03:41:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_raises_on_record_from_different_class
|
2021-03-03 10:03:21 -05:00
|
|
|
error = assert_raises(ArgumentError) { Post.excluding(@post, comments(:greetings)) }
|
2021-03-03 10:02:00 -05:00
|
|
|
assert_equal "You must only pass a single or collection of Post objects to #excluding.", error.message
|
2021-02-14 03:41:49 -05:00
|
|
|
end
|
2021-02-16 14:41:14 -05:00
|
|
|
|
2021-03-03 09:14:34 -05:00
|
|
|
private
|
2021-03-03 09:58:27 -05:00
|
|
|
def assert_no_excludes(relation)
|
2021-03-03 10:03:21 -05:00
|
|
|
assert_includes relation, @post
|
2021-03-03 09:58:27 -05:00
|
|
|
assert_equal Post.count, relation.count
|
2021-03-03 09:14:34 -05:00
|
|
|
end
|
2021-02-14 03:41:49 -05:00
|
|
|
end
|