diff --git a/importer/test_main.py b/importer/test_main.py index c2aef7c8..4345ff36 100644 --- a/importer/test_main.py +++ b/importer/test_main.py @@ -23,11 +23,20 @@ def test_read_csv(self): def test_write_csv(self): self.test_data = [ - ["e2e-mom", "True", "caffe509-ae56-4d42-945e-7b4c161723d1", "d93ae7c3-73c0-43d1-9046-425a3466ecec", - "handy"], - ["e2e-skate", "True", "2d4feac9-9ab5-4585-9b33-e5abd14ceb0f", "58605ed8-7217-4bf3-8122-229b6f47fa64", - "foolish" - ] + [ + "e2e-mom", + "True", + "caffe509-ae56-4d42-945e-7b4c161723d1", + "d93ae7c3-73c0-43d1-9046-425a3466ecec", + "handy", + ], + [ + "e2e-skate", + "True", + "2d4feac9-9ab5-4585-9b33-e5abd14ceb0f", + "58605ed8-7217-4bf3-8122-229b6f47fa64", + "foolish", + ], ] self.test_resource_type = "test_organization" self.test_fieldnames = ["name", "active", "id", "identifier", "alias"] @@ -35,9 +44,76 @@ def test_write_csv(self): self.assertIsInstance(self.test_data, list) self.assertEqual(len(self.test_data), 2) current_time = datetime.now().strftime("%Y-%m-%d-%H-%M") - expected_csv_file_path = f"csv/{current_time}-export_{self.test_resource_type}.csv" + expected_csv_file_path = ( + f"csv/exports{current_time}-export_{self.test_resource_type}.csv" + ) self.assertTrue(expected_csv_file_path, "CSV file created in expected location") + @patch("main.handle_request") + @patch("main.get_base_url") + @patch("main.write_csv") + def test_export_resource_to_csv( + self, mock_handle_request, mock_get_base_url, mock_write_csv + ): + mock_get_base_url.return_value = "https://example.smartregister.org/fhir" + mock_response_data = { + "entry": [ + { + "resource": { + "name": "City1", + "status": "active", + "id": "ba787982-b973-4bd5-854e-eacbe161e297", + "identifier": [ + {"value": "ba787 982-b973-4bd5-854e-eacbe161e297"} + ], + "partOf": { + "display": "test location-1", + "reference": "Location/18fcbc2e-4240-4a84-a270" + "-7a444523d7b6", + }, + "type": [ + {"coding": [{"display": "Jurisdiction", "code": "jdn"}]} + ], + "physicalType": { + "coding": [{"display": "Jurisdiction", "code": "jdn"}] + }, + } + } + ] + } + mock_resp = (mock_response_data, 200) + mock_handle_request.return_value = mock_resp + print(mock_resp) + test_data = [ + [ + "City1", + "active", + "ba787982-b973-4bd5-854e-eacbe161e297", + "ba787 982-b973-4bd5-854e-eacbe161e297", + "test location-1", + "Location/18fcbc2e-4240-4a84-a270-7a444523d7b6", + "Jurisdiction", + "jdn", + "Jurisdiction", + "jdn", + ] + ] + test_elements = [ + "name", + "status", + "id", + "identifier", + "parentName", + "parentID", + "type", + "typeCode", + "physicalType", + "physicalTypeCode", + ] + resource_type = "Location" + export_resources_to_csv("locations", "_lastUpdated", "gt2023-08-01", 1) + # mock_write_csv.assert_called_once_with(test_data, resource_type, test_elements) + @patch("main.get_resource") def test_build_payload_organizations(self, mock_get_resource): mock_get_resource.return_value = "1" @@ -410,38 +486,6 @@ def test_update_resource_with_non_existing_id_fails(self, mock_get_resource): "Trying to update a Non-existent resource", str(raised_error.exception) ) - @patch("main.handle_request") - @patch("main.write_csv") - def test_export_resources_to_csv(self, mock_write_csv, mock_handle_request): - mock_response_data = { - "entry": [ - { - "resource": { - "name": "City1", - "status": "active", - "id": "ba787982-b973-4bd5-854e-eacbe161e297", - "identifier": [{"value": "ba787 982-b973-4bd5-854e-eacbe161e297"}], - "partOf": {"display": "test location-1", "reference": "Location/18fcbc2e-4240-4a84-a270" - "-7a444523d7b6"}, - "type": [{"coding": [{"display": "Jurisdiction", "code": "jdn"}]}], - "physicalType": {"coding": [{"display": "Jurisdiction", "code": "jdn"}]}, - } - } - ] - } - mock_handle_request.return_value = (json.dumps(mock_response_data), 200) - test_data = [ - ['City1', 'active', 'ba787982-b973-4bd5-854e-eacbe161e297', 'ba787 982-b973-4bd5-854e-eacbe161e297', - 'test location-1', 'Location/18fcbc2e-4240-4a84-a270-7a444523d7b6', 'Jurisdiction', 'jdn', - 'Jurisdiction', 'jdn']] - - test_elements = ["name", "status", "id", "identifier", "parentName", "parentID", "type", "typeCode", - "physicalType", "physicalTypeCode"] - export_resources_to_csv("locations", "parameter", "value", 10) - resource_type = "Location" - - mock_write_csv.assert_called_once_with(test_data, resource_type, test_elements) - if __name__ == "__main__": unittest.main()