cucumber_factory 2.1.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,30 @@
1
+ module CucumberFactory
2
+
3
+ class UpdateStrategy
4
+
5
+ def initialize(record)
6
+ @record = record
7
+ end
8
+
9
+ def assign_attributes(attributes)
10
+ active_record_strategy(attributes) ||
11
+ ruby_object_strategy(attributes)
12
+ end
13
+
14
+ private
15
+
16
+ def active_record_strategy(attributes)
17
+ return unless @record.respond_to?(:save!)
18
+
19
+ CucumberFactory::Switcher.assign_attributes(@record, attributes)
20
+ @record.save!
21
+ end
22
+
23
+ def ruby_object_strategy(attributes)
24
+ attributes.each do |name, value|
25
+ @record.send("#{name}=".to_sym, value)
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module CucumberFactory
2
- VERSION = '2.1.0'
2
+ VERSION = '2.4.0'
3
3
  end
@@ -0,0 +1 @@
1
+ This is a test file.
@@ -0,0 +1 @@
1
+ This is the second test file.
@@ -0,0 +1 @@
1
+ spec/assets/file.txt
@@ -111,7 +111,7 @@ describe 'steps provided by cucumber_factory' do
111
111
  payment.id.should == 2
112
112
  end
113
113
 
114
- it "should allow to name records and set a belongs_to association to that record by refering to that name" do
114
+ it "should allow to name records and set a belongs_to association to that record by referring to that name" do
115
115
  invoke_cucumber_step('"Some Prequel" is a movie with the title "Before Sunrise"')
116
116
  invoke_cucumber_step('there is a movie with the title "Limitless"')
117
117
  invoke_cucumber_step('there is a movie with the title "Before Sunset" and the prequel "Some Prequel"')
@@ -120,7 +120,7 @@ describe 'steps provided by cucumber_factory' do
120
120
  movie.prequel.should == prequel
121
121
  end
122
122
 
123
- it "should allow to set a belongs_to association to a previously created record by refering to any string attribute of that record" do
123
+ it "should allow to set a belongs_to association to a previously created record by referring to any string attribute of that record" do
124
124
  invoke_cucumber_step('there is a movie with the title "Before Sunrise"')
125
125
  invoke_cucumber_step('there is a movie with the title "Limitless"')
126
126
  invoke_cucumber_step('there is a movie with the title "Before Sunset" and the prequel "Before Sunrise"')
@@ -129,7 +129,17 @@ describe 'steps provided by cucumber_factory' do
129
129
  movie.prequel.should == prequel
130
130
  end
131
131
 
132
- it "should allow to set a belongs_to association to a previously created record by refering to their explicitely set primary keys" do
132
+ it "should allow to set a belongs_to association to a previously updated record by referring to any string attribute used when updating" do
133
+ invoke_cucumber_step('there is a movie')
134
+ invoke_cucumber_step('the movie above has the title "Before Sunrise"')
135
+ invoke_cucumber_step('there is a movie with the title "Limitless"')
136
+ invoke_cucumber_step('there is a movie with the title "Before Sunset" and the prequel "Before Sunrise"')
137
+ movie = Movie.find_by_title!('Before Sunset')
138
+ prequel = Movie.find_by_title!('Before Sunrise')
139
+ movie.prequel.should == prequel
140
+ end
141
+
142
+ it "should allow to set a belongs_to association to a previously created record by referring to their explicitely set primary keys" do
133
143
  invoke_cucumber_step('there is a movie with the ID 123')
134
144
  invoke_cucumber_step('there is a movie with the title "Before Sunset" and the prequel 123')
135
145
  movie = Movie.find_by_title!('Before Sunset')
@@ -165,7 +175,10 @@ describe 'steps provided by cucumber_factory' do
165
175
  it "should give created_at precedence over id when saying 'above' if the primary key is not numeric" do
166
176
  invoke_cucumber_step('there is a uuid user with the name "Jane" and the id "jane"')
167
177
  invoke_cucumber_step('there is a uuid user with the name "John" and the id "john"')
