2 Commits
0.0.4 ... 0.0.5

Author SHA1 Message Date
ddbf567a19 Replace concat() with || due to sqlite3 limitation
All checks were successful
Run Tests / run-tests (push) Successful in 59s
Build & Release / build-docker-image (push) Successful in 2m12s
Build & Release / deploy-to-production (push) Successful in 8s
2026-04-25 19:07:33 +02:00
cab4ca25ee Add database tests
Some checks failed
Run Tests / run-tests (push) Failing after 31s
2026-04-25 18:52:34 +02:00
5 changed files with 237 additions and 3 deletions

View File

@@ -29,7 +29,7 @@ def get_all_monthly_analytics(end_date:str = 'now'):
group by counter_id, strftime('%m', timestamp), strftime('%Y', timestamp) group by counter_id, strftime('%m', timestamp), strftime('%Y', timestamp)
) )
select select
concat(m.m,', ',m.y) as "month", m.m || ', ' || m.y as "month",
case case
when counter_id is null then json_object() when counter_id is null then json_object()
else json_group_object(name, count) else json_group_object(name, count)
@@ -69,7 +69,7 @@ def get_monthly_analytics(counter_id:int, end_date:str = 'now'):
group by strftime('%m', timestamp), strftime('%Y', timestamp) group by strftime('%m', timestamp), strftime('%Y', timestamp)
) )
SELECT SELECT
concat(m.m,', ',m.y) as "month", m.m || ', ' || m.y as "month",
coalesce(s.count, 0) as count coalesce(s.count, 0) as count
FROM months as m FROM months as m
LEFT JOIN stats as s on s.m = m.m and s.y = m.y LEFT JOIN stats as s on s.m = m.m and s.y = m.y

View File

@@ -41,7 +41,7 @@ python = ">=3.10,<4"
[tool.pytest.ini_options] [tool.pytest.ini_options]
log_cli = true log_cli = true
log_cli_level = "INFO" log_cli_level = "WARNING"
log_cli_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)" log_cli_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)"
log_cli_date_format="%Y-%m-%d %H:%M:%S" log_cli_date_format="%Y-%m-%d %H:%M:%S"
pythonpath = "./app" pythonpath = "./app"

View File

@@ -0,0 +1,39 @@
import queries.crud as crud
import queries.daily_stats as stats
from enums import CounterType
def test_create_counter():
crud.create_counter("Test", CounterType.SIMPLE, "020122")
counters = crud.get_counters()
assert len(counters) == 1
assert counters["id"][0] == 1
assert counters["name"][0] == "Test"
assert counters["type"][0] == CounterType.SIMPLE
assert counters["color"][0] == '020122'
def test_remove_counter():
crud.create_counter("Test", CounterType.SIMPLE, "020122")
assert len(crud.get_counters()) == 1
crud.remove_counter(1)
assert len(crud.get_counters()) == 0
def test_increment_counter():
crud.create_counter("Test", CounterType.SIMPLE, "020122")
assert len(crud.get_counters()) == 1
crud.increment_counter(1)
daily_stats = stats.get_daily_analytics(1)
assert daily_stats["count"][0] == 1
crud.increment_counter(1)
daily_stats = stats.get_daily_analytics(1)
assert daily_stats["count"][0] == 2

View File

