factory_girl 2.0.0.beta5 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -182,3 +182,11 @@ Feature: Use step definitions generated by factories
182
182
  Then there should be 3 categories
183
183
  Given 3 Categories exist with a name of "science"
184
184
  Then there should be 6 Categories
185
+
186
+ Scenario: step definitions work correctly with aliases
187
+ Given the following person exists:
188
+ | ID | Name |
189
+ | 123 | Joe |
190
+ Then I should find the following for the last user:
191
+ | id | name |
192
+ | 123 | Joe |
@@ -54,11 +54,10 @@ FactoryGirl.define do
54
54
  "email#{n}@example.com"
55
55
  end
56
56
 
57
- factory :user do
58
- end
59
-
60
- factory :admin_user, :parent => :user do
61
- admin true
57
+ factory :user, :aliases => [:person] do
58
+ factory :admin_user do
59
+ admin true
60
+ end
62
61
  end
63
62
 
64
63
  factory :category do
Binary file
@@ -84,8 +84,8 @@ module FactoryGirl
84
84
  proxy.result(@to_create_block)
85
85
  end
86
86
 
87
- def human_name(*args, &block)
88
- name.to_s.gsub('_', ' ')
87
+ def human_names
88
+ names.map {|name| name.to_s.gsub('_', ' ') }
89
89
  end
90
90
 
91
91
  def associations
@@ -16,7 +16,7 @@ module FactoryGirl
16
16
  end
17
17
 
18
18
  def each(&block)
19
- @items.values.each(&block)
19
+ @items.values.uniq.each(&block)
20
20
  end
21
21
 
22
22
  def [](name)
@@ -1,24 +1,92 @@
1
1
  module FactoryGirlStepHelpers
2
-
3
- def convert_association_string_to_instance(factory_name, assignment)
4
- attribute, value = assignment.split(':', 2)
5
- return if value.blank?
6
- factory = FactoryGirl.factory_by_name(factory_name)
7
- attributes = convert_human_hash_to_attribute_hash({attribute => value.strip}, factory.associations)
8
- model_class = factory.build_class
9
- model_class.find(:first, :conditions => attributes) or
10
- FactoryGirl.create(factory_name, attributes)
2
+ def convert_human_hash_to_attribute_hash(human_hash, associations = [])
3
+ HumanHashToAttributeHash.new(human_hash, associations).attributes
11
4
  end
12
5
 
13
- def convert_human_hash_to_attribute_hash(human_hash, associations = [])
14
- human_hash.inject({}) do |attribute_hash, (human_key, value)|
15
- key = human_key.downcase.gsub(' ', '_').to_sym
16
- if association = associations.detect {|association| association.name == key }
17
- association_instance = convert_association_string_to_instance(association.factory, value)
18
- key = "#{key}_id"
19
- value = association_instance.id if association_instance
6
+ class HumanHashToAttributeHash
7
+ attr_reader :associations
8
+
9
+ def initialize(human_hash, associations)
10
+ @human_hash = human_hash
11
+ @associations = associations
12
+ end
13
+
14
+ def attributes(strategy = CreateAttributes)
15
+ @human_hash.inject({}) do |attribute_hash, (human_key, value)|
16
+ attributes = strategy.new(self, *process_key_value(human_key, value))
17
+ attribute_hash.merge({ attributes.key => attributes.value })
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def process_key_value(key, value)
24
+ [key.downcase.gsub(' ', '_').to_sym, value.strip]
25
+ end
26
+
27
+ class AssociationManager
28
+ def initialize(human_hash_to_attributes_hash, key, value)
29
+ @human_hash_to_attributes_hash = human_hash_to_attributes_hash
30
+ @key = key
31
+ @value = value
32
+ end
33
+
34
+ def association
35
+ @human_hash_to_attributes_hash.associations.detect {|association| association.name == @key }
36
+ end
37
+
38
+ def association_instance
39
+ return unless association
40
+
41
+ if attributes_hash = nested_attribute_hash
42
+ factory.build_class.find(:first, :conditions => attributes_hash.attributes(FindAttributes)) or
43
+ FactoryGirl.create(association.factory, attributes_hash.attributes)
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def factory
50
+ FactoryGirl.factory_by_name(association.factory)
51
+ end
52
+
53
+ def nested_attribute_hash
54
+ attribute, value = @value.split(':', 2)
55
+ return if value.blank?
56
+
57
+ HumanHashToAttributeHash.new({ attribute => value }, factory.associations)
58
+ end
59
+ end
60
+
61
+ class AttributeStrategy
62
+ attr_reader :key, :value, :association_manager
63
+
64
+ def initialize(human_hash_to_attributes_hash, key, value)
65
+ @association_manager = AssociationManager.new(human_hash_to_attributes_hash, key, value)
66
+ @key = key
67
+ @value = value
68
+ end
69
+ end
70
+
71
+ class FindAttributes < AttributeStrategy
72
+ def initialize(human_hash_to_attributes_hash, key, value)
73
+ super
74
+
75
+ if association_manager.association
76
+ @key = "#{@key}_id"
77
+ @value = association_manager.association_instance.try(:id)
78
+ end
79
+ end
80
+ end
81
+
82
+ class CreateAttributes < AttributeStrategy
83
+ def initialize(human_hash_to_attributes_hash, key, value)
84
+ super
85
+
86
+ if association_manager.association
87
+ @value = association_manager.association_instance
88
+ end
20
89
  end
