Exemplo n.º 1
0
    def setUp(self):
        tags = {}
        for name in ['Python', 'Kaggle', 'Django']:
            tags[name] = TagFactory(name=name)

        PostFactory(tags=(tags['Python'], ))
        PostFactory(tags=(tags['Python'], tags['Django']))
        PostFactory(tags=(tags['Kaggle'], ))
Exemplo n.º 2
0
 def test_returns_correct_list_of_posts(self):
     post1 = PostFactory(status='published')
     post2 = PostFactory()
     post3 = PostFactory(status='published')
     response = self.client.get('/blog/')
     self.assertContains(response, post1.title)
     self.assertNotContains(response, post2.title)
     self.assertContains(response, post3.title)
Exemplo n.º 3
0
    def test_search_for_post_contains_keyword(self):
        PostFactory(title='今日', content='Today', description='0')
        PostFactory(title='Today', content='今日', description='0')
        PostFactory(title='明日', content='tomorrow', description='-1')
        PostFactory(title='tomorrow', content='明日', description='-1')

        response = self.client.get('/?keyword=今日')
        self.assertEqual(response.context['posts'].count(), 2)
        self.assertEqual(Post.objects.all().count(), 4)
Exemplo n.º 4
0
    def test_can_get_user_posts(self):
        user1 = UserFactory()
        user2 = UserFactory()
        post1 = PostFactory(author=user1)
        PostFactory(author=user2)
        post3 = PostFactory(author=user1)
        user1_posts = user1.blog_posts.all()

        self.assertEqual(len(user1_posts), 2)
        self.assertIn(post1, user1_posts)
        self.assertIn(post3, user1_posts)
Exemplo n.º 5
0
    def test_similar_posts(self):
        PostFactory(status='published')
        post2 = PostFactory(status='published')
        post3 = PostFactory(status='published')
        post4 = PostFactory(status='published')

        post4.tags.add('tag0', 'tag1', 'tag2')
        post3.tags.add('tag0', 'tag1')
        post2.tags.add('tag0')

        response = self.client.get(post4.get_absolute_url())

        self.assertEqual(list(response.context['similar_posts']),
                         [post3, post2])
Exemplo n.º 6
0
    def setUp(self):
        self.post0 = PostFactory(status='published')
        self.post1 = PostFactory(status='published')
        self.post2 = PostFactory()
        self.post3 = PostFactory(status='published')
        self.post4 = PostFactory(status='published')

        self.post3.tags.add('test_tag_0')
        self.post4.tags.add('test_tag_0')
        self.post4.tags.add('test_tag_1')

        self.browser = webdriver.Firefox()
        self.browser.get(self.live_server_url +
                         '/blog/')  # Go to the main page
Exemplo n.º 7
0
    def test_blog_tags__show_latest_posts(self):
        """Custom show_latest_posts tag works fine"""
        self.post5 = PostFactory(status='published')
        self.post6 = PostFactory(status='published')
        self.browser.get(self.live_server_url + '/blog/')

        # User sees five latest posts
        expected_posts = [
            self.post6, self.post5, self.post4, self.post3, self.post1
        ]
        actual_post_elements = self.browser.find_elements_by_css_selector(
            '#latest-posts a')
        actual_post_titles = [post.text for post in actual_post_elements]
        self.assertEqual(actual_post_titles,
                         [post.title for post in expected_posts])
Exemplo n.º 8
0
    def handle(self, *args, **options):
        # ====================================================================================
        # Users & Groups
        # ====================================================================================
        # create authors group
        authors_group, _ = Group.objects.get_or_create(name='authors')
        add_permission_to_add_model(Post, authors_group)

        # create 2 authors
        alberti, _ = User.objects.get_or_create(username='******', first_name='Rafael', last_name='Alberti')
        if authors_group not in alberti.groups.all():
            alberti.groups.add(authors_group)
        douglas, _ = User.objects.get_or_create(username='******', first_name='Douglas', last_name='Adams')
        if authors_group not in douglas.groups.all():
            douglas.groups.add(authors_group)
        authors = [alberti, douglas]

        # ====================================================================================
        # Posts
        # ====================================================================================
        if Post.objects.count() > 10:
            logger.info('There are {} posts already in DB, not generating more...'.format(Post.objects.count()))
        else:
            for _ in range(42):
                PostFactory(author=authors[int(round(random()))])
Exemplo n.º 9
0
 def test_if_tag_is_provided_filter_post_by_it(self):
     posts = [PostFactory(status='published') for _ in range(4)]
     posts[0].tags.add('test')
     posts[2].tags.add('test')
     response = self.client.get('/blog/tag/test/')
     self.assertContains(response, 'Posts tagged with "test"')
     self.assertEqual(list(response.context['posts']), [posts[2], posts[0]])
     self.assertEqual(response.context['tag'], Tag.objects.get(name='test'))
Exemplo n.º 10
0
def backgrond(context):
    posts = []
    for row in context.table:
        user = User.objects.filter(username=row['author']).first()
        posts.append(
            PostFactory(id=row['id'],
                        title=row['title'],
                        text=row['text'],
                        author=user))
