forked from opensafely-core/cohort-extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_codelistlib.py
63 lines (48 loc) · 2.22 KB
/
test_codelistlib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import pytest
from cohortextractor.codelistlib import (
codelist,
filter_codes_by_category,
combine_codelists,
)
def test_filter_codes_by_category():
codes = codelist([("1", "A"), ("2", "B"), ("3", "A"), ("4", "C")], "ctv3")
filtered = filter_codes_by_category(codes, include=["B", "C"])
assert filtered.system == codes.system
assert filtered == [("2", "B"), ("4", "C")]
def test_combine_codelists():
list_1 = codelist(["A", "B", "C"], system="ctv3")
list_2 = codelist(["X", "Y", "Z"], system="ctv3")
combined = combine_codelists(list_1, list_2)
expected = codelist(["A", "B", "C", "X", "Y", "Z"], system="ctv3")
assert combined == expected
assert combined.system == expected.system
def test_combine_codelists_with_categories():
list_1 = codelist([("A", "foo"), ("B", "bar")], system="icd10")
list_2 = codelist([("X", "foo"), ("Y", "bar")], system="icd10")
combined = combine_codelists(list_1, list_2)
expected = codelist(
[("A", "foo"), ("B", "bar"), ("X", "foo"), ("Y", "bar")], system="icd10"
)
assert combined == expected
assert combined.system == expected.system
def test_combine_codelists_raises_error_for_mixed_systems():
list_1 = codelist(["A", "B", "C"], system="ctv3")
list_2 = codelist(["X", "Y", "Z"], system="icd10")
with pytest.raises(ValueError):
combine_codelists(list_1, list_2)
def test_combine_codelists_raises_error_for_mixed_categorisation():
list_1 = codelist([("A", "foo"), ("B", "bar")], system="icd10")
list_2 = codelist(["X", "Y", "Z"], system="icd10")
with pytest.raises(ValueError):
combine_codelists(list_1, list_2)
def test_combine_codelists_with_duplicates():
list_1 = codelist(["A", "B", "C"], system="ctv3")
list_2 = codelist(["X", "B", "Z"], system="ctv3")
combined = combine_codelists(list_1, list_2)
expected = codelist(["A", "B", "C", "X", "Z"], system="ctv3")
assert combined == expected
def test_combine_codelists_raises_error_on_inconsistent_categorisation():
list_1 = codelist([("A", "foo"), ("B", "bar")], system="icd10")
list_2 = codelist([("X", "foo"), ("B", "foo")], system="icd10")
with pytest.raises(ValueError):
combine_codelists(list_1, list_2)