class RelationScopingTest

Public Instance Methods

test_circular_joins_with_scoping_does_not_crash() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 243
def test_circular_joins_with_scoping_does_not_crash
  posts = Post.joins(comments: :post).scoping do
    Post.first(10)
  end
  assert_equal posts, Post.joins(comments: :post).first(10)
end
test_circular_left_joins_with_scoping_does_not_crash() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 250
def test_circular_left_joins_with_scoping_does_not_crash
  posts = Post.left_joins(comments: :post).scoping do
    Post.first(10)
  end
  assert_equal posts, Post.left_joins(comments: :post).first(10)
end
test_current_scope_does_not_pollute_sibling_subclasses() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 214
def test_current_scope_does_not_pollute_sibling_subclasses
  Comment.none.scoping do
    assert_not SpecialComment.all.any?
    assert_not VerySpecialComment.all.any?
    assert_not SubSpecialComment.all.any?
  end

  SpecialComment.none.scoping do
    assert Comment.all.any?
    assert VerySpecialComment.all.any?
    assert_not SubSpecialComment.all.any?
  end

  SubSpecialComment.none.scoping do
    assert Comment.all.any?
    assert VerySpecialComment.all.any?
    assert SpecialComment.all.any?
  end
end
test_default_scope_filters_on_joins() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 192
def test_default_scope_filters_on_joins
  assert_equal 1, DeveloperFilteredOnJoins.all.count
  assert_equal DeveloperFilteredOnJoins.all.first, developers(:david).becomes(DeveloperFilteredOnJoins)
end
test_delete_all_default_scope_filters_on_joins() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 205
def test_delete_all_default_scope_filters_on_joins
  assert_not_equal [], DeveloperFilteredOnJoins.all

  DeveloperFilteredOnJoins.delete_all()

  assert_equal [], DeveloperFilteredOnJoins.all
  assert_not_equal [], Developer.all
end
test_double_reverse_order_produces_original_order() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 57
def test_double_reverse_order_produces_original_order
  assert_equal Developer.order("name DESC"), Developer.order("name DESC").reverse_order.reverse_order
end
test_ensure_that_method_scoping_is_correctly_restored() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 181
def test_ensure_that_method_scoping_is_correctly_restored
  begin
    Developer.where("name = 'Jamis'").scoping do
      raise "an exception"
    end
  rescue
  end

  assert_not Developer.all.to_sql.include?("name = 'Jamis'"), "scope was not restored"
end
test_reverse_order() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 41
def test_reverse_order
  assert_equal Developer.order("id DESC").to_a.reverse, Developer.order("id DESC").reverse_order
end
test_reverse_order_with_arel_node() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 45
def test_reverse_order_with_arel_node
  assert_equal Developer.order("id DESC").to_a.reverse, Developer.order(Developer.arel_table[:id].desc).reverse_order
end
test_reverse_order_with_arel_nodes_and_strings() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 53
def test_reverse_order_with_arel_nodes_and_strings
  assert_equal Developer.order("id DESC").order("name DESC").to_a.reverse, Developer.order("id DESC").order(Developer.arel_table[:name].desc).reverse_order
end
test_reverse_order_with_multiple_arel_nodes() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 49
def test_reverse_order_with_multiple_arel_nodes
  assert_equal Developer.order("id DESC").order("name DESC").to_a.reverse, Developer.order(Developer.arel_table[:id].desc).order(Developer.arel_table[:name].desc).reverse_order
end
test_scope_breaks_caching_on_collections() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 30
def test_scope_breaks_caching_on_collections
  author = authors :david
  ids = author.reload.special_posts_with_default_scope.map(&:id)
  assert_equal [1, 5, 6], ids.sort
  scoped_posts = SpecialPostWithDefaultScope.unscoped do
    author = authors :david
    author.reload.special_posts_with_default_scope.to_a
  end
  assert_equal author.posts.map(&:id).sort, scoped_posts.map(&:id).sort
end
test_scope_select_concatenates() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 112
def test_scope_select_concatenates
  Developer.select("id, name").scoping do
    developer = Developer.select("salary").where("name = 'David'").first
    assert_equal 80000, developer.salary
    assert developer.has_attribute?(:id)
    assert developer.has_attribute?(:name)
    assert developer.has_attribute?(:salary)
  end
end
test_scoped_count() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 122
def test_scoped_count
  Developer.where("name = 'David'").scoping do
    assert_equal 1, Developer.count
  end

  Developer.where("salary = 100000").scoping do
    assert_equal 8, Developer.count
    assert_equal 1, Developer.where("name LIKE 'fixture_1%'").count
  end
