Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update export resources function and documentation
Browse files Browse the repository at this point in the history
TraciebelWairimu committed Feb 27, 2024

Verified

This commit was signed with the committer’s verified signature.
jloleysens Jean-Louis Leysens
1 parent ce00ec2 commit a9450b3
Showing 3 changed files with 84 additions and 80 deletions.
7 changes: 4 additions & 3 deletions importer/README.md
Original file line number Diff line number Diff line change
@@ -133,9 +133,10 @@ The coverage report `coverage.html` will be at the working directory
- Set `cascade_delete` to True or False if you would like to automatically delete any linked resources. If you set it to False, and there are any linked resources, then the resources will NOT be deleted

## 10. Export resources from API endpoint to CSV file
- Run `python3 main.py --export_resources True --parameter _lastUpdated --value gt2023-08-01 --batch_size 20 --resource_type locations --log_level info`
- Run `python3 main.py --export_resources True --parameter _lastUpdated --value gt2023-08-01 --limit 20 --resource_type Location --log_level info`
- `export_resources` can either be True or False, checks if it is True and exports the resources
- The `parameter` is used as a filter for the resources "e.g _lastUpdated", and `value` is where you pass the actual parameter value "e.g gt2023-08-01" used to filter the resources
- The `batch_size` is the number of resources handled/processed at a time
- The `parameter` is used as a filter for the resources. The set default parameter is "_lastUpdated", other examples include, "name"
- The `value` is where you pass the actual parameter value to filter the resources. The set default value is "gt2023-01-01", other examples include, "Good Health Clinic 1"
- The `limit` is the number of resources exported at a time
- Specify the `resource_type` you want to export, different resource_types are exported to different csv_files
- The csv_file containing the exported resources is labelled using the current time, to know when the resources were exported for example, csv/2024-02-21-12-21-export_Location.csv
19 changes: 10 additions & 9 deletions importer/main.py
Original file line number Diff line number Diff line change
@@ -866,8 +866,8 @@ def clean_duplicates(users, cascade_delete):
def write_csv(data, resource_type, fieldnames):
logging.info("Writing to csv file")
current_time = datetime.now().strftime("%Y-%m-%d-%H-%M")
csv_file = f"csv/{current_time}-export_{resource_type}.csv"
with open(csv_file, 'w', newline='') as file:
csv_file = f"csv/exports/{current_time}-export_{resource_type}.csv"
with open(csv_file, "w", newline="") as file:
csv_writer = csv.writer(file)
csv_writer.writerow(fieldnames)
csv_writer.writerows(data)
@@ -882,7 +882,9 @@ def export_resources_to_csv(resource_type, parameter, value, limit):
base_url = get_base_url()
resource_url = "/".join([str(base_url), resource_type])
if len(parameter) > 0:
resource_url = resource_url + "?" + parameter + "=" + value + "&_count=" + str(limit)
resource_url = (
resource_url + "?" + parameter + "=" + value + "&_count=" + str(limit)
)
response = handle_request("GET", "", resource_url)
if response[1] == 200:
resources = json.loads(response[0])
@@ -932,9 +934,9 @@ def export_resources_to_csv(resource_type, parameter, value, limit):
@click.command()
@click.option("--csv_file", required=False)
@click.option("--export_resources", required=False)
@click.option("--parameter", required=False)
@click.option("--value", required=False)
@click.option("--batch_size", required=False)
@click.option("--parameter", required=False, default="_lastUpdated")
@click.option("--value", required=False, default="gt2023-01-01")
@click.option("--limit", required=False, default=1000)
@click.option("--access_token", required=False)
@click.option("--resource_type", required=False)
@click.option("--assign", required=False)
@@ -946,7 +948,7 @@ def export_resources_to_csv(resource_type, parameter, value, limit):
"--log_level", type=click.Choice(["DEBUG", "INFO", "ERROR"], case_sensitive=False)
)
def main(
csv_file, export_resources, parameter, value, batch_size, access_token, resource_type, assign, setup, group,
csv_file, export_resources, parameter, value, limit, access_token, resource_type, assign, setup, group,
roles_max,
cascade_delete,
log_level
@@ -964,8 +966,7 @@ def main(
logging.info("Starting export...")
if export_resources == "True":
logging.info("Exporting " + resource_type)
export_resources_to_csv(resource_type, parameter, value, batch_size)
logging.info("Successfully written to csv")
export_resources_to_csv(resource_type, parameter, value, limit)
exit()

# set access token
138 changes: 70 additions & 68 deletions importer/test_main.py
Original file line number Diff line number Diff line change
@@ -26,20 +26,22 @@ def test_write_csv(self):
[
"e2e-mom",
"True",
"update",
"caffe509-ae56-4d42-945e-7b4c161723d1",
"d93ae7c3-73c0-43d1-9046-425a3466ecec",
"handy",
],
[
"e2e-skate",
"True",
"update",
"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"]
self.test_fieldnames = ["name", "active", "method", "id", "identifier", "alias"]
write_csv(self.test_data, self.test_resource_type, self.test_fieldnames)
self.assertIsInstance(self.test_data, list)
self.assertEqual(len(self.test_data), 2)
@@ -49,73 +51,6 @@ def test_write_csv(self):
)
self.assertTrue(expected_csv_file_path, "CSV file created in expected location")

@patch("main.write_csv")
@patch("main.handle_request")
@patch("main.get_base_url")
def test_export_resource_to_csv(
self, mock_get_base_url, mock_handle_request, 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"}]
},
}
}
]
}

string_response = json.dumps(mock_response_data)
mock_response = (string_response, 200)
mock_handle_request.return_value = mock_response

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("Location", "_lastUpdated", "gt2023-08-01", 1)
mock_write_csv.assert_called_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"
@@ -488,6 +423,73 @@ 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.write_csv")
@patch("main.handle_request")
@patch("main.get_base_url")
def test_export_resource_to_csv(
self, mock_get_base_url, mock_handle_request, 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"}]
},
}
}
]
}
string_response = json.dumps(mock_response_data)
mock_response = (string_response, 200)
mock_handle_request.return_value = mock_response
test_data = [
[
"City1",
"active",
"update",
"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",
"method",
"id",
"identifier",
"parentName",
"parentID",
"type",
"typeCode",
"physicalType",
"physicalTypeCode",
]
resource_type = "Location"
export_resources_to_csv("Location", "_lastUpdated", "gt2023-08-01", 1)
mock_write_csv.assert_called_once_with(test_data, resource_type, test_elements)


if __name__ == "__main__":
unittest.main()

0 comments on commit a9450b3

Please sign in to comment.