Class: EnsuresImmutabilityOfTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/immutable-attribute-plugin/test/unit/plugin_test.rb

Instance Method Summary collapse

Instance Method Details

#test_assignment_if_nilObject



95
96
97
98
99
100
101
# File 'lib/immutable-attribute-plugin/test/unit/plugin_test.rb', line 95

def test_assignment_if_nil
   = accounts(:nil_account)
  assert_nothing_thrown do
    .username = 'jgreen'
  end
  assert .save
end

#test_association_assignment_if_nilObject



16
17
18
19
20
21
# File 'lib/immutable-attribute-plugin/test/unit/plugin_test.rb', line 16

def test_association_assignment_if_nil
   = accounts(:nil_account)
  assert_nothing_thrown do
    .info = infos(:foo)
  end
end

#test_belong_id_readonlyObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/immutable-attribute-plugin/test/unit/plugin_test.rb', line 54

def test_belong_id_readonly
  zee_info = ProtectedInfo.create :text => "zee"

   = accounts(:wmoxam)
  assert_nothing_raised do
    zee_info. = 
  end

   = accounts(:nil_account)

  zee_info. = .id
  assert_not_equal zee_info., .id, "Failed to forcefully set the id."
  zee_info.save!
  assert_equal zee_info., .id, "The reload did not reset the id to correct id."
end

#test_error_on_assignmentObject



124
125
126
127
128
129
# File 'lib/immutable-attribute-plugin/test/unit/plugin_test.rb', line 124

def test_error_on_assignment
   = accounts(:wmoxam)
  assert_raise ActiveRecord::ImmutableAttributeError do
    .username = 'jgreen'
  end
end

#test_error_on_association_assignmentObject



23
24
25
26
27
28
29
30
# File 'lib/immutable-attribute-plugin/test/unit/plugin_test.rb', line 23

def test_error_on_association_assignment
   = accounts(:wmoxam)
  .info = infos(:foo)

  assert_raise ActiveRecord::ImmutableAttributeError do
    .info = infos(:bar)
  end
end

#test_error_on_association_updateObject



32
33
34
35
36
37
38
39
40
# File 'lib/immutable-attribute-plugin/test/unit/plugin_test.rb', line 32

def test_error_on_association_update
   = accounts(:wmoxam)
	assert .info.nil?
  .info = infos(:foo)

  assert_raise ActiveRecord::ImmutableAttributeError do
    .update_attributes(:info => infos(:bar))
  end
end

#test_error_on_belong_assignmentObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/immutable-attribute-plugin/test/unit/plugin_test.rb', line 42

def test_error_on_belong_assignment
  zee_info = ProtectedInfo.create :text => "zee"

  assert_nothing_raised do
    zee_info. = accounts(:wmoxam)
  end

  assert_raise ActiveRecord::ImmutableAttributeError do
    zee_info. = accounts(:nil_account)
  end
end

#test_error_on_updateObject



131
132
133
134
135
136
# File 'lib/immutable-attribute-plugin/test/unit/plugin_test.rb', line 131

def test_error_on_update
   = accounts(:wmoxam)
  assert_raise ActiveRecord::ImmutableAttributeError do
    .update_attributes(:username => 'jgreen')
  end
end

#test_overrides_all_setters_when_configured_with_all_symbolObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/immutable-attribute-plugin/test/unit/plugin_test.rb', line 103

def test_overrides_all_setters_when_configured_with_all_symbol
   = ImmutableAccount.create({
    :username => 'johndoe',
    :email => '[email protected]',
    :info => infos(:foo),
    :infos => [infos(:bar)]
  })
  assert_raise ActiveRecord::ImmutableAttributeError do
    .username = 'janedoe'
  end
  assert_raise ActiveRecord::ImmutableAttributeError do
    .email = '[email protected]'
  end
  assert_raise ActiveRecord::ImmutableAttributeError do
    .info = infos(:bar)
  end
  assert_raise ActiveRecord::ImmutableAttributeError do
    .infos = [infos(:foo)]
  end
end

#test_protected_has_many_assignmentObject



70
71
72
73
74
75
76
77
# File 'lib/immutable-attribute-plugin/test/unit/plugin_test.rb', line 70

def test_protected_has_many_assignment
   = accounts(:nil_account)
	assert .infos.empty?, "Account infos should be empty"
	.infos = [infos(:bar)]
  assert_raise ActiveRecord::ImmutableAttributeError do
    .infos = [infos(:foo)]
  end
end

#test_set_on_creationObject



79
80
81
82
83
84
85
86
# File 'lib/immutable-attribute-plugin/test/unit/plugin_test.rb', line 79

def test_set_on_creation
   = nil
  assert_nothing_thrown do
     = Account.create(:username => 'jgreen')
  end
  assert .valid?
  assert .username == 'jgreen'
end

#test_set_sameObject



88
89
90
91
92
93
# File 'lib/immutable-attribute-plugin/test/unit/plugin_test.rb', line 88

def test_set_same
   = accounts(:wmoxam)
  assert_nothing_thrown do
    .username = 'wmoxam'
  end
end