factory_boy 1.0.5 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/README.rdoc +68 -31
  2. data/lib/blank_slate.rb +3 -0
  3. data/lib/plant.rb +37 -46
  4. data/lib/query.rb +44 -0
  5. data/lib/reflection.rb +40 -0
  6. data/lib/selector.rb +181 -0
  7. data/lib/setup.rb +13 -22
  8. data/lib/stubber.rb +164 -21
  9. data/test/Rakefile.rb +11 -0
  10. data/test/app/models/address.rb +9 -0
  11. data/test/app/models/customer.rb +3 -0
  12. data/test/app/models/profile.rb +2 -0
  13. data/test/app/models/user.rb +7 -0
  14. data/test/databases.rake.rb +513 -0
  15. data/test/db/migrate/20101230223546_create_users.rb.rb +14 -0
  16. data/test/db/migrate/20101230223547_create_profiles.rb +15 -0
  17. data/test/db/migrate/20101230223548_create_customers.rb +11 -0
  18. data/test/db/migrate/20101230223549_create_addresses.rb +13 -0
  19. data/test/db/schema.rb +41 -0
  20. data/test/help_test.rb +15 -5
  21. data/test/plants.rb +5 -5
  22. data/test/test_basic_queries.rb +36 -0
  23. data/test/test_plant_definition.rb +129 -0
  24. data/test/test_plants_ids.rb +16 -0
  25. data/test/test_queries_on_has_many_association.rb +51 -0
  26. data/test/test_queries_on_has_one_association.rb +45 -0
  27. data/test/test_queries_on_model_attributes.rb +59 -0
  28. data/test/test_queries_with_like.rb +22 -0
  29. data/test/test_queries_with_limit.rb +28 -0
  30. data/test/test_queries_with_named_scope.rb +18 -0
  31. data/test/test_queries_with_order.rb +17 -0
  32. data/test/test_queries_with_ranges.rb +21 -0
  33. data/test/test_selector_condition.rb +26 -0
  34. data/test/test_stubbing.rb +43 -0
  35. metadata +60 -22
  36. data/test/models/adress.rb +0 -12
  37. data/test/models/customer.rb +0 -7
  38. data/test/models/profile.rb +0 -8
  39. data/test/models/user.rb +0 -8
  40. data/test/plant_tests.rb +0 -115
  41. data/test/test_plant.rb +0 -7
  42. data/test/test_plant_with_active_support.rb +0 -8
