Skip to content

Commit 0c6f0de

Browse files
committed
Use index date for prescribed medications
* the start date of list_size analyses should be after INDEX_DATE * medications which are prescribed before INDEX_DATE should not appear at all in list_analyses
1 parent 78115d7 commit 0c6f0de

4 files changed

Lines changed: 89 additions & 10 deletions

File tree

openprescribing/web/api.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,27 @@
1818
# allow this to be configured by the user, or calculated directly from the data.
1919
DATE_COUNT = 96
2020

21+
# This is the first month that we have monthly list size updates for.
22+
INDEX_DATE = "2017-04-01"
23+
2124
# The number of individual medications shown as their own band in the "by medication"
2225
# stacked area chart. Any further medications are summed into a single "Other" band.
2326
MEDICATIONS_TOP_N = 10
2427

28+
PRESENTATIONS_PRESCRIBED_AFTER_INDEX_DATE_SQL = f"""
29+
SELECT bnf_code FROM presentation WHERE last_prescribed_date > '{INDEX_DATE}'
30+
"""
31+
2532
# Selects medications that have been prescribed: VMPs/AMPs whose BNF code appears in the
2633
# prescribing data, plus the parent VMPs of any prescribed AMPs.
27-
PRESCRIBED_MEDICATIONS_SQL = """
34+
PRESCRIBED_MEDICATIONS_SQL = f"""
2835
SELECT * FROM medications
29-
WHERE bnf_code IN (SELECT bnf_code FROM presentation)
36+
WHERE bnf_code IN ({PRESENTATIONS_PRESCRIBED_AFTER_INDEX_DATE_SQL})
3037
OR (
3138
NOT is_amp
3239
AND id IN (
3340
SELECT DISTINCT vmp_id FROM medications
34-
WHERE is_amp AND bnf_code IN (SELECT bnf_code FROM presentation)
41+
WHERE is_amp AND bnf_code IN ({PRESENTATIONS_PRESCRIBED_AFTER_INDEX_DATE_SQL})
3542
)
3643
)
3744
"""

tests/utils/data_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import datetime
2+
from enum import Enum
23

34
from openprescribing.data.models import BNFCode, Org, OrgRelation
5+
from openprescribing.web.api import INDEX_DATE
46

57

68
def generate_test_data(rxdb, bnf_codes):
@@ -60,3 +62,17 @@ def generate_test_data(rxdb, bnf_codes):
6062
)
6163

6264
return {"list_size_data": list_size_data, "prescribing_data": prescribing_data}
65+
66+
67+
class DateRelativeToIndexDate(Enum):
68+
BEFORE = 1
69+
AFTER = 2
70+
71+
72+
def default_date_relative_to_index_date(relation_to_index_date):
73+
if relation_to_index_date == DateRelativeToIndexDate.BEFORE:
74+
prescribing_datetime_delta = datetime.timedelta(weeks=-2)
75+
else:
76+
prescribing_datetime_delta = datetime.timedelta(weeks=2)
77+
78+
return str(datetime.date.fromisoformat(INDEX_DATE) + prescribing_datetime_delta)

tests/utils/rxdb_utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
CursorCacheKeyWrapper,
1515
)
1616
from openprescribing.data.utils.duckdb_utils import escape
17+
from tests.utils.data_utils import (
18+
DateRelativeToIndexDate,
19+
default_date_relative_to_index_date,
20+
)
1721

1822

1923
PRESCRIBING_SOURCE_SCHEMA = pyarrow.schema(
@@ -33,7 +37,6 @@
3337
PRESCRIBING_SOURCE_DEFAULTS = {
3438
"bnf_code": "",
3539
"snomed_code": 0,
36-
"date": datetime.date(2000, 1, 1),
3740
"practice_code": "",
3841
"quantity_value": 0.0,
3942
"items": 0,
@@ -125,6 +128,12 @@ def rxdb_ingest(conn, prescribing_data=(), list_size_data=()):
125128
If tests need to supply their own BNF code changes, we should add a bnf_code_changes
126129
parameter to this function.
127130
"""
131+
132+
# Set our default date to be after api's INDEX_DATE
133+
PRESCRIBING_SOURCE_DEFAULTS["date"] = default_date_relative_to_index_date(
134+
DateRelativeToIndexDate.AFTER
135+
)
136+
128137
prescribing_data = prepare_data(prescribing_data, PRESCRIBING_SOURCE_DEFAULTS)
129138
list_size_data = prepare_data(list_size_data, LIST_SIZE_SOURCE_DEFAULTS)
130139
prescribing_source = pyarrow.Table.from_pylist(

tests/web/test_api.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import pytest
55

66
from openprescribing.web import api
7+
from tests.utils.data_utils import (
8+
DateRelativeToIndexDate,
9+
default_date_relative_to_index_date,
10+
)
711
from tests.utils.ingest_utils import ingest_dmd_bnf_map_data, ingest_dmd_data
812

913

@@ -216,13 +220,27 @@ def test_prescribing_medications_groups_other(client, sample_data, monkeypatch):
216220
assert "Other" in medications
217221

218222

219-
def test_metadata_medications(client, rxdb, settings, tmp_path):
220-
rxdb.ingest([{"bnf_code": "1106000X0AAA4A4"}])
223+
@pytest.mark.parametrize(
224+
"prescribing_date_relative_to_index_date",
225+
[DateRelativeToIndexDate.BEFORE, DateRelativeToIndexDate.AFTER],
226+
)
227+
def test_metadata_medications(
228+
client, rxdb, settings, tmp_path, prescribing_date_relative_to_index_date
229+
):
230+
prescribing_date = default_date_relative_to_index_date(
231+
prescribing_date_relative_to_index_date
232+
)
233+
234+
rxdb.ingest([{"bnf_code": "1106000X0AAA4A4", "date": str(prescribing_date)}])
221235
ingest_dmd_data(settings, tmp_path)
222236
ingest_dmd_bnf_map_data(settings, tmp_path)
223237
rsp = client.get("/api/metadata/medications/")
224238
payload = rsp.json()
225239

240+
if prescribing_date_relative_to_index_date == DateRelativeToIndexDate.BEFORE:
241+
assert [] == payload["medications"]
242+
return
243+
226244
assert {
227245
"id": 9393711000001102,
228246
"bnf_code": "1106000X0AAA4A4",
@@ -247,7 +265,16 @@ def test_metadata_medications(client, rxdb, settings, tmp_path):
247265
} in payload["medications"]
248266

249267

250-
def test_metadata_dmd(client, rxdb, medications):
268+
@pytest.mark.parametrize(
269+
"prescribing_date_relative_to_index_date",
270+
[DateRelativeToIndexDate.BEFORE, DateRelativeToIndexDate.AFTER],
271+
)
272+
def test_metadata_dmd(
273+
client, rxdb, medications, prescribing_date_relative_to_index_date
274+
):
275+
prescribing_date = default_date_relative_to_index_date(
276+
prescribing_date_relative_to_index_date
277+
)
251278
# Two VMPs with distinct VTMs, ingredients and form/routes, plus an AMP belonging to
252279
# the first VMP; only the first VMP (and hence its AMP) is then prescribed.
253280
medications.add_rows(
@@ -273,10 +300,15 @@ def test_metadata_dmd(client, rxdb, medications):
273300
},
274301
]
275302
)
276-
rxdb.ingest([{"bnf_code": "1001030U0AAABAB"}])
303+
rxdb.ingest([{"bnf_code": "1001030U0AAABAB", "date": prescribing_date}])
277304

