Commit acf63fd4 authored by Florent Chehab's avatar Florent Chehab Committed by Maxime Emschwiller
Browse files

test(backend): add tests to check functions of stats computing

parent 22a43b4c
......@@ -67,7 +67,7 @@ test_back:
- make setup
script:
- cd backend
- pytest base_app/ backend_app/ --cov --cov-config .coveragerc --cov-report term --cov-report html
- pytest base_app/ backend_app/ stats_app/ --cov --cov-config .coveragerc --cov-report term --cov-report html
artifacts:
paths:
- backend/htmlcov/
......
......@@ -28,7 +28,7 @@ reformat_backend:
docker-compose exec backend sh -c "cd backend && black ."
test_backend:
docker-compose exec backend sh -c "cd backend && pytest --cov --cov-config .coveragerc --cov-report term base_app/ backend_app/"
docker-compose exec backend sh -c "cd backend && pytest --cov --cov-config .coveragerc --cov-report term base_app/ backend_app/ stats_app/"
test_frontend:
docker-compose exec frontend sh -c "cd frontend && yarn test"
......
from datetime import timedelta
from django.test import TestCase
from base_app.models import User
from stats_app.compute_stats import update_all_stats
from stats_app.models import DailyConnections
from stats_app.utils import get_daily_connections, get_today_as_datetime
class StatsConnectionsTest(TestCase):
def test_get_daily_connections(self):
today = get_today_as_datetime()
yesterday = today - timedelta(days=1)
for i in range(10):
User.objects.update_or_create(
username=f"{i}", defaults=dict(last_login=yesterday)
)
daily_connections = get_daily_connections()
self.assertEqual(daily_connections, 10)
two_days_ago = today - timedelta(days=2)
for i in range(10, 20):
User.objects.update_or_create(
username=f"{i}", defaults=dict(last_login=two_days_ago)
)
daily_connections = get_daily_connections()
self.assertEqual(daily_connections, 10)
def test_update_all_stats(self):
today = get_today_as_datetime()
yesterday = today - timedelta(days=1)
for i in range(10):
User.objects.update_or_create(
username=f"{i}", defaults=dict(last_login=yesterday)
)
update_all_stats()
yesterday_daily_connections = DailyConnections.objects.get(date=yesterday)
self.assertEqual(yesterday_daily_connections.nb_connections, 10)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment