diff --git a/pyproject.toml b/pyproject.toml index 579fc9a..c0b8727 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,7 +41,7 @@ python = ">=3.10,<4" [tool.pytest.ini_options] 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_date_format="%Y-%m-%d %H:%M:%S" pythonpath = "./app" diff --git a/tests/database/crud_db_test.py b/tests/database/crud_db_test.py new file mode 100644 index 0000000..4133a53 --- /dev/null +++ b/tests/database/crud_db_test.py @@ -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 + + diff --git a/tests/database/stats_db_test.py b/tests/database/stats_db_test.py new file mode 100644 index 0000000..405f01c --- /dev/null +++ b/tests/database/stats_db_test.py @@ -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 \ No newline at end of file diff --git a/tests/ui/counters_test.py b/tests/ui/counters_ui_test.py similarity index 100% rename from tests/ui/counters_test.py rename to tests/ui/counters_ui_test.py