278305
payload = client.get("/api/metadata/dmd/").json()
279306

307+
if prescribing_date_relative_to_index_date == DateRelativeToIndexDate.BEFORE:
308+
assert payload["vmp"] == []
309+
assert payload["amp"] == []
310+
return
311+
280312
# Only the prescribed VMP, its AMP, and the VTM, ingredient and form/route they
281313
# relate to are returned; the unprescribed VMP's objects are excluded.
282314
assert payload["vmp"] == [{"id": 1, "vtm_id": 1, "name": "Prescribed VMP"}]
@@ -286,15 +318,30 @@ def test_metadata_dmd(client, rxdb, medications):
286318
assert [record["descr"] for record in payload["ont_form_route"]] == ["tablet.oral"]
287319

288320

289-
def test_metadata_bnf(client, rxdb, bnf_codes, medications):
321+
@pytest.mark.parametrize(
322+
"prescribing_date_relative_to_index_date",
323+
[DateRelativeToIndexDate.BEFORE, DateRelativeToIndexDate.AFTER],
324+
)
325+
def test_metadata_bnf(
326+
client, rxdb, bnf_codes, medications, prescribing_date_relative_to_index_date
327+
):
328+
prescribing_date = default_date_relative_to_index_date(
329+
prescribing_date_relative_to_index_date
330+
)
331+
290332
# The bnf_codes fixture provides the BNF hierarchy; we also need to link a VMP to a
291333
# generic methotrexate presentation and prescribe it so that appears in the
292334
# medications view.
293335
medications.add_rows([{"bnf_code": "1001030U0BDAAAB"}])
294-
rxdb.ingest([{"bnf_code": "1001030U0BDAAAB"}])
336+
rxdb.ingest([{"bnf_code": "1001030U0BDAAAB", "date": prescribing_date}])
295337

296338
rsp = client.get("/api/metadata/bnf/")
297339
payload = rsp.json()
340+
341+
if prescribing_date_relative_to_index_date == DateRelativeToIndexDate.BEFORE:
342+
assert payload["bnf"] == []
343+
return
344+
298345
assert {
299346
"code": "10",
300347
"level": 1,

0 commit comments

Comments
 (0)