aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/readonly_test.rb
blob: 458cd6886f72aba7d43f61b43b63951fc5bb3c77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require 'abstract_unit'
require 'fixtures/developer'
require 'fixtures/project'

class ReadOnlyTest < Test::Unit::TestCase
  fixtures :developers, :projects, :developers_projects

  def test_cant_save_readonly_record
    dev = Developer.find(:first)
    assert !dev.readonly?

    dev.readonly!
    assert dev.readonly?

    assert_nothing_raised do
      dev.name = 'Luscious forbidden fruit.'
      assert !dev.save
      dev.name = 'Forbidden.'
    end
    assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save  }
    assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save! }
  end

  def test_find_with_readonly_option
    Developer.find(:all).each { |d| assert !d.readonly? }
    Developer.find(:all, :readonly => false).each { |d| assert !d.readonly? }
    Developer.find(:all, :readonly => true).each { |d| assert d.readonly? }
  end

  def test_find_with_joins_option_implies_readonly
    Developer.find(:all, :joins => '').each { |d| assert d.readonly? }
    Developer.find(:all, :joins => '', :readonly => false).each { |d| assert !d.readonly? }
  end

  def test_habtm_find_readonly
    dev = Developer.find(:first)
    dev.projects.each { |p| assert !p.readonly? }
    dev.projects.find(:all) { |p| assert !p.readonly? }
    dev.projects.find(:all, :readonly => true) { |p| assert p.readonly? }
  end
end