Skip to content

Commit ee92239

Browse files
authored
Refactor GRAMPSHOME to fix singletons (#57)
* Refactor GRAMPSHOME to fix singletons * Remove GRAMPSHOME global * Remove top level global TEST_CLIENT, TEST_COUNT_OBJECTS
1 parent 41e8383 commit ee92239

File tree

3 files changed

+59
-34
lines changed

3 files changed

+59
-34
lines changed

tests/__init__.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
"""Unit test package for gramps_webapi."""
22

33
import gzip
4-
import importlib
54
import os
65
import shutil
76
import tempfile
87
import unittest
9-
from typing import Optional
108

11-
import gramps.cli.clidbman
12-
import gramps.cli.grampscli
9+
TEST_GRAMPSHOME = tempfile.mkdtemp()
10+
os.environ["GRAMPSHOME"] = TEST_GRAMPSHOME
11+
12+
from gramps.cli.clidbman import CLIDbManager
13+
from gramps.cli.grampscli import CLIManager
1314
from gramps.gen.config import get as getconfig
1415
from gramps.gen.config import set as setconfig
16+
from gramps.gen.const import USER_DIRLIST
1517
from gramps.gen.db.base import DbReadBase
1618
from gramps.gen.db.utils import import_as_dict, make_database
1719
from gramps.gen.dbstate import DbState
@@ -26,6 +28,8 @@ class ExampleDbBase:
2628

2729
def __init__(self) -> None:
2830
"""Initialize self."""
31+
for path in USER_DIRLIST:
32+
os.makedirs(path, exist_ok=True)
2933
_resources = ResourcePath()
3034
doc_dir = _resources.doc_dir
3135
self.path = os.path.join(doc_dir, "example", "gramps", "example.gramps")
@@ -43,8 +47,6 @@ def __init__(self) -> None:
4347
" found at {}".format(os.path.dirname(self.path_gz))
4448
)
4549
self.path = self._extract_to_tempfile()
46-
self.tmp_dbdir = None
47-
self.old_path = None
4850

4951
def _extract_to_tempfile(self) -> str:
5052
"""Extract the example DB to a temp file and return the path."""
@@ -88,25 +90,17 @@ class ExampleDbSQLite(ExampleDbBase, WebDbManager):
8890
app = create_app(db_manager = exampledb)
8991
```
9092
91-
Instantiation should occur during test fixture setup, at which
92-
point GRAMPSHOME should have been set and the temporary Gramps
93-
user directory structure created. The database will be imported
94-
under there, and when testing is done the test fixture teardown
95-
is responsible for cleanup.
93+
Instantiation should occur during test fixture setup, which should
94+
insure the temporary Gramps user directory structure was created.
95+
The database will be imported under there, and when testing is done
96+
the test fixture teardown is responsible for cleanup.
9697
"""
9798

9899
def __init__(self, name: str = None) -> None:
99100
"""Prepare and import the example DB."""
100-
importlib.reload(gramps.cli.clidbman)
101-
from gramps.cli.clidbman import CLIDbManager
102-
103-
importlib.reload(gramps.cli.grampscli)
104-
from gramps.cli.grampscli import CLIManager
105-
106101
ExampleDbBase.__init__(self)
107102
self.db_path = os.path.join(os.environ["GRAMPSHOME"], "gramps", "grampsdb")
108-
if not os.path.isdir(self.db_path):
109-
os.makedirs(self.db_path)
103+
os.makedirs(self.db_path, exist_ok=True)
110104
setconfig("database.path", self.db_path)
111105
dbstate = DbState()
112106
dbman = CLIDbManager(dbstate)

tests/test_endpoints/__init__.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
from pkg_resources import resource_filename
1212

1313
import gramps_webapi.app
14+
from gramps_webapi.app import create_app
1415
from gramps_webapi.const import ENV_CONFIG_FILE, TEST_EXAMPLE_GRAMPS_CONFIG
1516

16-
from .. import ExampleDbSQLite
17-
18-
global TEST_GRAMPSHOME, TEST_CLIENT, TEST_OBJECT_COUNTS
17+
from .. import TEST_GRAMPSHOME, ExampleDbSQLite
1918

2019

2120
def get_object_count(gramps_object):
@@ -30,18 +29,7 @@ def get_test_client():
3029

3130
def setUpModule():
3231
"""Test module setup."""
33-
global TEST_GRAMPSHOME, TEST_CLIENT, TEST_OBJECT_COUNTS
34-
35-
TEST_GRAMPSHOME = tempfile.mkdtemp()
36-
os.environ["GRAMPSHOME"] = TEST_GRAMPSHOME
37-
importlib.reload(gramps.gen.const)
38-
from gramps.gen.const import USER_DIRLIST
39-
40-
for path in USER_DIRLIST:
41-
if not os.path.isdir(path):
42-
os.makedirs(path)
43-
importlib.reload(gramps_webapi.app)
44-
from gramps_webapi.app import create_app
32+
global TEST_CLIENT, TEST_OBJECT_COUNTS
4533

4634
test_db = ExampleDbSQLite()
4735
with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_EXAMPLE_GRAMPS_CONFIG}):
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Tests for the /api/filters endpoints using example_gramps."""
2+
3+
import unittest
4+
5+
from jsonschema import RefResolver, validate
6+
7+
from gramps_webapi.const import GRAMPS_NAMESPACES
8+
9+
from . import API_SCHEMA, get_test_client
10+
11+
12+
class TestFilters(unittest.TestCase):
13+
"""Test cases for the /api/filters/{namespace} endpoint."""
14+
15+
@classmethod
16+
def setUpClass(cls):
17+
"""Test class setup."""
18+
cls.client = get_test_client()
19+
20+
def test_filters_endpoint_404(self):
21+
"""Test response for unsupported namespace."""
22+
# check 404 returned for non-existent namespace
23+
rv = self.client.get("/api/filters/nothing")
24+
assert rv.status_code == 404
25+
26+
def test_filters_endpoint_schema(self):
27+
"""Test all namespaces against the filters schema."""
28+
for namespace in GRAMPS_NAMESPACES:
29+
rv = self.client.get("/api/filters/" + namespace)
30+
# check no custom filters present yet
31+
assert rv.json["filters"] == []
32+
# check rules were returned
33+
assert "rules" in rv.json
34+
# check all rule records found conform to expected schema
35+
resolver = RefResolver(
36+
base_uri="", referrer=API_SCHEMA, store={"": API_SCHEMA}
37+
)
38+
for rule in rv.json["rules"]:
39+
validate(
40+
instance=rule,
41+
schema=API_SCHEMA["definitions"]["FilterRuleDescription"],
42+
resolver=resolver,
43+
)

0 commit comments

Comments
 (0)