summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorGabriel Mazetto <brodock@gmail.com>2018-07-05 17:46:10 +0200
committerGabriel Mazetto <brodock@gmail.com>2018-07-24 18:44:07 +0200
commitc084e87ad7be45f39a79347b306f03f618705049 (patch)
treec6825ff5b5de1922d9d1ac042045a77a73c89b4a /spec
parent08d7ee65e7a16d6898d61758bffc70899b574065 (diff)
downloadgitlab-ce-c084e87ad7be45f39a79347b306f03f618705049.tar.gz
Added SiteStatistics as counter cache for Projects and Wikis
Diffstat (limited to 'spec')
-rw-r--r--spec/factories/projects.rb2
-rw-r--r--spec/factories/site_statistics.rb7
-rw-r--r--spec/models/project_feature_spec.rb42
-rw-r--r--spec/models/project_spec.rb16
-rw-r--r--spec/models/site_statistic_spec.rb83
5 files changed, 149 insertions, 1 deletions
diff --git a/spec/factories/projects.rb b/spec/factories/projects.rb
index fec1bea2751..1215b04913e 100644
--- a/spec/factories/projects.rb
+++ b/spec/factories/projects.rb
@@ -34,7 +34,7 @@ FactoryBot.define do
builds_access_level = [evaluator.builds_access_level, evaluator.repository_access_level].min
merge_requests_access_level = [evaluator.merge_requests_access_level, evaluator.repository_access_level].min
- project.project_feature.update_columns(
+ project.project_feature.update(
wiki_access_level: evaluator.wiki_access_level,
builds_access_level: builds_access_level,
snippets_access_level: evaluator.snippets_access_level,
diff --git a/spec/factories/site_statistics.rb b/spec/factories/site_statistics.rb
new file mode 100644
index 00000000000..dd8c795515a
--- /dev/null
+++ b/spec/factories/site_statistics.rb
@@ -0,0 +1,7 @@
+FactoryBot.define do
+ factory :site_statistics, class: 'SiteStatistic' do
+ id 1
+ repositories_count 999
+ wikis_count 555
+ end
+end
diff --git a/spec/models/project_feature_spec.rb b/spec/models/project_feature_spec.rb
index cd7f77024da..10617edec0f 100644
--- a/spec/models/project_feature_spec.rb
+++ b/spec/models/project_feature_spec.rb
@@ -119,4 +119,46 @@ describe ProjectFeature do
end
end
end
+
+ context 'Site Statistics' do
+ set(:project_with_wiki) { create(:project, :wiki_enabled) }
+ set(:project_without_wiki) { create(:project, :wiki_disabled) }
+
+ context 'when creating a project' do
+ it 'tracks wiki availability when wikis are enabled by default' do
+ expect { create(:project) }.to change { SiteStatistic.fetch.wikis_count }.by(1)
+ end
+
+ it 'does not track wiki availability when wikis are disabled by default' do
+ expect { create(:project, :wiki_disabled) }.not_to change { SiteStatistic.fetch.wikis_count }
+ end
+ end
+
+ context 'when updating a project_feature' do
+ it 'untracks wiki availability when disabling wiki access' do
+ expect { project_with_wiki.project_feature.update_attribute(:wiki_access_level, ProjectFeature::DISABLED) }
+ .to change { SiteStatistic.fetch.wikis_count }.by(-1)
+ end
+
+ it 'tracks again wiki availability when re-enabling wiki access as public' do
+ expect { project_without_wiki.project_feature.update_attribute(:wiki_access_level, ProjectFeature::ENABLED) }
+ .to change { SiteStatistic.fetch.wikis_count }.by(1)
+ end
+
+ it 'tracks again wiki availability when re-enabling wiki access as private' do
+ expect { project_without_wiki.project_feature.update_attribute(:wiki_access_level, ProjectFeature::PRIVATE) }
+ .to change { SiteStatistic.fetch.wikis_count }.by(1)
+ end
+ end
+
+ context 'when removing a project' do
+ it 'untracks wiki availability when removing a project with previous wiki access' do
+ expect { project_with_wiki.destroy }.to change { SiteStatistic.fetch.wikis_count }.by(-1)
+ end
+
+ it 'does not untrack wiki availability when removing a project without wiki access' do
+ expect { project_without_wiki.destroy }.not_to change { SiteStatistic.fetch.wikis_count }
+ end
+ end
+ end
end
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index b0ec725bf70..88442247093 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -102,6 +102,22 @@ describe Project do
end
end
+ context 'Site Statistics' do
+ context 'when creating a new project' do
+ it 'tracks project in SiteStatistic' do
+ expect { create(:project) }.to change { SiteStatistic.fetch.repositories_count }.by(1)
+ end
+ end
+
+ context 'when deleting a project' do
+ it 'untracks project in SiteStatistic' do
+ project = create(:project)
+
+ expect { project.destroy }.to change { SiteStatistic.fetch.repositories_count }.by(-1)
+ end
+ end
+ end
+
context 'updating cd_cd_settings' do
it 'does not raise an error' do
project = create(:project)
diff --git a/spec/models/site_statistic_spec.rb b/spec/models/site_statistic_spec.rb
new file mode 100644
index 00000000000..9b056fbf332
--- /dev/null
+++ b/spec/models/site_statistic_spec.rb
@@ -0,0 +1,83 @@
+require 'spec_helper'
+
+describe SiteStatistic do
+ describe '.fetch' do
+ context 'existing record' do
+ it 'returns existing SiteStatistic model' do
+ statistics = create(:site_statistics)
+
+ expect(described_class.fetch).to be_a(described_class)
+ expect(described_class.fetch).to eq(statistics)
+ end
+ end
+
+ context 'non existing record' do
+ it 'creates a new SiteStatistic model' do
+ expect(described_class.first).to be_nil
+ expect(described_class.fetch).to be_a(described_class)
+ end
+ end
+ end
+
+ describe '.track' do
+ context 'with allowed attributes' do
+ let(:statistics) { create(:site_statistics) }
+
+ it 'increases the attribute counter' do
+ expect { described_class.track('repositories_count') }.to change { statistics.reload.repositories_count }.by(1)
+ expect { described_class.track('wikis_count') }.to change { statistics.reload.wikis_count }.by(1)
+ end
+
+ it 'doesnt increase the attribute counter when an exception happens during transaction' do
+ expect do
+ begin
+ described_class.transaction do
+ described_class.track('repositories_count')
+
+ raise StandardError
+ end
+ rescue StandardError
+ # no-op
+ end
+ end.not_to change { statistics.reload.repositories_count }
+ end
+ end
+
+ context 'with not allowed attributes' do
+ it 'returns error' do
+ expect { described_class.track('something_else') }.to raise_error(ArgumentError).with_message(/Invalid attribute: \'something_else\' to \'track\' method/)
+ end
+ end
+ end
+
+ describe '.untrack' do
+ context 'with allowed attributes' do
+ let(:statistics) { create(:site_statistics) }
+
+ it 'decreases the attribute counter' do
+ expect { described_class.untrack('repositories_count') }.to change { statistics.reload.repositories_count }.by(-1)
+ expect { described_class.untrack('wikis_count') }.to change { statistics.reload.wikis_count }.by(-1)
+ end
+
+ it 'doesnt decrease the attribute counter when an exception happens during transaction' do
+ expect do
+ begin
+ described_class.transaction do
+ described_class.track('repositories_count')
+
+ raise StandardError
+ end
+ rescue StandardError
+ # no-op
+ end
+ end.not_to change { described_class.fetch.repositories_count }
+ end
+ end
+
+ context 'with not allowed attributes' do
+ it 'returns error' do
+ expect { described_class.untrack('something_else') }.to raise_error(ArgumentError).with_message(/Invalid attribute: \'something_else\' to \'untrack\' method/)
+ end
+ end
+ end
+end