168
- UuidUser.find_by_name("John").update_attributes!(:created_at => 1.day.ago)
178
+
179
+ uuid_user = UuidUser.find_by_name("John")
180
+ ActiveRecord::VERSION::MAJOR >= 6 ? uuid_user.update!(:created_at => 1.day.ago) : uuid_user.update_attributes!(:created_at => 1.day.ago)
181
+
169
182
  invoke_cucumber_step('there is a movie with the title "Before Sunset" and the uuid reviewer above')
170
183
  before_sunset = Movie.find_by_title!("Before Sunset")
171
184
  before_sunset.uuid_reviewer.name.should == "Jane"
@@ -174,7 +187,10 @@ describe 'steps provided by cucumber_factory' do
174
187
  it "should ignore created_at if the primary key is numeric" do
175
188
  invoke_cucumber_step('there is a user with the name "Jane"')
176
189
  invoke_cucumber_step('there is a user with the name "John"')
177
- User.find_by_name("John").update_attributes!(:created_at => 1.day.ago)
190
+
191
+ user = User.find_by_name("John")
192
+ ActiveRecord::VERSION::MAJOR >= 6 ? user.update!(:created_at => 1.day.ago) : user.update_attributes!(:created_at => 1.day.ago)
193
+
178
194
  invoke_cucumber_step('there is a movie with the title "Before Sunset" and the reviewer above')
179
195
  before_sunset = Movie.find_by_title!("Before Sunset")
180
196
  before_sunset.reviewer.name.should == "John"
@@ -188,7 +204,10 @@ describe 'steps provided by cucumber_factory' do
188
204
 
189
205
  it "should reload an object assigned to a belongs_to before assigning" do
190
206
  invoke_cucumber_step('"Jane" is a user who is deleted')
191
- User.last.update_attributes(:deleted => false)
207
+
208
+ user = User.last
209
+ ActiveRecord::VERSION::MAJOR >= 6 ? user.update!(:deleted => false) : user.update_attributes!(:deleted => false)
210
+
192
211
  proc { invoke_cucumber_step('there is a movie with the title "Before Sunset" and the reviewer "Jane"') }.should_not raise_error
193
212
  end
194
213
 
@@ -200,6 +219,15 @@ describe 'steps provided by cucumber_factory' do
200
219
  user.deleted.should == true
201
220
  end
202
221
 
222
+ it "should allow to name records and set a belongs_to association to that record by referring to that name" do
223
+ invoke_cucumber_step('"Some Prequel" is a movie with the title "Before Sunrise"')
224
+ invoke_cucumber_step('there is a movie with the title "Limitless"')
225
+ invoke_cucumber_step('there is a movie with the title "Before Sunset" and the prequel "Some Prequel"')
226
+ movie = Movie.find_by_title!('Before Sunset')
227
+ prequel = Movie.find_by_title!('Before Sunrise')
228
+ movie.prequel.should == prequel
229
+ end
230
+
203
231
  it "should allow to set positive boolean attributes with 'which' after the attribute list" do
204
232
  user = User.new
205
233
  User.stub(:new => user)
@@ -259,7 +287,7 @@ describe 'steps provided by cucumber_factory' do
259
287
  user.deleted.should == true
260
288
  end
261
289
 
262
- it "should allow to set a has_many association by refering to multiple named records in square brackets" do
290
+ it "should allow to set a has_many association by referring to multiple named records in square brackets" do
263
291
  invoke_cucumber_step('there is a movie with the title "Sunshine"')
264
292
  invoke_cucumber_step('there is a movie with the title "Limitless"')
265
293
  invoke_cucumber_step('there is a user with the reviewed movies ["Sunshine" and "Limitless"]')
@@ -356,6 +384,88 @@ title: Before Sunrise
356
384
  prequel = Movie.find_by_title!('Before Sunrise')
357
385
  movie.prequel.should == prequel
358
386
  end
