Skip to content

Commit

Permalink
migrated unit tests to MiniTest::Unit
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskottom committed Oct 23, 2011
1 parent ab8e545 commit 539e350
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 45 deletions.
132 changes: 132 additions & 0 deletions test/spec/patient_spec.rb
@@ -0,0 +1,132 @@
require "test_helper"

class PatientTest < MiniTest::Unit::TestCase

def test_should_not_allow_more_than_2_digits_in_state_field
patient = Factory.build(:patient, :state => "CTZ")

patient.save

assert patient.errors.invalid?(:state)

patient.state = "CT"

patient.save

assert !patient.errors.invalid?(:state)
end

def test_should_not_allow_more_than_10_digits_in_zip_field
patient = Factory.build(:patient, :zip => "1234567890!")

patient.save

assert patient.errors.invalid?(:zip),
"More than 10 digits allowed for zip"

patient.zip = "1234567890"

patient.save

assert !patient.errors.invalid?(:zip),
"10 or less digits are causing validation problems"
end

def test_time_in_pain_should_set_the_pain_length_in_days_attribute
valid_formats = {
"12d" => 12,
"1m" => 30,
"45 weeks" => 315,
"1 year" => 365,
"4 Months" => 120,
"1W " => 7,
"6Months" => 180,
"0days" => 0,
"03w" => 21,
"1.5months" => 30,
"0.5days" => 1,
".9 Months" => 0
}

patient = TestHelper.valid_patient

valid_formats.each do |format, result|
patient.time_in_pain = format

assert patient.save, "Couldn't save Patient with time_in_pain value = #{ format }"

assert_equal result, patient.pain_length_in_days
end
end

def test_invalid_time_in_pain_values_should_cause_validation_errors
invalid_formats = ["12dd", "1.5.1m", "45 weeks months", "about a year",
"15 minutes", "aaa", "123z"]

patient = TestHelper.valid_patient

invalid_formats.each do |format|
patient.time_in_pain = format

assert !patient.save, "Patient was saved with time_in_pain format = #{ format }"

assert patient.errors.invalid?(:time_in_pain), "#{format} is not a valid format"
end
end

def test_time_in_pain_validations_should_only_be_run_if_time_in_pain_is_set
patient = TestHelper.valid_patient

assert patient.save

assert !patient.errors.invalid?(:time_in_pain)
end

def test_travel_time_is_calculated
patient = TestHelper.valid_patient

patient.travel_time_minutes = 15

assert_equal 15, patient.travel_time_minutes

assert_equal 15, patient.travel_time

assert patient.save

patient.travel_time_hours = "1"

assert_equal 1, patient.travel_time_hours

assert_equal 75, patient.travel_time

assert patient.save
end

def test_text_entry_for_date_of_birth_should_accept_reasonable_date_formats
valid_formats = {
"05/23/2008" => Date.civil(2008, 5, 23),
"05-23-2008" => Date.civil(2008, 5, 23),
"05.23.2008" => Date.civil(2008, 5, 23),
}

patient = TestHelper.valid_patient

valid_formats.each do |format, result|
patient.date_of_birth = format

assert patient.save, "Couldn't save Patient with date_of_birth value = #{ format }"

assert_equal result, patient.date_of_birth
end
end

def test_text_entry_for_date_of_birth_should_not_accept_invalid_date_formats
patient = TestHelper.valid_patient

patient.date_of_birth = "23/1/1"

assert !patient.save,
"Saved patient with an invalid date_of_birth value"

end
end
43 changes: 22 additions & 21 deletions test/test_helper.rb
Expand Up @@ -5,12 +5,15 @@
require "minitest/autorun"


# Modified slightly and borrowed from MiniTest README.
# See: http://rdoc.info/github/seattlerb/minitest/master/file/README.txt
# Section: Customizable Test Runner Types
module MiniTestWithHooks
class Unit < MiniTest::Unit
def before_suites; end
def before_suite; end
def after_suites; end
def after_suite; end
def before_suites; end # run before all tests cases
def before_suite; end # run before all test suites
def after_suites; end # run after all test cases
def after_suite; end # run after all test suites

def _run_suites(suites, type)
begin
Expand All @@ -33,6 +36,7 @@ def _run_suite(suite, type)
end
MiniTest::Unit.runner = MiniTestWithHooks::Unit.new


module TestHelper
extend self;

Expand All @@ -50,9 +54,6 @@ def clinic_date(time=nil)
def valid_patient
Factory.build(:patient)
end
end

class MiniTest::Unit::TestCase

def create_test_patients(date=Date.today)
(6..17).map { |i| date + i.hours }.each do |datetime|
Expand All @@ -61,23 +62,23 @@ def create_test_patients(date=Date.today)
end

def create_test_prescriptions
@amoxicillin = Factory(:prescription,
:name => "Amoxicillin",
:quantity => 21,
:dosage => "500mg",
:cost => 12.99,
:strength => "1 YID x 7days")
Factory(:prescription,
:name => "Amoxicillin",
:quantity => 21,
:dosage => "500mg",
:cost => 12.99,
:strength => "1 YID x 7days")
end

def create_test_procedures
@oral_exam = Factory(:procedure,
:description => "Comp. Oral Exam",
:code => 150,
:cost => 90)
@pan_film = Factory(:procedure,
:description => "Panoramic film",
:code => 330,
:cost => 125)
Factory(:procedure,
:description => "Comp. Oral Exam",
:code => 150,
:cost => 90)
Factory(:procedure,
:description => "Panoramic film",
:code => 330,
:cost => 125)
end

def create_test_xray(time, patient)
Expand Down
37 changes: 22 additions & 15 deletions test/unit/clinic_summary_test.rb
@@ -1,31 +1,34 @@
require "test_helper"

