Skip to content

Commit d3740c5

Browse files
authored
3.0.2 (#44) (#45)
* Do not include file name in creating db path * Add db tests * Use consts for env vars * Formatting * Only check if the path exists * Improve test * Format tests
1 parent 975ceac commit d3740c5

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
default_target: local
22

33
COMMIT_HASH := $(shell git log -1 --pretty=format:"%h"|tail -1)
4-
VERSION = 3.0.1
4+
VERSION = 3.0.2
55

66
local:
77
cd web; flutter build web;

swatch/app.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from playhouse.sqliteq import SqliteQueueDatabase
1313

1414
from swatch.config import SwatchConfig
15-
from swatch.const import CONST_CONFIG_FILE, CONST_DB_FILE
15+
from swatch.const import CONST_CONFIG_FILE, CONST_DB_FILE, ENV_CONFIG, ENV_DB
1616
from swatch.http import create_app
1717
from swatch.image import ImageProcessor
1818
from swatch.detection import AutoDetector, DetectionCleanup
@@ -52,7 +52,7 @@ def __init_config__(self) -> None:
5252
"""Init SwatchApp with saved config file."""
5353
logger.info("Importing SwatchApp Config")
5454

55-
config_file = os.environ.get("CONFIG_FILE", CONST_CONFIG_FILE)
55+
config_file: str = os.environ.get(ENV_CONFIG, CONST_CONFIG_FILE)
5656

5757
if os.path.isfile(config_file):
5858
logger.info("Verified SwatchApp Config")
@@ -62,11 +62,12 @@ def __init_config__(self) -> None:
6262

6363
def __init_db__(self):
6464
"""Init the Swatch database."""
65-
db_file = os.environ.get("DB_FILE", CONST_DB_FILE)
65+
db_file: str = os.environ.get(ENV_DB, CONST_DB_FILE)
66+
db_path = db_file[: db_file.rfind("/")]
6667

67-
if not os.path.exists(db_file):
68-
logger.debug("%s doesn't exist, creating...", db_file)
69-
os.makedirs(db_file)
68+
if not os.path.exists(db_path):
69+
logger.debug("%s doesn't exist, creating...", db_path)
70+
os.makedirs(db_path)
7071

7172
swatch_db = SqliteExtDatabase(db_file)
7273

swatch/const.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
"""Constants."""
22

3+
### File * Path Vars
4+
35
CONST_CONFIG_FILE = "/config/config.yaml"
46
CONST_DB_FILE = "/database/swatch.db"
57
CONST_MEDIA_DIR = "/media"
8+
9+
### Env Vars
10+
11+
ENV_CONFIG = "CONFIG_FILE"
12+
ENV_DB = "DB_FILE"

tests/test_app.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Tests for SwatchApp"""
2+
3+
import os
4+
import unittest
5+
6+
from swatch.app import SwatchApp
7+
8+
9+
class TestApp(unittest.TestCase):
10+
"""Testing the configuration is parsed correctly."""
11+
12+
def setUp(self) -> None:
13+
"""setup simple"""
14+
self.db_path = "/media/databases/"
15+
self.db_file = "/media/databases/swatch.db"
16+
17+
def test_db_created(self) -> None:
18+
"""Test that the db is created and path is as expected."""
19+
app = SwatchApp()
20+
assert not os.path.exists(self.db_file)
21+
os.environ["DB_FILE"] = self.db_file
22+
app.__init_db__()
23+
assert os.path.exists(self.db_path)
24+
assert os.path.isfile(self.db_file)

tests/test_image.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
"""Tests for SwatchConfig"""
1+
"""Tests for SwatchImage"""
22

3-
import datetime
43
import unittest
54

65
from swatch.config import SwatchConfig
76

87

9-
class TestConfig(unittest.TestCase):
8+
class TestImage(unittest.TestCase):
109
"""Testing the configuration is parsed correctly."""
1110

1211
def setUp(self) -> None:

0 commit comments

Comments
 (0)