def test_handle_create(self, instance: los_org.OrgUnit, dar_uuid: UUID): importer = los_org.OrgUnitImporter() with self._mock_dar_lookup((instance.post_address, dar_uuid)): with self._mock_read_csv(instance): with self._mock_get_client_session(): with self._mock_create_details() as mock_create_details: self._run_until_complete( importer.handle_create("unused_filename.csv")) mock_create_details.assert_called_once()
def test_does_nothing_on_empty_addresses(self, org_units: List[los_org.OrgUnit]): importer = los_org.OrgUnitImporter() with self._mock_dar_lookup(("", None)): with mock.patch.object(importer, "write_failed_addresses"): self._run_until_complete( importer.handle_addresses(org_units, "filename.csv")) # Assert that we returned early because we only saw empty # addresses in `org_units`. assert importer.dar_cache == {}
def test_finds_failed_addresses(self, instance: los_org.OrgUnit): importer = los_org.OrgUnitImporter() expected_filename = "filename.csv" # Patch DAR lookup function to indicate a failed address lookup for any # address passed to it. with self._mock_dar_lookup((instance.post_address, None)): # Patch `write_failed_addresses` so we can test its arguments with mock.patch.object(importer, "write_failed_addresses") as mock_write: # Run method under test self._run_until_complete( importer.handle_addresses([instance], "filename.csv")) # Assert args to mocked `write_failed_addresses` expected_failed = [ los_org.FailedDARLookup( org_uuid=instance.org_uuid, post_address=instance.post_address, ) ] mock_write.assert_called_once_with(expected_failed, expected_filename)
def test_handle_initial(self, instance: los_org.OrgUnit, dar_uuid: UUID): importer = los_org.OrgUnitImporter() with self._mock_dar_lookup((instance.post_address, dar_uuid)): with self._mock_read_csv(instance): with self._mock_get_client_session(): with self._mock_create_details() as mock_create_details: self._run_until_complete( importer.handle_initial("unused_filename.csv")) mock_create_details.assert_called_once() heads = list(mock_create_details.call_args.args[1]) assert heads == [{ "type": "org_unit", "uuid": str(instance.org_uuid), "user_key": instance.bvn, "parent": { "uuid": str(instance.parent_uuid) }, "validity": { "from": str(instance.start_date), "to": str(instance.end_date), }, }]
def test_writes_csv_to_ftp(self, instance: los_org.FailedDARLookup): # Patch `config.get_config` so `write_failed_addresses` sees # `MockConfig.queries_dir` instead of actual setting. with mock_config(queries_dir="/tmp/"): # Patch `FTPFileSet.write_file` so we can test its arguments with mock.patch.object(los_files.FTPFileSet, "write_file") as mock_write: # Run `write_failed_addresses` importer = los_org.OrgUnitImporter() importer.write_failed_addresses([instance], "filename.csv") # Assert our mocked `write_file` got the expected args mock_write.assert_called_once() # First arg is filename, second arg is CSV file (as bytes) filename, csv_buf = mock_write.call_args_list[0].args # Filename is appended to output filename assert filename == "failed_addr_filename.csv" # Assert contents of the CSV file built csv_rows = list(csv.DictReader(csv_buf, delimiter="#")) assert len(csv_rows) == 1 assert csv_rows[0] == { "org_uuid": str(instance.org_uuid), "post_address": instance.post_address, }