class ClinicSummaryTest < ActiveSupport::TestCase
setup do
@report_date = TestHelper.clinic_date

create_test_prescriptions
create_test_procedures
(0..1).each { |i| create_test_patients(@report_date + i.days) }
class ClinicSummaryTest < MiniTest::Unit::TestCase
def self.before_suite
TestHelper.create_test_prescriptions
TestHelper.create_test_procedures
(0..1).each do |i|
TestHelper.create_test_patients(TestHelper.clinic_date + i.days)
end
end

def setup
@report_date = TestHelper.clinic_date
@patients = Patient.all(:order => "created_at",
:conditions => ["created_at::Date = ?", @report_date])
end

test "should report on the specified date and span" do
def test_should_report_on_the_specified_date_and_span
report = Reports::ClinicSummary.new(@report_date, "All")

assert_equal report.day, @report_date
assert_equal report.span, "All"
end

test "should be reporting all patients checked in" do
def test_should_be_reporting_all_patients_checked_in
report = Reports::ClinicSummary.new(@report_date, "All")

assert_equal @patients.count, report.patient_count
end

test "should report on patients for the selected time span only" do
def test_should_report_on_patients_for_the_selected_time_span_only
Reports::ClinicSummary::TIME_SPANS.each_with_index do |span, index|
if index == 0 # All
expected_patient_count = @patients.count
Expand All @@ -39,10 +42,12 @@ class ClinicSummaryTest < ActiveSupport::TestCase
end
end

test "should report on radiology numbers" do
def test_should_report_on_radiology_numbers
xray_time = TestHelper.clinic_date("8:30 AM")
xray_count = 3
@patients.first(xray_count).each { |p| create_test_xray(xray_time, p) }
@patients.first(xray_count).each do |patient|
TestHelper.create_test_xray(xray_time, patient)
end

report = Reports::ClinicSummary.new(@report_date, xray_time)
assert_equal xray_count, report.xrays
Expand All @@ -51,8 +56,9 @@ class ClinicSummaryTest < ActiveSupport::TestCase
assert_equal 0, report.xrays
end

test "should only report on procedures for the specified day / span" do
@procedures = Procedure.all
def test_should_only_report_on_procedures_for_the_specified_day_or_span
@oral_exam = Procedure.find_by_code(150)
@pan_film = Procedure.find_by_code(330)
PatientProcedure.create(:patient => @patients.first,
:procedure => @oral_exam,
:created_at => TestHelper.clinic_date("8:30 AM"))
Expand All @@ -73,7 +79,8 @@ class ClinicSummaryTest < ActiveSupport::TestCase
assert_equal 215, report.procedure_value
end

test "should only report on prescriptions for the specified day / span" do
def test_should_only_report_on_prescriptions_for_the_specified_day_or_span
@amoxicillin = Prescription.find_by_name("Amoxicillin")
["8:30 AM", "9:30 AM"].each do |time|
PatientPrescription.create(:patient => @patients.first,
:prescription => @amoxicillin,
Expand Down
18 changes: 9 additions & 9 deletions test/unit/patient_test.rb
@@ -1,8 +1,8 @@
require "test_helper"

class PatientTest < ActiveSupport::TestCase
class PatientTest < MiniTest::Unit::TestCase

test "shouldn't allow more than 2 digits in state field" do
def test_should_not_allow_more_than_2_digits_in_state_field
patient = Factory.build(:patient, :state => "CTZ")

patient.save
Expand All @@ -16,7 +16,7 @@ class PatientTest < ActiveSupport::TestCase
assert !patient.errors.invalid?(:state)
end

test "shouldn't allow more than 10 digits in zip field" do
def test_should_not_allow_more_than_10_digits_in_zip_field
patient = Factory.build(:patient, :zip => "1234567890!")

patient.save
Expand All @@ -32,7 +32,7 @@ class PatientTest < ActiveSupport::TestCase
"10 or less digits are causing validation problems"
end

test "time in pain should set the pain_length_in_days attribute" do
def test_time_in_pain_should_set_the_pain_length_in_days_attribute
valid_formats = {
"12d" => 12,
"1m" => 30,
Expand All @@ -59,7 +59,7 @@ class PatientTest < ActiveSupport::TestCase
end
end

test "invalid time in pain values should cause validation errors" do
def test_invalid_time_in_pain_values_should_cause_validation_errors
invalid_formats = ["12dd", "1.5.1m", "45 weeks months", "about a year",
"15 minutes", "aaa", "123z"]

Expand All @@ -74,15 +74,15 @@ class PatientTest < ActiveSupport::TestCase
end
end

test "time in pain validations should only be run if time in pain is set" do
def test_time_in_pain_validations_should_only_be_run_if_time_in_pain_is_set
patient = TestHelper.valid_patient

assert patient.save

assert !patient.errors.invalid?(:time_in_pain)
end

test "travel time is calculated" do
def test_travel_time_is_calculated
patient = TestHelper.valid_patient

patient.travel_time_minutes = 15
Expand All @@ -102,7 +102,7 @@ class PatientTest < ActiveSupport::TestCase
assert patient.save
end

test "text entry for date of birth should accept reasonable date formats" do
def test_text_entry_for_date_of_birth_should_accept_reasonable_date_formats
valid_formats = {
"05/23/2008" => Date.civil(2008, 5, 23),
"05-23-2008" => Date.civil(2008, 5, 23),
Expand All @@ -120,7 +120,7 @@ class PatientTest < ActiveSupport::TestCase
end
end

test "text entry for date of birth should not accept invalid date formats" do
def test_text_entry_for_date_of_birth_should_not_accept_invalid_date_formats
patient = TestHelper.valid_patient

patient.date_of_birth = "23/1/1"
Expand Down

0 comments on commit 539e350

Please sign in to comment.