Skip to content

Commit 9c59052

Browse files
reproducing with unit tests
1 parent 5602d59 commit 9c59052

1 file changed

Lines changed: 89 additions & 2 deletions

File tree

spec/unit/shoulda/matchers/active_record/define_enum_for_matcher_spec.rb

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,92 @@ def self.statuses
528528
end
529529
end
530530
end
531+
532+
it 'matches when there are different attributes with the same enum values with different prefixes' do
533+
STATUSES = { pending: 'pending', denied: 'denied', agreed: 'agreed' }
534+
535+
record = build_record_with_hash_values(
536+
model_name: 'ContactSuggestion',
537+
attribute_name: :candidate_user_status,
538+
column_type: :string,
539+
values: STATUSES,
540+
prefix: :user
541+
)
542+
543+
another_record = build_record_with_hash_values(
544+
model_name: 'ContactSuggestion',
545+
attribute_name: :candidate_contact_status,
546+
column_type: :string,
547+
values: STATUSES,
548+
prefix: :contact
549+
)
550+
551+
user_matcher = define_enum_for(:candidate_user_status).
552+
backed_by_column_of_type(:string).
553+
with_values(STATUSES).
554+
with_prefix(:user)
555+
556+
contact_matcher = define_enum_for(:candidate_contact_status).
557+
backed_by_column_of_type(:string).
558+
with_values(STATUSES).
559+
with_prefix(:contact)
560+
561+
expect(user_matcher.description).to eq(<<~MESSAGE.strip)
562+
define :candidate_user_status as an enum backed by a string with values ‹{pending: \"pending\", denied: \"denied\", agreed: \"agreed\"}›, prefix: :user
563+
MESSAGE
564+
565+
expect(contact_matcher.description).to eq(<<~MESSAGE.strip)
566+
define :candidate_contact_status as an enum backed by a string with values ‹{pending: \"pending\", denied: \"denied\", agreed: \"agreed\"}›, prefix: :contact
567+
MESSAGE
568+
569+
# this fails, can't reproduce
570+
expect(contact_matcher.description).to eq(<<~MESSAGE.strip)
571+
define :candidate_contact_status as an enum backed by a string with values ‹{pending: \"pending\", denied: \"denied\", agreed: \"agreed\"}›, prefix: :user
572+
MESSAGE
573+
end
574+
575+
it 'fails when there are different attributes with similar enum values with different prefixes' do
576+
USER_STATUSES = { pending: 'pending', denied: 'denied', agreed: 'agreed' }
577+
CANDIDATE_STATUSES = { a_differente_value:"testing", pending: 'pending', denied: 'denied', agreed: 'agreed' }
578+
579+
record = build_record_with_hash_values(
580+
model_name: 'ContactSuggestion',
581+
attribute_name: :candidate_user_status,
582+
column_type: :string,
583+
values: USER_STATUSES,
584+
prefix: :user
585+
)
586+
587+
another_record = build_record_with_hash_values(
588+
model_name: 'ContactSuggestion',
589+
attribute_name: :candidate_contact_status,
590+
column_type: :string,
591+
values: CANDIDATE_STATUSES,
592+
prefix: :contact
593+
)
594+
595+
user_matcher = define_enum_for(:candidate_user_status).
596+
backed_by_column_of_type(:string).
597+
with_values(USER_STATUSES).
598+
with_prefix(:user)
599+
600+
contact_matcher = define_enum_for(:candidate_contact_status).
601+
backed_by_column_of_type(:string).
602+
with_values(CANDIDATE_STATUSES).
603+
with_prefix(:contact)
604+
605+
expect(user_matcher.description).to eq(<<~MESSAGE.strip)
606+
define :candidate_user_status as an enum backed by a string with values ‹{pending: \"pending\", denied: \"denied\", agreed: \"agreed\"}›, prefix: :user
607+
MESSAGE
608+
609+
expect(contact_matcher.description).to eq(<<~MESSAGE.strip)
610+
define :candidate_contact_status as an enum backed by a string with values ‹{a_differente_value: \"testing\", pending: \"pending\", denied: \"denied\", agreed: \"agreed\"}›, prefix: :contact
611+
MESSAGE
612+
613+
expect(contact_matcher.description).to eq(<<~MESSAGE.strip)
614+
define :candidate_contact_status as an enum backed by a string with values ‹{a_differente_value: \"testing\", pending: \"pending\", denied: \"denied\", agreed: \"agreed\"}›, prefix: :user
615+
MESSAGE
616+
end
531617
end
532618

533619
context 'qualified with #with_suffix' do
@@ -1451,12 +1537,13 @@ def build_record_with_hash_values(
14511537
prefix: false,
14521538
suffix: false,
14531539
default: nil,
1454-
validate: false
1540+
validate: false,
1541+
column_type: :integer
14551542
)
14561543
build_record_with_enum_attribute(
14571544
model_name:,
14581545
attribute_name:,
1459-
column_type: :integer,
1546+
column_type:,
14601547
values:,
14611548
prefix:,
14621549
suffix:,

0 commit comments

Comments
 (0)