Skip to content

Commit

Permalink
fix(xmlupload): saving the csv file if there are more than 50 problem…
Browse files Browse the repository at this point in the history
…s with the ontology names (DEV-3112) (#685)
  • Loading branch information
Nora-Olivia-Ammann committed Dec 14, 2023
1 parent 71205f8 commit a7d2e59
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 29 deletions.
@@ -1,4 +1,5 @@
# sourcery skip: use-fstring-for-concatenation
from datetime import datetime
from pathlib import Path

import regex
Expand Down Expand Up @@ -29,9 +30,7 @@ def do_xml_consistency_check(onto_client: OntologyClient, root: etree._Element)
UserError: if there are any invalid properties or classes
"""
onto_check_info = OntoCheckInformation(
default_ontology_prefix=onto_client.default_ontology,
onto_lookup=onto_client.get_all_ontologies_from_server(),
save_location=onto_client.save_location,
default_ontology_prefix=onto_client.default_ontology, onto_lookup=onto_client.get_all_ontologies_from_server()
)
classes, properties = _get_all_classes_and_properties(root)
_find_problems_in_classes_and_properties(classes, properties, onto_check_info)
Expand All @@ -49,11 +48,11 @@ def _find_problems_in_classes_and_properties(
)
msg, df = problems.execute_problem_protocol()
if df is not None:
ex_name = "XML_syntax_errors.xlsx"
df.to_excel(excel_writer=Path(onto_check_info.save_location, ex_name), sheet_name=" ", index=False)
csv_file = f"XML_syntax_errors_{datetime.now().strftime('%Y-%m-%d_%H%M%S')}.csv"
df.to_csv(path_or_buf=Path(Path.cwd(), csv_file), index=False)
msg += (
"\n\n---------------------------------------\n\n"
f"\nAn excel: '{ex_name}' was saved at '{onto_check_info.save_location}' listing the problems."
f"\nAll the problems are listed in the file: '{Path.cwd()}/{csv_file}'"
)
raise UserError(msg)

Expand Down
Expand Up @@ -2,7 +2,6 @@

import itertools
from dataclasses import dataclass, field
from pathlib import Path

import pandas as pd

Expand All @@ -26,7 +25,6 @@ class OntoCheckInformation:

default_ontology_prefix: str
onto_lookup: dict[str, OntoInfo]
save_location: Path


@dataclass(frozen=True)
Expand Down Expand Up @@ -58,7 +56,10 @@ def execute_problem_protocol(self) -> tuple[str, pd.DataFrame | None]:
prop_msg = self._compose_problem_string_for_props()
if prop_msg:
msg += prop_msg
if len(self.classes) + len(self.properties) > maximum_prints:
if (
self._calculate_num_resources(self.classes) + self._calculate_num_resources(self.properties)
> maximum_prints
):
df = self._get_problems_as_df()
return msg, df
return msg, None
Expand Down
@@ -1,5 +1,3 @@
from pathlib import Path

import pytest
from lxml import etree
from pytest_unordered import unordered
Expand Down Expand Up @@ -90,7 +88,6 @@ def test_get_all_property_names_and_resource_ids_one_resouce() -> None:
def test_find_problems_in_classes_and_properties_all_good() -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=["classA", "classB"], properties=["propA", "propB"]),
"knora-api": OntoInfo(classes=["knoraClassA"], properties=["knoraPropA"]),
Expand All @@ -104,7 +101,6 @@ def test_find_problems_in_classes_and_properties_all_good() -> None:
def test_find_problems_in_classes_and_properties_problem() -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=["classA", "classB"], properties=["propA", "propB"]),
"knora-api": OntoInfo(classes=["knoraClassA"], properties=["knoraPropA"]),
Expand All @@ -120,7 +116,6 @@ class TestDiagnoseClass:
def test_no_knora_prefix(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=["classA", "classB"], properties=[]),
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
Expand All @@ -131,7 +126,6 @@ def test_no_knora_prefix(self) -> None:
def test_knora_prefix(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=["classA", "classB"], properties=[]),
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
Expand All @@ -142,7 +136,6 @@ def test_knora_prefix(self) -> None:
def test_no_default_prefix(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=["classA", "classB"], properties=[]),
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
Expand All @@ -153,7 +146,6 @@ def test_no_default_prefix(self) -> None:
def test_default_prefix(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=["classA", "classB"], properties=[]),
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
Expand All @@ -164,7 +156,6 @@ def test_default_prefix(self) -> None:
def test_unknown_class(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=["classA", "classB"], properties=[]),
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
Expand All @@ -175,7 +166,6 @@ def test_unknown_class(self) -> None:
def test_unknown_prefix(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=["classA", "classB"], properties=[]),
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
Expand All @@ -186,7 +176,6 @@ def test_unknown_prefix(self) -> None:
def test_diagnose_all_classes_no_problems(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=["classA", "classB"], properties=[]),
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
Expand All @@ -198,7 +187,6 @@ def test_diagnose_all_classes_no_problems(self) -> None:
def test_diagnose_all_classes_problems(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=["classA", "classB"], properties=[]),
"knora-api": OntoInfo(classes=["knoraClassA"], properties=[]),
Expand All @@ -212,7 +200,6 @@ class TestDiagnoseProperties:
def test_no_knora_prefix(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=[], properties=["propA", "propB"]),
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
Expand All @@ -223,7 +210,6 @@ def test_no_knora_prefix(self) -> None:
def test_knora_prefix(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=[], properties=["propA", "propB"]),
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
Expand All @@ -234,7 +220,6 @@ def test_knora_prefix(self) -> None:
def test_no_default_prefix(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=[], properties=["propA", "propB"]),
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
Expand All @@ -245,7 +230,6 @@ def test_no_default_prefix(self) -> None:
def test_default_prefix(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=[], properties=["propA", "propB"]),
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
Expand All @@ -256,7 +240,6 @@ def test_default_prefix(self) -> None:
def test_unknown_prefix(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=[], properties=["propA", "propB"]),
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
Expand All @@ -267,7 +250,6 @@ def test_unknown_prefix(self) -> None:
def test_unknown_property(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=[], properties=["propA", "propB"]),
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
Expand All @@ -278,7 +260,6 @@ def test_unknown_property(self) -> None:
def test_diagnose_all_properties_problems(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=[], properties=["propA", "propB"]),
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
Expand All @@ -291,7 +272,6 @@ def test_diagnose_all_properties_problems(self) -> None:
def test_diagnose_all_properties_no_problems(self) -> None:
onto_check_info = OntoCheckInformation(
default_ontology_prefix="test",
save_location=Path(""),
onto_lookup={
"test": OntoInfo(classes=[], properties=["propA", "propB"]),
"knora-api": OntoInfo(classes=[], properties=["knoraPropA"]),
Expand Down

0 comments on commit a7d2e59

Please sign in to comment.