Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/factory_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ def self.register_sequence(sequence)
sequence
end

def self.register_sequence_uuid(sequence)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather not add another public method for this. It would be nice if there was a way we could reuse register_sequence for this. Let's think about it a bit longer.

return unless sequence
sequence.uuids.each do |uuid|
sequences.register(uuid, sequence)
end
sequence
end

def self.sequence_by_name(name)
sequences.find(name)
end
Expand Down
1 change: 1 addition & 0 deletions lib/factory_bot/definition_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def method_missing(name, *args, &block)
# Except that no globally available sequence will be defined.
def sequence(name, *args, &block)
sequence = Sequence.new(name, *args, &block)
Copy link
Collaborator

@composerinteralia composerinteralia Jun 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It occurs to me that the name may not be important for these inline sequences. Could we just use some unique identifier as the name and register with register_sequence?

Thinking out loud here. What if we had an InlineSequence class or something like that whose names method just returned [object_id.to_s]?

FactoryBot.register_sequence_uuid(sequence)
add_attribute(name) { increment_sequence(sequence) }
end

Expand Down
7 changes: 7 additions & 0 deletions lib/factory_bot/sequence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def initialize(name, *args, &proc)
end
end

def uuids
proc = @proc || -> {}
names.map do |name|
[name, proc.try(:to_s)].join
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_s on a proc would look something like #<Proc:0x00007fc64bcd2b48>, correct? I don't love that. Is there something else we could use? Maybe an actual uuid? Or some other solution what we haven't thought of yet?

end
end

def next(scope = nil)
if @proc && scope
scope.instance_exec(value, &@proc)
Expand Down
89 changes: 89 additions & 0 deletions spec/acceptance/sequence_resetting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
describe "FactoryBot.rewind_sequences" do
include FactoryBot::Syntax::Methods

before do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put this setup right in the test rather than a before block, since the existing test doesn't use it.

define_model("User", age: :integer)
FactoryBot.define do
factory :user do
sequence(:id)
sequence(:age)
end
end

define_model("Project", number: :integer)
FactoryBot.define do
factory :project do
sequence(:id)
sequence(:number)
end
end
end

it "resets all sequences back to their starting values" do
FactoryBot.define do
sequence(:email) { |n| "somebody#{n}@example.com" }
Expand All @@ -22,4 +40,75 @@
expect(email).to eq "[email protected]"
expect(name).to eq "Joe"
end

it "resets sequences back to their starting values for factory sequences" do
user1 = FactoryBot.create(:user)
user2 = FactoryBot.create(:user)

expect(user1.id).to eq 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get rid of some of these expects. They are duplicating some of the other sequence tests.

I am imagining something more similar to the existing test:

it "resets inline sequences back to their starting value" do
  class User
    attr_accessor :email
  end

  FactoryBot.define do
    factory :user do
      sequence(:email) { |n| "somebody#{n}@example.com" }
    end
  end

  2.times do
    build(:user)
  end

  FactoryBot.rewind_sequences

  user = build(:user)
 
  expect(user.email).to eq "[email protected]"
end

expect(user1.age).to eq 1

expect(user2.id).to eq 2
expect(user2.age).to eq 2

User.destroy_all
FactoryBot.rewind_sequences

user1 = FactoryBot.create(:user)
user2 = FactoryBot.create(:user)
user3 = FactoryBot.create(:user)

expect(user1.id).to eq 1
expect(user1.age).to eq 1

expect(user2.id).to eq 2
expect(user2.age).to eq 2

expect(user3.id).to eq 3
expect(user3.age).to eq 3
end

it "resets without conflict with other factories" do
# Create users
user1 = FactoryBot.create(:user)
user2 = FactoryBot.create(:user)

expect(user1.id).to eq 1
expect(user1.age).to eq 1

expect(user2.id).to eq 2
expect(user2.age).to eq 2

# Create projects
project1 = FactoryBot.create(:project)
project2 = FactoryBot.create(:project)

expect(project1.id).to eq 1
expect(project1.number).to eq 1

expect(project2.id).to eq 2
expect(project2.number).to eq 2

User.destroy_all
Project.destroy_all
FactoryBot.rewind_sequences

user1 = FactoryBot.create(:user)
user2 = FactoryBot.create(:user)
expect(user1.id).to eq 1
expect(user1.age).to eq 1

expect(user2.id).to eq 2
expect(user2.age).to eq 2

# Create projects
project1 = FactoryBot.create(:project)
project2 = FactoryBot.create(:project)

expect(project1.id).to eq 1
expect(project1.number).to eq 1

expect(project2.id).to eq 2
expect(project2.number).to eq 2
end
end