-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add test save #686
Changes from 5 commits
e65ed68
8c15d60
6e08d63
c983e3b
0b06826
bd8ffe9
4f9269c
80e4673
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
# 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only thing |
||
# 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 |
There was a problem hiding this comment.
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
. Atoverwrite
fungerer må selvfølgelig testes eksplisitt som du gjør under.There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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...