Skip to content

Commit

Permalink
update handling of strings that cannot be formated with json laods
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed May 3, 2024
1 parent 6c24e6d commit 32baf1f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
38 changes: 12 additions & 26 deletions tests/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,38 +198,24 @@ async def run_mongo_op(
if not ignore_errors:
raise
else:
output.data = stdout
output.data = _process_mongo_operation_result(stdout)
logger.info("Done: '%s'", output)
return output


def _process_mongo_operation_result(stdout, stderr, expect_json_load):
def _process_mongo_operation_result(stdout: str):
"""Attempts to process the mongo operation result when `json.loads` fails to do so."""
try:
return json.loads(stdout)
except Exception:
logger.error(
"Could not serialize the output into json.{}{}".format(
f"\n\tSTDOUT:\n\t {stdout}" if stdout else "",
f"\n\tSTDERR:\n\t {stderr}" if stderr else "",
)
logger.info("Attempt to cast to python dict manually")
# cast to python dict
dict_string = re.sub(r"(\w+)(\s*:\s*)", r'"\1"\2', stdout)
dict_string = (
dict_string.replace("true", "True").replace("false", "False").replace("null", "None")
)
logger.error(f"Failed to load operation result: {stdout} to json")
if expect_json_load:
raise
else:
try:
logger.info("Attempt to cast to python dict manually")
# cast to python dict
dict_string = re.sub(r"(\w+)(\s*:\s*)", r'"\1"\2', stdout)
dict_string = (
dict_string.replace("true", "True")
.replace("false", "False")
.replace("null", "None")
)
return eval(dict_string)
except Exception:
logger.error(f"Failed to cast response to python dict. Returning stdout: {stdout}")
return stdout
return eval(dict_string)
except Exception:
logger.error(f"Failed to cast response to python dict. Returning stdout: {stdout}")
return stdout


def primary_host(rs_status_data: dict) -> Optional[str]:
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/relation_tests/test_charm_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async def verify_crud_operations(ops_test: OpsTest, connection_string: str):
ubuntu_name_updated = '{"$set": {"release_name": "Fancy Fossa"}}'
cmd = f"db.test_collection.updateOne({ubuntu_version}, {ubuntu_name_updated})"
result = await run_mongo_op(
ops_test, cmd, f'"{connection_string}"', stringify=False, expect_json_load=False
ops_test, cmd, f'"{connection_string}"', stringify=False, ignore_errors=True
)
assert result.data["acknowledged"] is True

Expand All @@ -123,7 +123,7 @@ async def verify_crud_operations(ops_test: OpsTest, connection_string: str):
# delete the data
cmd = 'db.test_collection.deleteOne({"release_name": "Fancy Fossa"})'
result = await run_mongo_op(
ops_test, cmd, f'"{connection_string}"', stringify=False, expect_json_load=False
ops_test, cmd, f'"{connection_string}"', stringify=False, ignore_errors=True
)
assert result.data["acknowledged"] is True

Expand Down

0 comments on commit 32baf1f

Please sign in to comment.