387
+
388
+ it 'supports named associations with polymorphic associations' do
389
+ invoke_cucumber_step('"my opera" is an opera')
390
+ invoke_cucumber_step('there is a movie with the premiere site "my opera"')
391
+ end
392
+
393
+ it 'does not support last record references with polymorphic associations as the target class cannot be guessed' do
394
+ invoke_cucumber_step('there is an opera')
395
+ expect {
396
+ invoke_cucumber_step('there is a movie with the premiere site above')
397
+ }.to raise_error(CucumberFactory::Factory::Error, 'Cannot set last Movie#premiere_site for polymorphic associations')
398
+ end
399
+
400
+ it 'supports carrierwave uploaders' do
401
+ invoke_cucumber_step('there is a payment with the attachment file:"spec/assets/file.txt"')
402
+ payment = Payment.last
403
+ expect(payment.attachment.file.read).to eq "This is a test file.\n"
404
+ end
405
+
406
+ it 'supports single quote for carrierwave uploaders' do
407
+ invoke_cucumber_step("there is a payment with the attachment file:'spec/assets/file.txt'")
408
+ payment = Payment.last
409
+ expect(payment.attachment.file.read).to eq "This is a test file.\n"
410
+ end
411
+
412
+ it 'is able to read symlinked files' do
413
+ invoke_cucumber_step("there is a payment with the attachment file:'spec/assets/symlink.txt'")
414
+ payment = Payment.last
415
+ expect(payment.attachment.file.read).to eq "This is a test file.\n"
416
+ end
417
+
418
+ it 'supports table syntax for carrierwave uploaders' do
419
+ invoke_cucumber_step('there is a payment with these attributes:', nil, <<-DATA_TABLE)
420
+ | attachment | file:"spec/assets/file.txt" |
421
+ DATA_TABLE
422
+ payment = Payment.last
423
+ expect(payment.attachment.file.read).to eq "This is a test file.\n"
424
+ end
425
+
426
+ it 'supports doc string syntax for carrierwave uploaders' do
427
+ invoke_cucumber_step('there is a payment with these attributes:', <<-DOC_STRING)
428
+ attachment: file:"spec/assets/file.txt"
429
+ DOC_STRING
430
+ payment = Payment.last
431
+ payment.save!
432
+ expect(payment.attachment.file.read).to eq "This is a test file.\n"
433
+ end
434
+
435
+ it 'allows updating a carrierwave attribute' do
436
+ invoke_cucumber_step('there is a payment with the attachment file:"spec/assets/file.txt"')
437
+ payment = Payment.last
438
+ expect(payment.attachment.file.read).to eq "This is a test file.\n"
439
+ invoke_cucumber_step('the payment above has the attachment file:"spec/assets/file2.txt"')
440
+ payment.reload
441
+ expect(payment.attachment.file.read).to eq "This is the second test file.\n"
442
+ end
443
+
444
+ it 'works with nested factories (BUGFIX)' do
445
+ invoke_cucumber_step('there is a subgenre movie')
446
+ end
447
+
448
+ describe 'references to named records are tracked correctly' do
449
+ # CucumberFactory keeps track of all created records by their attributes.
450
+ # For example, if you create a user with the name "Peter" you can reference him later on as "Peter".
451
+
452
+ it 'does not overwrite existing references when associating new records with another reference (BUGFIX)' do
453
+ invoke_cucumber_step('"reviewer" is a user with the id "1"')
454
+ movie1 = invoke_cucumber_step('there is a movie with the reviewer "reviewer" and the id "2"') # <- should not overwrite the 'reviewer' reference
455
+ movie2 = invoke_cucumber_step('there is a movie with the reviewer "reviewer" and the id "3"')
456
+
457
+ expect(movie1.reviewer_id).to eq(1)
458
+ expect(movie2.reviewer_id).to eq(1)
459
+ end
460
+
461
+ it 'does not set new references when setting primary keys of an association (BUGFIX)' do
462
+ movie1 = invoke_cucumber_step('there is a movie with the id "123" and the user id "456"') # <- should not set a '456' reference
463
+ movie2 = invoke_cucumber_step('there is a movie with the user id "456"')
464
+
465
+ expect(movie1.reviewer_id).to eq(456)
466
+ expect(movie2.reviewer_id).to eq(456)
467
+ end
468
+ end
359
469
  end
360
470
 
361
471
  context 'without FactoryBot' do
@@ -390,6 +500,13 @@ title: Before Sunrise
390
500
  obj.attributes[:total].to_s.should == "45.6"
391
501
  end
392
502
 
503
+ it "should allow to set file attributes with the file:'path' syntax" do
504
+ invoke_cucumber_step('there is a plain Ruby class with the file file:"spec/assets/file.txt"')
505
+ obj = PlainRubyClass.last
506
+ obj.attributes[:file].should be_a(File)
507
+ obj.attributes[:file].read.should == "This is a test file.\n"
508
+ end
509
+
393
510
  it "should allow set an array of strings with square brackets" do
