class ReadOnlyTest

Public Instance Methods

test_association_collection_method_missing_scoping_not_readonly() click to toggle source
# File activerecord/test/cases/readonly_test.rb, line 110
def test_association_collection_method_missing_scoping_not_readonly
  developer = Developer.find(1)
  project   = Post.find(1)

  assert !developer.projects.all_as_method.first.readonly?
  assert !developer.projects.all_as_scope.first.readonly?

  assert !project.comments.all_as_method.first.readonly?
  assert !project.comments.all_as_scope.first.readonly?
end
test_cant_save_readonly_record() click to toggle source
# File activerecord/test/cases/readonly_test.rb, line 17
def test_cant_save_readonly_record
  dev = Developer.find(1)
  assert !dev.readonly?

  dev.readonly!
  assert dev.readonly?

  assert_nothing_raised do
    dev.name = "Luscious forbidden fruit."
    assert !dev.save
    dev.name = "Forbidden."
  end

  e = assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save  }
  assert_equal "Developer is marked as readonly", e.message

  e = assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save! }
  assert_equal "Developer is marked as readonly", e.message

  e = assert_raise(ActiveRecord::ReadOnlyRecord) { dev.destroy }
  assert_equal "Developer is marked as readonly", e.message
end
test_find_with_joins_option_does_not_imply_readonly() click to toggle source
# File activerecord/test/cases/readonly_test.rb, line 47
def test_find_with_joins_option_does_not_imply_readonly
  Developer.joins("  ").each { |d| assert_not d.readonly? }
  Developer.joins("  ").readonly(true).each { |d| assert d.readonly? }

  Developer.joins(", projects").each { |d| assert_not d.readonly? }
  Developer.joins(", projects").readonly(true).each { |d| assert d.readonly? }
end
test_find_with_readonly_option() click to toggle source
# File activerecord/test/cases/readonly_test.rb, line 40
def test_find_with_readonly_option
  Developer.all.each { |d| assert !d.readonly? }
  Developer.readonly(false).each { |d| assert !d.readonly? }
  Developer.readonly(true).each { |d| assert d.readonly? }
  Developer.readonly.each { |d| assert d.readonly? }
end
test_has_many_find_readonly() click to toggle source
# File activerecord/test/cases/readonly_test.rb, line 55
def test_has_many_find_readonly
  post = Post.find(1)
  assert !post.comments.empty?
  assert !post.comments.any?(&:readonly?)
  assert !post.comments.to_a.any?(&:readonly?)
  assert post.comments.readonly(true).all?(&:readonly?)
end
test_has_many_with_through_is_not_implicitly_marked_readonly() click to toggle source
# File activerecord/test/cases/readonly_test.rb, line 63
def test_has_many_with_through_is_not_implicitly_marked_readonly
  assert people = Post.find(1).people
  assert !people.any?(&:readonly?)
end
test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_by_id() click to toggle source
# File activerecord/test/cases/readonly_test.rb, line 68
def test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_by_id
  assert !posts(:welcome).people.find(1).readonly?
end
test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_first() click to toggle source
# File activerecord/test/cases/readonly_test.rb, line 72
def test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_first
  assert !posts(:welcome).people.first.readonly?
end
test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_last() click to toggle source
# File activerecord/test/cases/readonly_test.rb, line 76
def test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_last
  assert !posts(:welcome).people.last.readonly?
end
test_readonly_scoping() click to toggle source
# File activerecord/test/cases/readonly_test.rb, line 80
def test_readonly_scoping
  Post.where("1=1").scoping do
    assert !Post.find(1).readonly?
    assert Post.readonly(true).find(1).readonly?
    assert !Post.readonly(false).find(1).readonly?
  end

  Post.joins("   ").scoping do
    assert !Post.find(1).readonly?
    assert Post.readonly.find(1).readonly?
    assert !Post.readonly(false).find(1).readonly?
  end

  # Oracle barfs on this because the join includes unqualified and
  # conflicting column names
  unless current_adapter?(:OracleAdapter)
    Post.joins(", developers").scoping do
      assert_not Post.find(1).readonly?
      assert Post.readonly.find(1).readonly?
      assert !Post.readonly(false).find(1).readonly?
    end
  end

  Post.readonly(true).scoping do
    assert Post.find(1).readonly?
    assert Post.readonly.find(1).readonly?
    assert !Post.readonly(false).find(1).readonly?
  end
end