end
test_scoped_create_with_create_with() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 163
def test_scoped_create_with_create_with
  new_comment = VerySpecialComment.create_with(post_id: 1).scoping do
    VerySpecialComment.create body: "Wonderful world"
  end

  assert_equal 1, new_comment.post_id
  assert_includes Post.find(1).comments, new_comment
end
test_scoped_create_with_create_with_has_higher_priority() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 172
def test_scoped_create_with_create_with_has_higher_priority
  new_comment = VerySpecialComment.where(post_id: 2).create_with(post_id: 1).scoping do
    VerySpecialComment.create body: "Wonderful world"
  end

  assert_equal 1, new_comment.post_id
  assert_includes Post.find(1).comments, new_comment
end
test_scoped_create_with_where() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 154
def test_scoped_create_with_where
  new_comment = VerySpecialComment.where(post_id: 1).scoping do
    VerySpecialComment.create body: "Wonderful world"
  end

  assert_equal 1, new_comment.post_id
  assert_includes Post.find(1).comments, new_comment
end
test_scoped_find() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 61
def test_scoped_find
  Developer.where("name = 'David'").scoping do
    assert_nothing_raised { Developer.find(1) }
  end
end
test_scoped_find_all() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 98
def test_scoped_find_all
  Developer.where("name = 'David'").scoping do
    assert_equal [developers(:david)], Developer.all
  end
end
test_scoped_find_combines_and_sanitizes_conditions() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 92
def test_scoped_find_combines_and_sanitizes_conditions
  Developer.where("salary = 9000").scoping do
    assert_equal developers(:poor_jamis), Developer.where("name = 'Jamis'").first
  end
end
test_scoped_find_first() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 67
def test_scoped_find_first
  developer = Developer.find(10)
  Developer.where("salary = 100000").scoping do
    assert_equal developer, Developer.order("name").first
  end
end
test_scoped_find_include() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 133
def test_scoped_find_include
  # with the include, will retrieve only developers for the given project
  scoped_developers = Developer.includes(:projects).scoping do
    Developer.where("projects.id" => 2).to_a
  end
  assert_includes scoped_developers, developers(:david)
  assert_not_includes scoped_developers, developers(:jamis)
  assert_equal 1, scoped_developers.size
end
test_scoped_find_joins() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 143
def test_scoped_find_joins
  scoped_developers = Developer.joins("JOIN developers_projects ON id = developer_id").scoping do
    Developer.where("developers_projects.project_id = 2").to_a
  end

  assert_includes scoped_developers, developers(:david)
  assert_not_includes scoped_developers, developers(:jamis)
  assert_equal 1, scoped_developers.size
  assert_equal developers(:david).attributes, scoped_developers.first.attributes
end
test_scoped_find_last() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 74
def test_scoped_find_last
  highest_salary = Developer.order("salary DESC").first

  Developer.order("salary").scoping do
    assert_equal highest_salary, Developer.last
  end
end
test_scoped_find_last_preserves_scope() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 82
def test_scoped_find_last_preserves_scope
  lowest_salary  = Developer.order("salary ASC").first
  highest_salary = Developer.order("salary DESC").first

  Developer.order("salary").scoping do
    assert_equal highest_salary, Developer.last
    assert_equal lowest_salary, Developer.first
  end
end
test_scoped_find_select() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 104
def test_scoped_find_select
  Developer.select("id, name").scoping do
    developer = Developer.where("name = 'David'").first
    assert_equal "David", developer.name
    assert !developer.has_attribute?(:salary)
  end
end
test_scoping_is_correctly_restored() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 234
def test_scoping_is_correctly_restored
  Comment.unscoped do
    SpecialComment.unscoped.created
  end

  assert_nil Comment.current_scope
  assert_nil SpecialComment.current_scope
end
test_unscoped_breaks_caching() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 21
def test_unscoped_breaks_caching
  author = authors :mary
  assert_nil author.first_post
  post = FirstPost.unscoped do
    author.reload.first_post
  end
  assert post
end
test_update_all_default_scope_filters_on_joins() click to toggle source
# File activerecord/test/cases/scoping/relation_scoping_test.rb, line 197
def test_update_all_default_scope_filters_on_joins
  DeveloperFilteredOnJoins.update_all(salary: 65000)
  assert_equal 65000, Developer.find(developers(:david).id).salary

  # has not changed jamis
  assert_not_equal 65000, Developer.find(developers(:jamis).id).salary
end