Esempio n. 1
0
    def test_saving_and_deleting_categories(self):

        with test_database(db,
                           (Users, Articles, Categories, ArticleCategories)):
            Users.create_user(username="******", password="******")
            user1 = Users.select().get()

            art = Articles.create_article(title="test article1",
                                          body="test",
                                          draft=True,
                                          author=user1)
            art2 = Articles.create_article(title="test article2",
                                           body="test",
                                           draft=True,
                                           author=user1)

            art.save_article_categories([u"cat1", u"cat2", u"cat3"])

            self.assertIn("cat1", str(tuple(art.get_article_categories())))
            self.assertIn("cat2", str(tuple(art.get_article_categories())))
            self.assertIn("cat3", str(tuple(art.get_article_categories())))

            art.save_article_categories(["cat1", "cat2", "cat3"])

            cat = Categories.select().count()

            self.assertEquals(3, cat)

            cat4 = Categories.create(name="cat4")
            cat5 = Categories.create(name="cat5")
            cat6 = Categories.create(name="cat6")

            art.save_article_categories(["cat4"])
            self.assertIn("cat4", str(tuple(art.get_article_categories())))
            self.assertEquals(4, art.get_article_categories().count())

            art2.save_article_categories(("cat1", "cat4", "cat7"))

            cats = Categories.select().group_by(Categories.name)

            self.assertIn("cat7", str(tuple(cats)))
            self.assertEquals(7, cats.count())

            art.delete_instance(recursive=True)

            self.assertEquals(7, cats.count())

            self.assertEquals(
                ArticleCategories.select().group_by(
                    ArticleCategories.id).count(), 4)
            cat4.delete_instance(recursive=True)

            self.assertEquals(
                ArticleCategories.select().group_by(
                    ArticleCategories.id).count(), 3)
Esempio n. 2
0
 def get_context(self):
     existing_categories = Categories.select()
     return dict(draft=True,
                 additional_controls=True,
                 existing_categories=existing_categories,
                 title_placeholder="Title of your Article",
                 body_placeholder="and content here...")
Esempio n. 3
0
    def test_saving_and_deleting_categories(self):

        with test_database(db, (Users, Articles, Categories, ArticleCategories)):
            Users.create_user(username = "******", password = "******")
            user1 = Users.select().get()

            art = Articles.create_article(title = "test article1", body = "test", draft = True, author = user1)
            art2 = Articles.create_article(title = "test article2", body = "test", draft = True, author = user1)


            art.save_article_categories([u"cat1", u"cat2", u"cat3"])


            self.assertIn("cat1", str(tuple(art.get_article_categories())))
            self.assertIn("cat2", str(tuple(art.get_article_categories())))
            self.assertIn("cat3", str(tuple(art.get_article_categories())))

            art.save_article_categories(["cat1", "cat2", "cat3"])

            cat = Categories.select().count()

            self.assertEquals(3, cat)

            cat4 = Categories.create(name = "cat4")
            cat5 = Categories.create(name = "cat5")
            cat6 = Categories.create(name = "cat6")

            art.save_article_categories(["cat4"])
            self.assertIn("cat4", str(tuple(art.get_article_categories())))
            self.assertEquals(4, art.get_article_categories().count())

            art2.save_article_categories(("cat1", "cat4", "cat7"))

            cats = Categories.select().group_by(Categories.name)

            self.assertIn("cat7", str(tuple(cats)))
            self.assertEquals(7, cats.count())

            art.delete_instance(recursive = True)

            self.assertEquals(7, cats.count())


            self.assertEquals(ArticleCategories.select().group_by(ArticleCategories.id).count(), 4)
            cat4.delete_instance(recursive = True)

            self.assertEquals(ArticleCategories.select().group_by(ArticleCategories.id).count(), 3)
