def test_topic_save(forum, user): """Test the save topic method with creating and editing a topic.""" post = Post(content="Test Content") topic = Topic(title="Test Title") assert forum.last_post_id is None assert forum.post_count == 0 assert forum.topic_count == 0 topic.save(forum=forum, post=post, user=user) assert topic.title == "Test Title" topic.title = "Test Edit Title" topic.save() assert topic.title == "Test Edit Title" # The first post in the topic is also the last post assert topic.first_post_id == post.id assert topic.last_post_id == post.id assert forum.last_post_id == post.id assert forum.post_count == 1 assert forum.topic_count == 1
def insert_mass_data(topics=100, posts=100): """Creates 100 topics in the first forum and each topic has 100 posts. Returns ``True`` if the topics and posts were successfully created. :param topics: The amount of topics in the forum. :param posts: The number of posts in each topic. """ user1 = User.query.filter_by(id=1).first() user2 = User.query.filter_by(id=2).first() forum = Forum.query.filter_by(id=1).first() if not user1 or user2 or forum: raise "Please make sure that there are at least 2 users and 1 forum." # create 1000 topics for i in range(1, topics + 1): # create a topic topic = Topic() post = Post() topic.title = "Test Title %s" % i post.content = "Test Content" topic.save(post=post, user=user1, forum=forum) # create 100 posts in each topic for j in range(1, posts): post = Post() post.content = "Test Post" post.save(user=user2, topic=topic)
def create_test_data(): """ Creates 5 users, 2 categories and 2 forums in each category. It also opens a new topic topic in each forum with a post. """ create_default_groups() create_default_settings() # create 5 users for u in range(1, 6): username = "******" % u email = "*****@*****.**" % u user = User(username=username, password="******", email=email) user.primary_group_id = u user.save() user1 = User.query.filter_by(id=1).first() user2 = User.query.filter_by(id=2).first() # create 2 categories for i in range(1, 3): category_title = "Test Category %s" % i category = Category(title=category_title, description="Test Description") category.save() # create 2 forums in each category for j in range(1, 3): if i == 2: j += 2 forum_title = "Test Forum %s %s" % (j, i) forum = Forum(title=forum_title, description="Test Description", category_id=i) forum.save() # create a topic topic = Topic() post = Post() topic.title = "Test Title %s" % j post.content = "Test Content" topic.save(post=post, user=user1, forum=forum) # create a second post in the forum post = Post() post.content = "Test Post" post.save(user=user2, topic=topic)
def create_test_data(): """Creates 5 users, 2 categories and 2 forums in each category. It also creates a new topic topic in each forum with a post. """ create_default_groups() create_default_settings() # create 5 users for u in range(1, 6): username = "******" % u email = "*****@*****.**" % u user = User(username=username, password="******", email=email) user.primary_group_id = u user.save() user1 = User.query.filter_by(id=1).first() user2 = User.query.filter_by(id=2).first() # create 2 categories for i in range(1, 3): category_title = "Test Category %s" % i category = Category(title=category_title, description="Test Description") category.save() # create 2 forums in each category for j in range(1, 3): if i == 2: j += 2 forum_title = "Test Forum %s %s" % (j, i) forum = Forum(title=forum_title, description="Test Description", category_id=i) forum.save() # create a topic topic = Topic() post = Post() topic.title = "Test Title %s" % j post.content = "Test Content" topic.save(post=post, user=user1, forum=forum) # create a second post in the forum post = Post() post.content = "Test Post" post.save(user=user2, topic=topic)
def create_test_data(): create_default_groups() # create 5 users for u in range(1, 6): username = "******" % u email = "*****@*****.**" % u user = User(username=username, password="******", email=email) user.primary_group_id = u user.save() # create a category category = Forum(is_category=True, title="Test Category", description="Test Description") category.save() # create 2 forums in the category for i in range(1, 3): forum_title = "Test Forum %s " % i forum = Forum(title=forum_title, description="Test Description", parent_id=category.id) forum.save() # Create a subforum subforum_title = "Test Subforum %s " % i subforum = Forum(title=subforum_title, description="Test Description", parent_id=forum.id) subforum.save() user = User.query.filter_by(id=1).first() # create 1 topic in each forum for i in range(2, 6): # Forum ids are not sequential because categories. forum = Forum.query.filter_by(id=i).first() topic = Topic() post = Post() topic.title = "Test Title %s" % (i-1) post.content = "Test Content" topic.save(user=user, forum=forum, post=post)
def insert_mass_data(topics=100, posts=100): """Creates a few topics in the first forum and each topic has a few posts. WARNING: This might take very long! Returns the count of created topics and posts. :param topics: The amount of topics in the forum. :param posts: The number of posts in each topic. """ user1 = User.query.filter_by(id=1).first() user2 = User.query.filter_by(id=2).first() forum = Forum.query.filter_by(id=1).first() created_posts = 0 created_topics = 0 if not (user1 or user2 or forum): return False # create 1000 topics for i in range(1, topics + 1): # create a topic topic = Topic() post = Post() topic.title = "Test Title %s" % i post.content = "Test Content" topic.save(post=post, user=user1, forum=forum) created_topics += 1 # create 100 posts in each topic for j in range(1, posts + 1): post = Post() post.content = "Test Post" post.save(user=user2, topic=topic) created_posts += 1 return created_topics, created_posts
def insert_mass_data(): """ Creates 100 topics in the first forum and each topic has 100 posts. """ user1 = User.query.filter_by(id=1).first() user2 = User.query.filter_by(id=2).first() forum = Forum.query.filter_by(id=1).first() # create 1000 topics for i in range(1, 101): # create a topic topic = Topic() post = Post() topic.title = "Test Title %s" % i post.content = "Test Content" topic.save(post=post, user=user1, forum=forum) # create 100 posts in each topic for j in range(1, 100): post = Post() post.content = "Test Post" post.save(user=user2, topic=topic)
def createall(): """ Creates the database with some example content. """ # Just for testing purposes dbfile = os.path.join(Config._basedir, "flaskbb.sqlite") if os.path.exists(dbfile): os.remove(dbfile) db.create_all() groups = OrderedDict(( ('Administrator', { 'description': 'The Administrator Group', 'admin': True, 'super_mod': False, 'mod': False, 'banned': False, 'guest': False, 'editpost': True, 'deletepost': True, 'deletetopic': True, 'posttopic': True, 'postreply': True, 'viewtopic': True, 'viewprofile': True }), ('Super Moderator', { 'description': 'The Super Moderator Group', 'admin': False, 'super_mod': True, 'mod': False, 'banned': False, 'guest': False, 'editpost': True, 'deletepost': True, 'deletetopic': True, 'posttopic': True, 'postreply': True, 'viewtopic': True, 'viewprofiles': True }), ('Moderator', { 'description': 'The Moderator Group', 'admin': False, 'super_mod': False, 'mod': True, 'banned': False, 'guest': False, 'editpost': True, 'deletepost': True, 'deletetopic': True, 'posttopic': True, 'postreply': True, 'viewtopic': True, 'viewprofile': True }), ('Member', { 'description': 'The Member Group', 'admin': False, 'super_mod': False, 'mod': False, 'banned': False, 'guest': False, 'editpost': True, 'deletepost': False, 'deletetopic': False, 'posttopic': True, 'postreply': True, 'viewtopic': True, 'viewprofile': True }), ('Banned', { 'description': 'The Banned Group', 'admin': False, 'super_mod': False, 'mod': False, 'banned': True, 'guest': False, 'editpost': False, 'deletepost': False, 'deletetopic': False, 'posttopic': False, 'postreply': False, 'viewtopic': False, 'viewprofile': False }), ('Guest', { 'description': 'The Guest Group', 'admin': False, 'super_mod': False, 'mod': False, 'banned': False, 'guest': True, 'editpost': False, 'deletepost': False, 'deletetopic': False, 'posttopic': False, 'postreply': False, 'viewtopic': False, 'viewprofile': False }) )) # create 5 groups for key, value in groups.items(): group = Group(name=key) for k, v in value.items(): setattr(group, k, v) db.session.add(group) db.session.commit() # create 5 users groups = Group.query.all() for u in range(1, 6): username = "******" % u email = "*****@*****.**" % u user = User(username=username, password="******", email=email) user.secondary_groups.append(groups[u-1]) user.primary_group_id = u db.session.add(user) db.session.commit() # create 2 categories for i in range(1, 3): category_title = "Test Category %s" % i category = Forum(is_category=True, title=category_title, description="Test Description") db.session.add(category) # create 2 forums in each category for j in range(1, 3): if i == 2: j += 2 forum_title = "Test Forum %s %s" % (j, i) forum = Forum(title=forum_title, description="Test Description", parent_id=i) db.session.add(forum) db.session.commit() # create 1 topic in each forum for k in [2, 3, 5, 6]: # Forum ids are not sequential because categories. topic = Topic() first_post = Post() topic.title = "Test Title %s" % k topic.user_id = 1 topic.forum_id = k db.session.add(topic) db.session.commit() first_post.content = "Test Content" first_post.user_id = 1 first_post.topic_id = topic.id db.session.add(first_post) db.session.commit() # Invalidate relevant caches topic.invalidate_cache() topic.forum.invalidate_cache() # create 2 additional posts for each topic for m in range(1, 3): post = Post(content="Test Post", user_id=2, topic_id=k) db.session.add(post) db.session.commit() # Update the post count post.user.invalidate_cache() topic.invalidate_cache() topic.forum.invalidate_cache() db.session.commit() db.session.commit()
def create_test_data(users=5, categories=2, forums=2, topics=1, posts=1): """Creates 5 users, 2 categories and 2 forums in each category. It also creates a new topic topic in each forum with a post. Returns the amount of created users, categories, forums, topics and posts as a dict. :param users: The number of users. :param categories: The number of categories. :param forums: The number of forums which are created in each category. :param topics: The number of topics which are created in each forum. :param posts: The number of posts which are created in each topic. """ create_default_groups() create_default_settings() data_created = { 'users': 0, 'categories': 0, 'forums': 0, 'topics': 0, 'posts': 0 } # create 5 users for u in range(1, users + 1): username = "******" % u email = "*****@*****.**" % u user = User(username=username, password="******", email=email) user.primary_group_id = u user.save() data_created['users'] += 1 user1 = User.query.filter_by(id=1).first() user2 = User.query.filter_by(id=2).first() # lets send them a few private messages for i in range(1, 3): # TODO pass # create 2 categories for i in range(1, categories + 1): category_title = "Test Category %s" % i category = Category(title=category_title, description="Test Description") category.save() data_created['categories'] += 1 # create 2 forums in each category for j in range(1, forums + 1): if i == 2: j += 2 forum_title = "Test Forum %s %s" % (j, i) forum = Forum(title=forum_title, description="Test Description", category_id=i) forum.save() data_created['forums'] += 1 for t in range(1, topics + 1): # create a topic topic = Topic() post = Post() topic.title = "Test Title %s" % j post.content = "Test Content" topic.save(post=post, user=user1, forum=forum) data_created['topics'] += 1 for p in range(1, posts + 1): # create a second post in the forum post = Post() post.content = "Test Post" post.save(user=user2, topic=topic) data_created['posts'] += 1 return data_created
def create_test_data(users=5, categories=2, forums=2, topics=1, posts=1): """Creates 5 users, 2 categories and 2 forums in each category. It also creates a new topic topic in each forum with a post. Returns the amount of created users, categories, forums, topics and posts as a dict. :param users: The number of users. :param categories: The number of categories. :param forums: The number of forums which are created in each category. :param topics: The number of topics which are created in each forum. :param posts: The number of posts which are created in each topic. """ create_default_groups() create_default_settings() data_created = {'users': 0, 'categories': 0, 'forums': 0, 'topics': 0, 'posts': 0} # create 5 users for u in range(1, users + 1): username = "******" % u email = "*****@*****.**" % u user = User(username=username, password="******", email=email) user.primary_group_id = u user.activated = True user.save() data_created['users'] += 1 user1 = User.query.filter_by(id=1).first() user2 = User.query.filter_by(id=2).first() # lets send them a few private messages for i in range(1, 3): # TODO pass # create 2 categories for i in range(1, categories + 1): category_title = "Test Category %s" % i category = Category(title=category_title, description="Test Description") category.save() data_created['categories'] += 1 # create 2 forums in each category for j in range(1, forums + 1): if i == 2: j += 2 forum_title = "Test Forum %s %s" % (j, i) forum = Forum(title=forum_title, description="Test Description", category_id=i) forum.save() data_created['forums'] += 1 for t in range(1, topics + 1): # create a topic topic = Topic() post = Post() topic.title = "Test Title %s" % j post.content = "Test Content" topic.save(post=post, user=user1, forum=forum) data_created['topics'] += 1 for p in range(1, posts + 1): # create a second post in the forum post = Post() post.content = "Test Post" post.save(user=user2, topic=topic) data_created['posts'] += 1 return data_created