summaryrefslogtreecommitdiff
path: root/spec/models
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/application_setting_spec.rb126
-rw-r--r--spec/models/concerns/batch_destroy_dependent_associations_spec.rb60
-rw-r--r--spec/models/concerns/discussion_on_diff_spec.rb2
-rw-r--r--spec/models/diff_note_spec.rb62
-rw-r--r--spec/models/event_spec.rb13
-rw-r--r--spec/models/internal_id_spec.rb37
-rw-r--r--spec/models/note_diff_file_spec.rb11
7 files changed, 152 insertions, 159 deletions
diff --git a/spec/models/application_setting_spec.rb b/spec/models/application_setting_spec.rb
index 7e47043a1cb..f8f07205623 100644
--- a/spec/models/application_setting_spec.rb
+++ b/spec/models/application_setting_spec.rb
@@ -391,68 +391,6 @@ describe ApplicationSetting do
end
describe 'performance bar settings' do
- describe 'performance_bar_allowed_group_id=' do
- context 'with a blank path' do
- before do
- setting.performance_bar_allowed_group_id = create(:group).full_path
- end
-
- it 'persists nil for a "" path and clears allowed user IDs cache' do
- expect(Gitlab::PerformanceBar).to receive(:expire_allowed_user_ids_cache)
-
- setting.performance_bar_allowed_group_id = ''
-
- expect(setting.performance_bar_allowed_group_id).to be_nil
- end
- end
-
- context 'with an invalid path' do
- it 'does not persist an invalid group path' do
- setting.performance_bar_allowed_group_id = 'foo'
-
- expect(setting.performance_bar_allowed_group_id).to be_nil
- end
- end
-
- context 'with a path to an existing group' do
- let(:group) { create(:group) }
-
- it 'persists a valid group path and clears allowed user IDs cache' do
- expect(Gitlab::PerformanceBar).to receive(:expire_allowed_user_ids_cache)
-
- setting.performance_bar_allowed_group_id = group.full_path
-
- expect(setting.performance_bar_allowed_group_id).to eq(group.id)
- end
-
- context 'when the given path is the same' do
- context 'with a blank path' do
- before do
- setting.performance_bar_allowed_group_id = nil
- end
-
- it 'clears the cached allowed user IDs' do
- expect(Gitlab::PerformanceBar).not_to receive(:expire_allowed_user_ids_cache)
-
- setting.performance_bar_allowed_group_id = ''
- end
- end
-
- context 'with a valid path' do
- before do
- setting.performance_bar_allowed_group_id = group.full_path
- end
-
- it 'clears the cached allowed user IDs' do
- expect(Gitlab::PerformanceBar).not_to receive(:expire_allowed_user_ids_cache)
-
- setting.performance_bar_allowed_group_id = group.full_path
- end
- end
- end
- end
- end
-
describe 'performance_bar_allowed_group' do
context 'with no performance_bar_allowed_group_id saved' do
it 'returns nil' do
@@ -464,11 +402,11 @@ describe ApplicationSetting do
let(:group) { create(:group) }
before do
- setting.performance_bar_allowed_group_id = group.full_path
+ setting.update!(performance_bar_allowed_group_id: group.id)
end
it 'returns the group' do
- expect(setting.performance_bar_allowed_group).to eq(group)
+ expect(setting.reload.performance_bar_allowed_group).to eq(group)
end
end
end
@@ -478,67 +416,11 @@ describe ApplicationSetting do
let(:group) { create(:group) }
before do
- setting.performance_bar_allowed_group_id = group.full_path
+ setting.update!(performance_bar_allowed_group_id: group.id)
end
it 'returns true' do
- expect(setting.performance_bar_enabled).to be_truthy
- end
- end
- end
-
- describe 'performance_bar_enabled=' do
- context 'when the performance bar is enabled' do
- let(:group) { create(:group) }
-
- before do
- setting.performance_bar_allowed_group_id = group.full_path
- end
-
- context 'when passing true' do
- it 'does not clear allowed user IDs cache' do
- expect(Gitlab::PerformanceBar).not_to receive(:expire_allowed_user_ids_cache)
-
- setting.performance_bar_enabled = true
-
- expect(setting.performance_bar_allowed_group_id).to eq(group.id)
- expect(setting.performance_bar_enabled).to be_truthy
- end
- end
-
- context 'when passing false' do
- it 'disables the performance bar and clears allowed user IDs cache' do
- expect(Gitlab::PerformanceBar).to receive(:expire_allowed_user_ids_cache)
-
- setting.performance_bar_enabled = false
-
- expect(setting.performance_bar_allowed_group_id).to be_nil
- expect(setting.performance_bar_enabled).to be_falsey
- end
- end
- end
-
- context 'when the performance bar is disabled' do
- context 'when passing true' do
- it 'does nothing and does not clear allowed user IDs cache' do
- expect(Gitlab::PerformanceBar).not_to receive(:expire_allowed_user_ids_cache)
-
- setting.performance_bar_enabled = true
-
- expect(setting.performance_bar_allowed_group_id).to be_nil
- expect(setting.performance_bar_enabled).to be_falsey
- end
- end
-
- context 'when passing false' do
- it 'does nothing and does not clear allowed user IDs cache' do
- expect(Gitlab::PerformanceBar).not_to receive(:expire_allowed_user_ids_cache)
-
- setting.performance_bar_enabled = false
-
- expect(setting.performance_bar_allowed_group_id).to be_nil
- expect(setting.performance_bar_enabled).to be_falsey
- end
+ expect(setting.reload.performance_bar_enabled).to be_truthy
end
end
end
diff --git a/spec/models/concerns/batch_destroy_dependent_associations_spec.rb b/spec/models/concerns/batch_destroy_dependent_associations_spec.rb
new file mode 100644
index 00000000000..c16b245bea8
--- /dev/null
+++ b/spec/models/concerns/batch_destroy_dependent_associations_spec.rb
@@ -0,0 +1,60 @@
+require 'spec_helper'
+
+describe BatchDestroyDependentAssociations do
+ class TestProject < ActiveRecord::Base
+ self.table_name = 'projects'
+
+ has_many :builds, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
+ has_many :notification_settings, as: :source, dependent: :delete_all # rubocop:disable Cop/ActiveRecordDependent
+ has_many :pages_domains
+ has_many :todos
+
+ include BatchDestroyDependentAssociations
+ end
+
+ describe '#dependent_associations_to_destroy' do
+ set(:project) { TestProject.new }
+
+ it 'returns the right associations' do
+ expect(project.dependent_associations_to_destroy.map(&:name)).to match_array([:builds])
+ end
+ end
+
+ describe '#destroy_dependent_associations_in_batches' do
+ set(:project) { create(:project) }
+ set(:build) { create(:ci_build, project: project) }
+ set(:notification_setting) { create(:notification_setting, project: project) }
+ let!(:todos) { create(:todo, project: project) }
+
+ it 'destroys multiple builds' do
+ create(:ci_build, project: project)
+
+ expect(Ci::Build.count).to eq(2)
+
+ project.destroy_dependent_associations_in_batches
+
+ expect(Ci::Build.count).to eq(0)
+ end
+
+ it 'destroys builds in batches' do
+ expect(project).to receive_message_chain(:builds, :find_each).and_yield(build)
+ expect(build).to receive(:destroy).and_call_original
+
+ project.destroy_dependent_associations_in_batches
+
+ expect(Ci::Build.count).to eq(0)
+ expect(Todo.count).to eq(1)
+ expect(User.count).to be > 0
+ expect(NotificationSetting.count).to eq(User.count)
+ end
+
+ it 'excludes associations' do
+ project.destroy_dependent_associations_in_batches(exclude: [:builds])
+
+ expect(Ci::Build.count).to eq(1)
+ expect(Todo.count).to eq(1)
+ expect(User.count).to be > 0
+ expect(NotificationSetting.count).to eq(User.count)
+ end
+ end
+end
diff --git a/spec/models/concerns/discussion_on_diff_spec.rb b/spec/models/concerns/discussion_on_diff_spec.rb
index 30572ce9332..8cd129dc851 100644
--- a/spec/models/concerns/discussion_on_diff_spec.rb
+++ b/spec/models/concerns/discussion_on_diff_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'
describe DiscussionOnDiff do
- subject { create(:diff_note_on_merge_request).to_discussion }
+ subject { create(:diff_note_on_merge_request, line_number: 18).to_discussion }
describe "#truncated_diff_lines" do
let(:truncated_lines) { subject.truncated_diff_lines }
diff --git a/spec/models/diff_note_spec.rb b/spec/models/diff_note_spec.rb
index fb51c0172ab..8624f0daa4d 100644
--- a/spec/models/diff_note_spec.rb
+++ b/spec/models/diff_note_spec.rb
@@ -84,7 +84,47 @@ describe DiffNote do
end
end
- describe "#diff_file" do
+ describe '#create_diff_file callback' do
+ let(:noteable) { create(:merge_request) }
+ let(:project) { noteable.project }
+
+ context 'merge request' do
+ let!(:diff_note) { create(:diff_note_on_merge_request, project: project, noteable: noteable) }
+
+ it 'creates a diff note file' do
+ expect(diff_note.reload.note_diff_file).to be_present
+ end
+
+ it 'does not create diff note file if it is a reply' do
+ expect { create(:diff_note_on_merge_request, noteable: noteable, in_reply_to: diff_note) }
+ .not_to change(NoteDiffFile, :count)
+ end
+ end
+
+ context 'commit' do
+ let!(:diff_note) { create(:diff_note_on_commit, project: project) }
+
+ it 'creates a diff note file' do
+ expect(diff_note.reload.note_diff_file).to be_present
+ end
+
+ it 'does not create diff note file if it is a reply' do
+ expect { create(:diff_note_on_commit, in_reply_to: diff_note) }
+ .not_to change(NoteDiffFile, :count)
+ end
+ end
+ end
+
+ describe '#diff_file', :clean_gitlab_redis_shared_state do
+ context 'when note_diff_file association exists' do
+ it 'returns persisted diff file data' do
+ diff_file = subject.diff_file
+
+ expect(diff_file.diff.to_hash.with_indifferent_access)
+ .to include(subject.note_diff_file.to_hash)
+ end
+ end
+
context 'when the discussion was created in the diff' do
it 'returns correct diff file' do
diff_file = subject.diff_file
@@ -115,6 +155,26 @@ describe DiffNote do
expect(diff_file.diff_refs).to eq(position.diff_refs)
end
end
+
+ context 'note diff file creation enqueuing' do
+ it 'enqueues CreateNoteDiffFileWorker if it is the first note of a discussion' do
+ subject.note_diff_file.destroy!
+
+ expect(CreateNoteDiffFileWorker).to receive(:perform_async).with(subject.id)
+
+ subject.reload.diff_file
+ end
+
+ it 'does not enqueues CreateNoteDiffFileWorker if not first note of a discussion' do
+ mr = create(:merge_request)
+ diff_note = create(:diff_note_on_merge_request, project: mr.project, noteable: mr)
+ reply_diff_note = create(:diff_note_on_merge_request, in_reply_to: diff_note)
+
+ expect(CreateNoteDiffFileWorker).not_to receive(:perform_async).with(reply_diff_note.id)
+
+ reply_diff_note.reload.diff_file
+ end
+ end
end
describe "#diff_line" do
diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb
index 8ea92410022..c1eac4fa489 100644
--- a/spec/models/event_spec.rb
+++ b/spec/models/event_spec.rb
@@ -50,6 +50,19 @@ describe Event do
end
end
+ describe '#set_last_repository_updated_at' do
+ it 'only updates once every Event::REPOSITORY_UPDATED_AT_INTERVAL minutes' do
+ last_known_timestamp = (Event::REPOSITORY_UPDATED_AT_INTERVAL - 1.minute).ago
+ project.update(last_repository_updated_at: last_known_timestamp)
+ project.reload # a reload removes fractions of seconds
+
+ expect do
+ create_push_event(project, project.owner)
+ project.reload
+ end.not_to change { project.last_repository_updated_at }
+ end
+ end
+
describe 'after_create :track_user_interacted_projects' do
let(:event) { build(:push_event, project: project, author: project.owner) }
diff --git a/spec/models/internal_id_spec.rb b/spec/models/internal_id_spec.rb
index 8ef91e8fab5..581fd0293cc 100644
--- a/spec/models/internal_id_spec.rb
+++ b/spec/models/internal_id_spec.rb
@@ -5,7 +5,7 @@ describe InternalId do
let(:usage) { :issues }
let(:issue) { build(:issue, project: project) }
let(:scope) { { project: project } }
- let(:init) { ->(s) { s.project.issues.maximum(:iid) } }
+ let(:init) { ->(s) { s.project.issues.size } }
context 'validations' do
it { is_expected.to validate_presence_of(:usage) }
@@ -39,29 +39,6 @@ describe InternalId do
end
end
- context 'with an InternalId record present and existing issues with a higher internal id' do
- # This can happen if the old NonatomicInternalId is still in use
- before do
- issues = Array.new(rand(1..10)).map { create(:issue, project: project) }
-
- issue = issues.last
- issue.iid = issues.map { |i| i.iid }.max + 1
- issue.save
- end
-
- let(:maximum_iid) { project.issues.map { |i| i.iid }.max }
-
- it 'updates last_value to the maximum internal id present' do
- subject
-
- expect(described_class.find_by(project: project, usage: described_class.usages[usage.to_s]).last_value).to eq(maximum_iid + 1)
- end
-
- it 'returns next internal id correctly' do
- expect(subject).to eq(maximum_iid + 1)
- end
- end
-
context 'with concurrent inserts on table' do
it 'looks up the record if it was created concurrently' do
args = { **scope, usage: described_class.usages[usage.to_s] }
@@ -104,8 +81,7 @@ describe InternalId do
describe '#increment_and_save!' do
let(:id) { create(:internal_id) }
- let(:maximum_iid) { nil }
- subject { id.increment_and_save!(maximum_iid) }
+ subject { id.increment_and_save! }
it 'returns incremented iid' do
value = id.last_value
@@ -126,14 +102,5 @@ describe InternalId do
expect(subject).to eq(1)
end
end
-
- context 'with maximum_iid given' do
- let(:id) { create(:internal_id, last_value: 1) }
- let(:maximum_iid) { id.last_value + 10 }
-
- it 'returns maximum_iid instead' do
- expect(subject).to eq(12)
- end
- end
end
end
diff --git a/spec/models/note_diff_file_spec.rb b/spec/models/note_diff_file_spec.rb
new file mode 100644
index 00000000000..591c1a89748
--- /dev/null
+++ b/spec/models/note_diff_file_spec.rb
@@ -0,0 +1,11 @@
+require 'rails_helper'
+
+describe NoteDiffFile do
+ describe 'associations' do
+ it { is_expected.to belong_to(:diff_note) }
+ end
+
+ describe 'validations' do
+ it { is_expected.to validate_presence_of(:diff_note) }
+ end
+end