2026-04-25 18:52:34 +02:00
|
|
|
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("""
|
2026-04-30 14:16:57 +02:00
|
|
|
INSERT INTO entries (counter_id, user_id, "timestamp", increment)
|
2026-04-25 18:52:34 +02:00
|
|
|
VALUES
|
2026-05-01 17:41:42 +02:00
|
|
|
(1, 1, date('2026-06-15'), 1),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-1 days'), 2),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-3 days'), 3),
|
|
|
|
|
(2, 1, date('2026-06-15'), 2),
|
|
|
|
|
(2, 1, date(date('2026-06-15'), '-1 days'), 4),
|
|
|
|
|
(2, 1, date(date('2026-06-15'), '-3 days'), 6)
|
2026-04-25 18:52:34 +02:00
|
|
|
""")
|
|
|
|
|
session.execute(query)
|
|
|
|
|
session.commit()
|
|
|
|
|
|
2026-05-01 17:41:42 +02:00
|
|
|
stats = daily_stats.get_all_daily_analytics('2026-06-15')
|
2026-04-25 18:52:34 +02:00
|
|
|
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("""
|
2026-04-30 14:16:57 +02:00
|
|
|
INSERT INTO entries (counter_id, user_id, "timestamp", increment)
|
2026-04-25 18:52:34 +02:00
|
|
|
VALUES
|
2026-05-01 17:41:42 +02:00
|
|
|
(1, 1, date('2026-06-15'), 1),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-1 days'), 2),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-3 days'), 3)
|
2026-04-25 18:52:34 +02:00
|
|
|
""")
|
|
|
|
|
session.execute(query)
|
|
|
|
|
session.commit()
|
|
|
|
|
|
2026-05-01 17:41:42 +02:00
|
|
|
stats = daily_stats.get_daily_analytics(1, '2026-06-15')
|
2026-04-25 18:52:34 +02:00
|
|
|
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("""
|
2026-04-30 14:16:57 +02:00
|
|
|
INSERT INTO entries (counter_id, user_id, "timestamp", increment)
|
2026-04-25 18:52:34 +02:00
|
|
|
VALUES
|
2026-05-01 17:41:42 +02:00
|
|
|
(1, 1, date('2026-06-15'), 1),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), 'start of month', '-1 month'), 2),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), 'start of month', '-3 months'), 3),
|
|
|
|
|
(2, 1, date('2026-06-15'), 2),
|
|
|
|
|
(2, 1, date(date('2026-06-15'), 'start of month', '-2 months'), 4),
|
|
|
|
|
(2, 1, date(date('2026-06-15'), 'start of month', '-3 months'), 6)
|
2026-04-25 18:52:34 +02:00
|
|
|
""")
|
|
|
|
|
session.execute(query)
|
|
|
|
|
session.commit()
|
|
|
|
|
|
2026-05-01 17:41:42 +02:00
|
|
|
stats = monthly_stats.get_all_monthly_analytics('2026-06-15')
|
2026-04-25 18:52:34 +02:00
|
|
|
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("""
|
2026-04-30 14:16:57 +02:00
|
|
|
INSERT INTO entries (counter_id, user_id, "timestamp", increment)
|
2026-04-25 18:52:34 +02:00
|
|
|
VALUES
|
2026-05-01 17:41:42 +02:00
|
|
|
(1, 1, date('2026-06-15'), 1),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-1 months'), 2),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-3 months'), 3)
|
2026-04-25 18:52:34 +02:00
|
|
|
""")
|
|
|
|
|
session.execute(query)
|
|
|
|
|
session.commit()
|
|
|
|
|
|
2026-05-01 17:41:42 +02:00
|
|
|
stats = monthly_stats.get_monthly_analytics(1, '2026-06-15')
|
2026-04-25 18:52:34 +02:00
|
|
|
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("""
|
2026-04-30 14:16:57 +02:00
|
|
|
INSERT INTO entries (counter_id, user_id, "timestamp", increment)
|
2026-04-25 18:52:34 +02:00
|
|
|
VALUES
|
2026-05-01 17:41:42 +02:00
|
|
|
(1, 1, date('2026-06-15'), 1),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-1 year'), 2),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-3 years'), 3),
|
|
|
|
|
(2, 1, date('2026-06-15'), 2),
|
|
|
|
|
(2, 1, date(date('2026-06-15'), '-2 years'), 4),
|
|
|
|
|
(2, 1, date(date('2026-06-15'), '-3 years'), 6)
|
2026-04-25 18:52:34 +02:00
|
|
|
""")
|
|
|
|
|
session.execute(query)
|
|
|
|
|
session.commit()
|
|
|
|
|
|
2026-05-01 17:41:42 +02:00
|
|
|
stats = yearly_stats.get_all_yearly_analytics('2026-06-15')
|
2026-04-25 18:52:34 +02:00
|
|
|
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("""
|
2026-04-30 14:16:57 +02:00
|
|
|
INSERT INTO entries (counter_id, user_id, "timestamp", increment)
|
2026-04-25 18:52:34 +02:00
|
|
|
VALUES
|
2026-05-01 17:41:42 +02:00
|
|
|
(1, 1, date('2026-06-15'), 1),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-1 years'), 2),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-3 years'), 3)
|
2026-04-25 18:52:34 +02:00
|
|
|
""")
|
|
|
|
|
session.execute(query)
|
|
|
|
|
session.commit()
|
|
|
|
|
|
2026-05-01 17:41:42 +02:00
|
|
|
stats = yearly_stats.get_yearly_analytics(1, '2026-06-15')
|
2026-04-25 18:52:34 +02:00
|
|
|
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("""
|
2026-04-30 14:16:57 +02:00
|
|
|
INSERT INTO entries (counter_id, user_id, "timestamp", increment)
|
2026-04-25 18:52:34 +02:00
|
|
|
VALUES
|
2026-05-01 17:41:42 +02:00
|
|
|
(1, 1, date('2026-06-15'), 1),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-7 days'), 2),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-21 days'), 3),
|
|
|
|
|
(2, 1, date('2026-06-15'), 2),
|
|
|
|
|
(2, 1, date(date('2026-06-15'), '-14 days'), 4),
|
|
|
|
|
(2, 1, date(date('2026-06-15'), '-21 days'), 6)
|
2026-04-25 18:52:34 +02:00
|
|
|
""")
|
|
|
|
|
session.execute(query)
|
|
|
|
|
session.commit()
|
|
|
|
|
|
2026-05-01 17:41:42 +02:00
|
|
|
stats = weekly_stats.get_all_weekly_analytics('2026-06-15')
|
2026-04-25 18:52:34 +02:00
|
|
|
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("""
|
2026-04-30 14:16:57 +02:00
|
|
|
INSERT INTO entries (counter_id, user_id, "timestamp", increment)
|
2026-04-25 18:52:34 +02:00
|
|
|
VALUES
|
2026-05-01 17:41:42 +02:00
|
|
|
(1, 1, date('2026-06-15'), 1),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-7 days'), 2),
|
|
|
|
|
(1, 1, date(date('2026-06-15'), '-21 days'), 3)
|
2026-04-25 18:52:34 +02:00
|
|
|
""")
|
|
|
|
|
session.execute(query)
|
|
|
|
|
session.commit()
|
|
|
|
|
|
2026-05-01 17:41:42 +02:00
|
|
|
stats = weekly_stats.get_weekly_analytics(1, '2026-06-15')
|
2026-04-25 18:52:34 +02:00
|
|
|
assert stats["count"][0] == 1
|
|
|
|
|
assert stats["count"][1] == 2
|
|
|
|
|
assert stats["count"][2] == 0
|
|
|
|
|
assert stats["count"][3] == 3
|