<s> from datetime import datetime <EOL> from flask import current_app <EOL> from flask_login import login_user , current_user , logout_user <EOL> from flaskbb . forum . models import Category , Forum , Topic , Post , ForumsRead , TopicsRead , Report <EOL> from flaskbb . user . models import User <EOL> from flaskbb . utils . settings import flaskbb_config <EOL> def test_category_save ( database ) : <EOL> """<STR_LIT>""" <EOL> category = Category ( title = "<STR_LIT>" ) <EOL> category . save ( ) <EOL> assert category . title == "<STR_LIT>" <EOL> def test_category_delete ( category ) : <EOL> """<STR_LIT>""" <EOL> category . delete ( ) <EOL> category = Category . query . filter_by ( id = category . id ) . first ( ) <EOL> assert category is None <EOL> def test_category_delete_with_user ( topic ) : <EOL> """<STR_LIT>""" <EOL> user = topic . user <EOL> forum = topic . forum <EOL> category = topic . forum . category <EOL> assert user . post_count == <NUM_LIT:1> <EOL> assert forum . post_count == <NUM_LIT:1> <EOL> assert forum . topic_count == <NUM_LIT:1> <EOL> category . delete ( [ user ] ) <EOL> assert user . post_count == <NUM_LIT:0> <EOL> category = Category . query . filter_by ( id = category . id ) . first ( ) <EOL> topic = Topic . query . filter_by ( id = topic . id ) . first ( ) <EOL> assert category is None <EOL> assert topic is None <EOL> def test_category_delete_with_forum ( forum ) : <EOL> """<STR_LIT>""" <EOL> forum . category . delete ( ) <EOL> assert forum is not None <EOL> assert forum . category is not None <EOL> category = Category . query . filter_by ( id = forum . category . id ) . first ( ) <EOL> forum = Forum . query . filter_by ( id = forum . id ) . first ( ) <EOL> assert forum is None <EOL> assert category is None <EOL> def test_category_get_forums ( forum , user ) : <EOL> category = forum . category <EOL> with current_app . test_request_context ( ) : <EOL> login_user ( user ) <EOL> assert current_user . is_authenticated <EOL> cat , forums = Category . get_forums ( category . id , current_user ) <EOL> assert isinstance ( forums , list ) is True <EOL> assert forums == [ ( forum , None ) ] <EOL> assert cat == category <EOL> logout_user ( ) <EOL> assert not current_user . is_authenticated <EOL> cat , forums = Category . get_forums ( category . id , current_user ) <EOL> assert isinstance ( forums , list ) is True <EOL> assert forums == [ ( forum , None ) ] <EOL> assert cat == category <EOL> def test_category_get_all ( forum , user ) : <EOL> category = forum . category <EOL> with current_app . test_request_context ( ) : <EOL> login_user ( user ) <EOL> assert current_user . is_authenticated <EOL> categories = Category . get_all ( current_user ) <EOL> assert isinstance ( categories , list ) <EOL> assert isinstance ( categories [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , list ) <EOL> assert categories == [ ( category , [ ( forum , None ) ] ) ] <EOL> logout_user ( ) <EOL> assert not current_user . is_authenticated <EOL> categories = Category . get_all ( current_user ) <EOL> assert isinstance ( categories , list ) <EOL> assert isinstance ( categories [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , list ) <EOL> assert categories == [ ( category , [ ( forum , None ) ] ) ] <EOL> def test_forum_save ( category , moderator_user ) : <EOL> """<STR_LIT>""" <EOL> forum = Forum ( title = "<STR_LIT>" , category_id = category . id ) <EOL> forum . moderators = [ moderator_user ] <EOL> forum . save ( ) <EOL> assert forum . title == "<STR_LIT>" <EOL> assert forum . moderators == [ moderator_user ] <EOL> def test_forum_delete ( forum ) : <EOL> """<STR_LIT>""" <EOL> forum . delete ( ) <EOL> forum = Forum . query . filter_by ( id = forum . id ) . first ( ) <EOL> assert forum is None <EOL> def test_forum_delete_with_user_and_topic ( topic , user ) : <EOL> """<STR_LIT>""" <EOL> assert user . post_count == <NUM_LIT:1> <EOL> topic . forum . delete ( [ user ] ) <EOL> forum = Forum . query . filter_by ( id = topic . forum_id ) . first ( ) <EOL> assert forum is None <EOL> assert user . post_count == <NUM_LIT:0> <EOL> def test_forum_update_last_post ( topic , user ) : <EOL> """<STR_LIT>""" <EOL> post = Post ( content = "<STR_LIT>" ) <EOL> post . save ( topic = topic , user = user ) <EOL> assert topic . forum . last_post == post <EOL> post . delete ( ) <EOL> topic . forum . update_last_post ( ) <EOL> assert topic . forum . last_post == topic . first_post <EOL> def test_forum_update_read ( database , user , topic ) : <EOL> """<STR_LIT>""" <EOL> forumsread = ForumsRead . query . filter ( ForumsRead . user_id == user . id , <EOL> ForumsRead . forum_id == topic . forum_id ) . first ( ) <EOL> topicsread = TopicsRead . query . filter ( TopicsRead . user_id == user . id , <EOL> TopicsRead . topic_id == topic . id ) . first ( ) <EOL> forum = topic . forum <EOL> with current_app . test_request_context ( ) : <EOL> login_user ( user ) <EOL> assert not forum . update_read ( current_user , forumsread , topicsread ) <EOL> topicsread = TopicsRead ( ) <EOL> topicsread . user_id = user . id <EOL> topicsread . topic_id = topic . id <EOL> topicsread . forum_id = topic . forum_id <EOL> topicsread . last_read = datetime . utcnow ( ) <EOL> topicsread . save ( ) <EOL> assert forum . update_read ( current_user , forumsread , topicsread ) <EOL> forumsread = ForumsRead . query . filter ( ForumsRead . user_id == user . id , <EOL> ForumsRead . forum_id == topic . forum_id ) . first ( ) <EOL> assert not forum . update_read ( current_user , forumsread , topicsread ) <EOL> post = Post ( content = "<STR_LIT>" ) <EOL> post . save ( user = user , topic = topic ) <EOL> topicsread . last_read = datetime . utcnow ( ) <EOL> topicsread . save ( ) <EOL> assert forum . update_read ( current_user , forumsread , topicsread ) <EOL> logout_user ( ) <EOL> assert not forum . update_read ( current_user , forumsread , topicsread ) <EOL> def test_forum_update_read_two_topics ( database , user , topic , topic_moderator ) : <EOL> """<STR_LIT>""" <EOL> forumsread = ForumsRead . query . filter ( ForumsRead . user_id == user . id , <EOL> ForumsRead . forum_id == topic . forum_id ) . first ( ) <EOL> forum = topic . forum <EOL> with current_app . test_request_context ( ) : <EOL> login_user ( user ) <EOL> topicsread = TopicsRead ( ) <EOL> topicsread . user_id = user . id <EOL> topicsread . topic_id = topic . id <EOL> topicsread . forum_id = topic . forum_id <EOL> topicsread . last_read = datetime . utcnow ( ) <EOL> topicsread . save ( ) <EOL> assert not forum . update_read ( current_user , forumsread , topicsread ) <EOL> def test_forum_url ( forum ) : <EOL> assert forum . url == "<STR_LIT>" <EOL> def test_forum_slugify ( forum ) : <EOL> assert forum . slug == "<STR_LIT>" <EOL> def test_forum_get_forum ( forum , user ) : <EOL> with current_app . test_request_context ( ) : <EOL> login_user ( user ) <EOL> forum_with_forumsread = Forum . get_forum ( forum_id = forum . id , user = current_user ) <EOL> assert forum_with_forumsread == ( forum , None ) <EOL> logout_user ( ) <EOL> forum_with_forumsread = Forum . get_forum ( forum_id = forum . id , user = current_user ) <EOL> assert forum_with_forumsread == ( forum , None ) <EOL> def test_forum_get_topics ( topic , user ) : <EOL> forum = topic . forum <EOL> with current_app . test_request_context ( ) : <EOL> login_user ( user ) <EOL> topics = Forum . get_topics ( forum_id = forum . id , user = current_user ) <EOL> assert topics . items == [ ( topic , None ) ] <EOL> logout_user ( ) <EOL> topics = Forum . get_topics ( forum_id = forum . id , user = current_user ) <EOL> assert topics . items == [ ( topic , None ) ] <EOL> def test_topic_save ( forum , user ) : <EOL> """<STR_LIT>""" <EOL> post = Post ( content = "<STR_LIT>" ) <EOL> topic = Topic ( title = "<STR_LIT>" ) <EOL> assert forum . last_post_id is None <EOL> assert forum . post_count == <NUM_LIT:0> <EOL> assert forum . topic_count == <NUM_LIT:0> <EOL> topic . save ( forum = forum , post = post , user = user ) <EOL> assert topic . title == "<STR_LIT>" <EOL> topic . title = "<STR_LIT>" <EOL> topic . save ( ) <EOL> assert topic . title == "<STR_LIT>" <EOL> assert topic . first_post_id == post . id <EOL> assert topic . last_post_id == post . id <EOL> assert forum . last_post_id == post . id <EOL> assert forum . post_count == <NUM_LIT:1> <EOL> assert forum . topic_count == <NUM_LIT:1> <EOL> def test_topic_delete ( topic ) : <EOL> """<STR_LIT>""" <EOL> assert topic . user . post_count == <NUM_LIT:1> <EOL> assert topic . post_count == <NUM_LIT:1> <EOL> assert topic . forum . topic_count == <NUM_LIT:1> <EOL> assert topic . forum . post_count == <NUM_LIT:1> <EOL> topic . delete ( users = [ topic . user ] ) <EOL> forum = Forum . query . filter_by ( id = topic . forum_id ) . first ( ) <EOL> user = User . query . filter_by ( id = topic . user_id ) . first ( ) <EOL> topic = Topic . query . filter_by ( id = topic . id ) . first ( ) <EOL> assert topic is None <EOL> assert user . post_count == <NUM_LIT:0> <EOL> assert forum . topic_count == <NUM_LIT:0> <EOL> assert forum . post_count == <NUM_LIT:0> <EOL> assert forum . last_post_id is None <EOL> def test_topic_move ( topic ) : <EOL> """<STR_LIT>""" <EOL> forum_other = Forum ( title = "<STR_LIT>" , category_id = <NUM_LIT:1> ) <EOL> forum_other . save ( ) <EOL> forum_old = Forum . query . filter_by ( id = topic . forum_id ) . first ( ) <EOL> assert topic . move ( forum_other ) <EOL> assert forum_old . topics . all ( ) == [ ] <EOL> assert forum_old . last_post_id is None <EOL> assert forum_old . topic_count == <NUM_LIT:0> <EOL> assert forum_old . post_count == <NUM_LIT:0> <EOL> assert forum_other . last_post_id == topic . last_post_id <EOL> assert forum_other . topic_count == <NUM_LIT:1> <EOL> assert forum_other . post_count == <NUM_LIT:1> <EOL> def test_topic_move_same_forum ( topic ) : <EOL> """<STR_LIT>""" <EOL> assert not topic . move ( topic . forum ) <EOL> def test_topic_tracker_needs_update ( database , user , topic ) : <EOL> """<STR_LIT>""" <EOL> forumsread = ForumsRead . query . filter ( ForumsRead . user_id == user . id , <EOL> ForumsRead . forum_id == topic . forum_id ) . first ( ) <EOL> topicsread = TopicsRead . query . filter ( TopicsRead . user_id == user . id , <EOL> TopicsRead . topic_id == topic . id ) . first ( ) <EOL> with current_app . test_request_context ( ) : <EOL> assert topic . tracker_needs_update ( forumsread , topicsread ) <EOL> topicsread = TopicsRead ( ) <EOL> topicsread . user_id = user . id <EOL> topicsread . topic_id = topic . id <EOL> topicsread . forum_id = topic . forum_id <EOL> topicsread . last_read = datetime . utcnow ( ) <EOL> topicsread . save ( ) <EOL> forumsread = ForumsRead ( ) <EOL> forumsread . user_id = user . id <EOL> forumsread . forum_id = topic . forum_id <EOL> forumsread . last_read = datetime . utcnow ( ) <EOL> forumsread . save ( ) <EOL> assert not topic . tracker_needs_update ( forumsread , topicsread ) <EOL> post = Post ( content = "<STR_LIT>" ) <EOL> post . save ( topic = topic , user = user ) <EOL> assert topic . tracker_needs_update ( forumsread , topicsread ) <EOL> def test_topic_tracker_needs_update_cleared ( database , user , topic ) : <EOL> """<STR_LIT>""" <EOL> forumsread = ForumsRead . query . filter ( ForumsRead . user_id == user . id , <EOL> ForumsRead . forum_id == topic . forum_id ) . first ( ) <EOL> topicsread = TopicsRead . query . filter ( TopicsRead . user_id == user . id , <EOL> TopicsRead . topic_id == topic . id ) . first ( ) <EOL> with current_app . test_request_context ( ) : <EOL> assert topic . tracker_needs_update ( forumsread , topicsread ) <EOL> forumsread = ForumsRead ( ) <EOL> forumsread . user_id = user . id <EOL> forumsread . forum_id = topic . forum_id <EOL> forumsread . last_read = datetime . utcnow ( ) <EOL> forumsread . cleared = datetime . utcnow ( ) <EOL> forumsread . save ( ) <EOL> assert not topic . tracker_needs_update ( forumsread , topicsread ) <EOL> def test_topic_update_read ( database , user , topic ) : <EOL> """<STR_LIT>""" <EOL> forumsread = ForumsRead . query . filter ( ForumsRead . user_id == user . id , <EOL> ForumsRead . forum_id == topic . forum_id ) . first ( ) <EOL> with current_app . test_request_context ( ) : <EOL> login_user ( user ) <EOL> assert current_user . is_authenticated <EOL> assert topic . update_read ( current_user , topic . forum , forumsread ) <EOL> assert not topic . update_read ( current_user , topic . forum , forumsread ) <EOL> post = Post ( content = "<STR_LIT>" ) <EOL> post . save ( topic = topic , user = user ) <EOL> forumsread = ForumsRead . query . filter ( ForumsRead . user_id == user . id , <EOL> ForumsRead . forum_id == topic . forum_id ) . first ( ) <EOL> flaskbb_config [ "<STR_LIT>" ] = <NUM_LIT:0> <EOL> assert not topic . update_read ( current_user , topic . forum , forumsread ) <EOL> flaskbb_config [ "<STR_LIT>" ] = <NUM_LIT:1> <EOL> assert topic . update_read ( current_user , topic . forum , forumsread ) <EOL> logout_user ( ) <EOL> assert not current_user . is_authenticated <EOL> assert not topic . update_read ( current_user , topic . forum , forumsread ) <EOL> def test_topic_url ( topic ) : <EOL> assert topic . url == "<STR_LIT>" <EOL> def test_topic_slug ( topic ) : <EOL> assert topic . slug == "<STR_LIT>" <EOL> def test_post_save ( topic , user ) : <EOL> """<STR_LIT>""" <EOL> post = Post ( content = "<STR_LIT>" ) <EOL> post . save ( topic = topic , user = user ) <EOL> assert post . content == "<STR_LIT>" <EOL> post . content = "<STR_LIT>" <EOL> post . save ( ) <EOL> assert post . content == "<STR_LIT>" <EOL> assert topic . user . post_count == <NUM_LIT:2> <EOL> assert topic . post_count == <NUM_LIT:2> <EOL> assert topic . last_post == post <EOL> assert topic . forum . post_count == <NUM_LIT:2> <EOL> def test_post_delete ( topic ) : <EOL> """<STR_LIT>""" <EOL> post_middle = Post ( content = "<STR_LIT>" ) <EOL> post_middle . save ( topic = topic , user = topic . user ) <EOL> post_last = Post ( content = "<STR_LIT>" ) <EOL> post_last . save ( topic = topic , user = topic . user ) <EOL> assert topic . post_count == <NUM_LIT:3> <EOL> assert topic . forum . post_count == <NUM_LIT:3> <EOL> assert topic . user . post_count == <NUM_LIT:3> <EOL> post_middle . delete ( ) <EOL> assert topic . last_post == post_last <EOL> assert topic . forum . last_post == post_last <EOL> post_last . delete ( ) <EOL> assert topic . post_count == <NUM_LIT:1> <EOL> assert topic . forum . post_count == <NUM_LIT:1> <EOL> assert topic . user . post_count == <NUM_LIT:1> <EOL> assert topic . first_post_id == topic . last_post_id <EOL> assert topic . forum . last_post_id == topic . last_post_id <EOL> def test_report ( topic , user ) : <EOL> """<STR_LIT>""" <EOL> report = Report ( reason = "<STR_LIT>" ) <EOL> report . save ( user = user , post = topic . first_post ) <EOL> assert report . reason == "<STR_LIT>" <EOL> report . reason = "<STR_LIT>" <EOL> report . save ( ) <EOL> assert report . reason == "<STR_LIT>" <EOL> report . delete ( ) <EOL> report = Report . query . filter_by ( id = report . id ) . first ( ) <EOL> assert report is None <EOL> def test_forumsread ( topic , user ) : <EOL> """<STR_LIT>""" <EOL> forumsread = ForumsRead ( ) <EOL> forumsread . user_id = user . id <EOL> forumsread . forum_id = topic . forum_id <EOL> forumsread . last_read = datetime . utcnow ( ) <EOL> forumsread . save ( ) <EOL> assert forumsread is not None <EOL> forumsread . delete ( ) <EOL> forumsread = ForumsRead . query . filter_by ( forum_id = forumsread . forum_id ) . first ( ) <EOL> assert forumsread is None <EOL> def test_topicsread ( topic , user ) : <EOL> """<STR_LIT>""" <EOL> topicsread = TopicsRead ( ) <EOL> topicsread . user_id = user . id <EOL> topicsread . topic_id = topic . id <EOL> topicsread . forum_id = topic . forum_id <EOL> topicsread . last_read = datetime . utcnow ( ) <EOL> topicsread . save ( ) <EOL> assert topicsread is not None <EOL> topicsread . delete ( ) <EOL> topicsread = TopicsRead . query . filter_by ( topic_id = topicsread . topic_id ) . first ( ) <EOL> assert topicsread is None </s>
