From 00e743b33f547aaf8a8e5665d24c995f4231ba65 Mon Sep 17 00:00:00 2001 From: fredkingham Date: Sat, 28 Jul 2018 17:48:47 +0100 Subject: [PATCH] adds in unit tests for the pathway.pre_save and step.pre_save #1573 --- opal/core/pathway/pathways.py | 19 ------ .../pathway/tests/pathway_test/pathways.py | 3 + opal/core/pathway/tests/test_pathways.py | 66 ++++++++++++++----- opal/core/pathway/tests/test_steps.py | 36 ++++++++++ 4 files changed, 87 insertions(+), 37 deletions(-) diff --git a/opal/core/pathway/pathways.py b/opal/core/pathway/pathways.py index bd547cd9a..fa7d87af2 100644 --- a/opal/core/pathway/pathways.py +++ b/opal/core/pathway/pathways.py @@ -104,24 +104,6 @@ def redirect_url(self, user=None, patient=None, episode=None): def pre_save(self, data, raw_data, user, patient=None, episode=None): pass - def sanity_check_data(self, data, raw_data, episode, user): - """ A Sanity check to make sure that what we're - saving now is the same as what we would have - been saving before the step change. - """ - data_copy = copy.copy(raw_data) - if episode: - data_copy = self.remove_unchanged_subrecords( - episode, data_copy, user - ) - for k, v in data_copy.items(): - if v and k not in data: - raise ValueError( - "Data has changed for {} with {}".format( - self.slug, k - ) - ) - @transaction.atomic def save(self, raw_data, user=None, patient=None, episode=None): if patient and not episode: @@ -146,7 +128,6 @@ def save(self, raw_data, user=None, patient=None, episode=None): patient = Patient() - self.sanity_check_data(data, raw_data, episode, user) patient.bulk_update(data, user, episode=episode) if not episode and patient.episode_set.count() == 1: diff --git a/opal/core/pathway/tests/pathway_test/pathways.py b/opal/core/pathway/tests/pathway_test/pathways.py index a257e762d..ca6b0621b 100644 --- a/opal/core/pathway/tests/pathway_test/pathways.py +++ b/opal/core/pathway/tests/pathway_test/pathways.py @@ -8,6 +8,9 @@ class SomeComplicatedStep(steps.Step): step_controller = "SomeController" template = "Sometemplate.html" + def pre_save(self, *args, **kwargs): + pass + class PagePathwayExample(pathways.PagePathway): display_name = "Dog Owner" diff --git a/opal/core/pathway/tests/test_pathways.py b/opal/core/pathway/tests/test_pathways.py index 07f79f30f..462cb4a5f 100644 --- a/opal/core/pathway/tests/test_pathways.py +++ b/opal/core/pathway/tests/test_pathways.py @@ -13,7 +13,9 @@ from opal.tests.models import ( DogOwner, Colour, PatientColour, FamousLastWords ) -from opal.core.pathway.tests.pathway_test.pathways import PagePathwayExample +from opal.core.pathway.tests.pathway_test.pathways import ( + PagePathwayExample, WizardPathwayExample +) from opal.core.pathway.steps import Step, delete_others @@ -31,8 +33,9 @@ class PathwayExample(pathways.Pathway): Step(model=DogOwner), ) -class ColourPathway(Pathway): - display_name = "colour" + +class FamousLastWordsPathway(Pathway): + display_name = "Famous Last Words" icon = "fa fa-something" template_url = "/somewhere" @@ -40,6 +43,7 @@ class ColourPathway(Pathway): FamousLastWords, ) + class OveridePathway(Pathway): @classmethod @@ -147,14 +151,22 @@ def test_init_raises(self): def test_pre_save_no_delete(self): multi_save = Step(model=Colour, multiple=True, delete_others=False) multi_save.pre_save( - {'colour': []}, Colour, patient=self.patient, episode=self.episode + {}, + {'colour': []}, + Colour, + patient=self.patient, + episode=self.episode ) self.assertEqual(Colour.objects.get().id, self.existing_colour.id) def test_pre_save_with_delete(self): multi_save = Step(model=Colour, multiple=True) multi_save.pre_save( - {'colour': []}, Colour, patient=self.patient, episode=self.episode + {}, + {'colour': []}, + Colour, + patient=self.patient, + episode=self.episode ) self.assertEqual(Colour.objects.count(), 0) @@ -256,11 +268,19 @@ def test_existing_patient_new_episode_save(self): DogOwner.objects.filter(episode_id=episode.id).exists() ) + def test_pre_save_called(self): + pathway = PathwayExample() + with mock.patch.object(pathway, 'pre_save') as ps: + patient, episode = self.new_patient_and_episode_please() + post_data = {"demographics": [{"hospital_number": "101"}]} + pathway.save(raw_data=post_data, user=self.user, patient=patient) + self.assertTrue(ps.called) + def test_users_patient_passed_in(self): - pathway = PagePathwayExample() + pathway = PathwayExample() patient, episode = self.new_patient_and_episode_please() post_data = {"demographics": [{"hospital_number": "101"}]} - pathway.save(data=post_data, user=self.user, patient=patient) + pathway.save(raw_data=post_data, user=self.user, patient=patient) demographics = patient.demographics() self.assertEqual( demographics.hospital_number, @@ -272,7 +292,10 @@ def test_users_episode_passed_in(self): patient, episode = self.new_patient_and_episode_please() post_data = {"dog_owner": [{"name": "fido"}]} pathway.save( - data=post_data, user=self.user, patient=patient, episode=episode + raw_data=post_data, + user=self.user, + patient=patient, + episode=episode ) self.assertEqual( episode.dogowner_set.get().name, @@ -313,7 +336,7 @@ def test_existing_patient_existing_episode_save(self): class TestRemoveUnChangedSubrecords(OpalTestCase): def setUp(self): self.patient, self.episode = self.new_patient_and_episode_please() - self.pathway_example = ColourPathway() + self.pathway_example = FamousLastWordsPathway() def test_dont_update_subrecords_that_havent_changed(self, subrecords): subrecords.return_value = [Colour] @@ -430,7 +453,7 @@ def test_integration(self, subrecords): ) dumped = json.loads(json.dumps(provided_dict, cls=OpalSerializer)) - self.pathway_example.save( + WizardPathwayExample().save( dumped, self.user, self.patient, self.episode ) @@ -450,13 +473,18 @@ def setUp(self): self.patient, self.episode = self.new_patient_and_episode_please() def test_get_slug(self): - self.assertEqual('colourpathway', ColourPathway.get_slug()) + self.assertEqual( + 'famouslastwordspathway', FamousLastWordsPathway.get_slug() + ) def test_get_slug_from_attribute(self): self.assertEqual('dog-owner', PathwayExample.get_slug()) def test_get_absolute_url(self): - self.assertEqual('/pathway/#/colourpathway/', ColourPathway.get_absolute_url()) + self.assertEqual( + '/pathway/#/famouslastwordspathway/', + FamousLastWordsPathway.get_absolute_url() + ) def test_get_icon(self): self.assertEqual('fa fa-tintin', PathwayExample.get_icon()) @@ -465,14 +493,14 @@ def test_get_display_name(self): self.assertEqual('Dog Owner', PathwayExample.get_display_name()) def test_as_menuitem(self): - menu = ColourPathway.as_menuitem() - self.assertEqual('/pathway/#/colourpathway/', menu.href) - self.assertEqual('/pathway/#/colourpathway/', menu.activepattern) + menu = FamousLastWordsPathway.as_menuitem() + self.assertEqual('/pathway/#/famouslastwordspathway/', menu.href) + self.assertEqual('/pathway/#/famouslastwordspathway/', menu.activepattern) self.assertEqual('fa fa-something', menu.icon) - self.assertEqual('colour', menu.display) + self.assertEqual('Famous Last Words', menu.display) def test_as_menuitem_from_kwargs(self): - menu = ColourPathway.as_menuitem( + menu = FamousLastWordsPathway.as_menuitem( href="/Blue", activepattern="/B", icon="fa-sea", display="Bleu" ) @@ -490,7 +518,9 @@ def test_as_menuitem_uses_getter_for_display(self): self.assertEqual('Overridden', menu.display) def test_slug(self): - self.assertEqual('colourpathway', ColourPathway().slug) + self.assertEqual( + 'famouslastwordspathway', FamousLastWordsPathway().slug + ) def test_get_by_hyphenated_slug(self): self.assertEqual(PathwayExample, Pathway.get('dog-owner')) diff --git a/opal/core/pathway/tests/test_steps.py b/opal/core/pathway/tests/test_steps.py index 9f8c794aa..57d0718f7 100644 --- a/opal/core/pathway/tests/test_steps.py +++ b/opal/core/pathway/tests/test_steps.py @@ -1,6 +1,7 @@ """ unittests for opal.core.pathway.steps """ +import copy from django.core.urlresolvers import reverse from mock import MagicMock @@ -127,6 +128,41 @@ def test_model_has_no_form_template(self): with self.assertRaises(exceptions.MissingTemplateError): step.get_template() + def test_pre_save_defined(self): + step = Step( + template="some_template.html", + display_name="A Step", + api_name="a_step" + ) + + with self.assertRaises(NotImplementedError) as er: + step.pre_save({}, {}, self.user) + self.assertEqual( + str(er.exception), + "No pre_save step defined for a_step" + ) + + def test_pre_save(self): + class SomeStep(Step): + model = test_models.Colour + + step = SomeStep() + data = {} + colour_data = { + test_models.Colour.get_api_name(): [ + dict(name="blue"), + dict(name="green") + ], + } + raw_data = copy.copy(colour_data) + raw_data["other"] = [ + {"other": "data"} + ] + step.pre_save(data, raw_data, self.user) + self.assertEqual( + data, colour_data + ) + class HelpTextStepTestCase(OpalTestCase): def test_get_help_text(self):