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

has_one_spec.rb « associations « partitioning « gitlab_patches « active_record « spec « activerecord-gitlab « gems - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aeb565c6dad221fc885f35c4f98cb64475093224 (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
# frozen_string_literal: true

RSpec.describe 'ActiveRecord::GitlabPatches::Partitioning::Associations::HasOne', :partitioning do
  let(:pipeline) { Pipeline.create!(partition_id: 100) }
  let(:job) { Job.create!(pipeline: pipeline, partition_id: pipeline.partition_id) }

  it 'finds associated record using partition_id' do
    find_statement = <<~SQL.squish
      SELECT \"metadata\".*
      FROM \"metadata\"
      WHERE \"metadata\".\"job_id\" = #{job.id}
      AND \"metadata\".\"partition_id\" = #{job.partition_id}
      LIMIT 1
    SQL

    result = QueryRecorder.log do
      job.reset.metadata
    end

    expect(result).to include(find_statement)
  end

  it 'builds records using partition_id' do
    metadata = job.build_metadata

    expect(metadata.job_id).to eq(job.id)
    expect(metadata.partition_id).to eq(job.partition_id)
  end

  it 'saves records using partition_id' do
    create_statement = <<~SQL.squish
      INSERT INTO \"metadata\" (\"job_id\", \"partition_id\") VALUES (#{job.id}, #{job.partition_id})
    SQL

    result = QueryRecorder.log do
      job.build_metadata.save!
    end

    expect(result).to include(create_statement)
  end

  it 'creates records using partition_id' do
    create_statement = <<~SQL.squish
      INSERT INTO \"metadata\" (\"job_id\", \"partition_id\") VALUES (#{job.id}, #{job.partition_id})
    SQL

    result = QueryRecorder.log do
      job.create_metadata
    end

    expect(result).to include(create_statement)
  end

  it 'uses nested attributes on create' do
    skip '`partitionable` will assign the `partition_id` value in this case.'

    statement1 = <<~SQL.squish
      INSERT INTO \"jobs\" (\"pipeline_id\", \"partition_id\", \"name\")
      VALUES (#{pipeline.id}, #{pipeline.partition_id}, 'test')
    SQL

    statement2 = <<~SQL.squish
      INSERT INTO \"metadata\" (\"job_id\", \"partition_id\", \"test_flag\")
      VALUES (#{job.id}, #{job.partition_id}, 1)
    SQL

    insert_statements = [statement1, statement2]

    result = QueryRecorder.log do
      pipeline.jobs.create!(name: 'test', metadata_attributes: { test_flag: true })
    end

    insert_statements.each do |statement|
      expect(result).to include(statement)
    end
  end

  it 'uses nested attributes on update' do
    statement1 = <<~SQL.squish
      UPDATE \"jobs\" SET \"name\" = 'other test'
      WHERE \"jobs\".\"id\" = #{job.id} AND \"jobs\".\"partition_id\" = #{job.partition_id}
    SQL

    statement2 = <<~SQL.squish
      INSERT INTO \"metadata\" (\"job_id\", \"partition_id\", \"test_flag\") VALUES (#{job.id}, #{job.partition_id}, 1)
    SQL

    update_statements = [statement1, statement2]

    job.name = 'other test'
    job.metadata_attributes = { test_flag: true }

    result = QueryRecorder.log do
      job.save!
    end

    update_statements.each do |statement|
      expect(result).to include(statement)
    end
  end
end