Skip to content

Commit

Permalink
test: remove stack dependency from some tests (DEV-3170) (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Jan 11, 2024
1 parent a02dda5 commit 0d5627f
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 122 deletions.
57 changes: 0 additions & 57 deletions test/e2e/commands/xmlupload/test_xmlupload.py

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,5 @@ def test_update_repo_after_cloning(rosetta_folder: Path) -> None:
assert is_rosetta_up_to_date


def test_create_data_model(rosetta_folder: Path) -> None:
success = rosetta._create_json(rosetta_folder=rosetta_folder)
assert success


def test_upload_data(rosetta_folder: Path) -> None:
success = rosetta._upload_xml(rosetta_folder=rosetta_folder)
assert success


if __name__ == "__main__":
pytest.main([__file__])
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import re
from dataclasses import dataclass
from pathlib import Path
from test.unittests.commands.xmlupload.connection_mock import ConnectionMockBase
from typing import Any, ClassVar

import pytest
from lxml import etree

from dsp_tools.commands.xmlupload.check_consistency_with_ontology import do_xml_consistency_check
from dsp_tools.commands.xmlupload.ontology_client import OntologyClientLive
from dsp_tools.models.exceptions import BaseError, UserError


@dataclass
class ConnectionMockRaising(ConnectionMockBase):
def get(
self,
route: str, # noqa: ARG002 (unused-method-argument)
headers: dict[str, str] | None = None, # noqa: ARG002 (unused-method-argument)
) -> dict[str, Any]:
raise BaseError("foo")


@dataclass
class ConnectionMockWithResponses(ConnectionMockBase):
get_responses: ClassVar[list[dict[str, Any]]] = [
{
"project": {
"ontologies": ["/testonto"],
}
},
{
"@graph": [
{
"@id": "testonto:ValidResourceClass",
"knora-api:isResourceClass": True,
}
]
},
{
"@graph": [
{
"@id": "knora-api:ValidResourceClass",
"knora-api:isResourceClass": True,
}
]
},
]

def get(
self,
route: str, # noqa: ARG002 (unused-method-argument)
headers: dict[str, str] | None = None, # noqa: ARG002 (unused-method-argument)
) -> dict[str, Any]:
return self.get_responses.pop(0)


def test_error_on_nonexistent_shortcode() -> None:
root = etree.parse("testdata/xml-data/test-data-minimal.xml").getroot()
con = ConnectionMockRaising()
ontology_client = OntologyClientLive(
con=con,
shortcode="9999",
default_ontology="foo",
save_location=Path("bar"),
)
with pytest.raises(UserError, match="A project with shortcode 9999 could not be found on the DSP server"):
do_xml_consistency_check(ontology_client, root)


def test_error_on_nonexistent_onto_name() -> None:
root = etree.fromstring(
'<knora shortcode="4124" default-ontology="notexistingfantasyonto">'
'<resource label="The only resource" restype=":minimalResource" id="the_only_resource"/>'
"</knora>"
)
con = ConnectionMockWithResponses()
ontology_client = OntologyClientLive(
con=con,
shortcode="4124",
default_ontology="notexistingfantasyonto",
save_location=Path("bar"),
)
expected = re.escape(
"\nSome property and/or class type(s) used in the XML are unknown.\n"
"The ontologies for your project on the server are:\n"
" - testonto\n"
" - knora-api\n\n"
"---------------------------------------\n\n"
"The following resource(s) have an invalid resource type:\n\n"
" Resource Type: ':minimalResource'\n"
" Problem: 'Unknown ontology prefix'\n"
" Resource ID(s):\n"
" - the_only_resource\n\n"
"---------------------------------------\n\n"
)
with pytest.raises(UserError, match=expected):
do_xml_consistency_check(ontology_client, root)


if __name__ == "__main__":
pytest.main([__file__])
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import regex
from lxml import etree

from dsp_tools.commands.project.create.project_create import create_project
from dsp_tools.commands.xmlupload.upload_config import UploadConfig
from dsp_tools.commands.xmlupload.xmlupload import xmlupload
from dsp_tools.models.exceptions import BaseError
from dsp_tools.utils.shared import check_notna

Expand Down Expand Up @@ -45,30 +42,6 @@ def test_script(generated_xml_file: Path) -> None:
assert _sort_xml_by_id(xml_expected) == _sort_xml_by_id(xml_returned)


def test_upload(generated_xml_file: Path) -> None:
"""Create the project on the DSP server, and upload the created XML to the DSP server"""
success_on_creation = create_project(
project_file_as_path_or_parsed="src/dsp_tools/import_scripts/import_project.json",
server="http://0.0.0.0:3333",
user_mail="root@example.com",
password="test",
verbose=False,
dump=False,
)
assert success_on_creation

success_on_xmlupload = xmlupload(
input_file=generated_xml_file,
server="http://0.0.0.0:3333",
user="root@example.com",
password="test",
imgdir="src/dsp_tools/import_scripts/",
sipi="http://0.0.0.0:1024",
config=UploadConfig(),
)
assert success_on_xmlupload


def _sort_xml_by_id(xml: str) -> str:
"""Sort the elements in the XML by their ID"""
xml_tree = etree.fromstring(xml.encode("utf-8"))
Expand Down
14 changes: 0 additions & 14 deletions testdata/invalid-testdata/xml-data/inexistent-ontoname.xml

This file was deleted.

14 changes: 0 additions & 14 deletions testdata/invalid-testdata/xml-data/inexistent-shortcode.xml

This file was deleted.

0 comments on commit 0d5627f

Please sign in to comment.