diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-20 11:43:02 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-20 11:43:02 +0300 |
commit | d9ab72d6080f594d0b3cae15f14b3ef2c6c638cb (patch) | |
tree | 2341ef426af70ad1e289c38036737e04b0aa5007 /spec/lib/gitlab/merge_requests | |
parent | d6e514dd13db8947884cd58fe2a9c2a063400a9b (diff) |
Add latest changes from gitlab-org/gitlab@14-4-stable-eev14.4.0-rc42
Diffstat (limited to 'spec/lib/gitlab/merge_requests')
3 files changed, 198 insertions, 0 deletions
diff --git a/spec/lib/gitlab/merge_requests/mergeability/check_result_spec.rb b/spec/lib/gitlab/merge_requests/mergeability/check_result_spec.rb new file mode 100644 index 00000000000..4f437e57600 --- /dev/null +++ b/spec/lib/gitlab/merge_requests/mergeability/check_result_spec.rb @@ -0,0 +1,140 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::MergeRequests::Mergeability::CheckResult do + subject(:check_result) { described_class } + + let(:time) { Time.current } + + around do |example| + freeze_time do + example.run + end + end + + describe '.default_payload' do + it 'returns the expected defaults' do + expect(check_result.default_payload).to eq({ last_run_at: time }) + end + end + + describe '.success' do + subject(:success) { check_result.success(payload: payload) } + + let(:payload) { {} } + + it 'creates a success result' do + expect(success.status).to eq described_class::SUCCESS_STATUS + end + + it 'uses the default payload' do + expect(success.payload).to eq described_class.default_payload + end + + context 'when given a payload' do + let(:payload) { { last_run_at: time + 1.day, test: 'test' } } + + it 'uses the payload passed' do + expect(success.payload).to eq payload + end + end + end + + describe '.failed' do + subject(:failed) { check_result.failed(payload: payload) } + + let(:payload) { {} } + + it 'creates a failure result' do + expect(failed.status).to eq described_class::FAILED_STATUS + end + + it 'uses the default payload' do + expect(failed.payload).to eq described_class.default_payload + end + + context 'when given a payload' do + let(:payload) { { last_run_at: time + 1.day, test: 'test' } } + + it 'uses the payload passed' do + expect(failed.payload).to eq payload + end + end + end + + describe '.from_hash' do + subject(:from_hash) { described_class.from_hash(hash) } + + let(:status) { described_class::SUCCESS_STATUS } + let(:payload) { { test: 'test' } } + let(:hash) do + { + status: status, + payload: payload + } + end + + it 'returns the expected status and payload' do + expect(from_hash.status).to eq status + expect(from_hash.payload).to eq payload + end + end + + describe '#to_hash' do + subject(:to_hash) { described_class.new(**hash).to_hash } + + let(:status) { described_class::SUCCESS_STATUS } + let(:payload) { { test: 'test' } } + let(:hash) do + { + status: status, + payload: payload + } + end + + it 'returns the expected hash' do + expect(to_hash).to eq hash + end + end + + describe '#failed?' do + subject(:failed) { described_class.new(status: status).failed? } + + context 'when it has failed' do + let(:status) { described_class::FAILED_STATUS } + + it 'returns true' do + expect(failed).to eq true + end + end + + context 'when it has succeeded' do + let(:status) { described_class::SUCCESS_STATUS } + + it 'returns false' do + expect(failed).to eq false + end + end + end + + describe '#success?' do + subject(:success) { described_class.new(status: status).success? } + + context 'when it has failed' do + let(:status) { described_class::FAILED_STATUS } + + it 'returns false' do + expect(success).to eq false + end + end + + context 'when it has succeeded' do + let(:status) { described_class::SUCCESS_STATUS } + + it 'returns true' do + expect(success).to eq true + end + end + end +end diff --git a/spec/lib/gitlab/merge_requests/mergeability/redis_interface_spec.rb b/spec/lib/gitlab/merge_requests/mergeability/redis_interface_spec.rb new file mode 100644 index 00000000000..e5475d04d86 --- /dev/null +++ b/spec/lib/gitlab/merge_requests/mergeability/redis_interface_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::MergeRequests::Mergeability::RedisInterface, :clean_gitlab_redis_shared_state do + subject(:redis_interface) { described_class.new } + + let(:merge_check) { double(cache_key: '13') } + let(:result_hash) { { 'test' => 'test' } } + let(:expected_key) { "#{merge_check.cache_key}:#{described_class::VERSION}" } + + describe '#save_check' do + it 'saves the hash' do + expect(Gitlab::Redis::SharedState.with { |redis| redis.get(expected_key) }).to be_nil + + redis_interface.save_check(merge_check: merge_check, result_hash: result_hash) + + expect(Gitlab::Redis::SharedState.with { |redis| redis.get(expected_key) }).to eq result_hash.to_json + end + end + + describe '#retrieve_check' do + it 'returns the hash' do + Gitlab::Redis::SharedState.with { |redis| redis.set(expected_key, result_hash.to_json) } + + expect(redis_interface.retrieve_check(merge_check: merge_check)).to eq result_hash + end + end +end diff --git a/spec/lib/gitlab/merge_requests/mergeability/results_store_spec.rb b/spec/lib/gitlab/merge_requests/mergeability/results_store_spec.rb new file mode 100644 index 00000000000..d376dcb5b18 --- /dev/null +++ b/spec/lib/gitlab/merge_requests/mergeability/results_store_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::MergeRequests::Mergeability::ResultsStore do + subject(:results_store) { described_class.new(merge_request: merge_request, interface: interface) } + + let(:merge_check) { double } + let(:interface) { double } + let(:merge_request) { double } + + describe '#read' do + it 'calls #retrieve on the interface' do + expect(interface).to receive(:retrieve_check).with(merge_check: merge_check) + + results_store.read(merge_check: merge_check) + end + end + + describe '#write' do + let(:result_hash) { double } + + it 'calls #save_check on the interface' do + expect(interface).to receive(:save_check).with(merge_check: merge_check, result_hash: result_hash) + + results_store.write(merge_check: merge_check, result_hash: result_hash) + end + end +end |