From a98262bf1f8475a654d67b26d796c9833df614cd Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Fri, 27 Jul 2007 20:38:08 +0000 Subject: [PATCH] ActionItem6: test and fixtures fixed for the new model hierachy --- app/models/person.rb | 6 ++++++ app/models/profile.rb | 4 ++-- db/migrate/002_create_profiles.rb | 1 + test/fixtures/enterprises.yml | 8 -------- test/fixtures/profiles.yml | 4 ++++ test/unit/person_test.rb | 41 +++++++++++++++++++++++++++++++++++++++++ test/unit/profile_test.rb | 31 ------------------------------- test/unit/user_test.rb | 8 ++++---- 8 files changed, 58 insertions(+), 45 deletions(-) create mode 100644 app/models/person.rb delete mode 100644 test/fixtures/enterprises.yml create mode 100644 test/unit/person_test.rb diff --git a/app/models/person.rb b/app/models/person.rb new file mode 100644 index 0000000..347cfc3 --- /dev/null +++ b/app/models/person.rb @@ -0,0 +1,6 @@ +class Person < Profile + belongs_to :user + has_many :affiliations + has_many :profiles, :through => :affiliations + has_many :friends, :class_name => 'Person' +end diff --git a/app/models/profile.rb b/app/models/profile.rb index 150c5bf..b7f7df8 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -39,8 +39,8 @@ class Profile < ActiveRecord::Base # A profile_owner cannot have more than one profile, but many profiles can exist # without being associated to a particular user. - validates_uniqueness_of :profile_owner_id, :scope => :profile_owner_type, :if => (lambda do |profile| - ! profile.profile_owner_id.nil? + validates_uniqueness_of :user_id, :if => (lambda do |profile| + ! profile.user_id.nil? end) # creates a new Profile. By default, it is attached to the default diff --git a/db/migrate/002_create_profiles.rb b/db/migrate/002_create_profiles.rb index b177862..d9ba365 100644 --- a/db/migrate/002_create_profiles.rb +++ b/db/migrate/002_create_profiles.rb @@ -2,6 +2,7 @@ class CreateProfiles < ActiveRecord::Migration def self.up create_table :profiles do |t| t.column :name, :string + t.column :type, :string t.column :identifier, :string t.column :virtual_community_id, :integer t.column :flexible_template_template, :string, :default => "default" diff --git a/test/fixtures/enterprises.yml b/test/fixtures/enterprises.yml deleted file mode 100644 index 4e336b7..0000000 --- a/test/fixtures/enterprises.yml +++ /dev/null @@ -1,8 +0,0 @@ -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -one: - id: 1 - name: 'Enterprise 1' - manager_id: 1 - active: false -two: - id: 2 diff --git a/test/fixtures/profiles.yml b/test/fixtures/profiles.yml index 2f573b9..046ca6b 100644 --- a/test/fixtures/profiles.yml +++ b/test/fixtures/profiles.yml @@ -2,6 +2,7 @@ johndoe: id: 1 name: 'John Doe' + type: 'Person' identifier: johndoe virtual_community_id: 1 user_id: 1 @@ -11,6 +12,7 @@ johndoe: joerandomhacker: id: 2 name: 'Joe Random Hacker' + type: 'Person' identifier: joerandomhacker virtual_community_id: 1 user_id: 2 @@ -20,6 +22,7 @@ joerandomhacker: john_and_joe: id: 3 name: "John and Joe's Production Cooperative" + type: 'Person' identifier: john_and_joe virtual_community_id: 1 flexible_template_template: 'simple' @@ -28,6 +31,7 @@ john_and_joe: ze: id: 4 name: "Zé" + type: 'Person' identifier: ze virtual_community_id: 1 flexible_template_template: 'default' diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb new file mode 100644 index 0000000..3973b74 --- /dev/null +++ b/test/unit/person_test.rb @@ -0,0 +1,41 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class PersonTest < Test::Unit::TestCase + fixtures :profiles, :users + + # Replace this with your real tests. + def test_truth + assert true + end + + def test_can_have_user + p = profiles(:johndoe) + assert_kind_of User, p.user + end + + def test_may_have_no_user + p = profiles(:john_and_joe) + assert_nil p.user + assert p.valid? + end + + def test_only_one_profile_per_user + p1 = profiles(:johndoe) + assert_equal users(:johndoe), p1.user + + p2 = Person.new + p2.user = users(:johndoe) + assert !p2.valid? + assert p2.errors.invalid?(:user_id) + end + + def test_several_profiles_without_user + p1 = profiles(:john_and_joe) + assert p1.valid? + assert_nil p1.user + + p2 = Person.new + assert !p2.valid? + assert !p2.errors.invalid?(:user_id) + end +end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 689e2f2..12958e3 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -39,37 +39,6 @@ class ProfileTest < Test::Unit::TestCase assert_kind_of VirtualCommunity, p.virtual_community end - def test_can_have_user - p = profiles(:johndoe) - assert_kind_of User, p.profile_owner - end - - def test_may_have_no_user - p = profiles(:john_and_joe) - assert_nil p.profile_owner - assert p.valid? - end - - def test_only_one_profile_per_user - p1 = profiles(:johndoe) - assert_equal users(:johndoe), p1.profile_owner - - p2 = Profile.new - p2.profile_owner = users(:johndoe) - assert !p2.valid? - assert p2.errors.invalid?(:profile_owner_id) - end - - def test_several_profiles_without_user - p1 = profiles(:john_and_joe) - assert p1.valid? - assert_nil p1.profile_owner - - p2 = Profile.new - assert !p2.valid? - assert !p2.errors.invalid?(:profile_owner_id) - end - def test_cannot_rename p1 = profiles(:johndoe) assert_raise ArgumentError do diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index eb663ae..eaecbcf 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -68,16 +68,16 @@ class UserTest < Test::Unit::TestCase assert_nil users(:johndoe).remember_token end - def test_should_create_profile + def test_should_create_person users_count = User.count - profiles_count = Profile.count + person_count = Person.count user = User.create!(:login => 'new_user', :email => 'new_user@example.com', :password => 'test', :password_confirmation => 'test') - assert Profile.exists?(['profile_owner_id = ? and profile_owner_type = ?', user.id, 'User']) + assert Person.exists?(['user_id = ?', user.id]) assert_equal users_count + 1, User.count - assert_equal profiles_count + 1, Profile.count + assert_equal person_count + 1, Person.count end def test_login_validation -- libgit2 0.21.2