|
| 1 | +import pytest |
| 2 | +from django.urls import reverse |
| 3 | +from model_mommy import mommy |
| 4 | + |
| 5 | +from pythonpro.django_assertions import dj_assert_contains, dj_assert_not_contains |
| 6 | +from pythonpro.modules.models import Chapter, Module, Section, Topic |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def module(db): |
| 11 | + return mommy.make(Module) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def section(module): |
| 16 | + return mommy.make(Section, module=module) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def chapter(section): |
| 21 | + return mommy.make(Chapter, section=section) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def topics(chapter): |
| 26 | + return [mommy.make(Topic, chapter=chapter, order=i) for i in range(2)] |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def resp(client_with_member, django_user_model, topics): |
| 31 | + next_topic = topics[1] |
| 32 | + return client_with_member.get( |
| 33 | + reverse('modules:topic_detail', |
| 34 | + kwargs={'module_slug': next_topic.module_slug(), 'topic_slug': next_topic.slug}), |
| 35 | + secure=True) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def resp_first_topic(client_with_member, topics, logged_user): |
| 40 | + first_topic = topics[0] |
| 41 | + yield client_with_member.get( |
| 42 | + reverse( |
| 43 | + 'modules:topic_detail', kwargs={'module_slug': first_topic.module_slug(), 'topic_slug': first_topic.slug} |
| 44 | + ), |
| 45 | + secure=True |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def test_topic_with_previous_topic(resp, topics): |
| 50 | + previous_topic, _ = topics |
| 51 | + dj_assert_contains(resp, previous_topic.get_absolute_url()) |
| 52 | + |
| 53 | + |
| 54 | +def test_first_topic(resp_first_topic): |
| 55 | + dj_assert_not_contains(resp_first_topic, 'Conteúdo Anterior') |
| 56 | + |
| 57 | + |
| 58 | +def test_first_topic_previous_chapter(section): |
| 59 | + """Assert previous Chapter as previous content for the first Topic""" |
| 60 | + previous_chapter, next_chapter = [mommy.make(Chapter, section=section, order=order) for order in range(2)] |
| 61 | + topic = mommy.make(Topic, chapter=next_chapter) |
| 62 | + assert previous_chapter == topic.previous_content() |
| 63 | + |
| 64 | + |
| 65 | +def test_topic_previous_section(module): |
| 66 | + """Assert previous Section as previous content for the first Topic""" |
| 67 | + previous_section, next_section = [mommy.make(Section, module=module, order=order) for order in range(2)] |
| 68 | + chapter = mommy.make(Chapter, section=next_section) |
| 69 | + topic = mommy.make(Topic, chapter=chapter) |
| 70 | + assert previous_section == topic.previous_content() |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.django_db |
| 74 | +def test_topic_previous_module(): |
| 75 | + """Assert previous Module as previous content for the first Topic""" |
| 76 | + previous_module, next_module = [mommy.make(Module, order=order) for order in range(2)] |
| 77 | + section = mommy.make(Section, module=next_module) |
| 78 | + chapter = mommy.make(Chapter, section=section) |
| 79 | + topic = mommy.make(Topic, chapter=chapter) |
| 80 | + assert previous_module == topic.previous_content() |
| 81 | + |
| 82 | + |
| 83 | +def test_previous_topic_first_none(chapter): |
| 84 | + """Assert None as previous content for the First Topic of First Chapter of First Section of First Module""" |
| 85 | + topic = mommy.make(Topic, chapter=chapter) |
| 86 | + assert topic.previous_content() is None |
| 87 | + |
| 88 | + |
| 89 | +def test_cache(chapter, mocker): |
| 90 | + """Assert cache is used when calling previous content multiple times""" |
| 91 | + topic = mommy.make(Topic, chapter=chapter) |
| 92 | + mocker.spy(topic, '_previous_content_query_set') |
| 93 | + for _ in range(3): |
| 94 | + topic.previous_content() |
| 95 | + assert topic._previous_content_query_set.call_count == 1 |
0 commit comments