<commit_before>"""
Unit tests for OPAL.
"""
import datetime
import json

from django.contrib.auth.models import User
from django.core.serializers.json import DjangoJSONEncoder
from django.test import TestCase

from opal.models import Patient, Episode

class PatientTest(TestCase):
    fixtures = ['patients_users', 'patients_records', 'patients_options']
    maxDiff = None

    def setUp(self):
        self.user = User.objects.get(pk=1)
        self.patient = Patient.objects.create()

    def test_demographics_subrecord_created(self):
        self.assertEqual(1, self.patient.demographics_set.count())

    def test_can_create_episode(self):
        episode = self.patient.create_episode()
        self.assertEqual(Episode, type(episode))

    def test_get_active_episode(self):
        episode1 = self.patient.create_episode()
        episode2 = self.patient.create_episode()
        episode2.set_tag_names(['microbiology'], None)
        self.assertEqual(episode2.id, self.patient.get_active_episode().id)

    def test_get_active_episode_with_no_episodes(self):
        self.assertIsNone(self.patient.get_active_episode())

    def test_get_active_episode_with_no_active_episodes(self):
        self.patient.create_episode()
        self.patient.create_episode()
        self.assertIsNone(self.patient.get_active_episode())

    def test_cannot_create_episode_if_has_active_episode(self):
        episode = self.patient.create_episode()
        episode.set_tag_names(['microbiology'], None)
        with self.assertRaises(Exception):
            self.patient.create_episode()


class EpisodeTest(TestCase):
    fixtures = ['patients_users', 'patients_records', 'patients_options']

    def setUp(self):
        self.user = User.objects.get(pk=1)
        self.patient = Patient.objects.create()
        self.episode = self.patient.create_episode()

    def test_unicode(self):
        self.assertEqual(' |  | None', self.episode.__unicode__())

    def test_location_subrecord_created(self):
        self.assertEqual(1, self.episode.location_set.count())

    def test_can_set_tag_names(self):
        for tag_names in [
            ['microbiology', 'mine'],
            ['microbiology', 'hiv'],
            ['hiv', 'mine'],
        ]:
            self.episode.set_tag_names(tag_names, self.user)
            self.assertEqual(set(tag_names),
                             set(self.episode.get_tag_names(self.user)))

    def test_user_cannot_see_other_users_mine_tag(self):
        other_user = User.objects.get(pk=2)

        self.episode.set_tag_names(['hiv', 'mine'], self.user)
        self.assertEqual(['hiv'], self.episode.get_tag_names(other_user))

    def test_active_if_tagged_by_non_mine_tag(self):
        self.episode.set_tag_names(['microbiology'], self.user)
        self.assertTrue(self.episode.is_active())

    def test_inactive_if_only_tagged_by_mine_tag(self):
        self.episode.set_tag_names(['mine'], self.user)
        self.assertFalse(self.episode.is_active())

    def test_to_dict(self):
        self.maxDiff = None
        expected_data = {
            'id': self.episode.id,
            'prev_episodes': [
                ],
            'next_episodes': [],
            'active': False,
            'date_of_admission': None,
            'discharge_date': None,
            'consistency_token': '',
            'demographics': [{
                'id': self.patient.demographics_set.get().id,
                'patient_id': self.patient.id,
                'consistency_token': '',
                'date_of_birth': None,
                'hospital_number': '',
                'name': '',
                }],
            'location': [{
                'id': self.episode.location_set.get().id,
                'episode_id': self.episode.id,
                'bed': '',
                'category': '',
                'consistency_token': '',
                'hospital': '',
                'tags': {},
                'ward': '',
                }],
            'diagnosis': [],
            'past_medical_history': [],
            'general_note': [],
            'travel': [],
            'antimicrobial': [],
            'microbiology_input': [],
            'todo': [],
            'microbiology_test': [],
        }
        self.assertEqual(expected_data, self.episode.to_dict(self.user))

    def test_to_dict_with_multiple_episodes(self):
        episode = Episode.objects.get(pk=1)
        serialised = episode.to_dict(self.user)
        self.assertEqual(1, len(serialised['prev_episodes']))
        self.assertEqual(datetime.date(2012, 7, 25),
                         serialised['prev_episodes'][0]['date_of_admission'])

    def test_to_dict_episode_ordering(self):
        episode = Episode.objects.get(pk=1)
        patient = episode.patient
        admitted = datetime.date(2011, 7, 25)
        new_episode = Episode(patient=patient, date_of_admission=admitted)
        new_episode.save()

        serialised = episode.to_dict(self.user)
        self.assertEqual(2, len(serialised['prev_episodes']))
        self.assertEqual(datetime.date(2011, 7, 25),
                         serialised['prev_episodes'][0]['date_of_admission'])<commit_msg>Fix OPAL tests.<commit_after>"""
Unit tests for OPAL.
"""
import datetime
import json

from django.contrib.auth.models import User
from django.core.serializers.json import DjangoJSONEncoder
from django.test import TestCase

from opal.models import Patient, Episode

class PatientTest(TestCase):
    fixtures = ['patients_users', 'patients_records', 'patients_options']
    maxDiff = None

    def setUp(self):
        self.user = User.objects.get(pk=1)
        self.patient = Patient.objects.create()

    def test_demographics_subrecord_created(self):
        self.assertEqual(1, self.patient.demographics_set.count())

    def test_can_create_episode(self):
        episode = self.patient.create_episode()
        self.assertEqual(Episode, type(episode))

    def test_get_active_episode(self):
        episode1 = self.patient.create_episode()
        episode2 = self.patient.create_episode()
        episode2.set_tag_names(['microbiology'], None)
        self.assertEqual(episode2.id, self.patient.get_active_episode().id)

    def test_get_active_episode_with_no_episodes(self):
        self.assertIsNone(self.patient.get_active_episode())

    def test_get_active_episode_with_no_active_episodes(self):
        self.patient.create_episode()
        self.patient.create_episode()
        self.assertIsNone(self.patient.get_active_episode())

    def test_cannot_create_episode_if_has_active_episode(self):
        episode = self.patient.create_episode()
        episode.set_tag_names(['microbiology'], None)
        with self.assertRaises(Exception):
            self.patient.create_episode()


class EpisodeTest(TestCase):
    fixtures = ['patients_users', 'patients_records', 'patients_options']

    def setUp(self):
        self.user = User.objects.get(pk=1)
        self.patient = Patient.objects.create()
        self.episode = self.patient.create_episode()

    def test_unicode(self):
        self.assertEqual(' |  | None', self.episode.__unicode__())

    def test_location_subrecord_created(self):
        self.assertEqual(1, self.episode.location_set.count())

    def test_can_set_tag_names(self):
        for tag_names in [
            ['microbiology', 'mine'],
            ['microbiology', 'hiv'],
            ['hiv', 'mine'],
        ]:
            self.episode.set_tag_names(tag_names, self.user)
            self.assertEqual(set(tag_names),
                             set(self.episode.get_tag_names(self.user)))

    def test_user_cannot_see_other_users_mine_tag(self):
        other_user = User.objects.get(pk=2)

        self.episode.set_tag_names(['hiv', 'mine'], self.user)
        self.assertEqual(['hiv'], self.episode.get_tag_names(other_user))

    def test_active_if_tagged_by_non_mine_tag(self):
        self.episode.set_tag_names(['microbiology'], self.user)
        self.assertTrue(self.episode.is_active())

    def test_inactive_if_only_tagged_by_mine_tag(self):
        self.episode.set_tag_names(['mine'], self.user)
        self.assertFalse(self.episode.is_active())

    def test_to_dict(self):
        self.maxDiff = None
        expected_data = {
            'id': self.episode.id,
            'prev_episodes': [
                ],
            'next_episodes': [],
            'active': False,
            'allergies': [],
            'date_of_admission': None,
            'discharge_date': None,
            'consistency_token': '',
            'demographics': [{
                u'id': self.patient.demographics_set.get().id,
                'patient_id': self.patient.id,
                'consistency_token': '',
                'date_of_birth': None,
                'country_of_birth': '',
                'ethnicity': None,
                'hospital_number': '',
                'name': u'',
                }],
            'location': [{
                    u'id': self.episode.location_set.get().id,
                'episode_id': self.episode.id,
                'bed': u'',
                'category': u'',
                'consistency_token': u'',
                'hospital': u'',
                'tags': {},
                'ward': u'',
                }],
            'diagnosis': [],
            'past_medical_history': [],
            'general_note': [],
            'travel': [],
            'antimicrobial': [],
            'microbiology_input': [],
            'todo': [],
            'microbiology_test': [],
        }
        self.assertEqual(expected_data, self.episode.to_dict(self.user))

    def test_to_dict_with_multiple_episodes(self):
        episode = Episode.objects.get(pk=1)
        serialised = episode.to_dict(self.user)
        self.assertEqual(1, len(serialised['prev_episodes']))
        self.assertEqual(datetime.date(2012, 7, 25),
                         serialised['prev_episodes'][0]['date_of_admission'])

    def test_to_dict_episode_ordering(self):
        episode = Episode.objects.get(pk=1)
        patient = episode.patient
        admitted = datetime.date(2011, 7, 25)
        new_episode = Episode(patient=patient, date_of_admission=admitted)
        new_episode.save()

        serialised = episode.to_dict(self.user)
        self.assertEqual(2, len(serialised['prev_episodes']))
        self.assertEqual(datetime.date(2011, 7, 25),
                         serialised['prev_episodes'][0]['date_of_admission'])
{'fraction_non_alphanumeric': 0.1265014299332698, 'fraction_numerical': 0.006863679694947569, 'mean_word_length': 2.4419291338582676, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5150486', 'n_tokens_mistral': 3384, 'n_tokens_neox': 3116, 'n_words': 441}