Permalink
Browse files

fleshing out assignment tests for all association types

  • Loading branch information...
1 parent 69908c4 commit 0e161b087f6f58b9e2b0389f95d683d523e647e0 @cainlevy committed Dec 12, 2008
Showing with 183 additions and 4 deletions.
  1. +6 −1 test/db/models.rb
  2. +177 −3 test/unit/nested_assignment_test.rb
View
7 test/db/models.rb
@@ -22,8 +22,9 @@ class User < ActiveRecord::Base
has_and_belongs_to_many :roles
has_one :subscription
has_one :address, :as => :addressable
+ has_many :services, :through => :subscription
- accessible_associations :subscription
+ accessible_associations :subscription, :roles, :services
validates_presence_of :name
end
@@ -32,13 +33,17 @@ class Service < ActiveRecord::Base
has_many :subscriptions
has_many :users, :through => :subscriptions
+ accessible_associations :subscriptions
+
validates_presence_of :name
end
class Subscription < ActiveRecord::Base
belongs_to :service
belongs_to :user
+ accessible_associations :user
+
validates_presence_of :name
end
View
180 test/unit/nested_assignment_test.rb
@@ -28,7 +28,7 @@ def test_assigning_a_replacement_subscription
}
assert @user.subscription.new_record?, "the association is a new object"
assert_equal "Bobtastic", @user.subscription.name, "the new record has the specified name"
- assert !@subscription.reload.user_id.nil?, "the previously associated object has not been disassociated yet"
+ assert !@subscription.reload.user.nil?, "the previously associated object has not been disassociated yet"
end
def test_assigning_a_removed_subscription
@@ -43,24 +43,198 @@ def test_assigning_a_removed_subscription
assert_nothing_raised("the associated object has not been deleted yet") do @subscription.reload end
assert_equal "Bob/Free", @user.subscription.name, "the association attribute did not update"
end
-
end
class NestedAssignmentBelongsToTest < ActiveSupport::TestCase
+ def setup
+ @user = users(:bob)
+ @subscription = subscriptions(:bob_is_free)
+ end
+
+ def test_updating_the_subscription_user
+ @subscription.user_params = {
+ "1" => {
+ :id => @user.id,
+ :name => "William"
+ }
+ }
+ assert !@subscription.user.new_record?, "the association was not rebuilt"
+ assert_equal "William", @subscription.user.name, "the existing subscription's name has changed"
+ assert_equal "Bob", @user.reload.name, "the name change has not been saved"
+ end
+
+ def test_assigning_a_replacement_user
+ @subscription.user_params = {
+ "1" => {
+ :name => "William"
+ }
+ }
+ assert @subscription.user.new_record?, "the association is a new object"
+ assert_equal "William", @subscription.user.name, "the new record has the specified name"
+ assert !@subscription.reload.user.nil?, "the previously associated object has not been disassociated yet"
+ end
+
+ def test_assigning_a_removed_user
+ @subscription.user_params = {
+ "1" => {
+ :id => @user.id,
+ :name => "William",
+ :_delete => "1"
+ }
+ }
+ assert @subscription.user._delete, "the association is marked for deletion"
+ assert_nothing_raised("the associated object has not been deleted yet") do @user.reload end
+ assert_equal "Bob", @subscription.user.name, "the association attribute did not update"
+ end
end
class NestedAssignmentHasManyTest < ActiveSupport::TestCase
+ def setup
+ @service = services(:free)
+ @subscription = subscriptions(:bob_is_free)
+ end
+
+ def test_adding_a_subscription
+ @service.subscriptions_params = {
+ "1" => {
+ :name => "Foo"
+ }
+ }
+ assert @service.subscriptions.any?{|s| s.new_record?}, "a new record is added"
+ assert_equal "Foo", @service.subscriptions.detect{|s| s.new_record?}.name, "the new record has the specified attribute value"
+ end
+
+ def test_missing_subscriptions_do_not_delete
+ @service.subscriptions_params = {
+ "1" => {:name => "Foo"}
+ }
+ assert @service.subscriptions.any?{|s| s == @subscription}, "existing records remain in the collection"
+ end
+
+ def test_updating_a_subscription
+ @service.subscriptions_params = {
+ "1" => {
+ :id => @subscription.id,
+ :name => "Foo"
+ }
+ }
+ assert !@service.subscriptions.any?{|s| s.new_record?}, "no new record is created"
+ assert_equal "Foo", @service.subscriptions.detect{|s| s == @subscription}.name, "the name is updated"
+ assert_equal "Bob/Free", @subscription.reload.name, "the name is not saved"
+ end
+
+ def test_removing_a_subscription
+ @service.subscriptions_params = {
+ "1" => {
+ :id => @subscription.id,
+ :name => "Foo",
+ :_delete => "1"
+ }
+ }
+ assert @service.subscriptions.detect{|s| s == @subscription}._delete, "the associated record is marked for deletion"
+ assert_equal "Bob/Free", @service.subscriptions.detect{|s| s == @subscription}.name, "the association attribute did not update"
+ end
end
class NestedAssignmentHasAndBelongsToManyTest < ActiveSupport::TestCase
+ def setup
+ @user = users(:bob)
+ @role = roles(:cook)
+ end
+
+ def test_adding_a_role
+ @user.roles_params = {
+ "1" => {
+ :name => "Foo"
+ }
+ }
+ assert @user.roles.any?{|r| r.new_record?}, "a new record is added"
+ assert_equal "Foo", @user.roles.detect{|r| r.new_record?}.name, "the new record has the specified attribute value"
+ end
+
+ def test_missing_roles_do_not_delete
+ @user.roles_params = {
+ "1" => {:name => "Foo"}
+ }
+ assert @user.roles.any?{|r| r == @role}, "existing records remain in the collection"
+ end
+
+ def test_updating_a_role
+ @user.roles_params = {
+ "1" => {
+ :id => @role.id,
+ :name => "Foo"
+ }
+ }
+ assert !@user.roles.any?{|s| s.new_record?}, "no new record is created"
+ assert_equal "Foo", @user.roles.detect{|s| s == @role}.name, "the name is updated"
+ assert_equal "Cook", @role.reload.name, "the name is not saved"
+ end
+
+ def test_removing_a_role
+ @user.roles_params = {
+ "1" => {
+ :id => @role.id,
+ :name => "Foo",
+ :_delete => "1"
+ }
+ }
+ assert @user.roles.detect{|s| s == @role}._delete, "the associated record is marked for deletion"
+ assert_equal "Cook", @user.roles.detect{|s| s == @role}.name, "the association attribute did not update"
+ end
end
class NestedAssignmentHasManyThroughTest < ActiveSupport::TestCase
+ def setup
+ @user = users(:bob)
+ @service = services(:free)
+ end
+
+ def test_adding_a_service
+ @user.services_params = {
+ "1" => {
+ :name => "Foo"
+ }
+ }
+ assert @user.services.any?{|r| r.new_record?}, "a new record is added"
+ assert_equal "Foo", @user.services.detect{|r| r.new_record?}.name, "the new record has the specified attribute value"
+ end
+
+ def test_missing_services_do_not_delete
+ @user.services_params = {
+ "1" => {:name => "Foo"}
+ }
+ assert @user.services.any?{|r| r == @service}, "existing records remain in the collection"
+ end
+
+ def test_updating_a_service
+ @user.services_params = {
+ "1" => {
+ :id => @service.id,
+ :name => "Foo"
+ }
+ }
+ assert !@user.services.any?{|s| s.new_record?}, "no new record is created"
+ assert_equal "Foo", @user.services.detect{|s| s == @service}.name, "the name is updated"
+ assert_equal "Free", @service.reload.name, "the name is not saved"
+ end
+
+ def test_removing_a_service
+ @user.services_params = {
+ "1" => {
+ :id => @service.id,
+ :name => "Foo",
+ :_delete => "1"
+ }
+ }
+ assert @user.services.detect{|s| s == @service}._delete, "the associated record is marked for deletion"
+ assert_equal "Free", @user.services.detect{|s| s == @service}.name, "the association attribute did not update"
+ end
end
class NestedAssignmentHelperTest < ActiveSupport::TestCase
def test_association_names
- assert_equal [:address, :roles, :subscription], User.association_names.sort_by(&:to_s)
+ assert_equal [:address, :roles, :services, :subscription], User.association_names.sort_by(&:to_s)
end
def test_instantiated_associated_does_not_load_associations

0 comments on commit 0e161b0

Please sign in to comment.