Commit a98262bf1f8475a654d67b26d796c9833df614cd

Authored by MoisesMachado
1 parent 04bc7077

ActionItem6: test and fixtures fixed for the new model hierachy

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@200 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/person.rb 0 → 100644
@@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
  1 +class Person < Profile
  2 + belongs_to :user
  3 + has_many :affiliations
  4 + has_many :profiles, :through => :affiliations
  5 + has_many :friends, :class_name => 'Person'
  6 +end
app/models/profile.rb
@@ -39,8 +39,8 @@ class Profile &lt; ActiveRecord::Base @@ -39,8 +39,8 @@ class Profile &lt; ActiveRecord::Base
39 39
40 # A profile_owner cannot have more than one profile, but many profiles can exist 40 # A profile_owner cannot have more than one profile, but many profiles can exist
41 # without being associated to a particular user. 41 # without being associated to a particular user.
42 - validates_uniqueness_of :profile_owner_id, :scope => :profile_owner_type, :if => (lambda do |profile|  
43 - ! profile.profile_owner_id.nil? 42 + validates_uniqueness_of :user_id, :if => (lambda do |profile|
  43 + ! profile.user_id.nil?
44 end) 44 end)
45 45
46 # creates a new Profile. By default, it is attached to the default 46 # creates a new Profile. By default, it is attached to the default
db/migrate/002_create_profiles.rb
@@ -2,6 +2,7 @@ class CreateProfiles &lt; ActiveRecord::Migration @@ -2,6 +2,7 @@ class CreateProfiles &lt; ActiveRecord::Migration
2 def self.up 2 def self.up
3 create_table :profiles do |t| 3 create_table :profiles do |t|
4 t.column :name, :string 4 t.column :name, :string
  5 + t.column :type, :string
5 t.column :identifier, :string 6 t.column :identifier, :string
6 t.column :virtual_community_id, :integer 7 t.column :virtual_community_id, :integer
7 t.column :flexible_template_template, :string, :default => "default" 8 t.column :flexible_template_template, :string, :default => "default"
test/fixtures/enterprises.yml
@@ -1,8 +0,0 @@ @@ -1,8 +0,0 @@
1 -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html  
2 -one:  
3 - id: 1  
4 - name: 'Enterprise 1'  
5 - manager_id: 1  
6 - active: false  
7 -two:  
8 - id: 2  
test/fixtures/profiles.yml
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 johndoe: 2 johndoe:
3 id: 1 3 id: 1
4 name: 'John Doe' 4 name: 'John Doe'
  5 + type: 'Person'
5 identifier: johndoe 6 identifier: johndoe
6 virtual_community_id: 1 7 virtual_community_id: 1
7 user_id: 1 8 user_id: 1
@@ -11,6 +12,7 @@ johndoe: @@ -11,6 +12,7 @@ johndoe:
11 joerandomhacker: 12 joerandomhacker:
12 id: 2 13 id: 2
13 name: 'Joe Random Hacker' 14 name: 'Joe Random Hacker'
  15 + type: 'Person'
14 identifier: joerandomhacker 16 identifier: joerandomhacker
15 virtual_community_id: 1 17 virtual_community_id: 1
16 user_id: 2 18 user_id: 2
@@ -20,6 +22,7 @@ joerandomhacker: @@ -20,6 +22,7 @@ joerandomhacker:
20 john_and_joe: 22 john_and_joe:
21 id: 3 23 id: 3
22 name: "John and Joe's Production Cooperative" 24 name: "John and Joe's Production Cooperative"
  25 + type: 'Person'
23 identifier: john_and_joe 26 identifier: john_and_joe
24 virtual_community_id: 1 27 virtual_community_id: 1
25 flexible_template_template: 'simple' 28 flexible_template_template: 'simple'
@@ -28,6 +31,7 @@ john_and_joe: @@ -28,6 +31,7 @@ john_and_joe:
28 ze: 31 ze:
29 id: 4 32 id: 4
30 name: "Zé" 33 name: "Zé"
  34 + type: 'Person'
