<s> from __future__ import unicode_literals <EOL> from django . core . exceptions import ValidationError <EOL> import pytest <EOL> from machina . core . db . models import get_model <EOL> from machina . test . factories import build_category_forum <EOL> from machina . test . factories import build_link_forum <EOL> from machina . test . factories import create_category_forum <EOL> from machina . test . factories import create_forum <EOL> from machina . test . factories import create_link_forum <EOL> from machina . test . factories import create_topic <EOL> from machina . test . factories import PostFactory <EOL> from machina . test . factories import UserFactory <EOL> Forum = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Post = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Topic = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ pytest . mark . django_db <EOL> class TestForum ( object ) : <EOL> @ pytest . fixture ( autouse = True ) <EOL> def setup ( self ) : <EOL> self . u1 = UserFactory . create ( ) <EOL> self . top_level_cat = create_category_forum ( ) <EOL> self . top_level_forum = create_forum ( ) <EOL> self . top_level_link = create_link_forum ( ) <EOL> def test_has_a_margin_level_two_times_greater_than_its_real_level ( self ) : <EOL> sub_level_forum = create_forum ( parent = self . top_level_forum ) <EOL> assert self . top_level_forum . margin_level == <NUM_LIT:0> <EOL> assert sub_level_forum . margin_level == <NUM_LIT:2> <EOL> def test_category_cannot_be_the_child_of_another_category ( self ) : <EOL> with pytest . raises ( ValidationError ) : <EOL> cat = build_category_forum ( parent = self . top_level_cat ) <EOL> cat . full_clean ( ) <EOL> def test_can_not_be_the_child_of_a_forum_link ( self ) : <EOL> for forum_type , _ in Forum . TYPE_CHOICES : <EOL> with pytest . raises ( ValidationError ) : <EOL> forum = build_link_forum ( parent = self . top_level_link ) <EOL> forum . full_clean ( ) <EOL> def test_must_have_a_link_in_case_of_a_link_forum ( self ) : <EOL> with pytest . raises ( ValidationError ) : <EOL> forum = Forum ( parent = self . top_level_forum , name = '<STR_LIT>' , type = Forum . FORUM_LINK ) <EOL> forum . full_clean ( ) <EOL> def test_saves_its_numbers_of_posts_and_topics ( self ) : <EOL> topic = create_topic ( forum = self . top_level_forum , poster = self . u1 ) <EOL> PostFactory . create ( topic = topic , poster = self . u1 ) <EOL> PostFactory . create ( topic = topic , poster = self . u1 ) <EOL> assert self . top_level_forum . posts_count == topic . posts . filter ( approved = True ) . count ( ) <EOL> assert self . top_level_forum . topics_count == self . top_level_forum . topics . count ( ) <EOL> topic2 = create_topic ( forum = self . top_level_forum , poster = self . u1 , approved = False ) <EOL> PostFactory . create ( topic = topic2 , poster = self . u1 , approved = False ) <EOL> assert self . top_level_forum . posts_count == topic . posts . filter ( approved = True ) . count ( ) + topic2 . posts . filter ( approved = True ) . count ( ) <EOL> assert self . top_level_forum . topics_count == self . top_level_forum . topics . filter ( approved = True ) . count ( ) <EOL> def test_can_indicate_its_appartenance_to_a_forum_type ( self ) : <EOL> assert self . top_level_cat . is_category <EOL> assert self . top_level_forum . is_forum <EOL> assert self . top_level_link . is_link <EOL> def test_can_trigger_the_update_of_the_counters_of_a_new_parent ( self ) : <EOL> topic = create_topic ( forum = self . top_level_forum , poster = self . u1 ) <EOL> PostFactory . create ( topic = topic , poster = self . u1 ) <EOL> PostFactory . create ( topic = topic , poster = self . u1 ) <EOL> self . top_level_forum . parent = self . top_level_cat <EOL> self . top_level_forum . save ( ) <EOL> assert self . top_level_cat . posts_count == self . top_level_forum . posts_count <EOL> assert self . top_level_cat . topics_count == self . top_level_forum . topics_count <EOL> def test_can_trigger_the_update_of_the_counters_of_a_previous_parent ( self ) : <EOL> sub_level_forum = create_forum ( parent = self . top_level_forum ) <EOL> topic = create_topic ( forum = sub_level_forum , poster = self . u1 ) <EOL> PostFactory . create ( topic = topic , poster = self . u1 ) <EOL> PostFactory . create ( topic = topic , poster = self . u1 ) <EOL> sub_level_forum . parent = self . top_level_cat <EOL> sub_level_forum . save ( ) <EOL> self . top_level_forum = Forum . objects . get ( pk = self . top_level_forum . pk ) <EOL> assert self . top_level_forum . posts_count == <NUM_LIT:0> <EOL> assert self . top_level_forum . topics_count == <NUM_LIT:0> <EOL> def test_stores_its_last_post_datetime ( self ) : <EOL> sub_level_forum = create_forum ( parent = self . top_level_forum ) <EOL> topic = create_topic ( forum = sub_level_forum , poster = self . u1 ) <EOL> PostFactory . create ( topic = topic , poster = self . u1 ) <EOL> p2 = PostFactory . create ( topic = topic , poster = self . u1 ) <EOL> sub_level_forum . refresh_from_db ( ) <EOL> assert sub_level_forum . last_post_on == p2 . created <EOL> def test_can_reset_last_post_datetime_if_all_topics_have_been_deleted ( self ) : <EOL> sub_level_forum = create_forum ( parent = self . top_level_forum ) <EOL> topic = create_topic ( forum = sub_level_forum , poster = self . u1 ) <EOL> PostFactory . create ( topic = topic , poster = self . u1 ) <EOL> topic . delete ( ) <EOL> sub_level_forum . refresh_from_db ( ) <EOL> assert sub_level_forum . last_post_on is None </s>
