44import pytest
55
66from openprescribing .web import api
7+ from tests .utils .data_utils import (
8+ DateRelativeToIndexDate ,
9+ default_date_relative_to_index_date ,
10+ )
711from 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