Commit 6243a93b authored by Mike Kozono's avatar Mike Kozono

Extract ReplicableModel tests against Dummy

instead of writing these tests inside of replicator_spec.rb.
parent 4e9f2bf1
# frozen_string_literal: true
require 'spec_helper'
# Also see ee/spec/support/shared_examples/models/concerns/replicable_model_shared_examples.rb:
#
# - Place tests here in replicable_model_spec.rb if you want to run them once,
# against a DummyModel.
# - Place tests in replicable_model_shared_examples.rb if you want them to be
# run against every real Model.
RSpec.describe Gitlab::Geo::ReplicableModel do
include ::EE::GeoHelpers
let_it_be(:primary_node) { create(:geo_node, :primary) }
let_it_be(:secondary_node) { create(:geo_node) }
before(:all) do
create_dummy_model_table
end
after(:all) do
drop_dummy_model_table
end
before do
stub_dummy_replicator_class
stub_dummy_model_class
end
subject { DummyModel.new }
it_behaves_like 'a replicable model' do
let(:model_record) { subject }
end
describe '#replicator' do
it 'adds replicator method to the model' do
expect(subject).to respond_to(:replicator)
end
it 'instantiates a replicator into the model' do
expect(subject.replicator).to be_a(Geo::DummyReplicator)
end
end
end
...@@ -21,37 +21,15 @@ RSpec.describe Gitlab::Geo::Replicator do ...@@ -21,37 +21,15 @@ RSpec.describe Gitlab::Geo::Replicator do
end end
before(:all) do before(:all) do
ActiveRecord::Schema.define do create_dummy_model_table
create_table :dummy_models, force: true do |t|
t.binary :verification_checksum
end
end
end end
after(:all) do after(:all) do
ActiveRecord::Schema.define do drop_dummy_model_table
drop_table :dummy_models, force: true
end
end end
context 'with defined events' do
before do before do
stub_const('Geo::DummyReplicator', Class.new(Gitlab::Geo::Replicator)) stub_dummy_replicator_class
Geo::DummyReplicator.class_eval do
event :test
event :another_test
def self.model
::DummyModel
end
protected
def consume_event_test(user:, other:)
true
end
end
end end
context 'event DSL' do context 'event DSL' do
...@@ -74,35 +52,6 @@ RSpec.describe Gitlab::Geo::Replicator do ...@@ -74,35 +52,6 @@ RSpec.describe Gitlab::Geo::Replicator do
end end
end end
context 'model DSL' do
before do
stub_const('DummyModel', Class.new(ApplicationRecord))
DummyModel.class_eval do
include ActiveModel::Model
def self.after_create_commit(*args)
end
include Gitlab::Geo::ReplicableModel
with_replicator Geo::DummyReplicator
end
DummyModel.reset_column_information
end
subject { DummyModel.new }
it 'adds replicator method to the model' do
expect(subject).to respond_to(:replicator)
end
it 'instantiates a replicator into the model' do
expect(subject.replicator).to be_a(Geo::DummyReplicator)
end
end
describe '#publish' do describe '#publish' do
subject { Geo::DummyReplicator.new } subject { Geo::DummyReplicator.new }
...@@ -237,5 +186,4 @@ RSpec.describe Gitlab::Geo::Replicator do ...@@ -237,5 +186,4 @@ RSpec.describe Gitlab::Geo::Replicator do
describe '#in_replicables_for_geo_node?' do describe '#in_replicables_for_geo_node?' do
it { is_expected.to delegate_method(:in_replicables_for_geo_node?).to(:model_record) } it { is_expected.to delegate_method(:in_replicables_for_geo_node?).to(:model_record) }
end end
end
end end
...@@ -55,5 +55,67 @@ module EE ...@@ -55,5 +55,67 @@ module EE
# will appear as though the tracking DB were not available # will appear as though the tracking DB were not available
allow(::Gitlab::Geo).to receive(:geo_database_configured?).and_call_original allow(::Gitlab::Geo).to receive(:geo_database_configured?).and_call_original
end end
def stub_dummy_replicator_class
stub_const('Geo::DummyReplicator', Class.new(::Gitlab::Geo::Replicator))
Geo::DummyReplicator.class_eval do
event :test
event :another_test
def self.model
::DummyModel
end
def handle_after_create_commit
true
end
protected
def consume_event_test(user:, other:)
true
end
end
end
def stub_dummy_model_class
stub_const('DummyModel', Class.new(ApplicationRecord))
DummyModel.class_eval do
include ::Gitlab::Geo::ReplicableModel
with_replicator Geo::DummyReplicator
def self.replicables_for_geo_node
self.all
end
end
DummyModel.reset_column_information
end
# Example:
#
# before(:all) do
# create_dummy_model_table
# end
#
# after(:all) do
# drop_dummy_model_table
# end
def create_dummy_model_table
ActiveRecord::Schema.define do
create_table :dummy_models, force: true do |t|
t.binary :verification_checksum
end
end
end
def drop_dummy_model_table
ActiveRecord::Schema.define do
drop_table :dummy_models, force: true
end
end
end end
end end
...@@ -6,6 +6,13 @@ ...@@ -6,6 +6,13 @@
# #
# We do not use `described_class` here, so we can include this in replicator # We do not use `described_class` here, so we can include this in replicator
# strategy shared examples instead of in *every* model spec. # strategy shared examples instead of in *every* model spec.
#
# Also see ee/spec/lib/gitlab/geo/replicable_model_spec.rb:
#
# - Place tests in replicable_model_spec.rb if you want to run them once,
# against a DummyModel.
# - Place tests here in replicable_model_shared_examples.rb if you want them to
# be run against every real Model.
RSpec.shared_examples 'a replicable model' do RSpec.shared_examples 'a replicable model' do
include EE::GeoHelpers include EE::GeoHelpers
......
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