|
1 | 1 |
|
2 | 2 | from datetime import datetime
|
3 | 3 | from freezegun import freeze_time
|
| 4 | +from dateutil.relativedelta import relativedelta |
| 5 | +import pytest |
| 6 | + |
| 7 | +from django.utils.timezone import utc |
4 | 8 |
|
5 | 9 | from courseware.models import StudentModule
|
6 | 10 |
|
|
11 | 15 | from figures.mau import (
|
12 | 16 | get_mau_from_student_modules,
|
13 | 17 | get_mau_from_site_course,
|
| 18 | + mau_1g_for_month_as_of_day, |
| 19 | + site_mau_1g_for_month_as_of_day, |
14 | 20 | store_mau_metrics,
|
15 | 21 | )
|
16 | 22 |
|
| 23 | +from tests.factories import StudentModuleFactory |
| 24 | + |
17 | 25 |
|
18 | 26 | def test_get_mau_from_site_course(sm_test_data):
|
19 | 27 | """Basic test for coverage with simple check
|
@@ -47,6 +55,74 @@ def test_get_mau_from_sm_for_site(sm_test_data):
|
47 | 55 | assert set(users) == set(sm_check)
|
48 | 56 |
|
49 | 57 |
|
| 58 | +@pytest.mark.django_db |
| 59 | +def test_mau_1g_for_month_as_of_day_first_day_next_month(db): |
| 60 | + """ |
| 61 | + Test getting live MAU 1G values from StudentModule for the given day |
| 62 | +
|
| 63 | + Quick-n-dirty data setup: |
| 64 | +
|
| 65 | + We want to make sure we get the right records when the query happens on the |
| 66 | + first day of the next month. So we do the following |
| 67 | +
|
| 68 | + * Add a StudentModule record for two months before |
| 69 | + * Add at least one StudentModule record for the month we want |
| 70 | + * Add at least one StudentModule record for after the month we want |
| 71 | +
|
| 72 | + This sets up the scenario that we run the daily pipeline to capture MAU |
| 73 | + "as of" yesterday (the last day of the previous month) to capture MAU for |
| 74 | + the previous month |
| 75 | + """ |
| 76 | + mock_today = datetime(year=2020, month=4, day=1).replace(tzinfo=utc) |
| 77 | + month_before = datetime(year=2020, month=2, day=2).replace(tzinfo=utc) |
| 78 | + in_dates = [datetime(year=2020, month=3, day=1).replace(tzinfo=utc), |
| 79 | + datetime(year=2020, month=3, day=15).replace(tzinfo=utc), |
| 80 | + datetime(year=2020, month=3, day=31).replace(tzinfo=utc)] |
| 81 | + date_for = mock_today.date() - relativedelta(days=1) |
| 82 | + |
| 83 | + # Create a student module in the month before, and in month after |
| 84 | + StudentModuleFactory(created=month_before, modified=month_before) |
| 85 | + StudentModuleFactory(created=mock_today, modified=mock_today) |
| 86 | + sm_in = [StudentModuleFactory(created=rec, |
| 87 | + modified=rec) for rec in in_dates] |
| 88 | + expected_user_ids = [obj.student_id for obj in sm_in] |
| 89 | + |
| 90 | + sm_queryset = StudentModule.objects.all() |
| 91 | + user_ids = mau_1g_for_month_as_of_day(sm_queryset=sm_queryset, |
| 92 | + date_for=date_for) |
| 93 | + assert set([rec['student__id'] for rec in user_ids]) == set(expected_user_ids) |
| 94 | + |
| 95 | + |
| 96 | +def test_site_mau_1g_for_month_as_of_day(monkeypatch): |
| 97 | + """Test our wrapper function, site_mau_1g_for_month_as_of_day |
| 98 | +
|
| 99 | + All we really care about is the call stack is what we expect with the args |
| 100 | + we expect |
| 101 | + """ |
| 102 | + expected_site = 'this is our site' |
| 103 | + expected_date_for = 'this is my date' |
| 104 | + expected_sm_queryset = 'this is my expected student module queryset' |
| 105 | + expected_user_id_qs = 'this is my expected user id queryset' |
| 106 | + |
| 107 | + def mock_get_student_modules_for_site(site): |
| 108 | + assert site == expected_site |
| 109 | + return expected_sm_queryset |
| 110 | + |
| 111 | + def mock_mau_1g_for_month_as_of_day(sm_queryset, date_for): |
| 112 | + assert date_for == expected_date_for |
| 113 | + assert sm_queryset == expected_sm_queryset |
| 114 | + return expected_user_id_qs |
| 115 | + |
| 116 | + monkeypatch.setattr('figures.mau.mau_1g_for_month_as_of_day', |
| 117 | + mock_mau_1g_for_month_as_of_day) |
| 118 | + monkeypatch.setattr('figures.mau.get_student_modules_for_site', |
| 119 | + mock_get_student_modules_for_site) |
| 120 | + |
| 121 | + qs = site_mau_1g_for_month_as_of_day(site=expected_site, |
| 122 | + date_for=expected_date_for) |
| 123 | + assert qs == expected_user_id_qs |
| 124 | + |
| 125 | + |
50 | 126 | def test_store_mau_metrics(monkeypatch, sm_test_data):
|
51 | 127 | """
|
52 | 128 | Basic minimal test
|
|
0 commit comments