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

require 'spec_helper'
require_migration!

RSpec.describe RemoveEmptyGithubServiceTemplates do
  subject(:migration) { described_class.new }

  let(:services) do
    table(:services).tap do |klass|
      klass.class_eval do
        serialize :properties, JSON
      end
    end
  end

  before do
    services.delete_all

    create_service(properties: nil)
    create_service(properties: {})
    create_service(properties: { some: :value })
    create_service(properties: {}, template: false)
    create_service(properties: {}, type: 'SomeType')
  end

  def all_service_properties
    services.where(template: true, type: 'GithubService').pluck(:properties)
  end

  it 'correctly migrates up and down service templates' do
    reversible_migration do |migration|
      migration.before -> do
        expect(services.count).to eq(5)

        expect(all_service_properties)
          .to match(a_collection_containing_exactly(nil, {}, { 'some' => 'value' }))
      end

      migration.after -> do
        expect(services.count).to eq(4)

        expect(all_service_properties)
          .to match(a_collection_containing_exactly(nil, { 'some' => 'value' }))
      end
    end
  end

  def create_service(params)
    data = { template: true, type: 'GithubService' }
    data.merge!(params)

    services.create!(data)
  end
end