|
| 1 | +require 'pact_broker/db/data_migrations/set_latest_version_sequence_value' |
| 2 | + |
| 3 | +module PactBroker |
| 4 | + module DB |
| 5 | + module DataMigrations |
| 6 | + describe SetLatestVersionSequenceValue, data_migration: true do |
| 7 | + include MigrationHelpers |
| 8 | + |
| 9 | + describe ".call" do |
| 10 | + before (:all) do |
| 11 | + PactBroker::Database.migrate(20190509) |
| 12 | + end |
| 13 | + |
| 14 | + let(:now) { DateTime.new(2018, 2, 2) } |
| 15 | + |
| 16 | + subject { SetLatestVersionSequenceValue.call(database) } |
| 17 | + |
| 18 | + context "when there is no sequence value set" do |
| 19 | + context "when there are no versions" do |
| 20 | + it "initializes the sequence value - this is required at start up each time in case someone has changed the ordering configuration (date vs semantic)" do |
| 21 | + subject |
| 22 | + expect(database[:version_sequence_number].first[:value]).to eq 100 |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + context "when there are pre-existing versions" do |
| 27 | + let!(:consumer) { create(:pacticipants, {name: 'Consumer', created_at: now, updated_at: now}) } |
| 28 | + let!(:consumer_version) { create(:versions, {number: '1.2.3', order: 1, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) } |
| 29 | + let!(:consumer_version) { create(:versions, {number: '1.2.5', order: 3, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) } |
| 30 | + |
| 31 | + it "initializes the sequence value to the max version order with a margin of error" do |
| 32 | + subject |
| 33 | + expect(database[:version_sequence_number].first[:value]).to eq 103 |
| 34 | + end |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + context "when a value already exists and it is already higher than the max_order" do |
| 39 | + before do |
| 40 | + database[:version_sequence_number].insert(value: 5) |
| 41 | + end |
| 42 | + |
| 43 | + it "does not update the value" do |
| 44 | + subject |
| 45 | + expect(database[:version_sequence_number].first[:value]).to eq 5 |
| 46 | + expect(database[:version_sequence_number].count).to eq 1 |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context "when a value already exists and it not higher than the max_order" do |
| 51 | + before do |
| 52 | + database[:version_sequence_number].insert(value: 3) |
| 53 | + end |
| 54 | + |
| 55 | + let!(:consumer) { create(:pacticipants, {name: 'Consumer', created_at: now, updated_at: now}) } |
| 56 | + let!(:consumer_version) { create(:versions, {number: '1.2.3', order: 1, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) } |
| 57 | + let!(:consumer_version) { create(:versions, {number: '1.2.5', order: 3, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) } |
| 58 | + |
| 59 | + it "updates the value" do |
| 60 | + subject |
| 61 | + expect(database[:version_sequence_number].first[:value]).to eq 103 |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | +end |
0 commit comments