394
511
  invoke_cucumber_step('there is a plain Ruby class with the tags ["foo", "bar"] and the list ["bam", "baz"]')
395
512
  obj = PlainRubyClass.last
@@ -498,5 +615,54 @@ tags: ["foo", "bar"]
498
615
  lambda { invoke_cucumber_step("there is a machinist model with the attribute 'foo' and the ") }.should raise_error(ArgumentError, 'Unable to parse attributes " and the ".')
499
616
  lambda { invoke_cucumber_step("there is a machinist model with the attribute 'foo'. the other_attribute 'bar' and the third attribute 'baz'") }.should raise_error(ArgumentError, 'Unable to parse attributes ".".')
500
617
  end
618
+
619
+ it "should allow to update the last record of a type" do
620
+ invoke_cucumber_step('there is a user with the name "Bar" and the email "foo@example.com" who is not subscribed')
621
+ invoke_cucumber_step('the user above has the name "Baz", the email "foobar@example.com", and is subscribed')
622
+ user = User.last
623
+ user.name.should == 'Baz'
624
+ user.email.should == 'foobar@example.com'
625
+ user.subscribed.should == true
626
+ end
627
+
628
+ it "should be able to update a record with a given name" do
629
+ invoke_cucumber_step('"Foo" is a user with the name "Bar"')
630
+ invoke_cucumber_step('the user "Foo" has the email "foo@example.com"')
631
+ invoke_cucumber_step("the user 'Foo' is subscribed")
632
+ user = User.last
633
+ user.name.should == 'Bar'
634
+ user.email.should == 'foo@example.com'
635
+ user.subscribed.should == true
636
+ end
637
+
638
+ it "should be able to update a record with a Cucumber table expression" do
639
+ invoke_cucumber_step('"Foo" is a user with the name "Bar"')
640
+ invoke_cucumber_step('the user above has these attributes:', nil, <<-DATA_TABLE)
641
+ | name | Jane |
642
+ | locked | true |
643
+ DATA_TABLE
644
+ user = User.last
645
+ user.name.should == "Jane"
646
+ user.locked.should == true
647
+ end
648
+
649
+ it "should allow to update a record via additional data table" do
650
+ invoke_cucumber_step('"Foo" is a user with the name "Bar"')
651
+ invoke_cucumber_step('the user above has the email "test@invalid.com", is subscribed, and has these attributes:', nil, <<-DATA_TABLE)
652
+ | name | Jane |
653
+ DATA_TABLE
654
+ user = User.last
655
+ user.name.should == "Jane"
656
+ user.email.should == "test@invalid.com"
657
+ user.subscribed.should == true
658
+ end
659
+
660
+ it "should allow to update an association" do
661
+ invoke_cucumber_step('there is a movie with the title "Before Sunrise"')
662
+ invoke_cucumber_step('there is a user with the name "John"')
663
+ invoke_cucumber_step('the movie above has the reviewer above')
664
+ movie = Movie.last
665
+ movie.reviewer.name.should == "John"
666
+ end
501
667
  end
502
668
  end
data/spec/spec_helper.rb CHANGED
@@ -3,9 +3,12 @@ $: << File.join(File.dirname(__FILE__), "/../../lib" )
3
3
  require 'cucumber_factory'
4
4
  require 'gemika'
5
5
  require 'factory_bot'
6
+ require 'carrierwave'
7
+ require 'carrierwave/orm/activerecord'
6
8
 
7
9
  ActiveRecord::Base.default_timezone = :local
8
10
 
11
+ Dir["#{File.dirname(__FILE__)}/support/uploaders/*.rb"].sort.each {|f| require f}
9
12
  Dir["#{File.dirname(__FILE__)}/support/models/*.rb"].sort.each {|f| require f}
10
13
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each {|f| require f}
11
14
  Dir["#{File.dirname(__FILE__)}/shared_examples/**/*.rb"].sort.each {|f| require f}
