diff options
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/ci/test_case_failure_spec.rb | 73 | ||||
-rw-r--r-- | spec/models/ci/test_case_spec.rb | 31 | ||||
-rw-r--r-- | spec/models/ci/unit_test_failure_spec.rb | 73 | ||||
-rw-r--r-- | spec/models/ci/unit_test_spec.rb | 87 |
4 files changed, 160 insertions, 104 deletions
diff --git a/spec/models/ci/test_case_failure_spec.rb b/spec/models/ci/test_case_failure_spec.rb deleted file mode 100644 index 34f89b663ed..00000000000 --- a/spec/models/ci/test_case_failure_spec.rb +++ /dev/null @@ -1,73 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Ci::TestCaseFailure do - describe 'relationships' do - it { is_expected.to belong_to(:build) } - it { is_expected.to belong_to(:test_case) } - end - - describe 'validations' do - subject { build(:ci_test_case_failure) } - - it { is_expected.to validate_presence_of(:test_case) } - it { is_expected.to validate_presence_of(:build) } - it { is_expected.to validate_presence_of(:failed_at) } - end - - describe '.recent_failures_count' do - let_it_be(:project) { create(:project) } - - subject(:recent_failures) do - described_class.recent_failures_count( - project: project, - test_case_keys: test_case_keys - ) - end - - context 'when test case failures are within the date range and are for the test case keys' do - let(:tc_1) { create(:ci_test_case, project: project) } - let(:tc_2) { create(:ci_test_case, project: project) } - let(:test_case_keys) { [tc_1.key_hash, tc_2.key_hash] } - - before do - create_list(:ci_test_case_failure, 3, test_case: tc_1, failed_at: 1.day.ago) - create_list(:ci_test_case_failure, 2, test_case: tc_2, failed_at: 3.days.ago) - end - - it 'returns the number of failures for each test case key hash for the past 14 days by default' do - expect(recent_failures).to eq( - tc_1.key_hash => 3, - tc_2.key_hash => 2 - ) - end - end - - context 'when test case failures are within the date range but are not for the test case keys' do - let(:tc) { create(:ci_test_case, project: project) } - let(:test_case_keys) { ['some-other-key-hash'] } - - before do - create(:ci_test_case_failure, test_case: tc, failed_at: 1.day.ago) - end - - it 'excludes them from the count' do - expect(recent_failures[tc.key_hash]).to be_nil - end - end - - context 'when test case failures are not within the date range but are for the test case keys' do - let(:tc) { create(:ci_test_case, project: project) } - let(:test_case_keys) { [tc.key_hash] } - - before do - create(:ci_test_case_failure, test_case: tc, failed_at: 15.days.ago) - end - - it 'excludes them from the count' do - expect(recent_failures[tc.key_hash]).to be_nil - end - end - end -end diff --git a/spec/models/ci/test_case_spec.rb b/spec/models/ci/test_case_spec.rb deleted file mode 100644 index 45311e285a6..00000000000 --- a/spec/models/ci/test_case_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Ci::TestCase do - describe 'relationships' do - it { is_expected.to belong_to(:project) } - it { is_expected.to have_many(:test_case_failures) } - end - - describe 'validations' do - subject { build(:ci_test_case) } - - it { is_expected.to validate_presence_of(:project) } - it { is_expected.to validate_presence_of(:key_hash) } - end - - describe '.find_or_create_by_batch' do - it 'finds or creates records for the given test case keys', :aggregate_failures do - project = create(:project) - existing_tc = create(:ci_test_case, project: project) - new_key = Digest::SHA256.hexdigest(SecureRandom.hex) - keys = [existing_tc.key_hash, new_key] - - result = described_class.find_or_create_by_batch(project, keys) - - expect(result.map(&:key_hash)).to match_array([existing_tc.key_hash, new_key]) - expect(result).to all(be_persisted) - end - end -end diff --git a/spec/models/ci/unit_test_failure_spec.rb b/spec/models/ci/unit_test_failure_spec.rb new file mode 100644 index 00000000000..f9b8c66b603 --- /dev/null +++ b/spec/models/ci/unit_test_failure_spec.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Ci::UnitTestFailure do + describe 'relationships' do + it { is_expected.to belong_to(:build) } + it { is_expected.to belong_to(:unit_test) } + end + + describe 'validations' do + subject { build(:ci_unit_test_failure) } + + it { is_expected.to validate_presence_of(:unit_test) } + it { is_expected.to validate_presence_of(:build) } + it { is_expected.to validate_presence_of(:failed_at) } + end + + describe '.recent_failures_count' do + let_it_be(:project) { create(:project) } + + subject(:recent_failures) do + described_class.recent_failures_count( + project: project, + unit_test_keys: unit_test_keys + ) + end + + context 'when unit test failures are within the date range and are for the unit test keys' do + let(:test_1) { create(:ci_unit_test, project: project) } + let(:test_2) { create(:ci_unit_test, project: project) } + let(:unit_test_keys) { [test_1.key_hash, test_2.key_hash] } + + before do + create_list(:ci_unit_test_failure, 3, unit_test: test_1, failed_at: 1.day.ago) + create_list(:ci_unit_test_failure, 2, unit_test: test_2, failed_at: 3.days.ago) + end + + it 'returns the number of failures for each unit test key hash for the past 14 days by default' do + expect(recent_failures).to eq( + test_1.key_hash => 3, + test_2.key_hash => 2 + ) + end + end + + context 'when unit test failures are within the date range but are not for the unit test keys' do + let(:test) { create(:ci_unit_test, project: project) } + let(:unit_test_keys) { ['some-other-key-hash'] } + + before do + create(:ci_unit_test_failure, unit_test: test, failed_at: 1.day.ago) + end + + it 'excludes them from the count' do + expect(recent_failures[test.key_hash]).to be_nil + end + end + + context 'when unit test failures are not within the date range but are for the unit test keys' do + let(:test) { create(:ci_unit_test, project: project) } + let(:unit_test_keys) { [test.key_hash] } + + before do + create(:ci_unit_test_failure, unit_test: test, failed_at: 15.days.ago) + end + + it 'excludes them from the count' do + expect(recent_failures[test.key_hash]).to be_nil + end + end + end +end diff --git a/spec/models/ci/unit_test_spec.rb b/spec/models/ci/unit_test_spec.rb new file mode 100644 index 00000000000..2207a362be3 --- /dev/null +++ b/spec/models/ci/unit_test_spec.rb @@ -0,0 +1,87 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Ci::UnitTest do + describe 'relationships' do + it { is_expected.to belong_to(:project) } + it { is_expected.to have_many(:unit_test_failures) } + end + + describe 'validations' do + subject { build(:ci_unit_test) } + + it { is_expected.to validate_presence_of(:project) } + it { is_expected.to validate_presence_of(:key_hash) } + it { is_expected.to validate_presence_of(:name) } + it { is_expected.to validate_presence_of(:suite_name) } + end + + describe '.find_or_create_by_batch' do + let(:project) { create(:project) } + + it 'finds or creates records for the given unit test keys', :aggregate_failures do + existing_test = create(:ci_unit_test, project: project, suite_name: 'rspec', name: 'Math#sum adds numbers') + new_key = Digest::SHA256.hexdigest(SecureRandom.hex) + attrs = [ + { + key_hash: existing_test.key_hash, + name: 'This new name will not apply', + suite_name: 'This new suite name will not apply' + }, + { + key_hash: new_key, + name: 'Component works', + suite_name: 'jest' + } + ] + + result = described_class.find_or_create_by_batch(project, attrs) + + expect(result).to match_array([ + have_attributes( + key_hash: existing_test.key_hash, + suite_name: 'rspec', + name: 'Math#sum adds numbers' + ), + have_attributes( + key_hash: new_key, + suite_name: 'jest', + name: 'Component works' + ) + ]) + + expect(result).to all(be_persisted) + end + + context 'when a given name or suite_name exceeds the string size limit' do + before do + stub_const("#{described_class}::MAX_NAME_SIZE", 6) + stub_const("#{described_class}::MAX_SUITE_NAME_SIZE", 6) + end + + it 'truncates the values before storing the information' do + new_key = Digest::SHA256.hexdigest(SecureRandom.hex) + attrs = [ + { + key_hash: new_key, + name: 'abcdefg', + suite_name: 'abcdefg' + } + ] + + result = described_class.find_or_create_by_batch(project, attrs) + + expect(result).to match_array([ + have_attributes( + key_hash: new_key, + suite_name: 'abc...', + name: 'abc...' + ) + ]) + + expect(result).to all(be_persisted) + end + end + end +end |