summaryrefslogtreecommitdiff
path: root/spec/services/database/consistency_fix_service_spec.rb
blob: 9a0fac2191c2b3a8107486a5cce0eaa65966c980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Database::ConsistencyFixService do
  describe '#execute' do
    context 'fixing namespaces inconsistencies' do
      subject(:consistency_fix_service) do
        described_class.new(
          source_model: Namespace,
          target_model: Ci::NamespaceMirror,
          sync_event_class: Namespaces::SyncEvent,
          source_sort_key: :id,
          target_sort_key: :namespace_id
        )
      end

      let(:table) { 'public.namespaces' }
      let!(:namespace) { create(:namespace) }
      let!(:namespace_mirror) { Ci::NamespaceMirror.find_by(namespace_id: namespace.id) }

      context 'when both objects exist' do
        it 'creates a Namespaces::SyncEvent to modify the target object' do
          expect do
            consistency_fix_service.execute(ids: [namespace.id])
          end.to change {
            Namespaces::SyncEvent.where(namespace_id: namespace.id).count
          }.by(1)
        end

        it 'enqueues the worker to process the Namespaces::SyncEvents' do
          expect(::Namespaces::ProcessSyncEventsWorker).to receive(:perform_async)
          consistency_fix_service.execute(ids: [namespace.id])
        end
      end

      context 'when the source object has been deleted, but not the target' do
        before do
          namespace.delete
        end

        it 'deletes the target object' do
          expect do
            consistency_fix_service.execute(ids: [namespace.id])
          end.to change { Ci::NamespaceMirror.where(namespace_id: namespace.id).count }.by(-1)
        end
      end
    end

    context 'fixing projects inconsistencies' do
      subject(:consistency_fix_service) do
        described_class.new(
          source_model: Project,
          target_model: Ci::ProjectMirror,
          sync_event_class: Projects::SyncEvent,
          source_sort_key: :id,
          target_sort_key: :project_id
        )
      end

      let(:table) { 'public.projects' }
      let!(:project) { create(:project) }
      let!(:project_mirror) { Ci::ProjectMirror.find_by(project_id: project.id) }

      context 'when both objects exist' do
        it 'creates a Projects::SyncEvent to modify the target object' do
          expect do
            consistency_fix_service.execute(ids: [project.id])
          end.to change {
            Projects::SyncEvent.where(project_id: project.id).count
          }.by(1)
        end

        it 'enqueues the worker to process the Projects::SyncEvents' do
          expect(::Projects::ProcessSyncEventsWorker).to receive(:perform_async)
          consistency_fix_service.execute(ids: [project.id])
        end
      end

      context 'when the source object has been deleted, but not the target' do
        before do
          project.delete
        end

        it 'deletes the target object' do
          expect do
            consistency_fix_service.execute(ids: [project.id])
          end.to change { Ci::ProjectMirror.where(project_id: project.id).count }.by(-1)
        end
      end
    end
  end

  describe '#create_sync_event_for' do
    context 'when the source model is Namespace' do
      let(:namespace) { create(:namespace) }

      let(:service) do
        described_class.new(
          source_model: Namespace,
          target_model: Ci::NamespaceMirror,
          sync_event_class: Namespaces::SyncEvent,
          source_sort_key: :id,
          target_sort_key: :namespace_id
        )
      end

      it 'creates a Namespaces::SyncEvent object' do
        expect do
          service.send(:create_sync_event_for, namespace.id)
        end.to change { Namespaces::SyncEvent.where(namespace_id: namespace.id).count }.by(1)
      end
    end

    context 'when the source model is Project' do
      let(:project) { create(:project) }

      let(:service) do
        described_class.new(
          source_model: Project,
          target_model: Ci::ProjectMirror,
          sync_event_class: Projects::SyncEvent,
          source_sort_key: :id,
          target_sort_key: :project_id
        )
      end

      it 'creates a Projects::SyncEvent object' do
        expect do
          service.send(:create_sync_event_for, project.id)
        end.to change { Projects::SyncEvent.where(project_id: project.id).count }.by(1)
      end
    end
  end

  context 'when the source model is User' do
    let(:service) do
      described_class.new(
        source_model: User,
        target_model: Ci::ProjectMirror,
        sync_event_class: Projects::SyncEvent,
        source_sort_key: :id,
        target_sort_key: :project_id
      )
    end

    it 'raises an error' do
      expect do
        service.send(:create_sync_event_for, 1)
      end.to raise_error("Unknown Source Model User")
    end
  end
end