Skip to content

Commit

Permalink
Centralized test set-up code into a pytest fixture.
Browse files Browse the repository at this point in the history
This deregisters each directory on teardown, to ensure that one module's
registered directory doesn't affect tests in other modules.
  • Loading branch information
LTLA committed Nov 25, 2024
1 parent 005e71d commit 60eb49b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 123 deletions.
26 changes: 18 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
"""
Dummy conftest.py for sewerrat.
import pytest
import sewerrat
import tempfile
import os

If you don't know what this is for, just leave it empty.
Read more about conftest.py under:
- https://docs.pytest.org/en/stable/fixture.html
- https://docs.pytest.org/en/stable/writing_plugins.html
"""
@pytest.fixture(scope="module")
def basic_config():
_, url = sewerrat.start_sewerrat()

# import pytest
mydir = tempfile.mkdtemp()
with open(os.path.join(mydir, "metadata.json"), "w") as handle:
handle.write('{ "first": "Aaron", "last": "Lun" }')

os.mkdir(os.path.join(mydir, "diet"))
with open(os.path.join(mydir, "diet", "metadata.json"), "w") as handle:
handle.write('{ "meal": "lunch", "ingredients": "water" }')

sewerrat.register(mydir, ["metadata.json"], url=url)
yield (url, mydir)
sewerrat.deregister(mydir, url=url)
24 changes: 3 additions & 21 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,23 @@ def test_basic():
with open(os.path.join(mydir, "diet", "metadata.json"), "w") as handle:
handle.write('{ "meal": "lunch", "ingredients": "water" }')

# Checking that registration works as expected.
_, url = sewerrat.start_sewerrat()
sewerrat.register(mydir, ["metadata.json"], url=url)

try:
sewerrat.register(mydir, ["metadata.json"], url=url)

res = sewerrat.query(url, "aaron")
assert len(res) == 1

res = sewerrat.query(url, "lun%")
assert len(res) == 2

res = sewerrat.query(url, "lun% AND aaron")
assert len(res) == 1

res = sewerrat.query(url, "meal:lun%")
assert len(res) == 1

res = sewerrat.query(url, path="diet/") # has 'diet/' in the path
assert len(res) == 1

res = sewerrat.query(url, after=time.time() - 60)
assert len(res) == 2

# Successfully deregistered:
sewerrat.deregister(mydir, url=url)

res = sewerrat.query(url, "aaron")
assert len(res) == 0

# We can also register a string.
sewerrat.register(mydir, "metadata.json", url=url)

res = sewerrat.query(url, "aaron")
assert len(res) == 1

finally:
# Okay, stop the service:
sewerrat.stop_sewerrat()
sewerrat.deregister(mydir, url=url)
29 changes: 8 additions & 21 deletions tests/test_list_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,14 @@
import time


def test_list_files():
mydir = tempfile.mkdtemp()
with open(os.path.join(mydir, "metadata.json"), "w") as handle:
handle.write('{ "first": "Aaron", "last": "Lun" }')
def test_list_files(basic_config):
url, mydir = basic_config

os.mkdir(os.path.join(mydir, "diet"))
with open(os.path.join(mydir, "diet", "metadata.json"), "w") as handle:
handle.write('{ "meal": "lunch", "ingredients": "water" }')
out = sewerrat.list_files(mydir, url=url)
assert sorted(out) == [ "diet/metadata.json", "metadata.json" ]

_, url = sewerrat.start_sewerrat()
out = sewerrat.list_files(mydir + "/diet", url=url)
assert sorted(out) == [ "metadata.json" ]

sewerrat.register(mydir, ["metadata.json"], url=url)
try:
out = sewerrat.list_files(mydir, url=url)
assert sorted(out) == [ "diet/metadata.json", "metadata.json" ]

out = sewerrat.list_files(mydir + "/diet", url=url)
assert sorted(out) == [ "metadata.json" ]

out = sewerrat.list_files(mydir, url=url, force_remote=True)
assert sorted(out) == [ "diet/metadata.json", "metadata.json" ]

finally:
sewerrat.deregister(mydir, url=url)
out = sewerrat.list_files(mydir, url=url, force_remote=True)
assert sorted(out) == [ "diet/metadata.json", "metadata.json" ]
31 changes: 22 additions & 9 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,30 @@
import pytest


def test_query_truncation(capfd):
mydir = tempfile.mkdtemp()
with open(os.path.join(mydir, "metadata.json"), "w") as handle:
handle.write('{ "first": "Aaron", "last": "Lun" }')
def test_query_basic(basic_config):
url, mydir = basic_config

os.mkdir(os.path.join(mydir, "diet"))
with open(os.path.join(mydir, "diet", "metadata.json"), "w") as handle:
handle.write('{ "meal": "lunch", "ingredients": "water" }')
res = sewerrat.query(url, "aaron")
assert len(res) == 1

_, url = sewerrat.start_sewerrat()
sewerrat.register(mydir, ["metadata.json"], url=url)
res = sewerrat.query(url, "lun%")
assert len(res) == 2

res = sewerrat.query(url, "lun% AND aaron")
assert len(res) == 1

res = sewerrat.query(url, "meal:lun%")
assert len(res) == 1

res = sewerrat.query(url, path="diet/") # has 'diet/' in the path
assert len(res) == 1

