From bcc7a8d6b7fa54d00206b6ba70485c91c77e1719 Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Sun, 18 Jul 2021 20:17:10 +0200 Subject: [PATCH] unit test dao --- stacosys/db/dao.py | 3 ++- tests/test_db.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/test_db.py diff --git a/stacosys/db/dao.py b/stacosys/db/dao.py index 05a2777..36ebdd4 100644 --- a/stacosys/db/dao.py +++ b/stacosys/db/dao.py @@ -35,7 +35,7 @@ def find_published_comments_by_url(url): def count_published_comments(url): return Comment.select(Comment).where( (Comment.url == url) & (Comment.published.is_null(False))).count() if url else Comment.select(Comment).where( - Comment.publishd.is_null(False)).count() + Comment.published.is_null(False)).count() def create_comment(url, author_name, author_site, author_gravatar, message): @@ -51,3 +51,4 @@ def create_comment(url, author_name, author_site, author_gravatar, message): published=None, ) comment.save() + return comment diff --git a/tests/test_db.py b/tests/test_db.py new file mode 100644 index 0000000..2d99936 --- /dev/null +++ b/tests/test_db.py @@ -0,0 +1,55 @@ +import unittest + +from stacosys.db import dao +from stacosys.db import database + + +class DbTestCase(unittest.TestCase): + + def setUp(self): + db = database.Database() + db.setup(":memory:") + + def test_dao_published(self): + + # test count published + self.assertEqual(0, dao.count_published_comments("")) + c1 = dao.create_comment("/post1", "Yax", "", "", "Comment 1") + self.assertEqual(0, dao.count_published_comments("")) + dao.publish_comment(c1) + self.assertEqual(1, dao.count_published_comments("")) + c2 = dao.create_comment("/post2", "Yax", "", "", "Comment 2") + dao.publish_comment(c2) + self.assertEqual(2, dao.count_published_comments("")) + c3 = dao.create_comment("/post2", "Yax", "", "", "Comment 3") + dao.publish_comment(c3) + self.assertEqual(1, dao.count_published_comments("/post1")) + self.assertEqual(2, dao.count_published_comments("/post2")) + + # test find published + self.assertEqual(0, len(dao.find_published_comments_by_url("/"))) + self.assertEqual(1, len(dao.find_published_comments_by_url("/post1"))) + self.assertEqual(2, len(dao.find_published_comments_by_url("/post2"))) + + dao.delete_comment(c1) + self.assertEqual(0, len(dao.find_published_comments_by_url("/post1"))) + + def test_dao_notified(self): + + # test count notified + self.assertEqual(0, len(dao.find_not_notified_comments())) + c1 = dao.create_comment("/post1", "Yax", "", "", "Comment 1") + self.assertEqual(1, len(dao.find_not_notified_comments())) + c2 = dao.create_comment("/post2", "Yax", "", "", "Comment 2") + self.assertEqual(2, len(dao.find_not_notified_comments())) + dao.notify_comment(c1) + dao.notify_comment(c2) + self.assertEqual(0, len(dao.find_not_notified_comments())) + c3 = dao.create_comment("/post2", "Yax", "", "", "Comment 3") + self.assertEqual(1, len(dao.find_not_notified_comments())) + dao.notify_comment(c3) + self.assertEqual(0, len(dao.find_not_notified_comments())) + + +if __name__ == '__main__': + unittest.main()