Esempio n. 4
0
    def test_getting_similar_articles(self):

        with test_database(db, (Users, Articles, Categories, ArticleCategories)):
            Users.create_user(username = "******", password = "******")
            user1 = Users.select().get()


            for x in range(1, 8):
                globals()["article" + str(x)] = Articles.create_article(title = "test article" + str(x), body = "test", draft = True, author = user1)

            for y in range(1, 5):
                globals()["category" + str(y)] = Categories.create(name = "test" + str(y))


            ArticleCategories.create(article = article1, category = category1)
            ArticleCategories.create(article = article1, category = category2)
            ArticleCategories.create(article = article1, category = category3)

            ArticleCategories.create(article = article2, category = category1)
            ArticleCategories.create(article = article2, category = category2)
            ArticleCategories.create(article = article2, category = category3)

            ArticleCategories.create(article = article3, category = category1)
            ArticleCategories.create(article = article3, category = category2)
            ArticleCategories.create(article = article3, category = category3)


            ArticleCategories.create(article = article4, category = category1)
            ArticleCategories.create(article = article4, category = category2)

            ArticleCategories.create(article = article5, category = category1)
            ArticleCategories.create(article = article5, category = category2)
            ArticleCategories.create(article = article5, category = category3)

            ArticleCategories.create(article = article6, category = category2)

            ArticleCategories.create(article = article7, category = category4)


            sel = article1.get_similar_articles()

            self.assertIn("test article2", str(tuple(sel)))
            self.assertIn("test article3", str(tuple(sel)))
            self.assertIn("test article5", str(tuple(sel)))
            self.assertNotIn("test article1", str(tuple(sel)))
            self.assertNotIn("test article4", str(tuple(sel)))


            sel = article1.get_similar_articles(limit = 4)


            self.assertIn("test article2", str(tuple(sel)))
            self.assertIn("test article3", str(tuple(sel)))
            self.assertIn("test article5", str(tuple(sel)))
            self.assertIn("test article4", str(tuple(sel)))
            self.assertNotIn("test article6", str(tuple(sel)))
            self.assertNotIn("test article1", str(tuple(sel)))


            sel = article1.get_similar_articles(common_categories = 3, limit = 5)

            self.assertIn("test article2", str(tuple(sel)))
            self.assertIn("test article3", str(tuple(sel)))
            self.assertIn("test article5", str(tuple(sel)))
            self.assertNotIn("test article1", str(tuple(sel)))
            self.assertNotIn("test article4", str(tuple(sel)))



            sel = article7.get_similar_articles()

            self.assertEquals(sel.wrapped_count(False), 0)

            # Test getting article categories while we're at it

            q = article1.get_article_categories()
            
            self.assertIn("test1",str(tuple(q)) )
            self.assertIn("test2",str(tuple(q)))
            self.assertNotIn("test4", str(tuple(q)))
            q = article4.get_article_categories()
            self.assertIn("test1",str(tuple(q)) )
            self.assertNotIn("test3", str(tuple(q)))

            q = article7.get_article_categories()
            self.assertIn("test4",str(tuple(q)) )
            self.assertNotIn("test3", str(tuple(q)))
Esempio n. 5
0
    def test_getting_similar_articles(self):

        with test_database(db,
                           (Users, Articles, Categories, ArticleCategories)):
            Users.create_user(username="******", password="******")
            user1 = Users.select().get()

            for x in range(1, 8):
                globals()["article" + str(x)] = Articles.create_article(
                    title="test article" + str(x),
                    body="test",
                    draft=True,
                    author=user1)

            for y in range(1, 5):
                globals()["category" +
                          str(y)] = Categories.create(name="test" + str(y))

            ArticleCategories.create(article=article1, category=category1)
            ArticleCategories.create(article=article1, category=category2)
            ArticleCategories.create(article=article1, category=category3)

            ArticleCategories.create(article=article2, category=category1)
            ArticleCategories.create(article=article2, category=category2)
            ArticleCategories.create(article=article2, category=category3)

            ArticleCategories.create(article=article3, category=category1)
            ArticleCategories.create(article=article3, category=category2)
            ArticleCategories.create(article=article3, category=category3)

            ArticleCategories.create(article=article4, category=category1)
            ArticleCategories.create(article=article4, category=category2)

            ArticleCategories.create(article=article5, category=category1)
            ArticleCategories.create(article=article5, category=category2)
            ArticleCategories.create(article=article5, category=category3)

            ArticleCategories.create(article=article6, category=category2)

            ArticleCategories.create(article=article7, category=category4)

            sel = article1.get_similar_articles()

            self.assertIn("test article2", str(tuple(sel)))
            self.assertIn("test article3", str(tuple(sel)))
            self.assertIn("test article5", str(tuple(sel)))
            self.assertNotIn("test article1", str(tuple(sel)))
            self.assertNotIn("test article4", str(tuple(sel)))

            sel = article1.get_similar_articles(limit=4)

            self.assertIn("test article2", str(tuple(sel)))
            self.assertIn("test article3", str(tuple(sel)))
            self.assertIn("test article5", str(tuple(sel)))
            self.assertIn("test article4", str(tuple(sel)))
            self.assertNotIn("test article6", str(tuple(sel)))
            self.assertNotIn("test article1", str(tuple(sel)))

            sel = article1.get_similar_articles(common_categories=3, limit=5)

            self.assertIn("test article2", str(tuple(sel)))
            self.assertIn("test article3", str(tuple(sel)))
            self.assertIn("test article5", str(tuple(sel)))
            self.assertNotIn("test article1", str(tuple(sel)))
            self.assertNotIn("test article4", str(tuple(sel)))

            sel = article7.get_similar_articles()

            self.assertEquals(sel.wrapped_count(False), 0)

            # Test getting article categories while we're at it

            q = article1.get_article_categories()

            self.assertIn("test1", str(tuple(q)))
            self.assertIn("test2", str(tuple(q)))
            self.assertNotIn("test4", str(tuple(q)))
            q = article4.get_article_categories()
            self.assertIn("test1", str(tuple(q)))
            self.assertNotIn("test3", str(tuple(q)))

            q = article7.get_article_categories()
            self.assertIn("test4", str(tuple(q)))
            self.assertNotIn("test3", str(tuple(q)))