Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test save #686

Merged
merged 8 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ def get_version(self, as_iri=False) -> str:
"No versionIRI or versionInfo " f"in Ontology {self.base_iri!r}"
)
_, _, version_info = tokens[0]
return version_info.strip('"').strip("'")
return version_info.split("^^")[0].strip('"')

def set_version(self, version=None, version_iri=None):
"""Assign version to ontology by asigning owl:versionIRI.
Expand Down
99 changes: 99 additions & 0 deletions tests/test_save.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pathlib import Path


def test_save(tmpdir: "Path", testonto: "Ontology") -> None:
import pytest

from ontopy import get_ontology
from ontopy.ontology import EMMOntoPyException
from pathlib import Path

# Save ontology in a different location
testonto.save(tmpdir / "testonto_saved.ttl")
Copy link
Collaborator

@jesper-friis jesper-friis Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stedet for å skrive til en temporær mappe som automatisk slettes når testen har kjørt, er det enklere å debugge hvis man skriver til en output/ undermappe under tests/. Hvis du legger inn en .gitignore fil i den mappen som inneholder en linje med "*", vill den mappen bli en del av git-repoet men alle filene som blir generert i den vill bli ignorert av git. Men de vill fortsatt være enkelt tilgjengelige for debugging.

Eneste ulempen med en slik løsning er at output/ ikke er garantert å være tom ved start av testen. Dvs. man må sette overwrite=True. At overwrite fungerer må selvfølgelig testes eksplisitt som du gjør under.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to make use of temporary directories that are cleaned up after use and that tests are run in a clean directory every time if that is the intended purpose. Overwrite=False is the default, I don't want to have overwrite=True unless I want to specifically test that. I have added a suggestion for writing to a local directory in debug mode (setting debug=True). Then we meet half way :)

Copy link
Collaborator

@jesper-friis jesper-friis Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you wish... but I really don't see the point of such complexity. Why introducing different test modes? The tests are not part of the distributed production code, but ment to be simple and helpful for ensuring code quality. Easy debugging is part of that.

All extra complexity added to the tests just make the code harder to maintain - and will sooner or later require that we have to add tests for the tests...

# check that the file is in tmpdir
assert (tmpdir / "testonto_saved.ttl").exists()
testonto.save(format="rdfxml")

# provide a format and filename
testonto.save(tmpdir / "testonto_saved.owl", format="rdfxml")
assert (tmpdir / "testonto_saved.owl").exists()

# Provide only filename
# Note that when not giving a filename and not giving a directory
# the file will be saved in the current directory
testonto.save(format="rdfxml")
assert Path(testonto.name + ".rdfxml").exists()
# check if testonto_saved.owl and testonto.rdfxml are identical files
with open(tmpdir / "testonto_saved.owl") as f:
owlfile = f.read()
with open(Path(testonto.name + ".rdfxml")) as f:
rdfxmlfile = f.read()
assert owlfile == rdfxmlfile
# Delete the file from the current directory
Path(testonto.name + ".rdfxml").unlink()

# Provide format and directory
testonto.save(format="rdfxml", dir=tmpdir)
assert (tmpdir / str(testonto.name + ".rdfxml")).exists()

# Provide directory that does not exist, but add mkdir=True
testonto.save(format="owl", dir=tmpdir / "subdir", mkdir=True)
assert (tmpdir / "subdir" / (testonto.name + ".owl")).exists()

# Check that file is overwritten only wityh overwrite=True, and
# not by default (overwrite=False).
# To do this we
# 1. check that the file testonto.owl is the same as
# testonto_saved.owl
# 2. save testonto to testonto.owl again
# 3. check that testonto.owl is not the same as testonto_saved.owl
# 4. save testonto to testonto.owl again, but with overwrite=True
# 5. check that testonto.owl is the same as testonto_saved.owl
# NB! this is not currently working, issue #685

# 1.
with open(tmpdir / "testonto_saved.owl") as f:
owlfile = f.read()
with open(tmpdir / "testonto.rdfxml") as f:
owlfile2 = f.read()
assert owlfile == owlfile2
# 2.
testonto.save(format="rdfxml", dir=tmpdir)
# 3.
with open(tmpdir / "testonto_saved.owl") as f:
owlfile = f.read()
with open(tmpdir / "testonto.rdfxml") as f:
owlfile2 = f.read()
# assert owlfile != owlfile2 # to be uncommented when issue #685 is fixed
Comment on lines +76 to +82
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hvorfor skal owlfile og owlfile2 være ulike. Har du endret på testonto?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I have saved testonto.rdfxml again. Since overwrite=False is the default it should append to it, thus make the two files different. Stated it even more explicitly in the comment aboe (step2)

Copy link
Collaborator

@jesper-friis jesper-friis Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing overwrite=True does is to remove the destination before writing to it. Without overwrite, the behaviour is defined by owlready2 for rdfxml and rdflib for turtle. I haven't checked their documentation, but I don't expect that any of them will duplicate a triple when writing twice to the file.

# 4.
testonto.save(format="rdfxml", dir=tmpdir, overwrite=True)
# 5.
with open(tmpdir / "testonto_saved.owl") as f:
owlfile = f.read()
with open(tmpdir / "testonto.rdfxml") as f:
owlfile2 = f.read()
assert owlfile == owlfile2

# Test that the ontology is saved recursively when deisred
testonto.save(
format="ttl", dir=tmpdir / "recursively", mkdir=True, recursive=True
)
assert (tmpdir / "recursively" / "testonto.ttl").exists()
# Recursive save is not working . Issue #687
# assert (tmpdir / "recursively" / "models.ttl").exists()

# print name of files in tmpdir
# print("Files in tmpdir:")
# for file in (tmpdir / "recursively").iterdir():
# print(file)
francescalb marked this conversation as resolved.
Show resolved Hide resolved

# squash merge during save

# Write catalogfile

# append_catalog

# catalog_filename