Skip to content

Commit

Permalink
use the tagging schema to the options but allow for team options that…
Browse files Browse the repository at this point in the history
… are not in patient lists refs #1615
  • Loading branch information
fredkingham committed Jun 6, 2018
1 parent 1ab847e commit 7289fbd
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 23 deletions.
8 changes: 7 additions & 1 deletion search/extract_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,14 @@ class EpisodeTeamExtractField(CsvFieldWrapper):
description_template = "search/field_descriptions/episode/team.html"

def extract(self, obj):
title_to_slug = subrecord_discoverable.get_team_display_name_to_slug()
slug_to_title = {v: i for i, v in title_to_slug.items()}
return text_type("; ".join(
obj.get_tag_names(self.user, historic=True)
[
slug_to_title[i] for i in obj.get_tag_names(
self.user, historic=True
)
]
))


Expand Down
6 changes: 4 additions & 2 deletions search/search_rule_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class EpisodeTeam(

@property
def enum(self):
return [i["title"] for i in models.Tagging.build_field_schema()]
return subrecord_discoverable.get_team_display_name_to_slug().keys()

def translate_titles_to_names(self, titles):
result = []
Expand Down Expand Up @@ -235,8 +235,10 @@ def query(self, given_query):
""".strip()
raise SearchException(err.format(query_type))

team_names = self.translate_titles_to_names(team_display_names)
team_map = subrecord_discoverable.get_team_display_name_to_slug()
team_names = [team_map[i] for i in team_display_names]
qs = models.Episode.objects.all()

if given_query["query_type"] == self.ALL_OF:
for team_name in team_names:
qs = qs.filter(tagging__value=team_name)
Expand Down
22 changes: 22 additions & 0 deletions search/subrecord_discoverable.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from functools import wraps
import itertools
from opal import models
from opal.core import subrecords
from opal.core import fields
from opal.utils import camelcase_to_underscore
from search.exceptions import SearchException
from django.db import models as djangomodels

Expand Down Expand Up @@ -71,6 +73,26 @@ def is_select(some_field):
)


def get_team_display_name_to_slug():
result = {}
tagging_schema = models.Tagging.build_field_schema()
for schema in tagging_schema:
result[schema["title"]] = schema["name"]

tag_slugs = set(
models.Tagging.objects.values_list("value", flat=True).distinct()
)

result_slugs = set(result.values())

for tag_slug in tag_slugs:
if tag_slug not in result_slugs:
title = camelcase_to_underscore(tag_slug).replace("_", " ").title()
result[title] = tag_slug

return result


class SubrecordFieldWrapper(object):
model = None
field_name = None
Expand Down
31 changes: 24 additions & 7 deletions search/tests/test_extract_rules.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mock
from opal.core.test import OpalTestCase
from search import extract_rules

Expand Down Expand Up @@ -78,11 +79,13 @@ def test_location_rule_order(self):


class EpisodeTestBase(OpalTestCase):
def test_field_order(self):
episode_rule = extract_rules.ExtractRule.get_rule(
def setUp(self):
self.rule = extract_rules.ExtractRule.get_rule(
"episode", self.user
)
fields = episode_rule.get_fields()

def test_field_order(self):
fields = self.rule.get_fields()
self.assertEqual(
fields[0].get_name(), "patient_id"
)
Expand All @@ -100,14 +103,28 @@ def test_field_order(self):
)

def test_get_fields_for_schema(self):
episode_rule = extract_rules.ExtractRule.get_rule(
"episode", self.user
)
fields = episode_rule.get_fields_for_schema()
fields = self.rule.get_fields_for_schema()
field_names = {i.get_name() for i in fields}
self.assertNotIn("patient_id", field_names)
self.assertNotIn("id", field_names)

@mock.patch(
"search.extract_rules.subrecord_discoverable\
.get_team_display_name_to_slug"
)
def test_team_extract_with_multiple_tags(
self, get_team_display_name_to_slug
):
get_team_display_name_to_slug.return_value = {
"This": "this",
"That": "that"
}
_, episode = self.new_patient_and_episode_please()
episode.set_tag_names(["this", "that"], self.user)
team_rule = self.rule.get_field("team")
result = team_rule.extract(episode)
self.assertEqual("This; That", result)


class ManyToManyTestCase(OpalTestCase):
def setUp(self, *args, **kwargs):
Expand Down
29 changes: 16 additions & 13 deletions search/tests/test_search_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,6 @@ def test_episode_team_wrong_query_param(self):
"unrecognised query type for the episode team query with asdfsadf"
)

def test_episode_team_unknown_team(self):
query_end = dict(
query_type="Any Of",
value=["Some Team"],
field="team"
)
with self.assertRaises(exceptions.SearchException) as er:
self.episode_rule.query(query_end)
self.assertEqual(
str(er.exception),
"unable to find the tag titled Some Team"
)

def test_episode_team_all_of_one(self):
"""
test all of with a single tag
Expand Down Expand Up @@ -292,6 +279,22 @@ def test_episode_team_all_of_many(self):
self.assertEqual(len(result), 1)
self.assertEqual(result[0].id, self.episode_1.id)

@patch("search.search_rule_fields.models.Tagging.build_field_schema")
def test_episode_team_enum(self, bfs):
bfs.return_value = [
dict(
name="plant",
title="Plant"
),
dict(
name="tree",
title="Tree"
),
]
self.assertEqual(
["Plant", "Tree"], self.episode_rule.get_field("team").enum
)

def test_episide_team_any_of_one(self):
"""
test all of with a single tag
Expand Down
64 changes: 64 additions & 0 deletions search/tests/test_subrecord_discoverable.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,67 @@ class HoundOwnerOverride(
hound_owner_override.sort_fields(fields),
[fields[1], fields[0]]
)


class GetTeamDisplayNameToSlugTestCase(OpalTestCase):
def test_get_team_display_name_to_slug(self):
_, episode = self.new_patient_and_episode_please()
episode.tagging_set.create(value="bark_and_stem", archived=True)

with patch.object(
subrecord_discoverable.models.Tagging, "build_field_schema"
) as bfs:
bfs.return_value = [
dict(
name="plant",
title="Plant"
),
dict(
name="tree",
title="Tree"
),
]
result = subrecord_discoverable.get_team_display_name_to_slug()
expected = {
"Bark And Stem": "bark_and_stem",
"Plant": "plant",
"Tree": "tree"
}

self.assertEqual(
result, expected
)

def test_avoids_duplicate_slugs(self):
_, episode = self.new_patient_and_episode_please()
episode.tagging_set.create(value="bark_and_stem", archived=True)

with patch.object(
subrecord_discoverable.models.Tagging, "build_field_schema"
) as bfs:
bfs.return_value = [
dict(
name="bark_and_stem",
title="Plant"
),
dict(
name="tree",
title="Tree"
),
]
result = subrecord_discoverable.get_team_display_name_to_slug()
expected = {
"Plant": "bark_and_stem",
"Tree": "tree"
}

self.assertEqual(
result, expected
)

def test_integration(self):
""" we are mocking up build_field_schema here, just a quick
check sanity check in case this behavour has changed
"""
result = subrecord_discoverable.get_team_display_name_to_slug()
self.assertTrue(bool(result))

0 comments on commit 7289fbd

Please sign in to comment.