aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lib
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib')
-rw-r--r--spec/lib/health_checks/checks/days_ago_check_spec.rb66
-rw-r--r--spec/lib/health_checks/health_checkable_spec.rb128
-rw-r--r--spec/lib/health_checks/health_checks_spec.rb77
-rw-r--r--spec/lib/public_body_categories_spec.rb42
4 files changed, 271 insertions, 42 deletions
diff --git a/spec/lib/health_checks/checks/days_ago_check_spec.rb b/spec/lib/health_checks/checks/days_ago_check_spec.rb
new file mode 100644
index 000000000..33b4642cd
--- /dev/null
+++ b/spec/lib/health_checks/checks/days_ago_check_spec.rb
@@ -0,0 +1,66 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
+
+describe HealthChecks::Checks::DaysAgoCheck do
+ include HealthChecks::Checks
+
+ it { should be_kind_of(HealthChecks::HealthCheckable) }
+
+ it 'defaults to comparing to one day ago' do
+ check = HealthChecks::Checks::DaysAgoCheck.new
+ expect(check.days).to eq(1)
+ end
+
+ it 'accepts a custom number of days' do
+ check = HealthChecks::Checks::DaysAgoCheck.new(:days => 4)
+ expect(check.days).to eq(4)
+ end
+
+ describe :check do
+
+ it 'is successful if the subject is in the last day' do
+ check = HealthChecks::Checks::DaysAgoCheck.new { Time.now }
+ expect(check.check).to be_true
+ end
+
+ it 'fails if the subject is over a day ago' do
+ check = HealthChecks::Checks::DaysAgoCheck.new { 2.days.ago }
+ expect(check.check).to be_false
+ end
+
+ end
+
+ describe :failure_message do
+
+ it 'includes the check subject in the default message' do
+ subject = 2.days.ago
+ check = HealthChecks::Checks::DaysAgoCheck.new { subject }
+ expect(check.failure_message).to include(subject.to_s)
+ end
+
+ it 'includes the check subject in a custom message' do
+ params = { :failure_message => 'This check failed' }
+ subject = 2.days.ago
+ check = HealthChecks::Checks::DaysAgoCheck.new(params) { subject }
+ expect(check.failure_message).to include(subject.to_s)
+ end
+
+ end
+
+ describe :success_message do
+
+ it 'includes the check subject in the default message' do
+ subject = Time.now
+ check = HealthChecks::Checks::DaysAgoCheck.new { subject }
+ expect(check.failure_message).to include(subject.to_s)
+ end
+
+ it 'includes the check subject in a custom message' do
+ params = { :success_message => 'This check succeeded' }
+ subject = Time.now
+ check = HealthChecks::Checks::DaysAgoCheck.new(params) { subject }
+ expect(check.success_message).to include(subject.to_s)
+ end
+
+ end
+
+end
diff --git a/spec/lib/health_checks/health_checkable_spec.rb b/spec/lib/health_checks/health_checkable_spec.rb
new file mode 100644
index 000000000..abfeb5c21
--- /dev/null
+++ b/spec/lib/health_checks/health_checkable_spec.rb
@@ -0,0 +1,128 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe HealthChecks::HealthCheckable do
+
+ before(:each) do
+ class MockCheck
+ include HealthChecks::HealthCheckable
+ end
+ @subject = MockCheck.new
+ end
+
+ describe :initialize do
+
+ it 'allows a custom failure message to be set' do
+ @subject = MockCheck.new(:failure_message => 'F')
+ expect(@subject.failure_message).to eq('F')
+ end
+
+ it 'allows a custom success message to be set' do
+ @subject = MockCheck.new(:success_message => 'S')
+ expect(@subject.success_message).to eq('S')
+ end
+
+ end
+
+ describe :name do
+
+ it 'returns the name of the check' do
+ expect(@subject.name).to eq('MockCheck')
+ end
+
+ end
+
+ describe :check do
+
+ it 'is intended to be overridden by the includer' do
+ expect{ @subject.check }.to raise_error(NotImplementedError)
+ end
+
+ end
+
+ describe :ok? do
+
+ it 'returns true if the check was successful' do
+ @subject.stub(:check => true)
+ expect(@subject.ok?).to be_true
+ end
+
+ it 'returns false if the check failed' do
+ @subject.stub(:check => false)
+ expect(@subject.ok?).to be_false
+ end
+
+ end
+
+ describe :failure_message do
+
+ it 'returns a default message if one has not been set' do
+ expect(@subject.failure_message).to eq('Failed')
+ end
+
+ end
+
+ describe :failure_message= do
+
+ it 'allows a custom failure message to be set' do
+ @subject.failure_message = 'F'
+ expect(@subject.failure_message).to eq('F')
+ end
+
+ end
+
+ describe :success_message do
+
+ it 'returns a default message if one has not been set' do
+ expect(@subject.success_message).to eq('Success')
+ end
+
+ end
+
+ describe :success_message= do
+
+ it 'allows a custom success message to be set' do
+ @subject.success_message = 'S'
+ expect(@subject.success_message).to eq('S')
+ end
+
+ end
+
+ describe :message do
+
+ context 'if the check succeeds' do
+
+ before(:each) do
+ @subject.stub(:check => true)
+ end
+
+ it 'returns the default success message' do
+ expect(@subject.message).to eq('Success')
+ end
+
+ it 'returns a custom success message if one has been set' do
+ @subject.success_message = 'Custom Success'
+ expect(@subject.message).to eq('Custom Success')
+ end
+
+ end
+
+ context 'if the check fails' do
+
+ before(:each) do
+ @subject.stub(:check => false)
+ end
+
+ it 'returns the default failure message' do
+ expect(@subject.message).to eq('Failed')
+ end
+
+ it 'returns a custom failure message if one has been set' do
+ @subject.failure_message = 'Custom Failed'
+ expect(@subject.message).to eq('Custom Failed')
+ end
+
+ end
+
+ end
+
+end
diff --git a/spec/lib/health_checks/health_checks_spec.rb b/spec/lib/health_checks/health_checks_spec.rb
new file mode 100644
index 000000000..c7037b813
--- /dev/null
+++ b/spec/lib/health_checks/health_checks_spec.rb
@@ -0,0 +1,77 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe HealthChecks do
+ include HealthChecks
+
+ describe :add do
+
+ it 'adds a check to the collection and returns the check' do
+ check = double('MockCheck', :check => true)
+ expect(add(check)).to eq(check)
+ end
+
+ it 'does not add checks that do not define the check method' do
+ check = double('BadCheck')
+ expect(add(check)).to eq(false)
+ end
+
+ end
+
+ describe :all do
+
+ it 'returns all the checks' do
+ check1 = double('MockCheck', :check => true)
+ check2 = double('AnotherCheck', :check => false)
+ add(check1)
+ add(check2)
+ expect(all).to include(check1, check2)
+ end
+
+ end
+
+ describe :each do
+
+ it 'iterates over each check' do
+ expect(subject).to respond_to(:each)
+ end
+
+ end
+
+ describe :ok? do
+
+ it 'returns true if all checks are ok' do
+ checks = [
+ double('MockCheck', :ok? => true),
+ double('FakeCheck', :ok? => true),
+ double('TestCheck', :ok? => true)
+ ]
+ HealthChecks.stub(:all => checks)
+
+ expect(HealthChecks.ok?).to be_true
+ end
+
+ it 'returns false if all checks fail' do
+ checks = [
+ double('MockCheck', :ok? => false),
+ double('FakeCheck', :ok? => false),
+ double('TestCheck', :ok? => false)
+ ]
+ HealthChecks.stub(:all => checks)
+
+ expect(HealthChecks.ok?).to be_false
+ end
+
+ it 'returns false if a single check fails' do
+ checks = [
+ double('MockCheck', :ok? => true),
+ double('FakeCheck', :ok? => false),
+ double('TestCheck', :ok? => true)
+ ]
+ HealthChecks.stub(:all => checks)
+
+ expect(HealthChecks.ok?).to be_false
+ end
+
+ end
+
+end
diff --git a/spec/lib/public_body_categories_spec.rb b/spec/lib/public_body_categories_spec.rb
deleted file mode 100644
index e53d9a028..000000000
--- a/spec/lib/public_body_categories_spec.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-
-describe PublicBodyCategories do
-
- before do
- load_test_categories
- end
-
- describe 'when asked for categories with headings' do
-
- it 'should return a list of headings as plain strings, each followed by n tag specifications as
- lists in the form:
- ["tag_to_use_as_category", "Sub category title", "Instance description"]' do
- expected_categories = ["Local and regional", ["local_council",
- "Local councils",
- "a local council"],
- "Miscellaneous", ["other",
- "Miscellaneous",
- "miscellaneous"]]
- PublicBodyCategories::get().with_headings().should == expected_categories
- end
-
- end
-
- describe 'when asked for headings' do
-
- it 'should return a list of headings' do
- PublicBodyCategories::get().headings().should == ['Local and regional', 'Miscellaneous']
- end
-
- end
-
- describe 'when asked for tags by headings' do
-
- it 'should return a hash of tags keyed by heading' do
- PublicBodyCategories::get().by_heading().should == {'Local and regional' => ['local_council'],
- 'Miscellaneous' => ['other']}
- end
-
- end
-
-end \ No newline at end of file