-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Rewinding all sequences and not just the global ones #1078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It occurs to me that the Thinking out loud here. What if we had an |
||
FactoryBot.register_sequence_uuid(sequence) | ||
add_attribute(name) { increment_sequence(sequence) } | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
end | ||
end | ||
|
||
def next(scope = nil) | ||
if @proc && scope | ||
scope.instance_exec(value, &@proc) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,24 @@ | |
describe "FactoryBot.rewind_sequences" do | ||
include FactoryBot::Syntax::Methods | ||
|
||
before do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's put this setup right in the test rather than a |
||
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" } | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can get rid of some of these 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 |
There was a problem hiding this comment.
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.