Skip to content

Commit

Permalink
adds in unit tests for the pathway.pre_save and step.pre_save #1573
Browse files Browse the repository at this point in the history
  • Loading branch information
fredkingham committed Jul 28, 2018
1 parent edc5756 commit 00e743b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 37 deletions.
19 changes: 0 additions & 19 deletions opal/core/pathway/pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions opal/core/pathway/tests/pathway_test/pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
66 changes: 48 additions & 18 deletions opal/core/pathway/tests/test_pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -31,15 +33,17 @@ 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"

steps = (
FamousLastWords,
)


class OveridePathway(Pathway):

@classmethod
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
)

Expand All @@ -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())
Expand All @@ -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"
)
Expand All @@ -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'))
Expand Down
36 changes: 36 additions & 0 deletions opal/core/pathway/tests/test_steps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
unittests for opal.core.pathway.steps
"""
import copy
from django.core.urlresolvers import reverse
from mock import MagicMock

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 00e743b

Please sign in to comment.