|
59 | 59 | end
|
60 | 60 | end
|
61 | 61 |
|
62 |
| -describe "a stubbed instance with no primary key" do |
63 |
| - it "builds a stubbed instance" do |
64 |
| - using_model_without_pk do |
65 |
| - FactoryBot.define do |
66 |
| - factory :model_without_pk |
| 62 | +describe "overridden primary keys conventions" do |
| 63 | + describe "a stubbed instance with a uuid primary key" do |
| 64 | + it "builds a stubbed instance" do |
| 65 | + using_model("ModelWithUuid", primary_key: :uuid) do |
| 66 | + FactoryBot.define do |
| 67 | + factory :model_with_uuid |
| 68 | + end |
| 69 | + |
| 70 | + model = FactoryBot.build_stubbed(:model_with_uuid) |
| 71 | + expect(model).to be_truthy |
67 | 72 | end
|
| 73 | + end |
| 74 | + |
| 75 | + it "behaves like a persisted record" do |
| 76 | + using_model("ModelWithUuid", primary_key: :uuid) do |
| 77 | + FactoryBot.define do |
| 78 | + factory :model_with_uuid |
| 79 | + end |
68 | 80 |
|
69 |
| - model = FactoryBot.build_stubbed(:model_without_pk) |
70 |
| - expect(model).to be_truthy |
| 81 | + model = FactoryBot.build_stubbed(:model_with_uuid) |
| 82 | + expect(model).to be_persisted |
| 83 | + expect(model).not_to be_new_record |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + it "has a uuid primary key" do |
| 88 | + using_model("ModelWithUuid", primary_key: :uuid) do |
| 89 | + FactoryBot.define do |
| 90 | + factory :model_with_uuid |
| 91 | + end |
| 92 | + |
| 93 | + model = FactoryBot.build_stubbed(:model_with_uuid) |
| 94 | + expect(model.id).to be_a(String) |
| 95 | + end |
71 | 96 | end
|
72 | 97 | end
|
73 | 98 |
|
74 |
| - it "behaves like a persisted record" do |
75 |
| - using_model_without_pk do |
76 |
| - FactoryBot.define do |
77 |
| - factory :model_without_pk |
| 99 | + describe "a stubbed instance with no primary key" do |
| 100 | + it "builds a stubbed instance" do |
| 101 | + using_model("ModelWithoutPk", primary_key: false) do |
| 102 | + FactoryBot.define do |
| 103 | + factory :model_without_pk |
| 104 | + end |
| 105 | + |
| 106 | + model = FactoryBot.build_stubbed(:model_without_pk) |
| 107 | + expect(model).to be_truthy |
78 | 108 | end
|
| 109 | + end |
79 | 110 |
|
80 |
| - model = FactoryBot.build_stubbed(:model_without_pk) |
81 |
| - expect(model).to be_persisted |
82 |
| - expect(model).not_to be_new_record |
| 111 | + it "behaves like a persisted record" do |
| 112 | + using_model("ModelWithoutPk", primary_key: false) do |
| 113 | + FactoryBot.define do |
| 114 | + factory :model_without_pk |
| 115 | + end |
| 116 | + |
| 117 | + model = FactoryBot.build_stubbed(:model_without_pk) |
| 118 | + expect(model).to be_persisted |
| 119 | + expect(model).not_to be_new_record |
| 120 | + end |
83 | 121 | end
|
84 | 122 | end
|
85 | 123 |
|
86 |
| - def using_model_without_pk |
87 |
| - define_class("ModelWithoutPk", ActiveRecord::Base) |
| 124 | + def using_model(name, primary_key:) |
| 125 | + define_class(name, ActiveRecord::Base) |
88 | 126 |
|
89 | 127 | connection = ActiveRecord::Base.connection
|
90 | 128 | begin
|
91 |
| - clear_generated_table("model_without_pks") |
92 |
| - connection.create_table("model_without_pks", id: false) do |t| |
| 129 | + clear_generated_table(name.tableize) |
| 130 | + connection.create_table(name.tableize, id: primary_key) do |t| |
93 | 131 | t.column :updated_at, :datetime
|
94 | 132 | end
|
95 | 133 |
|
96 | 134 | yield
|
97 | 135 | ensure
|
98 |
| - clear_generated_table("model_without_pks") |
| 136 | + clear_generated_table(name.tableize) |
99 | 137 | end
|
100 | 138 | end
|
101 | 139 | end
|
0 commit comments