@@ -0,0 +1,22 @@
1
+ require 'help_test'
2
+
3
+ class TestQueriesWithLike < ActiveSupport::TestCase
4
+
5
+
6
+ def test_should_handle_sql_like_predicate
7
+ user = Plant(:user, :name => 'Gwaenaelle', :profile => Plant(:profile, :password => 'password'))
8
+
9
+ assert_equal([user], User.where("name like 'Gwaenaelle'"))
10
+ assert_equal([user], User.where("name like ?",'%Gwaenaelle'))
11
+ assert_equal([user], User.where("name like ?",'%Gwaena%'))
12
+ assert_equal([], User.where("name like ?",'%Gwaen'))
13
+
14
+ assert_equal([user], User.where("profiles.password like ?", '%pass%').joins(:profile))
15
+ assert_equal([user], User.where("name LIKE 'Gwaenaelle'"))
16
+
17
+ user.name = ' Like '
18
+ assert_equal([user], User.where("name LIKE ' Like '"))
19
+ end
20
+
21
+
22
+ end
@@ -0,0 +1,28 @@
1
+ require 'help_test'
2
+
3
+ class TestQueriesWithLimit < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @users = (1..10).map {|n| Plant(:user, :name => 'Joe', :age => n)}
7
+ end
8
+
9
+ def test_queries_with_limit_clause
10
+ assert_equal 5, User.limit(5).size
11
+ assert_equal 5, User.all.limit(5).size
12
+ assert_equal 6, User.all.limit(6).size
13
+ assert_equal 10, User.all.limit(11).size
14
+ assert_equal 3, User.where(:name => 'Joe').limit(3).size
15
+ end
16
+
17
+ def test_queries_with_limit_and_offset_clause
18
+ first_age = @users.first.age
19
+
20
+ assert_equal 5, User.limit(5).offset(2).size
21
+ assert_equal first_age + 2, User.limit(5).offset(2).first.age
22
+ assert_equal first_age + 2 + 4, User.limit(5).offset(2).last.age
23
+
24
+ end
25
+
26
+
27
+
28
+ end
@@ -0,0 +1,18 @@
1
+ require 'help_test'
2
+
3
+ class TestQueriesWithNamedScope < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @albert = Plant(:user, :name => 'Albert', :age => 30)
7
+ end
8
+
9
+ def test_should_handle_named_scopes
10
+ assert_equal [@albert], User.albert
11
+ end
12
+
13
+ def test_should_handle_scope_with_chained_where
14
+ assert_equal [@albert], User.albert.where(:age => 30)
15
+ assert_equal [], User.albert.where(:age => 20)
16
+ end
17
+
18
+ end
@@ -0,0 +1,17 @@
1
+ require 'help_test'
2
+
3
+ class TestQueriesWithOrder < ActiveSupport::TestCase
4
+
5
+ def test_should_handle_query_with_one_condition_on_order_clause
6
+ zorro, albert, joe = [Plant(:user, :name => 'Zorro', :age => 30), Plant(:user, :name => 'Albert', :age => 31), Plant(:user, :name => 'Joe', :age => 32)]
7
+
8
+ assert_equal [albert, joe, zorro], User.all.order('name asc')
9
+ assert_equal [zorro, joe, albert], User.all.order('name desc')
10
+ assert_equal [zorro, albert, joe], User.all.order('age asc')
11
+ assert_equal [joe, albert, zorro], User.all.order('age desc')
12
+
13
+ assert_equal [joe, albert, zorro], User.where('age > 20').order('age desc')
14
+ end
15
+
16
+
17
+ end
@@ -0,0 +1,21 @@
1
+ require 'help_test'
2
+
3
+ class TestQueriesWithRanges < ActiveSupport::TestCase
4
+
5
+ def test_should_handle_sql_in_predicate
6
+ user_1, user_2 = [Plant(:user, :age => 20), Plant(:user, :age => 25)]
7
+
8
+ assert_equal [user_1, user_2], User.where(:age => [12, 20, 25, 30])
9
+ assert_equal [user_1, user_2], User.where('age in (12, 20, 25, 30)')
10
+ assert_equal [user_1], User.where(:age => [12, 20, 30])
11
+ end
12
+
13
+ def test_should_handle_sql_between_predicate
14
+ user_1, user_2 = [Plant(:user, :age => 20), Plant(:user, :age => 25)]
15
+
16
+ assert_equal [user_1, user_2], User.where(:age => (12..40))
17
+ assert_equal [user_1], User.where(:age => (12..20))
18
+ end
19
+
20
+
21
+ end
@@ -0,0 +1,26 @@
1
+ require 'help_test'
2
+
3
+ class TestSelectorCondition < ActiveSupport::TestCase
4
+
5
+ def test_should_transform_sql_conditions_to_ruby_select_conditions_with_one_where
6
+ condition = Plant::Selector::Condition.new("(users.name = 'Joe')", User)
7
+
8
+ assert_equal "(users.name == 'Joe')", condition.to_ruby
9
+ end
10
+
11
+ def test_should_transform_sql_conditions_to_ruby_select_conditions_with_several_wheres
12
+ condition = Plant::Selector::Condition.new(["(users.name = 'Joe' or users.age = 10)", "(users.age = 30)"], User)
13
+
14
+ assert_equal "(users.name == 'Joe' or users.age == 10) and (users.age == 30)", condition.to_ruby
15
+ end
16
+
17
+ def test_with_inequality_operators
18
+ condition = Plant::Selector::Condition.new(["(users.age <= 10)"], User)
19
+
20
+ assert_equal "(users.age <= 10)", condition.to_ruby
21
+ end
22
+
23
+
24
+
25
+
26
+ end
@@ -0,0 +1,43 @@
1
+ require 'help_test'
2
+
3
+
4
+ class StubbingTest < ActiveSupport::TestCase
5
+
6
+ def setup
7
+ assert_stubs_are_unstubbed_before_each_test
8
+ User.destroy_all
9
+ Profile.destroy_all
10
+ end
11
+
12
+ def test_stubs_should_be_on_only_when_plant_definitions_are_loaded
13
+ user = User.create(:name => 'Largo')
14
+ assert_equal [user], User.where(:name => 'Largo')
15
+
16
+ user = Plant(:user, :name => 'Tintin')
17
+ assert_equal [], User.where(:name => 'Largo')
18
+ assert_equal [user], User.where(:name => 'Tintin')
19
+ end
20
+
21
+ def test_includes_are_stubbed_only_when_plant_definitions_are_loaded
22
+ profile = Profile.create(:password => 'azerty')
23
+ joe = User.create(:name => 'Joe', :age => 30, :profile => profile)
24
+
25
+ assert_equal [joe], User.where(:name => 'Joe').where("profiles.password = 'azerty'").includes(:profile)
26
+
27
+ profile = Plant(:profile, :password => 'xy')
28
+ joe = Plant(:user, :name => 'Joe', :profile => profile)
29
+
30
+ assert_equal [], User.where(:name => 'Joe').where("profiles.password = 'azerty'").includes(:profile)
31
+ assert_equal [joe], User.where(:name => 'Joe').where("profiles.password = 'xy'").includes(:profile)
32
+ end
33
+
34
+ private
35
+
36
+ def assert_stubs_are_unstubbed_before_each_test
37
+ User.destroy_all
38
+ user = User.create(:name => 'Largo')
39
+ assert_equal user, User.first
40
+ assert_equal [user], User.where(:name => 'Largo')
41
+ end
42
+
43
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_boy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
4
+ hash: 15
5
+ prerelease:
6
6
  segments:
