-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[1452] update traits_for_enum to accept _prefix and _suffix #1513
base: master
Are you sure you want to change the base?
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 |
---|---|---|
@@ -1,9 +1,20 @@ | ||
module FactoryBot | ||
# @api private | ||
class Enum | ||
def initialize(attribute_name, values = nil) | ||
def initialize(attribute_name, values = nil, prefix: false, suffix: false) | ||
@attribute_name = attribute_name | ||
@values = values | ||
@prefix = if prefix == true | ||
"#{@attribute_name}_" | ||
elsif prefix | ||
"#{prefix}_" | ||
end | ||
|
||
@suffix = if suffix == true | ||
"_#{@attribute_name}" | ||
elsif suffix | ||
"_#{suffix}" | ||
end | ||
end | ||
|
||
def build_traits(klass) | ||
|
@@ -12,14 +23,16 @@ def build_traits(klass) | |
end | ||
end | ||
|
||
attr_reader :attribute_name | ||
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. Are we using this anywhere? 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. This was being used in 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. this is now also being used in the registered_enums hash as the key to look up to ensure enums are not registered twice |
||
|
||
private | ||
|
||
def enum_values(klass) | ||
@values || klass.send(@attribute_name.to_s.pluralize) | ||
end | ||
|
||
def build_trait(trait_name, attribute_name, value) | ||
Trait.new(trait_name) do | ||
Trait.new("#{@prefix}#{trait_name}#{@suffix}") do | ||
add_attribute(attribute_name) { value } | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,211 @@ def each(&block) | |
expect(task.status).to eq(trait_name) | ||
end | ||
end | ||
|
||
context 'when prefix is used' do | ||
context 'when values are a hash' 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. Do prefix and suffixes interact differently with a hash vs. list, or is there something specific we're trying to cover by testing against both? If not, I think we could choose one or the other to test with, since we've already got coverage of the various enumerable types. 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. With the current form of the code, no they don't behave differently, however with hash being passed in one of the tests (enum_for_trait_spec.rb:60) was failing as the keynames of the hash didn't match with the prefix, suffix options, so i chose to leave the 2 different tests in (for now at least) to ensure i pushed a solution that worked for all implementation types of the api |
||
context 'when prefix is a boolean' do | ||
it "builds traits for each enumerated value, applies default prefix and does not build the auto enum trait" do | ||
statuses = { queued: 0, started: 1, finished: 2 } | ||
define_model("Task", status: :integer) do | ||
enum status: statuses, _prefix: true | ||
end | ||
|
||
FactoryBot.define do | ||
factory :task do | ||
traits_for_enum :status, _prefix: true | ||
end | ||
end | ||
|
||
statuses.each_key do |trait_name| | ||
task = FactoryBot.build(:task, "status_#{trait_name}") | ||
|
||
expect{ FactoryBot.build(:task, trait_name) }.to raise_error KeyError, /Trait not registered/ | ||
expect(task.status).to eq(trait_name.to_s) | ||
end | ||
|
||
Task.reset_column_information | ||
end | ||
end | ||
|
||
context 'when prefix is a string' do | ||
it "builds traits for each enumerated value, applies custom prefix and does not build the auto enum trait" do | ||
statuses = { queued: 0, started: 1, finished: 2 } | ||
define_model("Task", status: :integer) do | ||
enum status: statuses, _prefix: 'foobar' | ||
end | ||
|
||
FactoryBot.define do | ||
factory :task do | ||
traits_for_enum :status, _prefix: 'foobar' | ||
end | ||
end | ||
|
||
statuses.each_key do |trait_name| | ||
task = FactoryBot.build(:task, "foobar_#{trait_name}") | ||
|
||
expect{ FactoryBot.build(:task, trait_name) }.to raise_error KeyError, /Trait not registered/ | ||
expect(task.status).to eq(trait_name.to_s) | ||
end | ||
|
||
Task.reset_column_information | ||
end | ||
end | ||
end | ||
|
||
context 'when values are a list' do | ||
context 'when prefix is a boolean' do | ||
it "builds traits for each enumerated value, applies default prefix and does not build the auto enum trait" do | ||
statuses = %w[queued started finished] | ||
|
||
define_model("Task", status: :integer) do | ||
enum status: statuses, _prefix: true | ||
end | ||
|
||
FactoryBot.define do | ||
factory :task do | ||
traits_for_enum :status, _prefix: true | ||
end | ||
end | ||
|
||
statuses.each do |trait_name| | ||
task = FactoryBot.build(:task, "status_#{trait_name}") | ||
|
||
expect{ FactoryBot.build(:task, trait_name) }.to raise_error KeyError, /Trait not registered/ | ||
expect(task.status).to eq(trait_name) | ||
end | ||
|
||
Task.reset_column_information | ||
end | ||
end | ||
|
||
context 'when prefix is a string' do | ||
it "builds traits for each enumerated value, applies custom prefix and does not build the auto enum trait" do | ||
statuses = %w[queued started finished] | ||
define_model("Task", status: :integer) do | ||
enum status: statuses, _prefix: 'foobar' | ||
end | ||
|
||
FactoryBot.define do | ||
factory :task do | ||
traits_for_enum :status, _prefix: 'foobar' | ||
end | ||
end | ||
|
||
statuses.each do |trait_name| | ||
task = FactoryBot.build(:task, "foobar_#{trait_name}") | ||
|
||
expect{ FactoryBot.build(:task, trait_name) }.to raise_error KeyError, /Trait not registered/ | ||
expect(task.status).to eq(trait_name) | ||
end | ||
|
||
Task.reset_column_information | ||
end | ||
end | ||
end | ||
end | ||
|
||
context 'when suffix is used' do | ||
context 'when values are a hash' do | ||
context 'when suffix is a boolean' do | ||
it "builds traits for each enumerated value, applies default suffix and does not build the auto enum trait" do | ||
statuses = { queued: 0, started: 1, finished: 2 } | ||
define_model("Task", status: :integer) do | ||
enum status: statuses, _suffix: true | ||
end | ||
|
||
FactoryBot.define do | ||
factory :task do | ||
traits_for_enum :status, _suffix: true | ||
end | ||
end | ||
|
||
statuses.each_key do |trait_name| | ||
task = FactoryBot.build(:task, "#{trait_name}_status") | ||
|
||
expect{ FactoryBot.build(:task, trait_name) }.to raise_error KeyError, /Trait not registered/ | ||
expect(task.status).to eq(trait_name.to_s) | ||
end | ||
|
||
Task.reset_column_information | ||
end | ||
end | ||
|
||
context 'when suffix is a string' do | ||
it "builds traits for each enumerated value, applies custom suffix and does not build the auto enum trait" do | ||
statuses = {queued: 0, started: 1, finished: 2} | ||
define_model("Task", status: :integer) do | ||
enum status: statuses, _suffix: 'foobar' | ||
end | ||
|
||
FactoryBot.define do | ||
factory :task do | ||
traits_for_enum :status, _suffix: 'foobar' | ||
end | ||
end | ||
|
||
statuses.each do |trait_name, _trait_value| | ||
task = FactoryBot.build(:task, "#{trait_name}_foobar") | ||
|
||
expect{ FactoryBot.build(:task, trait_name) }.to raise_error KeyError, /Trait not registered/ | ||
expect(task.status).to eq(trait_name.to_s) | ||
end | ||
|
||
Task.reset_column_information | ||
end | ||
end | ||
end | ||
|
||
context 'when values are a list' do | ||
context 'when suffix is a boolean' do | ||
it "builds traits for each enumerated value, applies default suffix and does not build the auto enum trait" do | ||
statuses = %w[queued started finished] | ||
define_model("Task", status: :integer) do | ||
enum status: statuses, _suffix: true | ||
end | ||
|
||
FactoryBot.define do | ||
factory :task do | ||
traits_for_enum :status, _suffix: true | ||
end | ||
end | ||
|
||
statuses.each do |trait_name| | ||
task = FactoryBot.build(:task, "#{trait_name}_status") | ||
|
||
expect{ FactoryBot.build(:task, trait_name) }.to raise_error KeyError, /Trait not registered/ | ||
expect(task.status).to eq(trait_name) | ||
end | ||
|
||
Task.reset_column_information | ||
end | ||
end | ||
|
||
context 'when suffix is a string' do | ||
it "builds traits for each enumerated value, applies custom suffix and does not build the auto enum trait" do | ||
statuses = %w[queued started finished] | ||
define_model("Task", status: :integer) do | ||
enum status: statuses, _suffix: 'foobar' | ||
end | ||
|
||
FactoryBot.define do | ||
factory :task do | ||
traits_for_enum :status, _suffix: 'foobar' | ||
end | ||
end | ||
|
||
statuses.each do |trait_name| | ||
task = FactoryBot.build(:task, "#{trait_name}_foobar") | ||
|
||
expect(task.status).to eq(trait_name) | ||
expect{ FactoryBot.build(:task, trait_name) }.to raise_error KeyError, /Trait not registered/ | ||
end | ||
|
||
Task.reset_column_information | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
context "when automatically_define_enum_traits is false" do | ||
|
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.
Might be worth pointing out we would need to update the docs here if we end up using the signature without the hash arguments.