Commit 4e0ff314 authored by David Kim's avatar David Kim

Merge branch 'cherry-pick-eecb80d8' into 'master'

Avoid calling belongs_to dynamically in tests

See merge request gitlab-org/gitlab!60774
parents b3aea360 6d9dcb9a
......@@ -5,6 +5,10 @@ require 'spec_helper'
RSpec.describe BulkInsertSafe do
before(:all) do
ActiveRecord::Schema.define do
create_table :bulk_insert_parent_items, force: true do |t|
t.string :name, null: false
end
create_table :bulk_insert_items, force: true do |t|
t.string :name, null: true
t.integer :enum_value, null: false
......@@ -12,6 +16,7 @@ RSpec.describe BulkInsertSafe do
t.string :encrypted_secret_value_iv, null: false
t.binary :sha_value, null: false, limit: 20
t.jsonb :jsonb_value, null: false
t.belongs_to :bulk_insert_parent_item, foreign_key: true, null: true
t.index :name, unique: true
end
......@@ -21,9 +26,23 @@ RSpec.describe BulkInsertSafe do
after(:all) do
ActiveRecord::Schema.define do
drop_table :bulk_insert_items, force: true
drop_table :bulk_insert_parent_items, force: true
end
end
BulkInsertParentItem = Class.new(ActiveRecord::Base) do
self.table_name = :bulk_insert_parent_items
self.inheritance_column = :_type_disabled
def self.name
table_name.singularize.camelcase
end
end
let_it_be(:bulk_insert_parent_item) do
BulkInsertParentItem.create!(name: 'parent')
end
let_it_be(:bulk_insert_item_class) do
Class.new(ActiveRecord::Base) do
self.table_name = 'bulk_insert_items'
......@@ -33,6 +52,8 @@ RSpec.describe BulkInsertSafe do
validates :name, :enum_value, :secret_value, :sha_value, :jsonb_value, presence: true
belongs_to :bulk_insert_parent_item
sha_attribute :sha_value
enum enum_value: { case_1: 1 }
......@@ -51,8 +72,8 @@ RSpec.describe BulkInsertSafe do
'BulkInsertItem'
end
def self.valid_list(count)
Array.new(count) { |n| new(name: "item-#{n}", secret_value: 'my-secret') }
def self.valid_list(count, bulk_insert_parent_item: nil)
Array.new(count) { |n| new(name: "item-#{n}", secret_value: 'my-secret', bulk_insert_parent_item: bulk_insert_parent_item) }
end
def self.invalid_list(count)
......@@ -117,6 +138,14 @@ RSpec.describe BulkInsertSafe do
bulk_insert_item_class.bulk_insert!(items, batch_size: 5)
end
it 'inserts items with belongs_to association' do
items = bulk_insert_item_class.valid_list(10, bulk_insert_parent_item: bulk_insert_parent_item)
bulk_insert_item_class.bulk_insert!(items, batch_size: 5)
expect(bulk_insert_item_class.last(items.size).map(&:bulk_insert_parent_item)).to eq([bulk_insert_parent_item] * 10)
end
it 'items can be properly fetched from database' do
items = bulk_insert_item_class.valid_list(10)
......@@ -129,8 +158,7 @@ RSpec.describe BulkInsertSafe do
it 'rolls back the transaction when any item is invalid' do
# second batch is bad
all_items = bulk_insert_item_class.valid_list(10) +
bulk_insert_item_class.invalid_list(10)
all_items = bulk_insert_item_class.valid_list(10) + bulk_insert_item_class.invalid_list(10)
expect do
bulk_insert_item_class.bulk_insert!(all_items, batch_size: 2) rescue nil
......
......@@ -30,10 +30,6 @@ RSpec.shared_examples 'a BulkInsertSafe model' do |klass|
expect { target_class.set_callback(name) {} }.not_to raise_error
end
end
it 'does not raise an error when the call is triggered by belongs_to' do
expect { target_class.belongs_to(:other_record) }.not_to raise_error
end
end
describe '.bulk_insert!' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment