|
| 1 | +from datetime import datetime |
| 2 | +from typing import List |
| 3 | + |
| 4 | +import pytest |
| 5 | +import pytz |
| 6 | +from django.urls import reverse |
| 7 | +from django.utils import timezone |
| 8 | +from freezegun import freeze_time |
| 9 | +from model_mommy import mommy |
| 10 | + |
| 11 | +from pythonpro.dashboard.models import TopicInteraction |
| 12 | +from pythonpro.dashboard.templatetags.dashboard_tags import duration |
| 13 | +from pythonpro.django_assertions import dj_assert_contains |
| 14 | +from pythonpro.modules.models import Topic |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def interactions(logged_user, topic): |
| 19 | + with freeze_time("2019-07-22 00:00:00"): |
| 20 | + first_interaction = mommy.make( |
| 21 | + TopicInteraction, |
| 22 | + user=logged_user, |
| 23 | + topic=topic, |
| 24 | + topic_duration=125, |
| 25 | + total_watched_time=125, |
| 26 | + max_watched_time=95 |
| 27 | + ) |
| 28 | + |
| 29 | + with freeze_time("2019-07-22 01:00:00"): |
| 30 | + second_interaction = mommy.make( |
| 31 | + TopicInteraction, |
| 32 | + user=logged_user, |
| 33 | + topic=topic, |
| 34 | + topic_duration=125, |
| 35 | + total_watched_time=34, |
| 36 | + max_watched_time=14 |
| 37 | + ) |
| 38 | + with freeze_time("2019-07-22 00:30:00"): |
| 39 | + third_interaction = mommy.make( |
| 40 | + TopicInteraction, |
| 41 | + user=logged_user, |
| 42 | + topic=topic, |
| 43 | + topic_duration=125, |
| 44 | + total_watched_time=64, |
| 45 | + max_watched_time=34 |
| 46 | + ) |
| 47 | + return [ |
| 48 | + first_interaction, |
| 49 | + second_interaction, |
| 50 | + third_interaction, |
| 51 | + ] |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def resp(client_with_lead, interactions): |
| 56 | + return client_with_lead.get( |
| 57 | + reverse('dashboard:home'), |
| 58 | + secure=True |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def test_status_code(resp): |
| 63 | + return resp.status_code == 200 |
| 64 | + |
| 65 | + |
| 66 | +def test_topic_title_is_present(resp, topic): |
| 67 | + dj_assert_contains(resp, topic.title) |
| 68 | + |
| 69 | + |
| 70 | +def test_table_instructions(resp, topic): |
| 71 | + dj_assert_contains(resp, 'Confira os dados consolidados por tópico') |
| 72 | + |
| 73 | + |
| 74 | +def test_topic_url(resp, topic: Topic): |
| 75 | + dj_assert_contains(resp, topic.get_absolute_url()) |
| 76 | + |
| 77 | + |
| 78 | +def test_module_table_row(resp, topic: Topic): |
| 79 | + module = topic.find_module() |
| 80 | + dj_assert_contains(resp, f'<td><a href="{module.get_absolute_url()}">{module.title}</a></td>') |
| 81 | + |
| 82 | + |
| 83 | +def test_max_creation(resp, interactions): |
| 84 | + tz = timezone.get_current_timezone() |
| 85 | + last_interaction_utc = datetime(2019, 7, 22, 1, 0, 0, tzinfo=pytz.utc) |
| 86 | + last_interaction_local = last_interaction_utc.astimezone(tz).strftime('%d/%m/%Y %H:%M:%S') |
| 87 | + dj_assert_contains(resp, last_interaction_local) |
| 88 | + |
| 89 | + |
| 90 | +def test_max_watched_time(resp, interactions: List[TopicInteraction]): |
| 91 | + max_watched_time = max(interaction.max_watched_time for interaction in interactions) |
| 92 | + max_watched_time_str = duration(max_watched_time) |
| 93 | + dj_assert_contains(resp, max_watched_time_str) |
| 94 | + |
| 95 | + |
| 96 | +def test_total_watched_time(resp, interactions: List[TopicInteraction]): |
| 97 | + total_watched_time = sum(interaction.total_watched_time for interaction in interactions) |
| 98 | + total_watched_time_str = duration(total_watched_time) |
| 99 | + dj_assert_contains(resp, total_watched_time_str) |
| 100 | + |
| 101 | + |
| 102 | +def test_interactions_count(resp, interactions: List[TopicInteraction]): |
| 103 | + interactions_count = len(interactions) |
| 104 | + dj_assert_contains(resp, f'<td>{interactions_count}</td>') |
| 105 | + |
| 106 | + |
| 107 | +@pytest.fixture |
| 108 | +def resp_without_interactions(client_with_lead): |
| 109 | + return client_with_lead.get( |
| 110 | + reverse('dashboard:home'), |
| 111 | + secure=True |
| 112 | + ) |
| 113 | + |
| 114 | + |
| 115 | +def test_not_existing_aggregation_msg_is_present(resp_without_interactions, topic): |
| 116 | + dj_assert_contains(resp_without_interactions, "Ainda não existem dados agregados") |
0 commit comments