Skip to content
Snippets Groups Projects
Commit d81e4d74 authored by Benedikt's avatar Benedikt
Browse files

captured warnings in pytest

parent 1be616c5
No related branches found
No related tags found
No related merge requests found
Pipeline #50539 passed
import os import os
import json import json
import pytest import pytest
import warnings
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from dccXMLJSONConv.dccServer import app from dccXMLJSONConv.dccServer import app
...@@ -18,17 +19,26 @@ def test_root(): ...@@ -18,17 +19,26 @@ def test_root():
"message": "Hello!\nBetter use the URL /json2dcc/?js={...} \n or /dcc2json (POST method)"} "message": "Hello!\nBetter use the URL /json2dcc/?js={...} \n or /dcc2json (POST method)"}
@pytest.mark.skipif(not os.path.exists(os.path.join(data_dir, "APITestXMLStub.xml")), reason="Test XML file not found") @pytest.mark.skipif(not os.path.exists(os.path.join(data_dir, "APITestXMLStub.xml")),
reason="Test XML file not found")
def test_dcc2json_with_valid_xml(): def test_dcc2json_with_valid_xml():
xml_data_path = os.path.join(data_dir, "APITestXMLStub.xml") xml_data_path = os.path.join(data_dir, "APITestXMLStub.xml")
with open(xml_data_path, "r") as f: with open(xml_data_path, "r") as f:
xml_data = f.read() xml_data = f.read()
response = client.post("/dcc2json/", json={"xml": xml_data}) # Capture the expected warning
with pytest.warns(UserWarning, match="XML->JSON Validation error:.*"):
response = client.post("/dcc2json/", json={"xml": xml_data})
# Validate response
response_json = response.json() response_json = response.json()
expected_json_path = os.path.join(data_dir, "APITestJSONStub.json")
with open(expected_json_path, "r") as f:
expected_json = json.load(f)
assert response.status_code == 200 assert response.status_code == 200
assert response_json == json.loads(open(os.path.join(data_dir, "APITestJSONStub.json"), "r").read()) assert response_json == expected_json
def test_dcc2json_with_empty_xml(): def test_dcc2json_with_empty_xml():
...@@ -43,9 +53,17 @@ def test_json2dcc_with_valid_json(): ...@@ -43,9 +53,17 @@ def test_json2dcc_with_valid_json():
with open(json_data_path, "r") as f: with open(json_data_path, "r") as f:
json_data = json.load(f) json_data = json.load(f)
response = client.post("/json2dcc/", json={"js": json_data}) # Capture the expected warning during the request
with pytest.warns(UserWarning, match="JSON->XML Validation error:.*"):
response = client.post("/json2dcc/", json={"js": json_data})
# Validate response
assert response.status_code == 200 assert response.status_code == 200
assert response.text == open(os.path.join(data_dir, "JSONTOXMLRespons.xml"), "r").read() expected_xml_path = os.path.join(data_dir, "JSONTOXMLRespons.xml")
with open(expected_xml_path, "r") as f:
expected_xml = f.read()
assert response.text == expected_xml
def test_json2dcc_with_empty_json(): def test_json2dcc_with_empty_json():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment