summaryrefslogtreecommitdiff
path: root/spec/migrations/add_has_external_wiki_trigger_spec.rb
blob: 10c6888c87e48fca79be3c7cf0ae74dd26a6e679 (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
# frozen_string_literal: true

require 'spec_helper'

require_migration!

RSpec.describe AddHasExternalWikiTrigger do
  let(:migration) { described_class.new }
  let(:namespaces) { table(:namespaces) }
  let(:projects) { table(:projects) }
  let(:services) { table(:services) }

  before do
    @namespace = namespaces.create!(name: 'foo', path: 'foo')
    @project = projects.create!(namespace_id: @namespace.id)
  end

  describe '#up' do
    before do
      migrate!
    end

    describe 'INSERT trigger' do
      it 'sets `has_external_wiki` to true when active `ExternalWikiService` is inserted' do
        expect do
          services.create!(type: 'ExternalWikiService', active: true, project_id: @project.id)
        end.to change { @project.reload.has_external_wiki }.to(true)
      end

      it 'does not set `has_external_wiki` to true when service is for a different project' do
        different_project = projects.create!(namespace_id: @namespace.id)

        expect do
          services.create!(type: 'ExternalWikiService', active: true, project_id: different_project.id)
        end.not_to change { @project.reload.has_external_wiki }
      end

      it 'does not set `has_external_wiki` to true when inactive `ExternalWikiService` is inserted' do
        expect do
          services.create!(type: 'ExternalWikiService', active: false, project_id: @project.id)
        end.not_to change { @project.reload.has_external_wiki }
      end

      it 'does not set `has_external_wiki` to true when active other service is inserted' do
        expect do
          services.create!(type: 'MyService', active: true, project_id: @project.id)
        end.not_to change { @project.reload.has_external_wiki }
      end
    end

    describe 'UPDATE trigger' do
      it 'sets `has_external_wiki` to true when `ExternalWikiService` is made active' do
        service = services.create!(type: 'ExternalWikiService', active: false, project_id: @project.id)

        expect do
          service.update!(active: true)
        end.to change { @project.reload.has_external_wiki }.to(true)
      end

      it 'sets `has_external_wiki` to false when `ExternalWikiService` is made inactive' do
        service = services.create!(type: 'ExternalWikiService', active: true, project_id: @project.id)

        expect do
          service.update!(active: false)
        end.to change { @project.reload.has_external_wiki }.to(false)
      end

      it 'does not change `has_external_wiki` when service is for a different project' do
        different_project = projects.create!(namespace_id: @namespace.id)
        service = services.create!(type: 'ExternalWikiService', active: false, project_id: different_project.id)

        expect do
          service.update!(active: true)
        end.not_to change { @project.reload.has_external_wiki }
      end
    end

    describe 'DELETE trigger' do
      it 'sets `has_external_wiki` to false when `ExternalWikiService` is deleted' do
        service = services.create!(type: 'ExternalWikiService', active: true, project_id: @project.id)

        expect do
          service.delete
        end.to change { @project.reload.has_external_wiki }.to(false)
      end

      it 'does not change `has_external_wiki` when service is for a different project' do
        different_project = projects.create!(namespace_id: @namespace.id)
        service = services.create!(type: 'ExternalWikiService', active: true, project_id: different_project.id)

        expect do
          service.delete
        end.not_to change { @project.reload.has_external_wiki }
      end
    end
  end

  describe '#down' do
    before do
      migration.up
      migration.down
    end

    it 'drops the INSERT trigger' do
      expect do
        services.create!(type: 'ExternalWikiService', active: true, project_id: @project.id)
      end.not_to change { @project.reload.has_external_wiki }
    end

    it 'drops the UPDATE trigger' do
      service = services.create!(type: 'ExternalWikiService', active: false, project_id: @project.id)
      @project.update!(has_external_wiki: false)

      expect do
        service.update!(active: true)
      end.not_to change { @project.reload.has_external_wiki }
    end

    it 'drops the DELETE trigger' do
      service = services.create!(type: 'ExternalWikiService', active: true, project_id: @project.id)
      @project.update!(has_external_wiki: true)

      expect do
        service.delete
      end.not_to change { @project.reload.has_external_wiki }
    end
  end
end