7
- - 1
7
+ - 2
8
8
  - 0
9
- - 5
10
- version: 1.0.5
9
+ - 0
10
+ version: 2.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Philippe Cantin
@@ -15,11 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-21 00:00:00 +02:00
18
+ date: 2011-01-23 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
22
- description: Factory Girl with database accesses stubbed
22
+ description: " Factory Girl with database accesses stubbed.\n The versions 2+ only work with Rails 3 (AR 3+) for stubbing queries.\n Now handle Rails 3 (only Rails 3) queries stubbing,\n Transform rail3 queries into ruby select on Plants created with factory boy.\n Example\n user = Plant(:user => 'toto', :addresses => [Plant(:address, :street => 'here')])\n User.where(:name => 'toto').where('addresses.street = 'here').joins(:addresses) will be stubbed into\n a select ruby on plants and return here, user.\n See more on github and in unit tests.\n"
23
23
  email:
24
24
  executables: []
25
25
 
@@ -28,20 +28,41 @@ extensions: []
28
28
  extra_rdoc_files:
29
29
  - README.rdoc
30
30
  files:
31
+ - lib/blank_slate.rb
31
32
  - lib/factory_boy.rb
32
33
  - lib/plant.rb
34
+ - lib/query.rb
35
+ - lib/reflection.rb
36
+ - lib/selector.rb
33
37
  - lib/setup.rb
34
38
  - lib/stubber.rb
35
39
  - README.rdoc
40
+ - test/app/models/address.rb
41
+ - test/app/models/customer.rb
42
+ - test/app/models/profile.rb
43
+ - test/app/models/user.rb
44
+ - test/databases.rake.rb
45
+ - test/db/migrate/20101230223546_create_users.rb.rb
46
+ - test/db/migrate/20101230223547_create_profiles.rb
47
+ - test/db/migrate/20101230223548_create_customers.rb
48
+ - test/db/migrate/20101230223549_create_addresses.rb
49
+ - test/db/schema.rb
36
50
  - test/help_test.rb
