compressedfhir 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of compressedfhir might be problematic. Click here for more details.

Files changed (41) hide show
  1. compressedfhir/__init__.py +0 -0
  2. compressedfhir/fhir/__init__.py +0 -0
  3. compressedfhir/fhir/base_resource_list.py +165 -0
  4. compressedfhir/fhir/fhir_bundle.py +291 -0
  5. compressedfhir/fhir/fhir_bundle_entry.py +234 -0
  6. compressedfhir/fhir/fhir_bundle_entry_list.py +82 -0
  7. compressedfhir/fhir/fhir_bundle_entry_request.py +71 -0
  8. compressedfhir/fhir/fhir_bundle_entry_response.py +64 -0
  9. compressedfhir/fhir/fhir_bundle_entry_search.py +75 -0
  10. compressedfhir/fhir/fhir_identifier.py +84 -0
  11. compressedfhir/fhir/fhir_link.py +63 -0
  12. compressedfhir/fhir/fhir_meta.py +47 -0
  13. compressedfhir/fhir/fhir_resource.py +163 -0
  14. compressedfhir/fhir/fhir_resource_list.py +143 -0
  15. compressedfhir/fhir/fhir_resource_map.py +193 -0
  16. compressedfhir/fhir/test/__init__.py +0 -0
  17. compressedfhir/fhir/test/test_bundle_entry.py +129 -0
  18. compressedfhir/fhir/test/test_bundle_entry_list.py +187 -0
  19. compressedfhir/fhir/test/test_bundle_entry_request.py +74 -0
  20. compressedfhir/fhir/test/test_bundle_entry_response.py +65 -0
  21. compressedfhir/fhir/test/test_fhir_bundle.py +245 -0
  22. compressedfhir/fhir/test/test_fhir_resource.py +225 -0
  23. compressedfhir/fhir/test/test_fhir_resource_list.py +160 -0
  24. compressedfhir/fhir/test/test_fhir_resource_map.py +293 -0
  25. compressedfhir/py.typed +0 -0
  26. compressedfhir/utilities/__init__.py +0 -0
  27. compressedfhir/utilities/compressed_dict/__init__.py +0 -0
  28. compressedfhir/utilities/compressed_dict/v1/__init__.py +0 -0
  29. compressedfhir/utilities/compressed_dict/v1/compressed_dict.py +635 -0
  30. compressedfhir/utilities/compressed_dict/v1/compressed_dict_access_error.py +2 -0
  31. compressedfhir/utilities/compressed_dict/v1/compressed_dict_storage_mode.py +50 -0
  32. compressedfhir/utilities/compressed_dict/v1/test/__init__.py +0 -0
  33. compressedfhir/utilities/compressed_dict/v1/test/test_compressed_dict.py +360 -0
  34. compressedfhir/utilities/fhir_json_encoder.py +30 -0
  35. compressedfhir/utilities/json_helpers.py +181 -0
  36. compressedfhir-0.0.1.dist-info/METADATA +28 -0
  37. compressedfhir-0.0.1.dist-info/RECORD +41 -0
  38. compressedfhir-0.0.1.dist-info/WHEEL +5 -0
  39. compressedfhir-0.0.1.dist-info/licenses/LICENSE +201 -0
  40. compressedfhir-0.0.1.dist-info/top_level.txt +2 -0
  41. tests/__init__.py +0 -0
