cucumber_factory 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3153238ae9f4c30fd21e6dd493621e361d4bc6b508521effc93fbb4cb4be8af2
4
- data.tar.gz: c152087fd0231027a94abf15f2abff5c3b3857c714212f3c1eefa67902b32b3e
3
+ metadata.gz: baa05dc176d4705c737b2ad4f7257f01f0da4d0e07aae0513acc8248c7577550
4
+ data.tar.gz: 68ff25b1099b7eabb458e60e9cb95e8edb8ec7051befdd2c493353b7c23ef0f1
5
5
  SHA512:
6
- metadata.gz: 48b39f6ebaedb5bca521d07579384cbcffea01eb4638787de026ce683b1db2abc70fd91227f272cd88a75cb3a15cd8e780989b99a3429313aa6f9e0f6a8575c9
7
- data.tar.gz: 5b16cb2d4e73c1fde061b12e31a23944a3d6889316002805cd0f0bb7d0355cf520d01cb587c5a0ab58648633f83189ee2bba9597b2bda7217315db5fc48ca218
6
+ metadata.gz: 4ca06816ddcc668384556e40eef5466bb231dc9c4c458bb277f7296fd715d32e73d70af414d2e8c2cc8a9e5bd6157d59ec99c9e93ca737c5a2b015bfb1c82970
7
+ data.tar.gz: 293eabcf22252aca840ca43800d24f9e75a5a4cbf20aa6efbcfa52051782e2f3c5c6286166fc44c56decaad247972d7d487cc0fe08f7661527512ce2c80da7d9
@@ -15,6 +15,13 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
15
15
 
16
16
  -
17
17
 
18
+ ## 2.1.1 - 2020-05-20
19
+
20
+ ### Compatible changes
21
+
22
+ - Cucumber 2.1.0 introduced some regressions which are being addressed with this patch:
23
+ - Fix the assignment of polymorphic associations.
24
+ - Restore the support for inherited traits within nested factories.
18
25
 
19
26
  ## 2.1.0 - 2020-03-09
20
27
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cucumber_factory (2.1.0)
4
+ cucumber_factory (2.1.1)
5
5
  activerecord
6
6
  activesupport
7
7
  cucumber
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cucumber_factory (2.1.0)
4
+ cucumber_factory (2.1.1)
5
5
  activerecord
6
6
  activesupport
7
7
  cucumber
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cucumber_factory (2.1.0)
4
+ cucumber_factory (2.1.1)
5
5
  activerecord
6
6
  activesupport
7
7
  cucumber
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cucumber_factory (2.1.0)
4
+ cucumber_factory (2.1.1)
5
5
  activerecord
6
6
  activesupport
7
7
  cucumber
@@ -11,6 +11,7 @@ module CucumberFactory
11
11
  factory = factory_bot_factory(model_prose, variants)
12
12
 
13
13
  if factory
14
+ factory.compile # Required to load inherited traits!
14
15
  strategy = factory_bot_strategy(factory, model_prose, variants)
15
16
  transient_attributes = factory_bot_transient_attributes(factory, variants)
16
17
  else
@@ -146,14 +146,16 @@ module CucumberFactory
146
146
  end
147
147
 
148
148
  def attribute_value(world, model_class, transient_attributes, attribute, value)
149
- association_class = resolve_association_class(attribute, model_class, transient_attributes)
149
+ associated, association_class = resolve_association(attribute, model_class, transient_attributes)
150
150
 
151
151
  if matches_fully?(value, VALUE_ARRAY)
152
152
  elements_str = unquote(value)
153
153
  value = elements_str.scan(VALUE_SCALAR).map { |v| attribute_value(world, model_class, transient_attributes, attribute, v) }
154
- elsif association_class.present?
154
+ elsif associated
155
155
  if matches_fully?(value, VALUE_LAST_RECORD)
156
- value = CucumberFactory::Switcher.find_last(association_class) or raise Error, "There is no last #{attribute}"
156
+ raise(Error, "Cannot set last #{model_class}##{attribute} for polymorphic associations") unless association_class.present?
157
+
158
+ value = CucumberFactory::Switcher.find_last(association_class) || raise(Error, "There is no last #{attribute}")
157
159
  elsif matches_fully?(value, VALUE_STRING)
158
160
  value = unquote(value)
159
161
  value = get_named_record(world, value) || transform_value(world, value)
@@ -169,17 +171,25 @@ module CucumberFactory
169
171
  value
170
172
  end
171
173
 
172
- def resolve_association_class(attribute, model_class, transient_attributes)
174
+ def resolve_association(attribute, model_class, transient_attributes)
173
175
  return unless model_class.respond_to?(:reflect_on_association)
174
176
 
175
- klass = if model_class.reflect_on_association(attribute)
176
- model_class.reflect_on_association(attribute).klass
177
+ association = model_class.reflect_on_association(attribute)
178
+ association_class = nil
179
+
180
+ if association
181
+ association_class = association.klass unless association.polymorphic?
182
+ associated = true
177
183
  elsif transient_attributes.include?(attribute.to_sym)
178
184
  klass_name = attribute.to_s.camelize