res = sewerrat.query(url, after=time.time() - 60)
assert len(res) == 2


def test_query_truncation(basic_config, capfd):
url, mydir = basic_config

res = sewerrat.query(url, "lun", number=0)
out, err = capfd.readouterr()
Expand Down
66 changes: 28 additions & 38 deletions tests/test_registered_directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,40 @@
import time


def test_list_registered_directories():
mydir = tempfile.mkdtemp()
with open(os.path.join(mydir, "metadata.json"), "w") as handle:
handle.write('{ "first": "Aaron", "last": "Lun" }')
def test_list_registered_directories(basic_config):
url, mydir = basic_config

_, url = sewerrat.start_sewerrat()
regged = sewerrat.list_registered_directories(url)
assert len(regged) > 0

sewerrat.register(mydir, ["metadata.json"], url=url)
try:
regged = sewerrat.list_registered_directories(url)
assert len(regged) > 0
found = False
for x in regged:
if x["path"] == mydir:
found = True
assert x["names"] == [ "metadata.json" ]
assert found

found = False
for x in regged:
if x["path"] == mydir:
found = True
assert x["names"] == [ "metadata.json" ]
assert found
# Filter by user.
filtered = sewerrat.list_registered_directories(url, user=True)
assert regged == filtered

# Filter by user.
filtered = sewerrat.list_registered_directories(url, user=True)
assert regged == filtered
filtered = sewerrat.list_registered_directories(url, user=regged[0]["user"] + "_asdasdasd")
assert len(filtered) == 0

filtered = sewerrat.list_registered_directories(url, user=regged[0]["user"] + "_asdasdasd")
assert len(filtered) == 0
# Filter by contains.
filtered = sewerrat.list_registered_directories(url, contains=os.path.join(mydir, "metadata.json"))
assert regged == filtered

# Filter by contains.
filtered = sewerrat.list_registered_directories(url, contains=os.path.join(mydir, "metadata.json"))
assert regged == filtered
filtered = sewerrat.list_registered_directories(url, contains=os.path.join(mydir + "-asdasd"))
assert len(filtered) == 0

filtered = sewerrat.list_registered_directories(url, contains=os.path.join(mydir + "-asdasd"))
assert len(filtered) == 0
# Filter by prefix.
filtered = sewerrat.list_registered_directories(url, prefix=os.path.dirname(mydir))
assert regged == filtered

# Filter by prefix.
filtered = sewerrat.list_registered_directories(url, prefix=os.path.dirname(mydir))
assert regged == filtered

filtered = sewerrat.list_registered_directories(url, prefix=os.path.dirname(mydir) + "-asdasdad")
assert len(filtered) == 0

# Multiple filters work.
filtered = sewerrat.list_registered_directories(url, prefix=os.path.dirname(mydir), user=True, contains=os.path.join(mydir, "metadata.json"))
assert regged == filtered

finally:
sewerrat.deregister(mydir, url=url)
filtered = sewerrat.list_registered_directories(url, prefix=os.path.dirname(mydir) + "-asdasdad")
assert len(filtered) == 0

# Multiple filters work.
filtered = sewerrat.list_registered_directories(url, prefix=os.path.dirname(mydir), user=True, contains=os.path.join(mydir, "metadata.json"))
assert regged == filtered
32 changes: 6 additions & 26 deletions tests/test_retrieve.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
import sewerrat
from sewerrat.retrieve_directory import _local_root
import pytest
import os
import tempfile
import json
import time


@pytest.fixture(scope="module")
def setup():
_, url = sewerrat.start_sewerrat()

mydir = tempfile.mkdtemp()
with open(os.path.join(mydir, "metadata.json"), "w") as handle:
handle.write('{ "first": "Aaron", "last": "Lun" }')

os.mkdir(os.path.join(mydir, "diet"))
with open(os.path.join(mydir, "diet", "metadata.json"), "w") as handle:
handle.write('{ "meal": "lunch", "ingredients": "water" }')

sewerrat.register(mydir, ["metadata.json"], url=url)
return mydir


def test_retrieve_file(setup):
mydir = setup
_, url = sewerrat.start_sewerrat()
def test_retrieve_file(basic_config):
url, mydir = basic_config

p = sewerrat.retrieve_file(mydir + "/metadata.json", url=url)
with open(p, "r") as f:
Expand Down Expand Up @@ -67,19 +49,17 @@ def test_retrieve_file(setup):
assert meta["first"] == "Aaron"


def test_retrieve_metadata(setup):
mydir = setup
_, url = sewerrat.start_sewerrat()
def test_retrieve_metadata(basic_config):
url, mydir = basic_config

fpath = mydir + "/diet/metadata.json"
meta = sewerrat.retrieve_metadata(fpath, url=url)
assert os.path.normpath(fpath) == os.path.normpath(meta["path"])
assert meta["metadata"]["meal"] == "lunch"


def test_retrieve_directory(setup):
mydir = setup
_, url = sewerrat.start_sewerrat()
def test_retrieve_directory(basic_config):
url, mydir = basic_config

dir = sewerrat.retrieve_directory(mydir, url=url)
with open(os.path.join(dir, "metadata.json"), "r") as f:
Expand Down

0 comments on commit 60eb49b

Please sign in to comment.