37
- - test/models/adress.rb
38
- - test/models/customer.rb
39
- - test/models/profile.rb
40
- - test/models/user.rb
41
- - test/plant_tests.rb
42
51
  - test/plants.rb
43
- - test/test_plant.rb
44
- - test/test_plant_with_active_support.rb
52
+ - test/Rakefile.rb
53
+ - test/test_basic_queries.rb
54
+ - test/test_plant_definition.rb
55
+ - test/test_plants_ids.rb
56
+ - test/test_queries_on_has_many_association.rb
57
+ - test/test_queries_on_has_one_association.rb
58
+ - test/test_queries_on_model_attributes.rb
59
+ - test/test_queries_with_like.rb
60
+ - test/test_queries_with_limit.rb
61
+ - test/test_queries_with_named_scope.rb
62
+ - test/test_queries_with_order.rb
63
+ - test/test_queries_with_ranges.rb
64
+ - test/test_selector_condition.rb
65
+ - test/test_stubbing.rb
45
66
  has_rdoc: true
46
67
  homepage: http://github.com/anoiaque/factory_boy
47
68
  licenses: []
@@ -72,17 +93,34 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
93
  requirements: []
73
94
 
74
95
  rubyforge_project:
75
- rubygems_version: 1.3.7
96
+ rubygems_version: 1.4.1
76
97
  signing_key:
77
98
  specification_version: 3
78
99
  summary: Create fixtures for unit testing as in Factory Girl but without database usage.
79
100
  test_files:
101
+ - test/app/models/address.rb
102
+ - test/app/models/customer.rb
103
+ - test/app/models/profile.rb
104
+ - test/app/models/user.rb
105
+ - test/databases.rake.rb
106
+ - test/db/migrate/20101230223546_create_users.rb.rb
107
+ - test/db/migrate/20101230223547_create_profiles.rb
108
+ - test/db/migrate/20101230223548_create_customers.rb
109
+ - test/db/migrate/20101230223549_create_addresses.rb
110
+ - test/db/schema.rb
80
111
  - test/help_test.rb
81
- - test/models/adress.rb
82
- - test/models/customer.rb
83
- - test/models/profile.rb
84
- - test/models/user.rb
85
- - test/plant_tests.rb
86
112
  - test/plants.rb
