195 lines
7.9 KiB
Python
195 lines
7.9 KiB
Python
|
|
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
|