def test_list_for_tag(self): self.mock_request.query_params = {'tag': 'micro'} self.episode.set_tag_names(['micro'], self.user) expected = models.Episode.objects.serialised(self.user, [self.episode]) response = api.EpisodeViewSet().list(self.mock_request) self.assertEqual(200, response.status_code) self.assertEqual(expected, response.data)
def test_create_sets_tagging_with_falsy_tags(self): pcount = models.Patient.objects.filter( demographics__hospital_number="9999000999").count() self.assertEqual(0, pcount) self.mock_request.data = { "tagging": { "micro": True, 'inpatient': False }, "start": "14/01/2015", "demographics": { "hospital_number": "9999000999", }, "location": { "ward": "West", "bed": "7" } } response = api.EpisodeViewSet().create(self.mock_request) patient = models.Patient.objects.get( demographics__hospital_number="9999000999") episode = patient.episode_set.get() tags = episode.get_tag_names(self.user) self.assertEqual(['micro'], tags) self.assertEqual(1, len(tags))
def test_create_new_patient(self): pcount = models.Patient.objects.filter( demographics__hospital_number="999000999").count() self.assertEqual(0, pcount) self.mock_request.data = { "tagging" :[ { "micro":True }], "date_of_admission" : "14/01/2015", "demographics" : { "hospital_number": "999000999" } } response = api.EpisodeViewSet().create(self.mock_request) episode = models.Episode.objects.get( patient__demographics__hospital_number="999000999" ) self.assertEqual( episode.created_by, self.mock_request.user ) self.assertEqual( episode.created.date(), timezone.now().date() ) self.assertIsNone(episode.updated) self.assertIsNone(episode.updated_by) self.assertEqual(201, response.status_code) pcount = models.Patient.objects.filter( demographics__hospital_number="999000999").count() self.assertEqual(1, pcount)
def test_list_for_subtag_empty(self): self.mock_request.query_params = { 'tag': 'micro', 'subtag': 'micro_ortho' } response = api.EpisodeViewSet().list(self.mock_request) self.assertEqual(200, response.status_code) self.assertEqual([], response.data)
def test_update(self): patient, episode = self.new_patient_and_episode_please() self.assertEqual(None, episode.date_of_admission) self.mock_request.data = {"date_of_admission": "14/01/2015"} response = api.EpisodeViewSet().update(self.mock_request, pk=episode.pk) e = models.Episode.objects.get(pk=episode.pk) self.assertEqual(date(2015, 1, 14), e.date_of_admission) response_dict = json.loads(response.content) self.assertEqual(response_dict["date_of_admission"], "14/01/2015")
def test_update_missing_consistency_token(self): p, e = self.new_patient_and_episode_please() e.set_consistency_token() e.save() self.mock_request.data = {"start": "14/01/2015"} response = api.EpisodeViewSet().update(self.mock_request, pk=e.pk) message = json.loads(response.content.decode('UTF-8')) self.assertEqual(message['error'], 'Missing consistency token') self.assertEqual(400, response.status_code)
def test_update_consistency_error(self): patient, episode = self.new_patient_and_episode_please() episode.consistency_token = 'FFFF' episode.save() self.mock_request.data = { "date_of_admission": "14/01/2015", "consistency_token": "EEEE" } response = api.EpisodeViewSet().update(self.mock_request, pk=episode.pk) self.assertEqual(409, response.status_code)
def test_create_without_hospital_number(self): self.mock_request.data = { "tagging" : { "micro":True }, "date_of_admission" : "14/01/2015", "demographics" : { "first_name": "James" } } response = api.EpisodeViewSet().create(self.mock_request) self.assertEqual(1, Demographics.objects.filter(first_name='James').count())
def test_update(self): patient, episode = self.new_patient_and_episode_please() self.assertEqual(None, episode.start) self.mock_request.data = {"start": "14/01/2015"} response = api.EpisodeViewSet().update(self.mock_request, pk=episode.pk) e = models.Episode.objects.get(pk=episode.pk) self.assertEqual(date(2015, 1, 14), e.start) response_dict = json.loads(response.content.decode('UTF-8')) self.assertEqual(response_dict["start"], "14/01/2015") self.assertEqual(len(response_dict["demographics"]), 1)
def test_create_existing_patient(self): self.demographics.hospital_number = '123123123' self.demographics.save() self.mock_request.data = { "tagging" : { "micro":True }, "start": "14/01/2015", "demographics" : { "hospital_number": self.demographics.hospital_number } } response = api.EpisodeViewSet().create(self.mock_request) self.assertEqual(201, response.status_code) self.assertEqual(2, self.patient.episode_set.count()) self.assertEqual("14/01/2015", json.loads(response.content.decode('UTF-8'))['start'])
def test_cant_set_invalid_stage(self): self.mock_request.data = { "tagging" : { "micro":True }, "start" : "14/01/2015", "stage" : "Whoops", "demographics" : { "hospital_number": "9999000999", }, "location" : { "ward": "West", "bed" : "7" } } with self.assertRaises(ValueError): response = api.EpisodeViewSet().create(self.mock_request)
def test_create_existing_patient(self): self.demographics.name = 'Aretha Franklin' self.demographics.hospital_number = '123123123' self.demographics.save() self.mock_request.data = { "tagging" :[ { "micro":True }], "date_of_admission": "14/01/2015", "demographics" : { "hospital_number": self.demographics.hospital_number } } response = api.EpisodeViewSet().create(self.mock_request) self.assertEqual(201, response.status_code) self.assertEqual(2, self.patient.episode_set.count()) self.assertEqual("14/01/2015", json.loads(response.content)['date_of_admission'])
def test_create_existing_patient(self): self.demographics.name = 'Aretha Franklin' self.demographics.hospital_number = '123123123' self.demographics.save() self.mock_request.data = { "tagging": [{ "micro": True }], "date_of_admission": "2015-01-14", "patient_hospital_number": self.demographics.hospital_number } response = api.EpisodeViewSet().create(self.mock_request) self.assertEqual(201, response.status_code) self.assertEqual(2, self.patient.episode_set.count()) self.assertEqual(date(2015, 1, 14), response.data['date_of_admission'])
def test_can_set_stage(self): self.mock_request.data = { "tagging" : { "micro":True }, "start" : "14/01/2015", "stage" : "Inpatient", "demographics" : { "hospital_number": "9999000999", }, "location" : { "ward": "West", "bed" : "7" } } response = api.EpisodeViewSet().create(self.mock_request) patient = models.Patient.objects.get( demographics__hospital_number="9999000999") episode = patient.episode_set.get() self.assertEqual('Inpatient', episode.stage)
def test_create_sets_location(self): pcount = models.Patient.objects.filter( demographics__hospital_number="9999000999").count() self.assertEqual(0, pcount) self.mock_request.data = { "tagging" :[ { "micro":True }], "date_of_admission" : "14/01/2015", "demographics" : { "hospital_number": "9999000999", }, "location": { "ward": "West", "bed" : "7" } } response = api.EpisodeViewSet().create(self.mock_request) patient = models.Patient.objects.get( demographics__hospital_number="9999000999") location = patient.episode_set.get().location_set.get() self.assertEqual("West", location.ward) self.assertEqual("7", location.bed)
def test_create_sets_demographics(self): pcount = models.Patient.objects.filter( demographics__hospital_number="9999000999").count() self.assertEqual(0, pcount) self.mock_request.data = { "tagging" :[ { "micro":True }], "date_of_admission" : "14/01/2015", "demographics" : { "first_name": "Alain", "surname": "Anderson", "sex": "Male", "hospital_number": "9999000999", } } response = api.EpisodeViewSet().create(self.mock_request) patient = models.Patient.objects.get( demographics__hospital_number="9999000999" ) demographics = patient.demographics_set.get() self.assertEqual("Alain", demographics.first_name) self.assertEqual("Anderson", demographics.surname) self.assertEqual("Male", demographics.sex)
def test_update_nonexistent(self): self.mock_request.data = {"date_of_admission": "14/01/2015"} response = api.EpisodeViewSet().update(self.mock_request, pk=8993939) self.assertEqual(404, response.status_code)
def test_retrieve_episode(self): response = api.EpisodeViewSet().retrieve(self.mock_request, pk=self.episode.pk) self.assertEqual(self.episode.to_dict(self.user), response.data)
def test_list(self): response = api.EpisodeViewSet().list(self.mock_request) self.assertEqual(200, response.status_code) response_content = json.loads(response.content) self.assertEqual([self.expected], response_content)
def test_retrieve_nonexistent_episode(self): response = api.EpisodeViewSet().retrieve(self.mock_request, pk=678687) self.assertEqual(404, response.status_code)
def test_retrieve_episode(self): response = json.loads(api.EpisodeViewSet().retrieve(self.mock_request, pk=self.episode.pk).content) self.assertEqual(self.expected, response)
def test_list(self): response = api.EpisodeViewSet().list(self.mock_request) expected = [self.episode.to_dict(self.user)] self.assertEqual(200, response.status_code) self.assertEqual(expected, response.data)