87
- - test/test_plant.rb
88
- - test/test_plant_with_active_support.rb
113
+ - test/Rakefile.rb
114
+ - test/test_basic_queries.rb
115
+ - test/test_plant_definition.rb
116
+ - test/test_plants_ids.rb
117
+ - test/test_queries_on_has_many_association.rb
118
+ - test/test_queries_on_has_one_association.rb
119
+ - test/test_queries_on_model_attributes.rb
120
+ - test/test_queries_with_like.rb
121
+ - test/test_queries_with_limit.rb
122
+ - test/test_queries_with_named_scope.rb
123
+ - test/test_queries_with_order.rb
124
+ - test/test_queries_with_ranges.rb
125
+ - test/test_selector_condition.rb
126
+ - test/test_stubbing.rb
@@ -1,12 +0,0 @@
1
- class Adress
2
- attr_accessor :number, :street
3
-
4
- def initialize args={}
5
- @street = args[:street]
6
- @number = args[:number]
7
- end
8
-
9
- def self.find *args
10
- "original_find"
11
- end
12
- end
@@ -1,7 +0,0 @@
1
- class Customer
2
- attr_accessor :name
3
-
4
- def self.find *args
5
- "original_find"
6
- end
7
- end
@@ -1,8 +0,0 @@
1
- class Profile
2
-
3
- attr_accessor :password
4
-
5
- def self.find *args
6
- "original_find"
7
- end
8
- end
data/test/models/user.rb DELETED
@@ -1,8 +0,0 @@
1
- class User
2
-
3
- attr_accessor :name, :age, :adresses, :profile
4
-
5
- def self.find *args
6
- "original_find"
7
- end
8
- end
data/test/plant_tests.rb DELETED
@@ -1,115 +0,0 @@
1
- module PlantTests
2
- def setup
3
- assert_find_is_unstubbed_for_each_class
4
- Plant(:customer)
5
- end
6
-
7
- def test_define_simple_definition
8
- assert customer = Plant(:customer)
9
- assert_nil customer.name
10
- end
11
-
12
- def test_define_assign_attributes
13
- user = Plant(:user)
14
- assert_equal "Zorro", user.name
15
- assert_equal 800, user.age
16
- end
17
-
18
- def test_define_stubs_find_without_parameter
19
- user = Plant(:user)
20
- assert_equal user, User.find
21
- end
22
-
23
- def test_define_stubs_find_with_array_result
24
- 2.times { Plant(:user) }
25
- assert_equal 2, User.find(:all).size
26
- end
27
-
28
- def test_define_with_association_has_one
29
- user = Plant(:user)
30
- profile = Profile.find
31
- assert_equal profile, user.profile
32
- end
33
-
34
- def test_define_with_association_has_many
35
- user = Plant(:user)
36
- assert adresses = user.adresses
37
- adress = Adress.find
38
- assert_equal adress, user.adresses.first
39
- end
40
-
41
- def test_stubs_find_with_option_all
42
- user = Plant(:user)
43
- users = User.find(:all)
44
- assert_equal [user], users
45
- end
46
-
47
- def test_find_all_must_return_an_empty_array_if_no_object
48
- users = User.find(:all)
49
- assert_equal [], users
50
- end
51
-
52
- def test_stubs_find_with_option_first
53
- users = 3.times.map { Plant(:user) }
54
- assert_equal users.first, User.find(:first)
55
- end
56
-
57
- def test_stubs_find_with_option_last
58
- users = 3.times.map { Plant(:user) }
59
- assert_equal users.last, User.find(:last)
60
- end
61
-
62
- def test_mocha_is_not_overidden_with_plant_setup
63
- Plant.expects(:pool).once
64
- Plant(:user)
65
- end
66
-
67
- def test_create_plant_with_hash_for_values
68
- adress = Plant(:adress)
69
- user = Plant(:user, :name => "Marie", :adresses => [adress])
70
- assert_equal "Marie", user.name
71
- assert_equal adress, user.adresses.first
72
- end
73
-
74
- def test_define_with_class_option
75
- Plant.define :marie, :class => User do |peter|
76
- peter.name = "Marie"
77
- peter.adresses = [Adress.new(:street => "Rue de Brest")]
78
- end
79
- marie = Plant(:marie)
80
- assert_equal "Marie", marie.name
81
- assert_equal "Rue de Brest", marie.adresses.first.street
82
- end
83
-
84
- def test_plants_are_reloaded_and_4_plants_are_defined
85
- assert_equal 4, Plant.plants.size
86
- end
87
-
88
- def test_define_with_dependent_attribute
89
- Plant.define :user do |user|
90
- user.name = "Marie"
91
- user.adresses = [Adress.new(:street => "Rue de #{user.name}")]
92
- end
93
- assert_equal "Rue de Marie", Plant(:user).adresses.first.street
94
- end
95
-
96
- def test_plant_sequences
97
- assert_equal "incognito1@kantena.com", Plant.next(:email)
98
- assert_equal "incognito2@kantena.com", Plant.next(:email)
99
- end
100
-
101
- def test_creation_of_two_plants_of_same_class_should_keep_each_object_safe
102
- user_1 = Plant(:user, :name => "Elise")
103
- user_2 = Plant(:user, :name => "Vincent")
104
-
105
- assert_equal "Elise", user_1.name
106
- assert_equal "Vincent", user_2.name
107
- end
108
-
109
- private
110
-
111
- def assert_find_is_unstubbed_for_each_class
112
- [Adress, Customer, Profile, User].each {|klass| assert_equal "original_find", klass.find }
113
- end
114
-
115
- end