|
| 1 | +import pytest |
| 2 | +from django.urls import reverse |
| 3 | +from model_mommy import mommy |
| 4 | + |
| 5 | +from pythonpro.dashboard.models import TopicInteraction |
| 6 | +from pythonpro.dashboard.templatetags.dashboard_tags import duration |
| 7 | +from pythonpro.django_assertions import dj_assert_contains |
| 8 | +from pythonpro.modules.models import Chapter, Module, Section, Topic |
| 9 | + |
| 10 | + |
| 11 | +# Tests user interacted with all topics |
| 12 | +@pytest.fixture |
| 13 | +def module(db): |
| 14 | + return mommy.make(Module) |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def sections(module): |
| 19 | + models = [] |
| 20 | + models.extend(mommy.make(Section, 2, module=module)) |
| 21 | + return models |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def chapters(sections): |
| 26 | + models = [] |
| 27 | + for s in sections: |
| 28 | + models.extend(mommy.make(Chapter, 2, section=s)) |
| 29 | + return models |
| 30 | + |
| 31 | + |
| 32 | +TOPIC_DURATION = 100 |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def topics(chapters): |
| 37 | + models = [] |
| 38 | + for c in chapters: |
| 39 | + models.extend(mommy.make(Topic, 2, chapter=c, duration=TOPIC_DURATION)) |
| 40 | + return models |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +def interactions(topics, logged_user): |
| 45 | + models = [] |
| 46 | + for t in topics: |
| 47 | + models.append( |
| 48 | + mommy.make( |
| 49 | + TopicInteraction, |
| 50 | + user=logged_user, |
| 51 | + topic=t, |
| 52 | + topic_duration=TOPIC_DURATION, |
| 53 | + total_watched_time=TOPIC_DURATION // 4, |
| 54 | + max_watched_time=TOPIC_DURATION // 2 |
| 55 | + ) |
| 56 | + ) |
| 57 | + return models |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture |
| 61 | +def resp(client_with_lead, interactions, module): |
| 62 | + return client_with_lead.get( |
| 63 | + reverse('dashboard:certificate', kwargs={'module_slug': module.slug}), |
| 64 | + secure=True |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +def test_status_code(resp, module): |
| 69 | + assert resp.status_code == 200 |
| 70 | + |
| 71 | + |
| 72 | +def test_module_title_is_present(resp, module): |
| 73 | + dj_assert_contains(resp, module.title) |
| 74 | + |
| 75 | + |
| 76 | +def test_user_name_is_present(resp, logged_user): |
| 77 | + dj_assert_contains(resp, logged_user.first_name) |
| 78 | + |
| 79 | + |
| 80 | +def test_user_progress(resp): |
| 81 | + dj_assert_contains(resp, 'concluiu 50%') |
| 82 | + |
| 83 | + |
| 84 | +def test_user_watched_time(resp, interactions): |
| 85 | + dj_assert_contains(resp, duration(len(interactions) * TOPIC_DURATION // 4)) |
| 86 | + |
| 87 | + |
| 88 | +def test_module_total_duration(resp, topics): |
| 89 | + dj_assert_contains(resp, duration(len(topics) * TOPIC_DURATION)) |
| 90 | + |
| 91 | + |
| 92 | +def test_finished_topics(resp): |
| 93 | + dj_assert_contains(resp, 'finalizado 0 aulas') |
| 94 | + |
| 95 | + |
| 96 | +def test_total_topis(resp, topics): |
| 97 | + dj_assert_contains(resp, f'total de {len(topics)}') |
0 commit comments