Skip to content

Commit

Permalink
Merge pull request #1562 from openhealthcare/run-postgres-tests-on-tr…
Browse files Browse the repository at this point in the history
…avis

Run postgres tests on travis
  • Loading branch information
davidmiller authored Jun 27, 2018
2 parents 8123c02 + 84b27bd commit dbf5ae0
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ python:
- "3.4"
- "3.5"
- "3.6"
services:
- postgresql
install:
- python setup.py develop
- gem install coveralls-lcov
Expand All @@ -13,6 +15,7 @@ install:
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- psql -c 'create database travis_ci_test;' -U postgres
script:
- opal test --coverage
- flake8
Expand Down
6 changes: 5 additions & 1 deletion opal/core/pathway/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ def test_integration(self, fake_pathway_get):
)
response = self.post_json(url, post_dict)
expected = {
'patient_id': 1, 'redirect_url': u'/#/patient/1/1', 'episode_id': 1
'patient_id': self.patient.id,
'redirect_url': u'/#/patient/{}/{}'.format(
self.patient.id, self.episode.id
),
'episode_id': self.episode.id
}
self.assertEqual(json.loads(response.content.decode('utf8')), expected)

Expand Down
29 changes: 19 additions & 10 deletions opal/core/pathway/tests/test_pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RedirectsToPatientMixinTestCase(OpalTestCase):
def test_redirect(self):
p, e = self.new_patient_and_episode_please()
url = pathways.RedirectsToPatientMixin().redirect_url(patient=p)
self.assertEqual('/#/patient/1', url)
self.assertEqual('/#/patient/{}'.format(p.id), url)


class PathwayTestCase(OpalTestCase):
Expand Down Expand Up @@ -239,13 +239,22 @@ def test_existing_patient_new_episode_save(self):
url = reverse(
"pathway", kwargs=dict(
name="dog_owner",
patient_id=1
patient_id=patient.id
)
)
self.post_data(url=url)
self.assertEqual(patient.episode_set.count(), 2)
self.assertEqual(DogOwner.objects.filter(episode_id=2).count(), 2)
self.assertFalse(DogOwner.objects.filter(episode_id=episode.id).exists())
new_episode = patient.episode_set.last()

# just validate that we definitely have created a new episode
self.assertNotEqual(episode.id, new_episode.id)

self.assertEqual(
DogOwner.objects.filter(episode_id=new_episode.id).count(), 2
)
self.assertFalse(
DogOwner.objects.filter(episode_id=episode.id).exists()
)

