Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moving to a more plausible (?) model/fixture setup

  • Loading branch information...
commit 92b948318da59fe140bf0afc8031546baf4e0484 1 parent 0e161b0
@cainlevy authored
View
66 test/db/models.rb
@@ -1,53 +1,53 @@
-# A set of classes design to offer not only every association, but every pair of associations.
-# For example, there's a has_one paired with a normal belongs_to, and another has_one paired with a polymorphic belongs_to.
-# To use, mix the PluginTestModels module into the TestCase
module PluginTestModels
def self.included(base)
base.set_fixture_class({
- :addresses => PluginTestModels::Address,
- :users => PluginTestModels::User,
- :services => PluginTestModels::Service,
- :subscriptions => PluginTestModels::Subscription,
- :roles => PluginTestModels::Role
+ :avatars => PluginTestModels::Avatar,
+ :groups => PluginTestModels::Group,
+ :managers => PluginTestModels::Manager,
+ :tags => PluginTestModels::Tag,
+ :tasks => PluginTestModels::Task,
+ :users => PluginTestModels::User
})
end
-
- class Address < ActiveRecord::Base
- belongs_to :addressable, :polymorphic => true
-
- validates_presence_of :name
- end
-
+
class User < ActiveRecord::Base
- has_and_belongs_to_many :roles
- has_one :subscription
- has_one :address, :as => :addressable
- has_many :services, :through => :subscription
+ has_one :avatar
+ belongs_to :manager
+ has_many :tasks
+ has_many :tags, :through => :tasks
+ has_and_belongs_to_many :groups
- accessible_associations :subscription, :roles, :services
+ accessible_associations :avatar, :manager, :tasks, :tags, :groups
validates_presence_of :name
end
-
- class Service < ActiveRecord::Base
- has_many :subscriptions
- has_many :users, :through => :subscriptions
-
- accessible_associations :subscriptions
+
+ class Avatar < ActiveRecord::Base
+ belongs_to :user
validates_presence_of :name
end
-
- class Subscription < ActiveRecord::Base
- belongs_to :service
+
+ class Manager < ActiveRecord::Base
+ has_many :users
+
+ validates_presence_of :name
+ end
+
+ class Task < ActiveRecord::Base
belongs_to :user
-
- accessible_associations :user
+ has_many :tags
+
+ validates_presence_of :name
+ end
+
+ class Tag < ActiveRecord::Base
+ belongs_to :task
validates_presence_of :name
end
-
- class Role < ActiveRecord::Base
+
+ class Group < ActiveRecord::Base
has_and_belongs_to_many :users
validates_presence_of :name
View
32 test/db/schema.rb
@@ -1,31 +1,37 @@
ActiveRecord::Schema.define(:version => 1) do
- create_table :addresses, :force => true do |t|
+ create_table :users, :force => true do |t|
+ t.column :manager_id, :integer
t.column :name, :string
- t.column :addressable_id, :integer
- t.column :addressable_type, :string
end
- create_table :users, :force => true do |t|
+ create_table :avatars, :force => true do |t|
+ t.column :user_id, :integer
t.column :name, :string
end
- create_table :roles_users, :force => true, :id => false do |t|
- t.column :user_id, :integer
- t.column :role_id, :integer
+ create_table :managers, :force => true do |t|
+ t.column :name, :string
end
- create_table :services, :force => true do |t|
+ create_table :tasks, :force => true do |t|
+ t.column :user_id, :integer
t.column :name, :string
end
- create_table :subscriptions, :force => true do |t|
+ create_table :tags, :force => true do |t|
+ t.column :task_id, :integer
t.column :name, :string
- t.column :service_id, :integer
- t.column :user_id, :integer
end
- create_table :roles, :force => true do |t|
+ create_table :groups, :force => true do |t|
+ t.column :user_id, :integer
t.column :name, :string
end
-end
+
+ create_table :groups_users, :force => true, :id => false do |t|
+ t.column :group_id, :integer
+ t.column :user_id, :integer
+ end
+
+end
View
15 test/fixtures/addresses.yml
@@ -1,15 +0,0 @@
-bobs_home:
- id: 1
- name: Home
- addressable_id: 1
- addressable_type: 'PluginTestModels::User'
-bobs_work:
- id: 2
- name: Work
- addressable_id: 1
- addressable_type: 'PluginTestModels::User'
-sues_home:
- id: 3
- name: Home
- addressable_id: 2
- addressable_type: 'PluginTestModels::User'
View
4 test/fixtures/avatars.yml
@@ -0,0 +1,4 @@
+bobs_avatar:
+ id: 1
+ user_id: 1
+ name: "mugshot"
View
3  test/fixtures/groups.yml
@@ -0,0 +1,3 @@
+developers:
+ id: 1
+ name: "developers"
View
4 test/fixtures/groups_users.yml
@@ -0,0 +1,4 @@
+bob_is_a_developer:
+ user_id: 1
+ group_id: 1
+
View
3  test/fixtures/managers.yml
@@ -0,0 +1,3 @@
+sue:
+ id: 1
+ name: Sue
View
6 test/fixtures/roles.yml
@@ -1,6 +0,0 @@
-cook:
- id: 1
- name: Cook
-gardener:
- id: 2
- name: Gardener
View
6 test/fixtures/roles_users.yml
@@ -1,6 +0,0 @@
-bob_is_a_cook:
- role_id: 1
- user_id: 1
-sue_is_a_gardener:
- role_id: 2
- user_id: 2
View
6 test/fixtures/services.yml
@@ -1,6 +0,0 @@
-free:
- id: 1
- name: Free
-premium:
- id: 2
- name: Premium
View
10 test/fixtures/subscriptions.yml
@@ -1,10 +0,0 @@
-bob_is_free:
- id: 1
- name: Bob/Free
- service_id: 1
- user_id: 1
-sue_is_premium:
- id: 2
- name: Sue/Premium
- service_id: 2
- user_id: 2
View
4 test/fixtures/tags.yml
@@ -0,0 +1,4 @@
+challenging:
+ id: 1
+ task_id: 1
+ name: challenging
View
4 test/fixtures/tasks.yml
@@ -0,0 +1,4 @@
+review:
+ id: 1
+ name: review
+ user_id: 1
View
4 test/fixtures/users.yml
@@ -1,6 +1,4 @@
bob:
id: 1
name: Bob
-sue:
- id: 2
- name: Sue
+ manager_id: 1
View
23,064 test/test.log
23,064 additions, 0 deletions not shown
View
32 test/unit/nested_assignment_helper_test.rb
@@ -0,0 +1,32 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class NestedAssignmentHelperTest < ActiveSupport::TestCase
+ def test_association_names
+ assert_equal [:avatar, :groups, :manager, :tags, :tasks], User.association_names.sort_by(&:to_s)
+ end
+
+ def test_instantiated_associated_does_not_load_associations
+ ActiveRecord::Associations::AssociationProxy.any_instance.expects(:loaded).never
+ user = User.find(:first)
+ assert_equal [], user.send(:instantiated_associated)
+ end
+
+ def test_instantiated_associated_retrieves_loaded_associations
+ user = User.find(:first)
+ group = user.groups.build(:name => "designers")
+ assert_equal [group], user.send(:instantiated_associated), "only one group is instantiated"
+ assert_equal 2, user.groups.length, "though there were more associated groups"
+ end
+
+ def test_instantiated_associated_returns_arrays_for_singular_association
+ user = User.find(:first)
+ assert !user.avatar.nil?
+ assert_equal [user.avatar], user.send(:instantiated_associated)
+ end
+
+ def test_instantiated_associated_returns_arrays_for_plural_association
+ user = User.find(:first)
+ assert !user.groups.empty?
+ assert_equal user.groups, user.send(:instantiated_associated)
+ end
+end
View
5 test/unit/nested_assignment_saving_test.rb
@@ -0,0 +1,5 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class NestedAssignmentSavingTest < ActiveSupport::TestCase
+
+end
View
252 test/unit/nested_assignment_test.rb
@@ -1,265 +1,231 @@
require File.dirname(__FILE__) + '/../test_helper'
-require 'ruby-debug'
-
class NestedAssignmentHasOneTest < ActiveSupport::TestCase
def setup
@user = users(:bob)
- @subscription = subscriptions(:bob_is_free)
+ @avatar = avatars(:bobs_avatar)
end
- def test_updating_a_subscription
- @user.subscription_params = {
+ def test_updating_a_avatar
+ @user.avatar_params = {
"1" => {
- :id => @subscription.id,
+ :id => @avatar.id,
:name => "Bobtastic"
}
}
- assert !@user.subscription.new_record?, "the association was not rebuilt"
- assert_equal "Bobtastic", @user.subscription.name, "the existing subscription's name has changed"
- assert_equal "Bob/Free", @subscription.reload.name, "the name change has not been saved"
+ assert !@user.avatar.new_record?, "the association was not rebuilt"
+ assert_equal "Bobtastic", @user.avatar.name, "the existing associated record's name has changed"
+ assert_equal "mugshot", @avatar.reload.name, "the name change has not been saved"
end
- def test_assigning_a_replacement_subscription
- @user.subscription_params = {
+ def test_assigning_a_replacement_avatar
+ @user.avatar_params = {
"1" => {
:name => "Bobtastic"
}
}
- 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.nil?, "the previously associated object has not been disassociated yet"
+ assert @user.avatar.new_record?, "the association is a new object"
+ assert_equal "Bobtastic", @user.avatar.name, "the new record has the specified name"
+ assert !@avatar.reload.user.nil?, "the previously associated object has not been disassociated yet"
end
- def test_assigning_a_removed_subscription
- @user.subscription_params = {
+ def test_assigning_a_removed_avatar
+ @user.avatar_params = {
"1" => {
- :id => @subscription.id,
+ :id => @avatar.id,
:name => "Bobtastic",
:_delete => "1"
}
}
- assert @user.subscription._delete, "the association is marked for deletion"
- 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"
+ assert @user.avatar._delete, "the association is marked for deletion"
+ assert_nothing_raised("the associated object has not been deleted yet") do @avatar.reload end
+ assert_equal "mugshot", @user.avatar.name, "the association attribute did not update"
end
end
class NestedAssignmentBelongsToTest < ActiveSupport::TestCase
def setup
@user = users(:bob)
- @subscription = subscriptions(:bob_is_free)
+ @manager = managers(:sue)
end
- def test_updating_the_subscription_user
- @subscription.user_params = {
+ def test_updating_the_manager
+ @user.manager_params = {
"1" => {
- :id => @user.id,
- :name => "William"
+ :id => @manager.id,
+ :name => "Susan"
}
}
- 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"
+ assert !@user.manager.new_record?, "the association was not rebuilt"
+ assert_equal "Susan", @user.manager.name, "the existing associated record's name has changed"
+ assert_equal "Sue", @manager.reload.name, "the name change has not been saved"
end
- def test_assigning_a_replacement_user
- @subscription.user_params = {
+ def test_assigning_a_replacement_manager
+ @user.manager_params = {
"1" => {
- :name => "William"
+ :name => "Susan"
}
}
- 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"
+ assert @user.manager.new_record?, "the association is a new object"
+ assert_equal "Susan", @user.manager.name, "the new record has the specified name"
+ assert !@user.reload.manager.nil?, "the previously associated object has not been disassociated yet"
end
- def test_assigning_a_removed_user
- @subscription.user_params = {
+ def test_assigning_a_removed_manager
+ @user.manager_params = {
"1" => {
- :id => @user.id,
- :name => "William",
+ :id => @manager.id,
+ :name => "Susan",
:_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"
+ assert @user.manager._delete, "the association is marked for deletion"
+ assert_nothing_raised("the associated object has not been deleted yet") do @manager.reload end
+ assert_equal "Sue", @user.manager.name, "the association attribute did not update"
end
end
class NestedAssignmentHasManyTest < ActiveSupport::TestCase
def setup
- @service = services(:free)
- @subscription = subscriptions(:bob_is_free)
+ @user = users(:bob)
+ @task = tasks(:review)
end
- def test_adding_a_subscription
- @service.subscriptions_params = {
+ def test_adding_a_task
+ @user.tasks_params = {
"1" => {
- :name => "Foo"
+ :name => "refactor"
}
}
- 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"
+ assert @user.tasks.any?{|s| s.new_record?}, "a new record is added"
+ assert_equal "refactor", @user.tasks.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"}
+ def test_missing_tasks_do_not_delete
+ @user.tasks_params = {
+ "1" => {:name => "refactor"}
}
- assert @service.subscriptions.any?{|s| s == @subscription}, "existing records remain in the collection"
+ assert @user.tasks.any?{|s| s == @task}, "existing records remain in the collection"
end
- def test_updating_a_subscription
- @service.subscriptions_params = {
+ def test_updating_a_task
+ @user.tasks_params = {
"1" => {
- :id => @subscription.id,
- :name => "Foo"
+ :id => @task.id,
+ :name => "refactor"
}
}
- 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"
+ assert !@user.tasks.any?{|s| s.new_record?}, "no new record is created"
+ assert_equal "refactor", @user.tasks.detect{|s| s == @task}.name, "the name is updated"
+ assert_equal "review", @task.reload.name, "the name is not saved"
end
- def test_removing_a_subscription
- @service.subscriptions_params = {
+ def test_removing_a_task
+ @user.tasks_params = {
"1" => {
- :id => @subscription.id,
- :name => "Foo",
+ :id => @task.id,
+ :name => "refactor",
:_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"
+ assert @user.tasks.detect{|s| s == @task}._delete, "the associated record is marked for deletion"
+ assert_equal "review", @user.tasks.detect{|s| s == @task}.name, "the association attribute did not update"
end
end
class NestedAssignmentHasAndBelongsToManyTest < ActiveSupport::TestCase
def setup
@user = users(:bob)
- @role = roles(:cook)
+ @group = groups(:developers)
end
- def test_adding_a_role
- @user.roles_params = {
+ def test_adding_a_group
+ @user.groups_params = {
"1" => {
- :name => "Foo"
+ :name => "designers"
}
}
- 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"
+ assert @user.groups.any?{|r| r.new_record?}, "a new record is added"
+ assert_equal "designers", @user.groups.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"}
+ def test_missing_groups_do_not_delete
+ @user.groups_params = {
+ "1" => {:name => "designers"}
}
- assert @user.roles.any?{|r| r == @role}, "existing records remain in the collection"
+ assert @user.groups.any?{|r| r == @group}, "existing records remain in the collection"
end
- def test_updating_a_role
- @user.roles_params = {
+ def test_updating_a_group
+ @user.groups_params = {
"1" => {
- :id => @role.id,
- :name => "Foo"
+ :id => @group.id,
+ :name => "engineers"
}
}
- 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"
+ assert !@user.groups.any?{|s| s.new_record?}, "no new record is created"
+ assert_equal "engineers", @user.groups.detect{|s| s == @group}.name, "the name is updated"
+ assert_equal "developers", @group.reload.name, "the name is not saved"
end
- def test_removing_a_role
- @user.roles_params = {
+ def test_removing_a_group
+ @user.groups_params = {
"1" => {
- :id => @role.id,
- :name => "Foo",
+ :id => @group.id,
+ :name => "engineers",
:_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"
+ assert @user.groups.detect{|s| s == @group}._delete, "the associated record is marked for deletion"
+ assert_equal "developers", @user.groups.detect{|s| s == @group}.name, "the association attribute did not update"
end
end
class NestedAssignmentHasManyThroughTest < ActiveSupport::TestCase
def setup
@user = users(:bob)
- @service = services(:free)
+ @tag = tags(:challenging)
end
- def test_adding_a_service
- @user.services_params = {
+ def test_adding_a_tag
+ @user.tags_params = {
"1" => {
- :name => "Foo"
+ :name => "easy"
}
}
- 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"
+ assert @user.tags.any?{|r| r.new_record?}, "a new record is added"
+ assert_equal "easy", @user.tags.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"}
+ def test_missing_tags_do_not_delete
+ @user.tags_params = {
+ "1" => {:name => "easy"}
}
- assert @user.services.any?{|r| r == @service}, "existing records remain in the collection"
+ assert @user.tags.any?{|r| r == @tag}, "existing records remain in the collection"
end
- def test_updating_a_service
- @user.services_params = {
+ def test_updating_a_tag
+ @user.tags_params = {
"1" => {
- :id => @service.id,
- :name => "Foo"
+ :id => @tag.id,
+ :name => "difficult"
}
}
- 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"
+ assert !@user.tags.any?{|s| s.new_record?}, "no new record is created"
+ assert_equal "difficult", @user.tags.detect{|s| s == @tag}.name, "the name is updated"
+ assert_equal "challenging", @tag.reload.name, "the name is not saved"
end
- def test_removing_a_service
- @user.services_params = {
+ def test_removing_a_tag
+ @user.tags_params = {
"1" => {
- :id => @service.id,
- :name => "Foo",
+ :id => @tag.id,
+ :name => "difficult",
:_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, :services, :subscription], User.association_names.sort_by(&:to_s)
- end
-
- def test_instantiated_associated_does_not_load_associations
- ActiveRecord::Associations::AssociationProxy.any_instance.expects(:loaded).never
- user = User.find(:first)
- assert_equal [], user.send(:instantiated_associated)
+ assert @user.tags.detect{|s| s == @tag}._delete, "the associated record is marked for deletion"
+ assert_equal "challenging", @user.tags.detect{|s| s == @tag}.name, "the association attribute did not update"
end
-
- def test_instantiated_associated_retrieves_loaded_associations
- user = User.find(:first)
- role = user.roles.build(:name => "Plumber")
- assert_equal [role], user.send(:instantiated_associated), "only one role is instantiated"
- assert_equal 2, user.roles.length, "though there were more associated roles"
- end
-
- def test_instantiated_associated_returns_arrays_for_singular_association
- user = User.find(:first)
- assert !user.address.nil?
- assert_equal [user.address], user.send(:instantiated_associated)
- end
-
- def test_instantiated_associated_returns_arrays_for_plural_association
- user = User.find(:first)
- assert !user.roles.empty?
- assert_equal user.roles, user.send(:instantiated_associated)
- end
-
end
View
5 test/unit/nested_assignment_validation_test.rb
@@ -0,0 +1,5 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class NestedAssignmentValidationTest < ActiveSupport::TestCase
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.