Improve test coverage.

Minor code clean up.
pull/3573/head
derekpierre 2024-12-17 16:00:13 -05:00
parent cd7a0de529
commit b7cb9375a7
No known key found for this signature in database
3 changed files with 50 additions and 9 deletions

View File

@ -20,6 +20,7 @@ def test_json_api_condition_initialization():
assert condition.endpoint == "https://api.example.com/data"
assert condition.query == "$.store.book[0].price"
assert condition.return_value_test.eval(0)
assert condition.timeout == JsonApiCondition.EXECUTION_CALL_TYPE.TIMEOUT
def test_json_api_condition_invalid_type():
@ -34,7 +35,7 @@ def test_json_api_condition_invalid_type():
)
def test_https_enforcement():
def test_json_api_https_enforcement():
with pytest.raises(InvalidCondition, match="Not a valid URL"):
_ = JsonApiCondition(
endpoint="http://api.example.com/data",
@ -43,7 +44,7 @@ def test_https_enforcement():
)
def test_invalid_authorization_token():
def test_json_api_invalid_authorization_token():
with pytest.raises(InvalidCondition, match="Invalid value for authorization token"):
_ = JsonApiCondition(
endpoint="https://api.example.com/data",
@ -161,7 +162,23 @@ def test_json_api_condition_verify_invalid_json(mocker):
condition.verify()
def test_non_json_response(mocker):
def test_json_api_non_200_status(mocker):
# Mock the requests.get method to return a response with non-JSON content
mock_response = mocker.Mock()
mock_response.status_code = 400
mocker.patch("requests.get", return_value=mock_response)
condition = JsonApiCondition(
endpoint="https://api.example.com/data",
query="$.store.book[0].price",
return_value_test=ReturnValueTest("==", 18),
)
with pytest.raises(JsonRequestException, match="Failed to fetch from endpoint"):
condition.verify()
def test_json_api_non_json_response(mocker):
# Mock the requests.get method to return a response with non-JSON content
mock_response = mocker.Mock()
mock_response.status_code = 200

View File

@ -11,11 +11,17 @@ def test_jsonpath_field_valid():
assert result == valid_jsonpath
def test_jsonpath_field_invalid():
@pytest.mark.parametrize(
"invalid_jsonpath",
[
"invalid jsonpath",
"}{[]$%",
12,
12.25,
True,
],
)
def test_jsonpath_field_invalid(invalid_jsonpath):
field = JSONPathField()
invalid_jsonpath = "invalid jsonpath"
with pytest.raises(
ValidationError,
match=f"'{invalid_jsonpath}' is not a valid JSONPath expression",
):
with pytest.raises(ValidationError):
field.deserialize(invalid_jsonpath)

View File

@ -28,6 +28,7 @@ def test_json_rpc_condition_initialization():
assert condition.method == "subtract"
assert condition.query == "$.mathresult"
assert condition.return_value_test.eval(19)
assert condition.timeout == JsonRpcCondition.EXECUTION_CALL_TYPE.TIMEOUT
def test_json_rpc_condition_invalid_type():
@ -116,6 +117,23 @@ def test_json_rpc_condition_verify_params_as_dict(mocker):
}
def test_json_rpc_non_200_status(mocker):
# Mock the requests.get method to return a response with non-JSON content
mock_response = mocker.Mock()
mock_response.status_code = 400
mocker.patch("requests.post", return_value=mock_response)
condition = JsonRpcCondition(
endpoint="https://math.example.com/",
method="subtract",
params=[42, 23],
return_value_test=ReturnValueTest("==", 19),
)
with pytest.raises(JsonRequestException, match="Failed to fetch from endpoint"):
condition.verify()
def test_json_rpc_condition_verify_error(mocker):
mock_response = mocker.Mock(status_code=200)
error = {