@@ -0,0 +1,6 @@
1
+ postgresql:
2
+ database: cucumber_factory_test
3
+ host: localhost
4
+ username: postgres
5
+ password: postgres
6
+ port: 5432
@@ -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|
@@ -30,6 +32,7 @@ Gemika::Database.new.rewrite_schema! do
30
32
  create_table :payments do |t|
31
33
  t.text :comment
32
34
  t.integer :amount
35
+ t.string :attachment
33
36
  end
34
37
 
35
38
  create_table :actors do |t|
@@ -1,5 +1,5 @@
1
- mysql:
1
+ postgres:
2
2
  database: cucumber_factory_test
3
3
  host: localhost
4
- username: root
5
- password: secret
4
+ username: postgres
5
+ password: postgres
@@ -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
@@ -1,8 +1,13 @@
1
- class Payment < ActiveRecord::Base
2
-
3
- if ActiveRecord::VERSION::MAJOR <= 3
4
- # Only the comment is accessible, amount isn't
5
- attr_accessible :comment
6
- end
7
-
8
- end
1
+ class Payment < ActiveRecord::Base
2
+ class AttachmentUploader < CarrierWave::Uploader::Base
3
+ storage :file
4
+ end
5
+
6
+ mount_uploader :attachment, AttachmentUploader
7
+
8
+ if ActiveRecord::VERSION::MAJOR <= 3
9
+ # Only the comment is accessible, amount isn't
10
+ attr_accessible :comment
11
+ end
12
+
13
+ end
@@ -0,0 +1,3 @@
1
+ class AttachmentUploader < CarrierWave::Uploader::Base
2
+ storage :file
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.4.0
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: 2021-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cucumber
@@ -73,10 +73,10 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
+ - ".github/workflows/test.yml"
76
77
  - ".gitignore"
77
78
  - ".rspec"
78
79
  - ".ruby-version"
79
- - ".travis.yml"
80
80
  - CHANGELOG.md
81
81
  - Gemfile
82
82
  - Gemfile.cucumber-1.3
@@ -87,6 +87,10 @@ files:
87
87
  - Gemfile.cucumber-3.0.lock
88
88
  - Gemfile.cucumber-3.1
89
89
  - Gemfile.cucumber-3.1.lock
90
+ - Gemfile.cucumber-4.1
91
+ - Gemfile.cucumber-4.1.lock
92
+ - Gemfile.cucumber-5.3
93
+ - Gemfile.cucumber-5.3.lock
90
94
  - Gemfile.lock
91
95
  - LICENSE
92
96
  - README.md
@@ -97,14 +101,18 @@ files:
97
101
  - lib/cucumber_factory/build_strategy.rb
98
102
  - lib/cucumber_factory/factory.rb
99
103
  - lib/cucumber_factory/switcher.rb
104
+ - lib/cucumber_factory/update_strategy.rb
100
105
  - lib/cucumber_factory/version.rb
106
+ - spec/assets/file.txt
107
+ - spec/assets/file2.txt
108
+ - spec/assets/symlink.txt
101
109
  - spec/cucumber_factory/factory/build_strategy_spec.rb
102
110
  - spec/cucumber_factory/steps_spec.rb
103
111
  - spec/spec_helper.rb
104
112
  - spec/support/cucumber_helper.rb
113
+ - spec/support/database.github.yml
105
114
  - spec/support/database.rb
106
115
  - spec/support/database.sample.yml
107
- - spec/support/database.travis.yml
108
116
  - spec/support/factories/factories.rb
109
117
  - spec/support/models/job_offer.rb
110
118
  - spec/support/models/machinist_model.rb
@@ -115,6 +123,7 @@ files:
115
123
  - spec/support/models/plain_ruby_class.rb
116
124
  - spec/support/models/user.rb
117
125
  - spec/support/models/uuid_user.rb
126
+ - spec/support/uploaders/attachment_uploader.rb
118
127
  homepage: http://github.com/makandra/cucumber_factory
119
128
  licenses:
120
129
  - MIT
@@ -134,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
143
  - !ruby/object:Gem::Version
135
144
  version: '0'
136
145
  requirements: []
137
- rubygems_version: 3.0.2
146
+ rubygems_version: 3.1.3
138
147
  signing_key:
139
148
  specification_version: 4
140
149
  summary: Create records from Cucumber features without writing step definitions.