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

legacy_bulk_insert_spec.rb « concerns « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0c6f84f391bfb9502c3cf9a63b15a1d0f9c40ef8 (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
# frozen_string_literal: true

require 'spec_helper'

# rubocop: disable Gitlab/BulkInsert
RSpec.describe LegacyBulkInsert do
  let(:model) { ApplicationRecord }

  describe '#bulk_insert' do
    before do
      allow(model).to receive(:connection).and_return(dummy_connection)
      allow(dummy_connection).to receive(:quote_column_name, &:itself)
      allow(dummy_connection).to receive(:quote, &:itself)
      allow(dummy_connection).to receive(:execute)
    end

    let(:dummy_connection) { double(:connection) }

    let(:rows) do
      [
        { a: 1, b: 2, c: 3 },
        { c: 6, a: 4, b: 5 }
      ]
    end

    it 'does nothing with empty rows' do
      expect(dummy_connection).not_to receive(:execute)

      model.legacy_bulk_insert('test', [])
    end

    it 'uses the ordering from the first row' do
      expect(dummy_connection).to receive(:execute) do |sql|
        expect(sql).to include('(1, 2, 3)')
        expect(sql).to include('(4, 5, 6)')
      end

      model.legacy_bulk_insert('test', rows)
    end

    it 'quotes column names' do
      expect(dummy_connection).to receive(:quote_column_name).with(:a)
      expect(dummy_connection).to receive(:quote_column_name).with(:b)
      expect(dummy_connection).to receive(:quote_column_name).with(:c)

      model.legacy_bulk_insert('test', rows)
    end

    it 'quotes values' do
      1.upto(6) do |i|
        expect(dummy_connection).to receive(:quote).with(i)
      end

      model.legacy_bulk_insert('test', rows)
    end

    it 'does not quote values of a column in the disable_quote option' do
      [1, 2, 4, 5].each do |i|
        expect(dummy_connection).to receive(:quote).with(i)
      end

      model.legacy_bulk_insert('test', rows, disable_quote: :c)
    end

    it 'does not quote values of columns in the disable_quote option' do
      [2, 5].each do |i|
        expect(dummy_connection).to receive(:quote).with(i)
      end

      model.legacy_bulk_insert('test', rows, disable_quote: [:a, :c])
    end

    it 'handles non-UTF-8 data' do
      expect { model.legacy_bulk_insert('test', [{ a: "\255" }]) }.not_to raise_error
    end

    context 'when using PostgreSQL' do
      it 'allows the returning of the IDs of the inserted rows' do
        result = double(:result, values: [['10']])

        expect(dummy_connection)
          .to receive(:execute)
          .with(/RETURNING id/)
          .and_return(result)

        ids = model
          .legacy_bulk_insert('test', [{ number: 10 }], return_ids: true)

        expect(ids).to eq([10])
      end

      it 'allows setting the upsert to do nothing' do
        expect(dummy_connection)
          .to receive(:execute)
          .with(/ON CONFLICT DO NOTHING/)

        model
          .legacy_bulk_insert('test', [{ number: 10 }], on_conflict: :do_nothing)
      end
    end
  end
end
# rubocop: enable Gitlab/BulkInsert