|
1119 | 1119 | end |
1120 | 1120 | end |
1121 | 1121 | end |
1122 | | - |
1123 | | - describe "unique index on active subscriptions" do |
1124 | | - # We skip model validations in this test in order to check that database unique index is applied |
1125 | | - |
1126 | | - let(:organization) { create(:organization) } |
1127 | | - let(:external_id) { SecureRandom.uuid } |
1128 | | - |
1129 | | - context "when two new active subscriptions share the same (organization_id, external_id)" do |
1130 | | - before do |
1131 | | - create(:subscription, customer: create(:customer, organization:), external_id:) |
1132 | | - end |
1133 | | - |
1134 | | - it "raises a database uniqueness error" do |
1135 | | - duplicate_sub = build(:subscription, customer: create(:customer, organization:), external_id:) |
1136 | | - |
1137 | | - expect { duplicate_sub.save(validate: false) }.to raise_error(ActiveRecord::RecordNotUnique) |
1138 | | - end |
1139 | | - end |
1140 | | - |
1141 | | - context "when two old pending subscriptions created before the index cutoff are activated after deploy" do |
1142 | | - let(:sub_a) do |
1143 | | - build(:subscription, :pending, customer: create(:customer, organization:), external_id:, created_at: 2.years.ago) |
1144 | | - end |
1145 | | - let(:sub_b) do |
1146 | | - build(:subscription, :pending, customer: create(:customer, organization:), external_id:, created_at: 2.years.ago) |
1147 | | - end |
1148 | | - |
1149 | | - # rubocop:disable Rails/SkipsModelValidations |
1150 | | - it "raises a database uniqueness error on the second activation" do |
1151 | | - sub_a.save(validate: false) |
1152 | | - sub_b.save(validate: false) |
1153 | | - |
1154 | | - sub_a.update_columns(status: described_class.statuses[:active], activated_at: Time.current) |
1155 | | - |
1156 | | - expect { |
1157 | | - sub_b.update_columns(status: described_class.statuses[:active], activated_at: Time.current) |
1158 | | - }.to raise_error(ActiveRecord::RecordNotUnique) |
1159 | | - end |
1160 | | - # rubocop:enable Rails/SkipsModelValidations |
1161 | | - end |
1162 | | - end |
1163 | 1122 | end |
0 commit comments