Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/test_database_filepath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Tests for resolving the Things database path."""

import os
from unittest import mock

from things import database


def test_explicit_thingsdb_bypasses_directory_discovery():
configured_path = "/tmp/Things Database.thingsdatabase/main.sqlite"

with mock.patch.dict(os.environ, {"THINGSDB": configured_path}):
with mock.patch.object(database.glob, "iglob") as discovery:
result = database.resolve_default_filepath()

assert result == configured_path
discovery.assert_not_called()


def test_database_discovery_is_used_without_thingsdb():
discovered_path = "/tmp/ThingsData-test/Things Database.thingsdatabase/main.sqlite"

with mock.patch.dict(os.environ, {}, clear=True):
with mock.patch.object(
database.glob, "iglob", return_value=iter([discovered_path])
) as discovery:
result = database.resolve_default_filepath()

assert result == discovered_path
discovery.assert_called_once()
22 changes: 17 additions & 5 deletions things/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@
"/Things Database.thingsdatabase/main.sqlite"
)

try:
DEFAULT_FILEPATH = next(glob.iglob(os.path.expanduser(DEFAULT_FILEPATH_31616502)))
except StopIteration:
DEFAULT_FILEPATH = os.path.expanduser(DEFAULT_FILEPATH_31516502)

ENVIRONMENT_VARIABLE_WITH_FILEPATH = "THINGSDB"


def resolve_default_filepath():
"""Resolve the database path, preferring an explicit environment override."""
configured_filepath = os.getenv(ENVIRONMENT_VARIABLE_WITH_FILEPATH)
if configured_filepath:
return os.path.expanduser(configured_filepath)

try:
return next(
glob.iglob(os.path.expanduser(DEFAULT_FILEPATH_31616502))
)
except StopIteration:
return os.path.expanduser(DEFAULT_FILEPATH_31516502)


DEFAULT_FILEPATH = resolve_default_filepath()

# Translate app language to database language

START_TO_FILTER = {
Expand Down