@@ -0,0 +1,195 @@
import json
import queries.crud as crud
import queries.daily_stats as daily_stats
import queries.monthly_stats as monthly_stats
import queries.yearly_stats as yearly_stats
import queries.weekly_stats as weekly_stats
from enums import CounterType
from queries.connection import connection
from sqlalchemy.sql.expression import text
def test_all_daily_stats():
crud.create_counter("Test", CounterType.SIMPLE, "020122")
crud.create_counter("Test2", CounterType.SIMPLE, "020122")
with connection().session as session:
query = text("""
INSERT INTO entries (counter_id, "timestamp", increment)
VALUES
(1, date(), 1),
(1, date(date(), '-1 days'), 2),
(1, date(date(), '-3 days'), 3),
(2, date(), 2),
(2, date(date(), '-1 days'), 4),
(2, date(date(), '-3 days'), 6)
""")
session.execute(query)
session.commit()
stats = daily_stats.get_all_daily_analytics()
assert json.loads(stats[::-1]["counters"].iloc[0])["Test"] == 1
assert json.loads(stats[::-1]["counters"].iloc[0])["Test2"] == 2
assert json.loads(stats[::-1]["counters"].iloc[1])["Test"] == 2
assert json.loads(stats[::-1]["counters"].iloc[1])["Test2"] == 4
assert len(json.loads(stats[::-1]["counters"].iloc[2]).keys()) == 0
assert json.loads(stats[::-1]["counters"].iloc[3])["Test"] == 3
assert json.loads(stats[::-1]["counters"].iloc[3])["Test2"] == 6
def test_daily_stats():
crud.create_counter("Test", CounterType.SIMPLE, "020122")
with connection().session as session:
query = text("""
INSERT INTO entries (counter_id, "timestamp", increment)
VALUES
(1, date(), 1),
(1, date(date(), '-1 days'), 2),
(1, date(date(), '-3 days'), 3)
""")
session.execute(query)
session.commit()
stats = daily_stats.get_daily_analytics(1)
assert stats["count"][0] == 1
assert stats["count"][1] == 2
assert stats["count"][2] == 0
assert stats["count"][3] == 3
def test_all_monthly_stats():
crud.create_counter("Test", CounterType.SIMPLE, "020122")
crud.create_counter("Test2", CounterType.SIMPLE, "020122")
with connection().session as session:
query = text("""
INSERT INTO entries (counter_id, "timestamp", increment)
VALUES
(1, date(), 1),
(1, date(date(), '-1 month'), 2),
(1, date(date(), '-3 months'), 3),
(2, date(), 2),
(2, date(date(), '-2 months'), 4),
(2, date(date(), '-3 months'), 6)
""")
session.execute(query)
session.commit()
stats = monthly_stats.get_all_monthly_analytics()
assert json.loads(stats[::-1]["counters"].iloc[0])["Test"] == 1
assert json.loads(stats[::-1]["counters"].iloc[0])["Test2"] == 2
assert json.loads(stats[::-1]["counters"].iloc[1])["Test"] == 2
assert json.loads(stats[::-1]["counters"].iloc[2])["Test2"] == 4
assert json.loads(stats[::-1]["counters"].iloc[3])["Test"] == 3
assert json.loads(stats[::-1]["counters"].iloc[3])["Test2"] == 6
def test_monthly_stats():
crud.create_counter("Test", CounterType.SIMPLE, "020122")
with connection().session as session:
query = text("""
INSERT INTO entries (counter_id, "timestamp", increment)
VALUES
(1, date(), 1),
(1, date(date(), '-1 months'), 2),
(1, date(date(), '-3 months'), 3)
""")
session.execute(query)
session.commit()
stats = monthly_stats.get_monthly_analytics(1)
assert stats[::-1]["count"].iloc[0] == 1
assert stats[::-1]["count"].iloc[1] == 2
assert stats[::-1]["count"].iloc[2] == 0
assert stats[::-1]["count"].iloc[3] == 3
def test_all_yearly_stats():
crud.create_counter("Test", CounterType.SIMPLE, "020122")
crud.create_counter("Test2", CounterType.SIMPLE, "020122")
with connection().session as session:
query = text("""
INSERT INTO entries (counter_id, "timestamp", increment)
VALUES
(1, date(), 1),
(1, date(date(), '-1 year'), 2),
(1, date(date(), '-3 years'), 3),
(2, date(), 2),
(2, date(date(), '-2 years'), 4),
(2, date(date(), '-3 years'), 6)
""")
session.execute(query)
session.commit()
stats = yearly_stats.get_all_yearly_analytics()
assert json.loads(stats[::-1]["counters"].iloc[0])["Test"] == 1
assert json.loads(stats[::-1]["counters"].iloc[0])["Test2"] == 2
assert json.loads(stats[::-1]["counters"].iloc[1])["Test"] == 2
assert json.loads(stats[::-1]["counters"].iloc[2])["Test2"] == 4
assert json.loads(stats[::-1]["counters"].iloc[3])["Test"] == 3
assert json.loads(stats[::-1]["counters"].iloc[3])["Test2"] == 6
def test_yearly_stats():
crud.create_counter("Test", CounterType.SIMPLE, "020122")
with connection().session as session:
query = text("""
INSERT INTO entries (counter_id, "timestamp", increment)
VALUES
(1, date(), 1),
(1, date(date(), '-1 years'), 2),
(1, date(date(), '-3 years'), 3)
""")
session.execute(query)
session.commit()
stats = yearly_stats.get_yearly_analytics(1)
assert stats[::-1]["count"].iloc[0] == 1
assert stats[::-1]["count"].iloc[1] == 2
assert stats[::-1]["count"].iloc[2] == 0
assert stats[::-1]["count"].iloc[3] == 3
def test_all_weekly_stats():
crud.create_counter("Test", CounterType.SIMPLE, "020122")
crud.create_counter("Test2", CounterType.SIMPLE, "020122")
with connection().session as session:
query = text("""
INSERT INTO entries (counter_id, "timestamp", increment)
VALUES
(1, date(), 1),
(1, date(date(), '-7 days'), 2),
(1, date(date(), '-21 days'), 3),
(2, date(), 2),
(2, date(date(), '-14 days'), 4),
(2, date(date(), '-21 days'), 6)
""")
session.execute(query)
session.commit()
stats = weekly_stats.get_all_weekly_analytics()
assert json.loads(stats[::-1]["counters"].iloc[0])["Test"] == 1
assert json.loads(stats[::-1]["counters"].iloc[0])["Test2"] == 2
assert json.loads(stats[::-1]["counters"].iloc[1])["Test"] == 2
assert json.loads(stats[::-1]["counters"].iloc[2])["Test2"] == 4
assert json.loads(stats[::-1]["counters"].iloc[3])["Test"] == 3
assert json.loads(stats[::-1]["counters"].iloc[3])["Test2"] == 6
def test_weekly_stats():
crud.create_counter("Test", CounterType.SIMPLE, "020122")
with connection().session as session:
query = text("""
INSERT INTO entries (counter_id, "timestamp", increment)
VALUES
(1, date(), 1),
(1, date(date(), '-7 days'), 2),
(1, date(date(), '-21 days'), 3)
""")
session.execute(query)
session.commit()
stats = weekly_stats.get_weekly_analytics(1)
assert stats["count"][0] == 1
assert stats["count"][1] == 2
assert stats["count"][2] == 0
assert stats["count"][3] == 3