179
- klass_name.constantize if Object.const_defined?(klass_name)
185
+ if Object.const_defined?(klass_name)
186
+ association_class = klass_name.constantize
187
+ associated = true
188
+ end
180
189
  else
181
- nil
190
+ associated = false
182
191
  end
192
+ [associated, association_class]
183
193
  end
184
194
 
185
195
  def resolve_scalar_value(world, model_class, attribute, value)
@@ -1,3 +1,3 @@
1
1
  module CucumberFactory
2
- VERSION = '2.1.0'
2
+ VERSION = '2.1.1'
3
3
  end
@@ -356,6 +356,44 @@ title: Before Sunrise
356
356
  prequel = Movie.find_by_title!('Before Sunrise')
357
357
  movie.prequel.should == prequel
358
358
  end
359
+
360
+ it 'supports named associations with polymorphic associations' do
361
+ invoke_cucumber_step('"my opera" is an opera')
362
+ invoke_cucumber_step('there is a movie with the premiere site "my opera"')
363
+ end
364
+
365
+ it 'does not support last record references with polymorphic associations as the target class cannot be guessed' do
366
+ invoke_cucumber_step('there is an opera')
367
+ expect {
368
+ invoke_cucumber_step('there is a movie with the premiere site above')
369
+ }.to raise_error(CucumberFactory::Factory::Error, 'Cannot set last Movie#premiere_site for polymorphic associations')
370
+ end
371
+
372
+ it 'works with nested factories (BUGFIX)' do
373
+ invoke_cucumber_step('there is a subgenre movie')
374
+ end
375
+
376
+ describe 'references to named records are tracked correctly' do
377
+ # CucumberFactory keeps track of all created records by their attributes.
378
+ # For example, if you create a user with the name "Peter" you can reference him later on as "Peter".
379
+
380
+ it 'does not overwrite existing references when associating new records with another reference (BUGFIX)' do
381
+ invoke_cucumber_step('"reviewer" is a user with the id "1"')
382
+ movie1 = invoke_cucumber_step('there is a movie with the reviewer "reviewer" and the id "2"') # <- should not overwrite the 'reviewer' reference
383
+ movie2 = invoke_cucumber_step('there is a movie with the reviewer "reviewer" and the id "3"')
384
+
385
+ expect(movie1.reviewer_id).to eq(1)
386
+ expect(movie2.reviewer_id).to eq(1)
387
+ end
388
+
389
+ it 'does not set new references when setting primary keys of an association (BUGFIX)' do
390
+ movie1 = invoke_cucumber_step('there is a movie with the id "123" and the user id "456"') # <- should not set a '456' reference
391
+ movie2 = invoke_cucumber_step('there is a movie with the user id "456"')
392
+
393
+ expect(movie1.reviewer_id).to eq(456)
394
+ expect(movie2.reviewer_id).to eq(456)
395
+ end
396
+ end
359
397
  end
360
398
 
361
399
  context 'without FactoryBot' do
@@ -7,6 +7,8 @@ Gemika::Database.new.rewrite_schema! do
7
7
  t.integer :reviewer_id
8
8
  t.string :uuid_reviewer_id
9
9
  t.integer :box_office_result
10
+ t.string :premiere_site_type
11
+ t.bigint :premiere_site_id
10
12
  end
11
13
 
12
14
  create_table :users do |t|
@@ -25,7 +25,21 @@ FactoryBot.define do
25
25
  end
26
26
  end
27
27
 
28
- factory :movie, :class => Movie
28
+ factory :movie, :class => Movie do
29
+ transient do
30
+ user { nil }
31
+ user_id { nil }
32
+ end
33
+
34
+ after(:build) do |movie, evaluator|
35
+ movie.reviewer = evaluator.user if evaluator.user
36
+ movie.reviewer_id = evaluator.user_id if evaluator.user_id
37
+ end
38
+
39
+ trait :parent_movie_trait
40
+
41
+ factory :subgenre_movie, traits: [:parent_movie_trait]
42
+ end
29
43
  factory :opera, :class => Opera
30
44
  factory :payment, :class => Payment
31
45
  factory :uuid_user, :class => UuidUser
@@ -3,6 +3,7 @@ class Movie < ActiveRecord::Base
3
3
  belongs_to :prequel, :class_name => "Movie"
4
4
  belongs_to :reviewer, :class_name => "User"
5
5
  belongs_to :uuid_reviewer, :class_name => "UuidUser"
6
+ belongs_to :premiere_site, polymorphic: true, required: false
6
7
 
7
8
  validate do |record|
8
9
  record.errors.add(:reviewer, 'may not be deleted') if record.reviewer and record.reviewer.deleted?
@@ -1,3 +1,3 @@
1
1
  class Opera < ActiveRecord::Base
2
-
2
+ has_many :movies, as: :premiere_site, inverse_of: :premiere_site
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cucumber_factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henning Koch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-31 00:00:00.000000000 Z
11
+ date: 2020-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cucumber
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  - !ruby/object:Gem::Version
135
135
  version: '0'
136
136
  requirements: []
137
- rubygems_version: 3.0.2
137
+ rubygems_version: 3.1.2
138
138
  signing_key:
139
139
  specification_version: 4
140
140
  summary: Create records from Cucumber features without writing step definitions.