66 lines
1.6 KiB
Python
66 lines
1.6 KiB
Python
import pathlib
|
|
import random
|
|
import string
|
|
import tempfile
|
|
|
|
from cry import database
|
|
|
|
|
|
def random_slug() -> str:
|
|
return "".join(
|
|
random.choices(
|
|
string.ascii_uppercase + string.ascii_lowercase + string.digits, k=8
|
|
)
|
|
)
|
|
|
|
|
|
def test_database_origin_path():
|
|
op = database.origin_path()
|
|
assert op is not None
|
|
|
|
|
|
def test_database_local_origin():
|
|
with tempfile.TemporaryDirectory() as op:
|
|
origin_file = pathlib.Path(op) / "origin"
|
|
assert not origin_file.exists()
|
|
|
|
origin = database.local_origin(origin_file)
|
|
|
|
assert origin_file.exists()
|
|
assert len(origin) > 0
|
|
|
|
|
|
def test_database_local_origin_repeatable():
|
|
with tempfile.TemporaryDirectory() as op:
|
|
origin_file = pathlib.Path(op) / "origin"
|
|
|
|
a = database.local_origin(origin_file)
|
|
b = database.local_origin(origin_file)
|
|
|
|
assert len(a) > 0
|
|
assert a == b
|
|
|
|
|
|
def test_database_origin_in_path():
|
|
slug = random_slug()
|
|
p = database.database_path(slug)
|
|
assert slug in str(p)
|
|
|
|
|
|
def test_database_schema():
|
|
db = database.Database(":memory:", random_slug())
|
|
db.ensure_database_schema()
|
|
|
|
c = db.db.execute("SELECT value FROM properties WHERE name = 'version'")
|
|
row = c.fetchone()
|
|
assert int(row[0]) == len(database.SCHEMA_STATEMENTS)
|
|
|
|
|
|
def test_database_prop_get_set():
|
|
db = database.Database(":memory:", random_slug())
|
|
db.ensure_database_schema()
|
|
|
|
assert db.get_property("foo") is None
|
|
val = random_slug()
|
|
db.set_property("foo", val)
|
|
assert db.get_property("foo") == val
|