@@ -0,0 +1,160 @@
1
+ import json
2
+ from unittest.mock import Mock
3
+
4
+ import pytest
5
+
6
+ from compressedfhir.fhir.fhir_resource_list import FhirResourceList
7
+
8
+
9
+ class TestFhirResourceList:
10
+ def test_get_resource_type_and_ids(self) -> None:
11
+ # Create mock FhirResource objects
12
+ mock_resources = [
13
+ Mock(resource_type="Patient", id="123"),
14
+ Mock(resource_type="Observation", id="456"),
15
+ ]
16
+
17
+ resource_list = FhirResourceList(mock_resources)
18
+
19
+ # Check the returned resource type and ids
20
+ expected_result = ["Patient/123", "Observation/456"]
21
+ assert resource_list.get_resource_type_and_ids() == expected_result
22
+
23
+ def test_get_operation_outcomes(self) -> None:
24
+ # Create mock FhirResource objects with different resource types
25
+ mock_resources = [
26
+ Mock(resource_type="OperationOutcome", id="err1"),
27
+ Mock(resource_type="Patient", id="123"),
28
+ Mock(resource_type="OperationOutcome", id="err2"),
29
+ ]
30
+
31
+ resource_list = FhirResourceList(mock_resources)
32
+
33
+ # Get operation outcomes
34
+ operation_outcomes = resource_list.get_operation_outcomes()
35
+
36
+ # Check the result
37
+ assert len(operation_outcomes) == 2
38
+ assert all(r.resource_type == "OperationOutcome" for r in operation_outcomes)
39
+
40
+ def test_get_resources_except_operation_outcomes(self) -> None:
41
+ # Create mock FhirResource objects with different resource types
42
+ mock_resources = [
43
+ Mock(resource_type="OperationOutcome", id="err1"),
44
+ Mock(resource_type="Patient", id="123"),
45
+ Mock(resource_type="Observation", id="456"),
46
+ Mock(resource_type="OperationOutcome", id="err2"),
47
+ ]
48
+
49
+ resource_list = FhirResourceList(mock_resources)
50
+
51
+ # Get resources except operation outcomes
52
+ valid_resources = resource_list.get_resources_except_operation_outcomes()
53
+
54
+ # Check the result
55
+ assert len(valid_resources) == 2
56
+ assert all(r.resource_type != "OperationOutcome" for r in valid_resources)
57
+
58
+ def test_remove_duplicates(self) -> None:
59
+ # Create mock FhirResource objects with duplicates
60
+ mock_resources = [
61
+ Mock(resource_type="Patient", id="123", resource_type_and_id="Patient/123"),
62
+ Mock(resource_type="Patient", id="123", resource_type_and_id="Patient/123"),
63
+ Mock(
64
+ resource_type="Observation",
65
+ id="456",
66
+ resource_type_and_id="Observation/456",
67
+ ),
68
+ Mock(resource_type="Patient", id="789", resource_type_and_id="Patient/789"),
69
+ ]
70
+
71
+ resource_list = FhirResourceList(mock_resources)
72
+
73
+ # Remove duplicates
74
+ resource_list.remove_duplicates()
75
+
76
+ # Check the result
77
+ assert len(resource_list) == 3
78
+ assert len(set(r.resource_type_and_id for r in resource_list)) == 3
79
+
80
+ def test_to_json(self) -> None:
81
+ # Create mock FhirResource objects
82
+ mock_resources = [
83
+ Mock(
84
+ resource_type="Patient",
85
+ id="123",
86
+ dict=lambda: {"id": "123", "resourceType": "Patient"},
87
+ ),
88
+ Mock(
89
+ resource_type="Observation",
90
+ id="456",
91
+ dict=lambda: {"id": "456", "resourceType": "Observation"},
92
+ ),
93
+ ]
94
+
95
+ resource_list = FhirResourceList(mock_resources)
96
+
97
+ # Convert to JSON
98
+ json_str = resource_list.json()
99
+
100
+ # Parse and check the JSON
101
+ parsed_json = json.loads(json_str)
102
+ assert len(parsed_json) == 2
103
+ assert parsed_json[0]["resourceType"] == "Patient"
104
+ assert parsed_json[1]["resourceType"] == "Observation"
105
+
106
+ @pytest.mark.asyncio
107
+ async def test_consume_resource_async_default(self) -> None:
108
+ # Create mock FhirResource objects
109
+ mock_resources = [
110
+ Mock(resource_type="Patient", id="123"),
111
+ Mock(resource_type="Observation", id="456"),
112
+ ]
113
+
114
+ resource_list = FhirResourceList(mock_resources)
115
+
116
+ # Consume resources asynchronously with default (None) batch size
117
+ async for batch in resource_list.consume_resource_batch_async(batch_size=None):
118
+ assert len(batch) == 2
119
+
120
+ # Ensure all resources are consumed
121
+ assert len(resource_list) == 0
122
+
123
+ @pytest.mark.asyncio
124
+ async def test_consume_resource_async_with_batch_size(self) -> None:
125
+ # Create mock FhirResource objects
126
+ mock_resources = [
127
+ Mock(resource_type="Patient", id="123"),
128
+ Mock(resource_type="Observation", id="456"),
129
+ Mock(resource_type="Condition", id="789"),
130
+ ]
131
+
132
+ resource_list = FhirResourceList(mock_resources)
133
+
134
+ # Consume resources asynchronously with batch size of 2
135
+ batches = []
136
+ async for batch in resource_list.consume_resource_batch_async(batch_size=2):
137
+ batches.append(batch)
138
+
139
+ # Check batches
140
+ assert len(batches) == 2
141
+ assert len(batches[0]) == 2
142
+ assert len(batches[1]) == 1
143
+
144
+ # Ensure all resources are consumed
145
+ assert len(resource_list) == 0
146
+
147
+ def test_consume_resource_async_invalid_batch_size(self) -> None:
148
+ resource_list = FhirResourceList()
149
+
150
+ # Test invalid batch sizes
151
+ with pytest.raises(ValueError, match="Batch size must be greater than 0."):
152
+
153
+ async def test() -> None:
154
+ async for _ in resource_list.consume_resource_batch_async(batch_size=0):
155
+ pass
156
+
157
+ # Run the async function
158
+ import asyncio
159
+
160
+ asyncio.run(test())
@@ -0,0 +1,293 @@
1
+ import pytest
2
+ import json
3
+
4
+ from compressedfhir.fhir.fhir_resource import FhirResource
5
+ from compressedfhir.fhir.fhir_resource_list import FhirResourceList
6
+ from compressedfhir.fhir.fhir_resource_map import FhirResourceMap
7
+ from compressedfhir.utilities.compressed_dict.v1.compressed_dict_storage_mode import (
8
+ CompressedDictStorageMode,
9
+ )
10
+
11
+
12
+ class TestFhirResourceMap:
13
+ def test_init_empty(self) -> None:
14
+ """Test initialization with no initial dictionary."""
15
+ resource_map = FhirResourceMap()
16
+ assert resource_map._resource_map == {}
17
+
18
+ def test_init_with_initial_dict(self) -> None:
19
+ """Test initialization with an initial dictionary."""
20
+ initial_dict = {
21
+ "Patient": FhirResourceList(
22
+ [
23
+ FhirResource(
24
+ {"id": "123", "resourceType": "Patient"},
25
+ storage_mode=CompressedDictStorageMode(),
26
+ )
27
+ ]
28
+ ),
29
+ "Observation": FhirResourceList(
30
+ [
31
+ FhirResource(
32
+ {"id": "456", "resourceType": "Observation"},
33
+ storage_mode=CompressedDictStorageMode(),
34
+ )
35
+ ]
36
+ ),
37
+ }
38
+ resource_map = FhirResourceMap(initial_dict=initial_dict)
39
+ assert resource_map._resource_map == initial_dict
40
+
41
+ def test_to_dict(self) -> None:
42
+ """Test conversion of resource map to dictionary."""
43
+ patient_resource = {"id": "123", "resourceType": "Patient"}
44
+ initial_dict = {
45
+ "Patient": FhirResourceList(
46
+ [
47
+ FhirResource(
48
+ patient_resource, storage_mode=CompressedDictStorageMode()
49
+ )
50
+ ]
51
+ )
52
+ }
53
+ resource_map = FhirResourceMap(initial_dict=initial_dict)
54
+ result = resource_map.dict()
55
+ assert result == {"Patient": [patient_resource]}
56
+
57
+ def test_get_existing_resource_type(self) -> None:
58
+ """Test getting resources for an existing resource type."""
59
+ patient_resources = FhirResourceList(
60
+ [
61
+ FhirResource(
62
+ {"id": "123", "resourceType": "Patient"},
63
+ storage_mode=CompressedDictStorageMode(),
64
+ )
65
+ ]
66
+ )
67
+ initial_dict = {"Patient": patient_resources}
68
+ resource_map = FhirResourceMap(initial_dict=initial_dict)
69
+
70
+ result = resource_map.get(resource_type="Patient")
71
+ assert result == patient_resources
72
+
73
+ def test_get_nonexistent_resource_type(self) -> None:
74
+ """Test getting resources for a non-existent resource type."""
75
+ resource_map = FhirResourceMap()
76
+ result = resource_map.get(resource_type="Patient")
77
+ assert result is None
78
+
79
+ def test_setitem(self) -> None:
80
+ """Test setting an item in the resource map."""
81
+ resource_map = FhirResourceMap()
82
+ patient_resources = FhirResourceList(
83
+ [
84
+ FhirResource(
85
+ {"id": "123", "resourceType": "Patient"},
86
+ storage_mode=CompressedDictStorageMode(),
87
+ )
88
+ ]
89
+ )
90
+ resource_map["Patient"] = patient_resources
91
+
92
+ assert resource_map._resource_map["Patient"] == patient_resources
93
+
94
+ def test_getitem(self) -> None:
95
+ """Test getting an item from the resource map."""
96
+ patient_resources = FhirResourceList(
97
+ [
98
+ FhirResource(
99
+ {"id": "123", "resourceType": "Patient"},
100
+ storage_mode=CompressedDictStorageMode(),
101
+ )
102
+ ]
103
+ )
104
+ resource_map = FhirResourceMap(initial_dict={"Patient": patient_resources})
105
+
106
+ result = resource_map["Patient"]
107
+ assert result == patient_resources
108
+
109
+ def test_delitem(self) -> None:
110
+ """Test deleting an item from the resource map."""
111
+ patient_resources = FhirResourceList(
112
+ [
113
+ FhirResource(
114
+ {"id": "123", "resourceType": "Patient"},
115
+ storage_mode=CompressedDictStorageMode(),
116
+ )
117
+ ]
118
+ )
119
+ resource_map = FhirResourceMap(initial_dict={"Patient": patient_resources})
120
+
121
+ del resource_map["Patient"]
122
+ assert "Patient" not in resource_map._resource_map
123
+
124
+ def test_delitem_nonexistent(self) -> None:
125
+ """Test deleting a non-existent item raises KeyError."""
126
+ resource_map = FhirResourceMap()
127
+ with pytest.raises(KeyError):
128
+ del resource_map["Patient"]
129
+
130
+ def test_contains(self) -> None:
131
+ """Test checking if a resource type exists in the map."""
132
+ patient_resources = FhirResourceList(
133
+ [
134
+ FhirResource(
135
+ {"id": "123", "resourceType": "Patient"},
136
+ storage_mode=CompressedDictStorageMode(),
137
+ )
138
+ ]
139
+ )
140
+ resource_map = FhirResourceMap(initial_dict={"Patient": patient_resources})
141
+
142
+ assert "Patient" in resource_map
143
+ assert "Observation" not in resource_map
144
+
145
+ def test_items(self) -> None:
146
+ """Test getting all items from the resource map."""
147
+ patient_resources = FhirResourceList(
148
+ [
149
+ FhirResource(
150
+ {"id": "123", "resourceType": "Patient"},
151
+ storage_mode=CompressedDictStorageMode(),
152
+ )
153
+ ]
154
+ )
155
+ obs_resources = FhirResourceList(
156
+ [
157
+ FhirResource(
158
+ {"id": "456", "resourceType": "Observation"},
159
+ storage_mode=CompressedDictStorageMode(),
160
+ )
161
+ ]
162
+ )
163
+ initial_dict = {"Patient": patient_resources, "Observation": obs_resources}
164
+ resource_map = FhirResourceMap(initial_dict=initial_dict)
165
+
166
+ items = resource_map.items()
167
+ assert len(items) == 2
168
+ assert ("Patient", patient_resources) in items
169
+ assert ("Observation", obs_resources) in items
170
+
171
+ def test_get_resource_count(self) -> None:
172
+ """Test getting the total number of resources."""
173
+ initial_dict = {
174
+ "Patient": FhirResourceList(
175
+ [
176
+ FhirResource({"id": "1"}, storage_mode=CompressedDictStorageMode()),
177
+ FhirResource({"id": "2"}, storage_mode=CompressedDictStorageMode()),
178
+ ]
179
+ ),
180
+ "Observation": FhirResourceList(
181
+ [FhirResource({"id": "3"}, storage_mode=CompressedDictStorageMode())]
182
+ ),
183
+ }
184
+ resource_map = FhirResourceMap(initial_dict=initial_dict)
185
+
186
+ assert resource_map.get_resource_count() == 3
187
+
188
+ def test_clear(self) -> None:
189
+ """Test clearing the resource map."""
190
+ patient_resources = FhirResourceList(
191
+ [
192
+ FhirResource(
193
+ {"id": "123", "resourceType": "Patient"},
194
+ storage_mode=CompressedDictStorageMode(),
195
+ )
196
+ ]
197
+ )
198
+ resource_map = FhirResourceMap(initial_dict={"Patient": patient_resources})
199
+
200
+ resource_map.clear()
201
+ assert len(resource_map._resource_map) == 0
202
+
203
+ def test_get_resource_type_and_ids(self) -> None:
204
+ """Test getting resource type and IDs."""
205
+ initial_dict = {
206
+ "Patient": FhirResourceList(
207
+ [
208
+ FhirResource(
209
+ {"id": "123"}, storage_mode=CompressedDictStorageMode()
210
+ ),
211
+ FhirResource(
212
+ {"id": "456"}, storage_mode=CompressedDictStorageMode()
213
+ ),
214
+ ]
215
+ ),
216
+ "Observation": FhirResourceList(
217
+ [FhirResource({"id": "789"}, storage_mode=CompressedDictStorageMode())]
218
+ ),
219
+ }
220
+ resource_map = FhirResourceMap(initial_dict=initial_dict)
221
+
222
+ result = resource_map.get_resource_type_and_ids()
223
+ assert set(result) == {"Patient/123", "Patient/456", "Observation/789"}
224
+
225
+ def test_get_operation_outcomes(self) -> None:
226
+ """Test getting operation outcomes."""
227
+ op_outcomes = FhirResourceList(
228
+ [FhirResource({"id": "op1"}, storage_mode=CompressedDictStorageMode())]
229
+ )
230
+ initial_dict = {"OperationOutcome": op_outcomes}
231
+ resource_map = FhirResourceMap(initial_dict=initial_dict)
232
+
233
+ result = resource_map.get_operation_outcomes()
234
+ assert result == op_outcomes
235
+
236
+ def test_get_operation_outcomes_empty(self) -> None:
237
+ """Test getting operation outcomes when none exist."""
238
+ resource_map = FhirResourceMap()
239
+ result = resource_map.get_operation_outcomes()
240
+ assert result == FhirResourceList()
241
+
242
+ def test_get_resources_except_operation_outcomes(self) -> None:
243
+ """Test getting resources excluding operation outcomes."""
244
+ initial_dict = {
245
+ "Patient": FhirResourceList(
246
+ [FhirResource({"id": "123"}, storage_mode=CompressedDictStorageMode())]
247
+ ),
248
+ "OperationOutcome": FhirResourceList(
249
+ [FhirResource({"id": "op1"}, storage_mode=CompressedDictStorageMode())]
250
+ ),
251
+ "Observation": FhirResourceList(
252
+ [FhirResource({"id": "456"}, storage_mode=CompressedDictStorageMode())]
253
+ ),
254
+ }
255
+ resource_map = FhirResourceMap(initial_dict=initial_dict)
256
+
257
+ result = resource_map.get_resources_except_operation_outcomes()
258
+ assert len(result) == 2
259
+ assert all(
260
+ resource.get("resourceType") != "OperationOutcome" for resource in result
261
+ )
262
+
263
+ def test_to_json(self) -> None:
264
+ """Test converting resource map to JSON."""
265
+ initial_dict = {
266
+ "Patient": FhirResourceList(
267
+ [
268
+ FhirResource(
269
+ {"id": "123", "resourceType": "Patient"},
270
+ storage_mode=CompressedDictStorageMode(),
271
+ )
272
+ ]
273
+ )
274
+ }
275
+ resource_map = FhirResourceMap(initial_dict=initial_dict)
276
+
277
+ json_str = resource_map.json()
278
+ parsed_json = json.loads(json_str)
279
+ assert parsed_json == {"Patient": [{"id": "123", "resourceType": "Patient"}]}
280
+
281
+ def test_get_count_of_resource_types(self) -> None:
282
+ """Test getting the count of unique resource types."""
283
+ initial_dict = {
284
+ "Patient": FhirResourceList(
285
+ [FhirResource({"id": "123"}, storage_mode=CompressedDictStorageMode())]
286
+ ),
287
+ "Observation": FhirResourceList(
288
+ [FhirResource({"id": "456"}, storage_mode=CompressedDictStorageMode())]
289
+ ),
290
+ }
291
+ resource_map = FhirResourceMap(initial_dict=initial_dict)
292
+
293
+ assert resource_map.get_count_of_resource_types() == 2
File without changes
File without changes
File without changes