Exemplo n.º 11
0
    def setUp(self):

        self.user_1 = UserFactory()
        self.user_2 = UserFactory()

        for i in range(15):

            user = self.user_1 if i % 2 == 0 else self.user_2
            published = True if i % 2 == 0 else False

            PostFactory(published=published, author=user)
Exemplo n.º 12
0
    def setUp(self):

        self.user_1 = UserFactory()
        self.user_2 = UserFactory()

        for i in range(10):
            user = self.user_1 if i % 2 == 0 else self.user_2
            published = True if i % 2 == 0 else False
            post = PostFactory(published=published,
                               author=user,
                               tags=(self.tag_1, ))
            print(post.tags)
Exemplo n.º 13
0
    def setUp(self):
        self.browser = webdriver.Firefox()
        self.post = PostFactory(status='published',
                                publish=timezone.datetime(2016,
                                                          10,
                                                          11,
                                                          2,
                                                          27,
                                                          tzinfo=UTC))
        self.post.tags.add('tag0', 'tag1')

        # He create and tag few posts
        self.post1 = PostFactory(status='published')
        self.post2 = PostFactory(status='published')
        self.post3 = PostFactory(status='published')

        self.post1.tags.add('tag0', 'tag1')
        self.post2.tags.add('tag0')

        # User goes to the post page
        self.browser.get(self.live_server_url + self.post.get_absolute_url())
Exemplo n.º 14
0
 def test_can_get_all_post_comments(self):
     post = PostFactory()
     comment1 = CommentFactory(post=post)
     CommentFactory()  # Comment from another post
     comment3 = CommentFactory(post=post)
     self.assertEqual(list(post.comments.all()), [comment1, comment3])
Exemplo n.º 15
0
 def test_published_manager_returns_only_published_posts(self):
     post1 = PostFactory(status='published')
     PostFactory()
     post3 = PostFactory(status='published')
     self.assertListEqual(list(Post.published.all()), [post3, post1])
Exemplo n.º 16
0
 def test_absolute_url(self):
     post = PostFactory()
     self.assertEqual(
         post.get_absolute_url(), '/blog/%04d/%02d/%02d/%s/' %
         (int(post.publish.year), int(
             post.publish.month), int(post.publish.day), post.slug))
Exemplo n.º 17
0
 def test_post_order_is_reverse_publish(self):
     post1 = PostFactory()
     post2 = PostFactory()
     post3 = PostFactory()
     self.assertEqual(list(Post.objects.all()), [post3, post2, post1])
Exemplo n.º 18
0
 def setUp(self):
     self.post = PostFactory(status='published')
     self.response = self.client.get('/blog/%s/share/' % self.post.id)
Exemplo n.º 19
0
 def test_returns_last_page_if_page_is_out_of_range(self):
     posts = [PostFactory(status='published') for _ in range(4)]
     response = self.client.get('/blog/?page=999')
     self.assertEqual(list(response.context['posts']), [posts[0]])
Exemplo n.º 20
0
 def setUp(self):
     self.author = self.make_user(username='******')
     self.commenter = self.make_user(username='******')
     self.post = PostFactory(author=self.author)
Exemplo n.º 21
0
 def test_returns_only_3_last_posts_by_default(self):
     posts = [PostFactory(status='published') for _ in range(4)]
     response = self.client.get('/blog/')
     self.assertEqual(list(response.context['posts']), posts[1:][::-1])
Exemplo n.º 22
0
    def setUp(self):

        self.post_1 = PostFactory()
Exemplo n.º 23
0
 def test_second_page_returns_correct_posts(self):
     posts = [PostFactory(status='published') for _ in range(4)]
     response = self.client.get('/blog/?page=2')
     self.assertEqual(list(response.context['posts']), [posts[0]])
Exemplo n.º 24
0
 def setUp(self):
     self.post_1 = PostFactory(published=False)
     self.post_2 = PostFactory()
Exemplo n.º 25
0
    def setUp(self):
        self.superuser = UserFactory(is_staff=True, is_superuser=True)
        self.nomaluser = UserFactory()

        self.published_post = PostFactory()  # pk = 1
        self.non_published_post = PostFactory(published_at=None)  # pk = 2
Exemplo n.º 26
0
 def test_default_status_is_draft(self):
     self.assertEqual('draft', PostFactory().status)
Exemplo n.º 27
0
 def setUp(self):
     self.browser = webdriver.Firefox()
     self.post = PostFactory(status='published')
Exemplo n.º 28
0
 def test_can_not_create_two_posts_with_same_slug_and_date(self):
     post = PostFactory()
     self.assertRaises(
         ValidationError,
         Post(slug=post.slug, publish=post.publish).validate_unique)
Exemplo n.º 29
0
def step_impl(context):
    posts = [PostFactory(title=row['title'], slug=row['slug'], author=UserFactory(username=row['author']), body=row['body']) for row in context.table]
Exemplo n.º 30
0
 def setUp(self):
     self.post = PostFactory(status='published')
     self.response = self.client.get(self.post.get_absolute_url())