From f37a8f797e3ab67949f326dc05829ee0f3caf207 Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Fri, 2 Dec 2022 08:59:55 +0100 Subject: [PATCH] improve tests --- tests/test_api.py | 5 +-- tests/test_config.py | 21 +++++++++-- tests/test_db.py | 88 ++++++++++++++++++++++++++++++++++++++----- tests/test_form.py | 3 +- tests/test_mail.py | 10 +++-- tests/test_rssfeed.py | 3 +- 6 files changed, 107 insertions(+), 23 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 0d84a0c..6badcbd 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -6,9 +6,8 @@ import logging import pytest -from stacosys.db import database, dao -from stacosys.interface import api -from stacosys.interface import app +from stacosys.db import dao, database +from stacosys.interface import api, app def init_test_db(): diff --git a/tests/test_config.py b/tests/test_config.py index fe44ef2..ae173c2 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -6,29 +6,42 @@ import pytest from stacosys.service import config from stacosys.service.configuration import ConfigParameter -EXPECTED_DB_SQLITE_FILE = "sqlite://db.sqlite" +EXPECTED_DB = "sqlite://db.sqlite" EXPECTED_HTTP_PORT = 8080 EXPECTED_LANG = "fr" @pytest.fixture def init_config(): - config.put(ConfigParameter.DB, EXPECTED_DB_SQLITE_FILE) - config.put(ConfigParameter.HTTP_PORT, EXPECTED_HTTP_PORT) + config.put(ConfigParameter.DB, EXPECTED_DB) + config.put(ConfigParameter.HTTP_PORT, EXPECTED_HTTP_PORT) + + +def test_split_key(): + section, param = config._split_key(ConfigParameter.HTTP_PORT) + assert section == "http" and param == "port" + def test_exists(init_config): assert config.exists(ConfigParameter.DB) + def test_get(init_config): - assert config.get(ConfigParameter.DB) == EXPECTED_DB_SQLITE_FILE + assert config.get(ConfigParameter.DB) == EXPECTED_DB assert config.get(ConfigParameter.HTTP_HOST) == "" assert config.get(ConfigParameter.HTTP_PORT) == str(EXPECTED_HTTP_PORT) assert config.get_int(ConfigParameter.HTTP_PORT) == EXPECTED_HTTP_PORT with pytest.raises(AssertionError): config.get_bool(ConfigParameter.DB) + def test_put(init_config): assert not config.exists(ConfigParameter.LANG) config.put(ConfigParameter.LANG, EXPECTED_LANG) assert config.exists(ConfigParameter.LANG) assert config.get(ConfigParameter.LANG) == EXPECTED_LANG + + +def test_check(init_config): + success, error = config.check() + assert not success and error diff --git a/tests/test_db.py b/tests/test_db.py index d53133c..ebacd80 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -1,22 +1,48 @@ #!/usr/bin/python # -*- coding: UTF-8 -*- +import time import pytest -from stacosys.db import dao -from stacosys.db import database +from stacosys.db import dao, database +from stacosys.model.comment import Comment + @pytest.fixture def setup_db(): database.configure("sqlite:memory://db.sqlite") -def test_dao_published(setup_db): +def equals_comment(comment: Comment, other): + return ( + comment.id == other.id + and comment.author_gravatar == other.author_gravatar + and comment.author_name == other.author_name + and comment.author_site == other.author_site + and comment.content == other.content + and comment.created == other.created + and comment.notified == other.notified + and comment.published == other.published + ) + + +def test_find_comment_by_id(setup_db): + assert dao.find_comment_by_id(1) is None + c1 = dao.create_comment("/post1", "Yax", "", "", "Comment 1") + assert c1.id is not None + find_c1 = dao.find_comment_by_id(c1.id) + assert find_c1 + assert equals_comment(c1, find_c1) + c1.id = find_c1.id + dao.delete_comment(c1) + assert dao.find_comment_by_id(c1.id) is None + - # test count published +def test_dao_published(setup_db): assert 0 == dao.count_published_comments("") c1 = dao.create_comment("/post1", "Yax", "", "", "Comment 1") assert 0 == dao.count_published_comments("") + assert 1 == len(dao.find_not_published_comments()) dao.publish_comment(c1) assert 1 == dao.count_published_comments("") c2 = dao.create_comment("/post2", "Yax", "", "", "Comment 2") @@ -24,20 +50,19 @@ def test_dao_published(setup_db): assert 2 == dao.count_published_comments("") c3 = dao.create_comment("/post2", "Yax", "", "", "Comment 3") dao.publish_comment(c3) + assert 0 == len(dao.find_not_published_comments()) + + # count published assert 1 == dao.count_published_comments("/post1") assert 2 == dao.count_published_comments("/post2") - # test find published + # find published assert 0 == len(dao.find_published_comments_by_url("/")) assert 1 == len(dao.find_published_comments_by_url("/post1")) assert 2 == len(dao.find_published_comments_by_url("/post2")) - dao.delete_comment(c1) - assert 0 == len(dao.find_published_comments_by_url("/post1")) def test_dao_notified(setup_db): - - # test count notified assert 0 == len(dao.find_not_notified_comments()) c1 = dao.create_comment("/post1", "Yax", "", "", "Comment 1") assert 1 == len(dao.find_not_notified_comments()) @@ -51,3 +76,48 @@ def test_dao_notified(setup_db): dao.notify_comment(c3) assert 0 == len(dao.find_not_notified_comments()) + +def create_comment(url, author_name, content): + return dao.create_comment(url, author_name, "", "", content) + + +def test_find_recent_published_comments(setup_db): + + comments = [] + comments.append(create_comment("/post", "Adam", "Comment 1")) + comments.append(create_comment("/post", "Arf", "Comment 2")) + comments.append(create_comment("/post", "Arwin", "Comment 3")) + comments.append(create_comment("/post", "Bill", "Comment 4")) + comments.append(create_comment("/post", "Bo", "Comment 5")) + comments.append(create_comment("/post", "Charles", "Comment 6")) + comments.append(create_comment("/post", "Dan", "Comment 7")) + comments.append(create_comment("/post", "Dwayne", "Comment 8")) + comments.append(create_comment("/post", "Erl", "Comment 9")) + comments.append(create_comment("/post", "Jay", "Comment 10")) + comments.append(create_comment("/post", "Kenny", "Comment 11")) + comments.append(create_comment("/post", "Lord", "Comment 12")) + + rows = dao.find_recent_published_comments() + assert len(rows) == 0 + + # publish every second + for comment in comments: + dao.publish_comment(comment) + time.sleep(1) + + rows = dao.find_recent_published_comments() + assert len(rows) == 10 + + authors = [row.author_name for row in rows] + assert authors == [ + "Lord", + "Kenny", + "Jay", + "Erl", + "Dwayne", + "Dan", + "Charles", + "Bo", + "Bill", + "Arwin", + ] diff --git a/tests/test_form.py b/tests/test_form.py index 1ddd4ae..9244483 100644 --- a/tests/test_form.py +++ b/tests/test_form.py @@ -6,8 +6,7 @@ import logging import pytest from stacosys.db import database -from stacosys.interface import app -from stacosys.interface import form +from stacosys.interface import app, form @pytest.fixture diff --git a/tests/test_mail.py b/tests/test_mail.py index 192c3cc..9f9597f 100644 --- a/tests/test_mail.py +++ b/tests/test_mail.py @@ -2,10 +2,12 @@ # -*- coding: UTF-8 -*- import pytest + from stacosys.service import mailer + def test_configure_and_check(): - mailer.configure_smtp("localhost", 2525, "admin", "admin") - mailer.configure_destination("admin@mydomain.com") - with pytest.raises(ConnectionRefusedError): - mailer.check() \ No newline at end of file + mailer.configure_smtp("localhost", 2525, "admin", "admin") + mailer.configure_destination("admin@mydomain.com") + with pytest.raises(ConnectionRefusedError): + mailer.check() diff --git a/tests/test_rssfeed.py b/tests/test_rssfeed.py index 713eead..021fc1e 100644 --- a/tests/test_rssfeed.py +++ b/tests/test_rssfeed.py @@ -3,5 +3,6 @@ from stacosys.service import rss + def test_configure(): - rss.configure("comments.xml", "blog", "http", "blog.mydomain.com") + rss.configure("comments.xml", "blog", "http", "blog.mydomain.com")