test_nb_connections.py 1.46 KB
Newer Older
1
2
3
4
5
from datetime import timedelta

from django.test import TestCase

from base_app.models import User
6
from stats_app.compute_stats import update_daily_connections
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)

32
    def test_update_daily_connections(self):
33
34
35
36
37
38
39
        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)
            )

40
        update_daily_connections()
41
42
43
44

        yesterday_daily_connections = DailyConnections.objects.get(date=yesterday)

        self.assertEqual(yesterday_daily_connections.nb_connections, 10)