def test_users_patient_passed_in(self):
pathway = PagePathwayExample()
Expand Down Expand Up @@ -278,8 +287,8 @@ def test_existing_patient_existing_episode_save(self):
url = reverse(
"pathway", kwargs=dict(
name="dog_owner",
episode_id=1,
patient_id=1
episode_id=episode.id,
patient_id=patient.id
)
)
self.post_data(url=url)
Expand Down Expand Up @@ -309,7 +318,7 @@ def setUp(self):
def test_dont_update_subrecords_that_havent_changed(self, subrecords):
subrecords.return_value = [Colour]
colour = Colour.objects.create(
consistency_token="unchanged",
consistency_token="unchange",
name="Red",
episode=self.episode,
created=timezone.now()
Expand Down Expand Up @@ -340,7 +349,7 @@ def test_save_new_subrecords(self, subrecords):
def test_update_changed_subrecords(self, subrecords):
subrecords.return_value = [Colour]
colour = Colour.objects.create(
consistency_token="unchanged",
consistency_token="unchange",
name="Blue",
episode=self.episode,
)
Expand All @@ -365,12 +374,12 @@ def test_update_changed_subrecords(self, subrecords):
def test_only_change_one_in_a_list(self, subrecords):
subrecords.return_value = [Colour]
colour_1 = Colour.objects.create(
consistency_token="unchanged",
consistency_token="unchange",
name="Blue",
episode=self.episode,
)
colour_2 = Colour.objects.create(
consistency_token="unchanged",
consistency_token="unchange",
name="Orange",
episode=self.episode,
)
Expand Down
27 changes: 20 additions & 7 deletions opal/core/search/tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,10 @@ def test_get_row(self):
self.episode.set_tag_names(["trees"], self.user)
# make sure we keep historic tags
self.episode.set_tag_names(["leaves"], self.user)
self.assertIn("trees;leaves", renderer.get_row(self.episode))
row = renderer.get_row(self.episode)
self.assertTrue(
"trees;leaves" in row or "leaves;trees" in row
)


@patch.object(PatientColour, "_get_fieldnames_to_extract")
Expand All @@ -381,6 +384,8 @@ def setUp(self):
self.patient_colour = PatientColour.objects.create(
name="blue", patient=self.patient
)
self.pid_str = str(self.patient.id)
self.eid_str = str(self.episode.id)

def test_get_header(self, field_names_to_extract):
field_names_to_extract.return_value = [
Expand All @@ -403,7 +408,9 @@ def test_get_row(self, field_names_to_extract):
self.user
)
rendered = renderer.get_row(self.patient_colour, self.episode.id)
self.assertEqual(["1", "1", "blue"], rendered)
self.assertEqual(
[str(self.episode.id), str(self.patient.id), "blue"], rendered
)

def test_get_rows(self, field_names_to_extract):
field_names_to_extract.return_value = [
Expand All @@ -417,10 +424,13 @@ def test_get_rows(self, field_names_to_extract):
rendered = list(
renderer.get_rows()
)
self.assertEqual([["1", "1", "blue"]], rendered)
expected = [[self.eid_str, self.pid_str, "blue"]]
self.assertEqual(expected, rendered)

def test_get_rows_same_patient(self, field_names_to_extract):
self.patient.create_episode()
first_episode = self.patient.episode_set.first()
last_episode = self.patient.episode_set.last()
field_names_to_extract.return_value = [
"patient_id", "name", "consistency_token", "id"
]
Expand All @@ -434,18 +444,20 @@ def test_get_rows_same_patient(self, field_names_to_extract):
renderer.get_rows()
)
self.assertEqual([
["1", "1", "blue"],
["2", "1", "blue"]
[str(first_episode.id), str(self.patient.id), "blue"],
[str(last_episode.id), str(self.patient.id), "blue"]
], rendered)


@patch.object(Colour, "_get_fieldnames_to_extract")
class TestEpisodeSubrecordCsvRenderer(PatientEpisodeTestCase):
def setUp(self):
_, self.episode = self.new_patient_and_episode_please()
self.patient, self.episode = self.new_patient_and_episode_please()
self.colour = Colour.objects.create(
name="blue", episode=self.episode
)
self.eid_str = str(self.episode.id)
self.pid_str = str(self.patient.id)

def test_get_header(self, field_names_to_extract):
field_names_to_extract.return_value = [
Expand All @@ -466,7 +478,8 @@ def test_get_row(self, field_names_to_extract):
Colour, models.Episode.objects.all(), self.user
)
rendered = renderer.get_row(self.colour)
self.assertEqual(["1", "1", "blue"], rendered)
expected = [self.pid_str, self.eid_str, "blue"]
self.assertEqual(expected, rendered)


@patch('opal.core.search.extract.subrecords')
Expand Down
16 changes: 10 additions & 6 deletions opal/core/search/tests/test_search_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ def test_episodes_for_m2m_fields_contains_synonym_and_name(self):
hatwearer.save()

query = queries.DatabaseQuery(self.user, [criteria])
self.assertEqual([self.episode, other_episode], query.get_episodes())
expected = set([self.episode.id, other_episode.id])
found = set([i.id for i in query.get_episodes()])
self.assertEqual(expected, found)

def test_fuzzy_query(self):
""" It should return the patients that
Expand Down Expand Up @@ -525,7 +527,9 @@ def test_episode_fkorft_contains_distinct(self):
hound_owner.dog = "Dalwinion"
hound_owner.save()
query = queries.DatabaseQuery(self.user, [criteria])
self.assertEqual([self.episode, episode_2], query.get_episodes())
expected = set([self.episode.id, episode_2.id])
found = set(i.id for i in query.get_episodes())
self.assertEqual(expected, found)

def test_episodes_for_criteria_episode_subrecord_string_field(self):
criteria = [
Expand Down Expand Up @@ -715,15 +719,15 @@ def test_get_patient_summaries(self):
query = queries.DatabaseQuery(self.user, self.name_criteria)
summaries = query.get_patient_summaries()
expected = [{
'id': self.patient.id,
'id': self.patient.demographics_set.first().id,
'count': 1,
'hospital_number': u'0',
'date_of_birth': self.DATE_OF_BIRTH,
'first_name': u'Sally',
'surname': u'Stevens',
'end': self.DATE_OF_EPISODE,
'start': self.DATE_OF_EPISODE,
'patient_id': 1,
'patient_id': self.patient.id,
'categories': [u'Inpatient']
}]
self.assertEqual(expected, summaries)
Expand All @@ -743,15 +747,15 @@ def test_update_patient_summaries(self):
query = queries.DatabaseQuery(self.user, self.name_criteria)
summaries = query.get_patient_summaries()
expected = [{
'id': self.patient.id,
'id': self.patient.demographics_set.first().id,
'count': 3,
'hospital_number': u'0',
'date_of_birth': self.DATE_OF_BIRTH,
'first_name': u'Sally',
'surname': u'Stevens',
'end': end_date,
'start': start_date,
'patient_id': 1,
'patient_id': self.patient.id,
'categories': [u'Inpatient']
}]
self.assertEqual(expected, summaries)
Expand Down
16 changes: 10 additions & 6 deletions opal/core/search/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ def setUp(self):
u'page_number': 1,
u'object_list': [{
u'count': 1,
u'id': self.patient.id,
u'id': self.patient.demographics_set.first().id,
u'first_name': u'Sean',
u'surname': u'Connery',
u'end': u'15/10/2015',
u'patient_id': 1,
u'patient_id': self.patient.id,
u'hospital_number': u'007',
u'date_of_birth': None,
u'start': u'15/10/2015',
Expand Down Expand Up @@ -285,11 +285,11 @@ def test_with_multiple_patient_episodes(self):
"first_name": "Ernst",
"surname": "Blofeld",
"start": None,
"patient_id": 2,
"patient_id": blofeld_patient.id,
"hospital_number": "23422",
"date_of_birth": None,
"end": None,
"id": 2,
"id": blofeld_patient.demographics_set.first().id,
"categories": ["Inpatient"]
}],
"page_number": 1,
Expand Down Expand Up @@ -367,15 +367,19 @@ def test_logged_in_dispatch(self):
self.assertEqual(response.status_code, 200)

def test_get(self):
models.Filter(user=self.user, name='testfilter', criteria='[]').save()
filter = models.Filter.objects.create(
user=self.user, name='testfilter', criteria='[]'
)
self.assertEqual(1, models.Filter.objects.count())

view = views.FilterView()
view.request = self.rf.get('/filter')
view.request.user = self.user

data = json.loads(view.get().content.decode('UTF-8'))
self.assertEqual([{'name': 'testfilter', 'criteria': [], 'id': 1}], data)
self.assertEqual(
[{'name': filter.name, 'criteria': [], 'id': filter.id}], data
)

def test_post(self):
view = views.FilterView()
Expand Down
14 changes: 8 additions & 6 deletions opal/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ def test_valid_synonym(self):
self.assertEqual("Stetson", form.clean_name())




class EpisodeAdminTestCase(AdminTestCase):
def setUp(self):
super(EpisodeAdminTestCase, self).setUp()
Expand All @@ -139,13 +137,15 @@ def setUp(self):
def test_episode_detail_link(self):
self.assertEqual(
self.admin.episode_detail_link(self.episode),
"<a href='/#/patient/1/1'>/#/patient/1/1</a>"
"<a href='/#/patient/{0}/{1}'>/#/patient/{0}/{1}</a>".format(
self.patient.id, self.episode.id
)
)

def test_view_on_site(self):
self.assertEqual(
self.admin.view_on_site(self.episode),
'/#/patient/1/1'
'/#/patient/{}/{}'.format(self.patient.id, self.episode.id)
)


Expand All @@ -157,11 +157,13 @@ def setUp(self):
def test_patient_detail_link(self):
self.assertEqual(
self.admin.patient_detail_link(self.patient),
"<a href='/#/patient/1'>/#/patient/1</a>"
"<a href='/#/patient/{0}'>/#/patient/{0}</a>".format(
self.patient.id
)
)

def test_view_on_site(self):
self.assertEqual(
self.admin.view_on_site(self.patient),
'/#/patient/1'
'/#/patient/{}'.format(self.patient.id)
)
22 changes: 14 additions & 8 deletions opal/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,12 @@ def test_list(self):
self.assertEqual([], json.loads(response.content.decode('UTF-8')))

def test_list_with_some_contents(self):
c1 = Colour(name="blue", episode=self.episode).save()
c2 = Colour(name="red", episode=self.episode).save()
c1 = Colour.objects.create(
name="blue", episode=self.episode
)
c2 = Colour.objects.create(
name="red", episode=self.episode
)
mock_request = MagicMock(name='mock request')
mock_request.user = self.user
response = self.viewset().list(mock_request)
Expand All @@ -220,8 +224,8 @@ def test_list_with_some_contents(self):
u'consistency_token': u'',
u'created': None,
u'created_by_id': None,
u'episode_id': 1,
u'id': 1,
u'episode_id': self.episode.id,
u'id': c1.id,
u'name': u'blue',
u'updated': None,
u'updated_by_id': None
Expand All @@ -230,8 +234,8 @@ def test_list_with_some_contents(self):
u'consistency_token': u'',
u'created': None,
u'created_by_id': None,
u'episode_id': 1,
u'id': 2,
u'episode_id': self.episode.id,
u'id': c2.id,
u'name': u'red',
u'updated': None,
u'updated_by_id': None
Expand Down Expand Up @@ -536,7 +540,7 @@ def test_user_profile(self):
'roles' : {'default': []},
'full_name' : '',
'avatar_url' : 'http://gravatar.com/avatar/5d9c68c6c50ed3d02a2fcf54f63993b6?s=80&r=g&d=identicon',
'user_id' : 1
'user_id' : self.user.id
}
self.assertEqual(expected, response.data)

Expand All @@ -561,7 +565,9 @@ def test_list(self):
self.assertEqual([self.user.profile.to_dict()], response.data)

def test_retrieve(self):
response = api.UserViewSet().retrieve(self.mock_request, pk=1)
response = api.UserViewSet().retrieve(
self.mock_request, pk=self.user.id
)
self.assertEqual(self.user.profile.to_dict(), response.data)


Expand Down
Loading

0 comments on commit dbf5ae0

Please sign in to comment.