Coverage for src/counter_os/infrastructure/persistence/sqlite_counter_repository.py: 91%
57 statements
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-29 14:36 -0600
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-29 14:36 -0600
1import sqlalchemy as sa
2import sqlalchemy.orm
4from counter_os.domain.entities.counter import Counter
5from counter_os.domain.repositories.counter_repository import CounterRepository
6from counter_os.domain.value_objects.count import Count
7from counter_os.infrastructure.persistence.counter_repository_type import (
8 CounterRepositoryType,
9)
12class SqliteCounterRepository(CounterRepository):
13 _session_factory: sa.orm.sessionmaker
14 _relative_file_path: str
16 def __init__(self, relative_file_path: str):
17 validate_relative_file_path_arg(relative_file_path)
18 self._relative_file_path = relative_file_path
19 sa_engine_config_url = "sqlite+pysqlite:///" + relative_file_path
20 engine = sa.create_engine(sa_engine_config_url)
21 Base.metadata.create_all(engine)
22 self._session_factory = sa.orm.sessionmaker(bind=engine)
23 with self._session_factory() as session:
24 record = session.get(SqliteCounter, 1)
25 if record is None:
26 session.add(SqliteCounter(id=1, count=0))
27 session.commit()
29 def save(self, counter: Counter) -> None:
30 with self._session_factory() as session:
31 record = session.get(SqliteCounter, 1)
32 if record is not None: 32 ↛ 35line 32 didn't jump to line 35 because the condition on line 32 was always true
33 record.count = counter.count.value
34 else:
35 session.add(SqliteCounter(id=1, count=counter.count.value))
36 session.commit()
38 def get(self) -> Counter:
39 with self._session_factory() as session:
40 record = session.get(SqliteCounter, 1)
41 if record is not None: 41 ↛ 44line 41 didn't jump to line 44 because the condition on line 41 was always true
42 return Counter(Count(record.count))
44 new_record = SqliteCounter(id=1, count=0)
45 session.add(new_record)
46 session.commit()
47 return Counter(Count(new_record.count))
49 def repository_type(self) -> str:
50 return CounterRepositoryType.SQLITE_LOCAL
52 def repository_location(self) -> str:
53 return self._relative_file_path
56def validate_relative_file_path_arg(relative_file_path: str):
57 if not isinstance(relative_file_path, str):
58 raise TypeError("`relative_file_path` arg must be of type `str`")
60 if relative_file_path == "":
61 raise ValueError("`relative_file_path` arg cannot be empty string")
63 invalid_chars: list[str] = ["\\", ":", "*", "?", '"', "<", ">", "|"]
64 for invalid_char in invalid_chars:
65 if invalid_char in relative_file_path:
66 raise ValueError(
67 "`relative_file_path` arg cannot contain invalid character: "
68 + invalid_char
69 )
71 if " " in relative_file_path:
72 raise ValueError("`relative_file_path` arg cannot contain spaces")
74 if relative_file_path.startswith("/"):
75 raise ValueError("`relative_file_path` arg cannot begin with `/`")
78class Base(sa.orm.DeclarativeBase):
79 pass
82class SqliteCounter(Base):
83 """SQL Alchemy ORM class for "counters" table"""
85 __tablename__ = "counters"
87 id: sa.orm.Mapped[int] = sa.orm.mapped_column(primary_key=True)
88 count: sa.orm.Mapped[int]