21
- attribute_hash.merge(key => value)
22
90
  end
23
91
  end
24
92
  end
@@ -26,30 +94,32 @@ end
26
94
  World(FactoryGirlStepHelpers)
27
95
 
28
96
  FactoryGirl.factories.each do |factory|
29
- Given /^the following (?:#{factory.human_name}|#{factory.human_name.pluralize}) exists?:$/i do |table|
30
- table.hashes.each do |human_hash|
31
- attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
32
- factory.run(FactoryGirl::Proxy::Create, attributes)
97
+ factory.human_names.each do |human_name|
98
+ Given /^the following (?:#{human_name}|#{human_name.pluralize}) exists?:$/i do |table|
99
+ table.hashes.each do |human_hash|
100
+ attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
101
+ FactoryGirl.create(factory.name, attributes)
102
+ end
33
103
  end
34
- end
35
104
 
36
- Given /^an? #{factory.human_name} exists$/i do
37
- FactoryGirl.create(factory.name)
38
- end
105
+ Given /^an? #{human_name} exists$/i do
106
+ FactoryGirl.create(factory.name)
107
+ end
39
108
 
40
- Given /^(\d+) #{factory.human_name.pluralize} exist$/i do |count|
41
- count.to_i.times { FactoryGirl.create(factory.name) }
42
- end
109
+ Given /^(\d+) #{human_name.pluralize} exist$/i do |count|
110
+ count.to_i.times { FactoryGirl.create(factory.name) }
111
+ end
43
112
 
44
- if factory.build_class.respond_to?(:columns)
45
- factory.build_class.columns.each do |column|
46
- human_column_name = column.name.downcase.gsub('_', ' ')
47
- Given /^an? #{factory.human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
48
- FactoryGirl.create(factory.name, column.name => value)
49
- end
113
+ if factory.build_class.respond_to?(:columns)
114
+ factory.build_class.columns.each do |column|
115
+ human_column_name = column.name.downcase.gsub('_', ' ')
116
+ Given /^an? #{human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
117
+ FactoryGirl.create(factory.name, column.name => value)
118
+ end
50
119
 
51
- Given /^(\d+) #{factory.human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
52
- count.to_i.times { FactoryGirl.create(factory.name, column.name => value) }
120
+ Given /^(\d+) #{human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
121
+ count.to_i.times { FactoryGirl.create(factory.name, column.name => value) }
122
+ end
53
123
  end
54
124
  end
55
125
  end
@@ -1,4 +1,4 @@
1
1
  module FactoryGirl
2
- VERSION = "2.0.0.beta5"
2
+ VERSION = "2.0.0.rc1"
3
3
  end
4
4
 
@@ -354,14 +354,22 @@ describe FactoryGirl::Factory do
354
354
 
355
355
  end
356
356
 
357
- describe FactoryGirl::Factory, "with an underscore in the name" do
358
- subject { FactoryGirl::Factory.new("happy_users") }
357
+ describe FactoryGirl::Factory, "human names" do
358
+ context "factory name without underscores" do
359
+ subject { FactoryGirl::Factory.new("user") }
360
+ its(:human_names) { should == ["user"] }
361
+ end
359
362
 
360
- it "has a human name" do
361
- subject.human_name.should == 'happy users'
363
+ context "factory name with underscores" do
364
+ subject { FactoryGirl::Factory.new("happy_user") }
365
+ its(:human_names) { should == ["happy user"] }
362
366
  end
363
- end
364
367
 
368
+ context "factory name with aliases" do
369
+ subject { FactoryGirl::Factory.new("happy_user", :aliases => ["gleeful_user", "person"]) }
370
+ its(:human_names) { should == ["happy user", "gleeful user", "person"] }
371
+ end
372
+ end
365
373
 
366
374
  describe FactoryGirl::Factory, "with aliases" do
367
375
  it "registers the aliases" do
@@ -370,4 +378,11 @@ describe FactoryGirl::Factory, "with aliases" do
370
378
  factory = FactoryGirl::Factory.new(:user, :aliases => [aliased_name])
371
379
  factory.names.should =~ [name, aliased_name]
372
380
  end
381
+
382
+ it "has human names" do
383
+ name = :user
384
+ aliased_name = :guest
385
+ factory = FactoryGirl::Factory.new(:user, :aliases => [aliased_name])
386
+ factory.human_names.should =~ [name.to_s, aliased_name.to_s]
387
+ end
373
388
  end
@@ -50,6 +50,19 @@ describe FactoryGirl::Registry do
50
50
  result.should =~ [factory, other_factory]
51
51
  end
52
52
 
53
+ it "iterates registered factories uniquely with aliases" do
54
+ other_factory = FactoryGirl::Factory.new(:string, :aliases => [:awesome])
55
+ subject.add(factory)
56
+ subject.add(other_factory)
57
+ result = []
58
+
59
+ subject.each do |value|
60
+ result << value
61
+ end
62
+
63
+ result.should =~ [factory, other_factory]
64
+ end
65
+
53
66
  it "registers an sequence" do
54
67
  sequence = FactoryGirl::Sequence.new(:email) { |n| "somebody#{n}@example.com" }
55
68
  subject.add(sequence)
metadata CHANGED
@@ -1,8 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_girl
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 15424087
4
5
  prerelease: 6
5
- version: 2.0.0.beta5
6
+ segments:
7
+ - 2
8
+ - 0
9
+ - 0
10
+ - rc
11
+ - 1
12
+ version: 2.0.0.rc1
6
13
  platform: ruby
7
14
  authors:
8
15
  - Joe Ferris
@@ -10,7 +17,7 @@ autorequire:
10
17
  bindir: bin
11
18
  cert_chain: []
12
19
 
13
- date: 2011-06-29 00:00:00 -04:00
20
+ date: 2011-06-30 00:00:00 -04:00
14
21
  default_executable:
15
22
  dependencies:
16
23
  - !ruby/object:Gem::Dependency
@@ -21,6 +28,9 @@ dependencies:
21
28
  requirements:
22
29
  - - ">="
23
30
  - !ruby/object:Gem::Version
31
+ hash: 3
32
+ segments:
33
+ - 0
24
34
  version: "0"
25
35
  type: :development
26
36
  version_requirements: *id001
@@ -32,6 +42,9 @@ dependencies:
32
42
  requirements:
33
43
  - - ">="
34
44
  - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
35
48
  version: "0"
36
49
  type: :development
37
50
  version_requirements: *id002
@@ -43,6 +56,9 @@ dependencies:
43
56
  requirements:
44
57
  - - ">="
45
58
  - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
46
62
  version: "0"
47
63
  type: :development
48
64
  version_requirements: *id003
@@ -54,6 +70,11 @@ dependencies:
54
70
  requirements:
55
71
  - - ~>
56
72
  - !ruby/object:Gem::Version
73
+ hash: 9
74
+ segments:
75
+ - 2
76
+ - 3
77
+ - 5
57
78
  version: 2.3.5
58
79
  type: :development
59
80
  version_requirements: *id004
@@ -65,6 +86,13 @@ dependencies:
65
86
  requirements:
66
87
  - - ~>
67
88
  - !ruby/object:Gem::Version
89
+ hash: 62196421
90
+ segments:
91
+ - 3
92
+ - 0
93
+ - 0
94
+ - beta
95
+ - 3
68
96
  version: 3.0.0.beta3
69
97
  type: :development
70
98
  version_requirements: *id005
@@ -76,6 +104,9 @@ dependencies:
76
104
  requirements:
77
105
  - - ">="
78
106
  - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
79
110
  version: "0"
80
111
  type: :development
81
112
  version_requirements: *id006
@@ -87,6 +118,9 @@ dependencies:
87
118
  requirements:
88
119
  - - ">="
89
120
  - !ruby/object:Gem::Version
121
+ hash: 3
122
+ segments:
123
+ - 0
90
124
  version: "0"
91
125
  type: :development
92
126
  version_requirements: *id007
@@ -201,12 +235,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
201
235
  requirements:
202
236
  - - ">="
203
237
  - !ruby/object:Gem::Version
238
+ hash: 3
239
+ segments:
240
+ - 0
204
241
  version: "0"
205
242
  required_rubygems_version: !ruby/object:Gem::Requirement
206
243
  none: false
207
244
  requirements:
208
245
  - - ">"
209
246
  - !ruby/object:Gem::Version
247
+ hash: 25
248
+ segments:
249
+ - 1
250
+ - 3
251
+ - 1
210
252
  version: 1.3.1
211
253
  requirements: []
212
254