diff options
author | Gabriel Mazetto <brodock@gmail.com> | 2018-07-05 18:46:10 +0300 |
---|---|---|
committer | Gabriel Mazetto <brodock@gmail.com> | 2018-07-24 19:44:07 +0300 |
commit | c084e87ad7be45f39a79347b306f03f618705049 (patch) | |
tree | c6825ff5b5de1922d9d1ac042045a77a73c89b4a /spec/models/site_statistic_spec.rb | |
parent | 08d7ee65e7a16d6898d61758bffc70899b574065 (diff) |
Added SiteStatistics as counter cache for Projects and Wikis
Diffstat (limited to 'spec/models/site_statistic_spec.rb')
-rw-r--r-- | spec/models/site_statistic_spec.rb | 83 |
1 files changed, 83 insertions, 0 deletions
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 |