31 identifier: ze 35 identifier: ze
32 virtual_community_id: 1 36 virtual_community_id: 1
33 flexible_template_template: 'default' 37 flexible_template_template: 'default'
test/unit/person_test.rb 0 → 100644
@@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class PersonTest < Test::Unit::TestCase
  4 + fixtures :profiles, :users
  5 +
  6 + # Replace this with your real tests.
  7 + def test_truth
  8 + assert true
  9 + end
  10 +
  11 + def test_can_have_user
  12 + p = profiles(:johndoe)
  13 + assert_kind_of User, p.user
  14 + end
  15 +
  16 + def test_may_have_no_user
  17 + p = profiles(:john_and_joe)
  18 + assert_nil p.user
  19 + assert p.valid?
  20 + end
  21 +
  22 + def test_only_one_profile_per_user
  23 + p1 = profiles(:johndoe)
  24 + assert_equal users(:johndoe), p1.user
  25 +
  26 + p2 = Person.new
  27 + p2.user = users(:johndoe)
  28 + assert !p2.valid?
  29 + assert p2.errors.invalid?(:user_id)
  30 + end
  31 +
  32 + def test_several_profiles_without_user
  33 + p1 = profiles(:john_and_joe)
  34 + assert p1.valid?
  35 + assert_nil p1.user
  36 +
  37 + p2 = Person.new
  38 + assert !p2.valid?
  39 + assert !p2.errors.invalid?(:user_id)
  40 + end
  41 +end
test/unit/profile_test.rb
@@ -39,37 +39,6 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -39,37 +39,6 @@ class ProfileTest &lt; Test::Unit::TestCase
39 assert_kind_of VirtualCommunity, p.virtual_community 39 assert_kind_of VirtualCommunity, p.virtual_community
40 end 40 end
41 41
42 - def test_can_have_user  
43 - p = profiles(:johndoe)  
44 - assert_kind_of User, p.profile_owner  
45 - end  
46 -  
47 - def test_may_have_no_user  
48 - p = profiles(:john_and_joe)  
49 - assert_nil p.profile_owner  
50 - assert p.valid?  
51 - end  
52 -  
53 - def test_only_one_profile_per_user  
54 - p1 = profiles(:johndoe)  
55 - assert_equal users(:johndoe), p1.profile_owner  
56 -  
57 - p2 = Profile.new  
58 - p2.profile_owner = users(:johndoe)  
59 - assert !p2.valid?  
60 - assert p2.errors.invalid?(:profile_owner_id)  
61 - end  
62 -  
63 - def test_several_profiles_without_user  
64 - p1 = profiles(:john_and_joe)  
65 - assert p1.valid?  
66 - assert_nil p1.profile_owner  
67 -  
68 - p2 = Profile.new  
69 - assert !p2.valid?  
70 - assert !p2.errors.invalid?(:profile_owner_id)  
71 - end  
72 -  
73 def test_cannot_rename 42 def test_cannot_rename
74 p1 = profiles(:johndoe) 43 p1 = profiles(:johndoe)
75 assert_raise ArgumentError do 44 assert_raise ArgumentError do
test/unit/user_test.rb
@@ -68,16 +68,16 @@ class UserTest &lt; Test::Unit::TestCase @@ -68,16 +68,16 @@ class UserTest &lt; Test::Unit::TestCase
68 assert_nil users(:johndoe).remember_token 68 assert_nil users(:johndoe).remember_token
69 end 69 end
70 70
71 - def test_should_create_profile 71 + def test_should_create_person
72 users_count = User.count 72 users_count = User.count
73 - profiles_count = Profile.count 73 + person_count = Person.count
74 74
75 user = User.create!(:login => 'new_user', :email => 'new_user@example.com', :password => 'test', :password_confirmation => 'test') 75 user = User.create!(:login => 'new_user', :email => 'new_user@example.com', :password => 'test', :password_confirmation => 'test')
76 76
77 - assert Profile.exists?(['profile_owner_id = ? and profile_owner_type = ?', user.id, 'User']) 77 + assert Person.exists?(['user_id = ?', user.id])
78 78
79 assert_equal users_count + 1, User.count 79 assert_equal users_count + 1, User.count
80 - assert_equal profiles_count + 1, Profile.count 80 + assert_equal person_count + 1, Person.count
81 end 81 end
82 82
83 def test_login_validation 83 def test_login_validation