Commit e43420a7 authored by Gabriel Couture's avatar Gabriel Couture

Better unittest

parent 7cdf3492
Pipeline #192 passed with stage
in 9 seconds
......@@ -10,7 +10,7 @@ class PrescriptionDoseIsMissingException(Exception):
pass
def is_dvh_has_a_prescription_dose(dvh: DVH) -> bool:
def does_dvh_has_a_prescription_dose(dvh: DVH) -> bool:
"""Check if DVH has a prescription dose.
Args:
......@@ -60,7 +60,7 @@ def convert_dvh_doses_to_relative(dvh: DVH) -> DVH:
if dvh.get_dose_unit() == '%':
return dvh
if not is_dvh_has_a_prescription_dose(dvh):
if not does_dvh_has_a_prescription_dose(dvh):
raise PrescriptionDoseIsMissingException(
'The DVH has no prescription dose. Therefor, relative doses can\'t be calculated.'
)
......@@ -83,7 +83,7 @@ def convert_dvh_doses_to_absolute(dvh: DVH) -> DVH:
if dvh.get_dose_unit() == 'GY':
return dvh
if not is_dvh_has_a_prescription_dose(dvh):
if not does_dvh_has_a_prescription_dose(dvh):
raise PrescriptionDoseIsMissingException(
'The DVH has no prescription dose. Therefor, absolute doses can\'t be calculated from relative doses.'
)
......
......@@ -30,12 +30,12 @@ class TestConversion(unittest.TestCase):
def test_givenADVHWithAPrescriptionDose_whenCheckingIfDVHHasAPrescriptionDose_thenResultIsTrue(self):
self.a_dvh_with_absolute_doses.set_prescription_dose(A_PRESCRIPTION_DOSE)
result = conversion.is_dvh_has_a_prescription_dose(self.a_dvh_with_absolute_doses)
result = conversion.does_dvh_has_a_prescription_dose(self.a_dvh_with_absolute_doses)
self.assertTrue(result)
def test_givenADVH_whenCheckingIfDVHHasAPrescriptionDose_thenResultIsFalse(self):
result = conversion.is_dvh_has_a_prescription_dose(self.a_dvh_with_absolute_doses)
result = conversion.does_dvh_has_a_prescription_dose(self.a_dvh_with_absolute_doses)
self.assertFalse(result)
......
......@@ -40,12 +40,12 @@ class TestConversion(unittest.TestCase):
def test_givenADVHWithAPrescriptionDose_whenCheckingIfDVHHasAPrescriptionDose_thenResultIsTrue(self):
self.a_dvh_with_absolute_doses.get_prescription_dose.return_value = A_PRESCRIPTION_DOSE
result = conversion.is_dvh_has_a_prescription_dose(self.a_dvh_with_absolute_doses)
result = conversion.does_dvh_has_a_prescription_dose(self.a_dvh_with_absolute_doses)
self.assertTrue(result)
def test_givenADVH_whenCheckingIfDVHHasAPrescriptionDose_thenResultIsFalse(self):
result = conversion.is_dvh_has_a_prescription_dose(self.a_dvh_with_absolute_doses)
result = conversion.does_dvh_has_a_prescription_dose(self.a_dvh_with_absolute_doses)
self.assertFalse(result)
......
......@@ -21,7 +21,7 @@ A_DVH_DATA = {
'dose_units': A_VALID_DOSE_UNIT,
'volumes': A_VOLUME_ARRAY,
'volume_units': A_VALID_VOLUME_UNIT,
'prescription_dose': A_PRESCRIPTION_DOSE
'prescription_dose': -1
}
......@@ -59,9 +59,9 @@ class TestDVH(unittest.TestCase):
)
)
@mock.patch('builtins.open')
@mock.patch('json.dump')
def test_givenADVHAndAFilePath_whenWritingToJson_thenJsonDumpFunctionIsCalled(self, mock_open: mock.Mock, mock_json_dump: mock.Mock):
@mock.patch('builtins.open')
def test_givenADVHAndAFilePath_whenWritingToJson_thenJsonDumpFunctionIsCalledWithExpectedArgument(self, mock_open: mock.MagicMock, mock_json_dump: mock.MagicMock):
a_file_path = 'a_file_path.json'
a_dvh = DVH(
doses=A_DOSE_ARRAY,
......@@ -72,15 +72,17 @@ class TestDVH(unittest.TestCase):
a_dvh.write_to_json(a_file_path)
mock_open.assert_called_once()
mock_open.assert_called_once_with(a_file_path, 'w')
mock_json_dump.assert_called_once()
_, passed_dvh_data, _ = mock_json_dump.mock_calls[0]
self.assertEqual(passed_dvh_data[0], A_DVH_DATA)
@mock.patch('builtins.open')
@mock.patch('json.load', lambda f: A_DVH_DATA)
def test_givenAFilePath_whenReadingDVHFromJsonFile_thenResultIsADVH(self, mock_open: mock.Mock):
def test_givenAFilePath_whenReadingDVHFromJsonFile_thenResultIsADVH(self, mock_open: mock.MagicMock):
a_file_path = 'a_file_path.json'
result = DVH.read_from_json(a_file_path)
mock_open.assert_called_once()
mock_open.assert_called_once_with(a_file_path, 'r')
self.assertIsInstance(result, DVH)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment