Welcome to mirror list, hosted at ThFree Co, Russian Federation.

symbolized_jsonb_spec.rb « type « database « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a8401667b340f4a7fc98e9a5acbeaac65c256ce4 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::Type::SymbolizedJsonb do
  let(:type) { described_class.new }

  describe '#deserialize' do
    using RSpec::Parameterized::TableSyntax

    subject { type.deserialize(json) }

    where(:json, :value) do
      nil                                   | nil
      '{"key":"value"}'                     | { key: 'value' }
      '{"key":[1,2,3]}'                     | { key: [1, 2, 3] }
      '{"key":{"subkey":"value"}}'          | { key: { subkey: 'value' } }
      '{"key":{"a":[{"b":"c"},{"d":"e"}]}}' | { key: { a: [{ b: 'c' }, { d: 'e' }] } }
    end

    with_them do
      it { is_expected.to match(value) }
    end
  end

  context 'when used by a model' do
    let(:model) do
      Class.new(ApplicationRecord) do
        self.table_name = :_test_symbolized_jsonb

        attribute :options, :sym_jsonb
      end
    end

    let(:record) do
      model.create!(name: 'test', options: { key: 'value' })
    end

    before do
      ApplicationRecord.connection.execute(<<~SQL)
        CREATE TABLE _test_symbolized_jsonb(
          id serial NOT NULL PRIMARY KEY,
          name text,
          options jsonb);
      SQL

      model.reset_column_information
    end

    it { expect(record.options).to match({ key: 'value' }) }

    it 'ignores changes to other attributes' do
      record.name = 'other test'

      expect(record.changes).to match('name' => ['test', 'other test'])
    end

    it 'tracks changes to options' do
      record.options = { key: 'other value' }

      expect(record.changes).to match('options' => [{ 'key' => 'value' }, { 'key' => 'other value' }])
    end
  end
end