improve tests

This commit is contained in:
Yax 2022-12-02 08:59:55 +01:00
parent 18d4225eec
commit f37a8f797e
6 changed files with 107 additions and 23 deletions

View file

@ -6,9 +6,8 @@ import logging
import pytest import pytest
from stacosys.db import database, dao from stacosys.db import dao, database
from stacosys.interface import api from stacosys.interface import api, app
from stacosys.interface import app
def init_test_db(): def init_test_db():

View file

@ -6,29 +6,42 @@ import pytest
from stacosys.service import config from stacosys.service import config
from stacosys.service.configuration import ConfigParameter from stacosys.service.configuration import ConfigParameter
EXPECTED_DB_SQLITE_FILE = "sqlite://db.sqlite" EXPECTED_DB = "sqlite://db.sqlite"
EXPECTED_HTTP_PORT = 8080 EXPECTED_HTTP_PORT = 8080
EXPECTED_LANG = "fr" EXPECTED_LANG = "fr"
@pytest.fixture @pytest.fixture
def init_config(): def init_config():
config.put(ConfigParameter.DB, EXPECTED_DB_SQLITE_FILE) config.put(ConfigParameter.DB, EXPECTED_DB)
config.put(ConfigParameter.HTTP_PORT, EXPECTED_HTTP_PORT) 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): def test_exists(init_config):
assert config.exists(ConfigParameter.DB) assert config.exists(ConfigParameter.DB)
def test_get(init_config): 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_HOST) == ""
assert config.get(ConfigParameter.HTTP_PORT) == str(EXPECTED_HTTP_PORT) assert config.get(ConfigParameter.HTTP_PORT) == str(EXPECTED_HTTP_PORT)
assert config.get_int(ConfigParameter.HTTP_PORT) == EXPECTED_HTTP_PORT assert config.get_int(ConfigParameter.HTTP_PORT) == EXPECTED_HTTP_PORT
with pytest.raises(AssertionError): with pytest.raises(AssertionError):
config.get_bool(ConfigParameter.DB) config.get_bool(ConfigParameter.DB)
def test_put(init_config): def test_put(init_config):
assert not config.exists(ConfigParameter.LANG) assert not config.exists(ConfigParameter.LANG)
config.put(ConfigParameter.LANG, EXPECTED_LANG) config.put(ConfigParameter.LANG, EXPECTED_LANG)
assert config.exists(ConfigParameter.LANG) assert config.exists(ConfigParameter.LANG)
assert config.get(ConfigParameter.LANG) == EXPECTED_LANG assert config.get(ConfigParameter.LANG) == EXPECTED_LANG
def test_check(init_config):
success, error = config.check()
assert not success and error

View file

@ -1,22 +1,48 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
import time
import pytest import pytest
from stacosys.db import dao from stacosys.db import dao, database
from stacosys.db import database from stacosys.model.comment import Comment
@pytest.fixture @pytest.fixture
def setup_db(): def setup_db():
database.configure("sqlite:memory://db.sqlite") 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
)
# test count 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
def test_dao_published(setup_db):
assert 0 == dao.count_published_comments("") assert 0 == dao.count_published_comments("")
c1 = dao.create_comment("/post1", "Yax", "", "", "Comment 1") c1 = dao.create_comment("/post1", "Yax", "", "", "Comment 1")
assert 0 == dao.count_published_comments("") assert 0 == dao.count_published_comments("")
assert 1 == len(dao.find_not_published_comments())
dao.publish_comment(c1) dao.publish_comment(c1)
assert 1 == dao.count_published_comments("") assert 1 == dao.count_published_comments("")
c2 = dao.create_comment("/post2", "Yax", "", "", "Comment 2") c2 = dao.create_comment("/post2", "Yax", "", "", "Comment 2")
@ -24,20 +50,19 @@ def test_dao_published(setup_db):
assert 2 == dao.count_published_comments("") assert 2 == dao.count_published_comments("")
c3 = dao.create_comment("/post2", "Yax", "", "", "Comment 3") c3 = dao.create_comment("/post2", "Yax", "", "", "Comment 3")
dao.publish_comment(c3) dao.publish_comment(c3)
assert 0 == len(dao.find_not_published_comments())
# count published
assert 1 == dao.count_published_comments("/post1") assert 1 == dao.count_published_comments("/post1")
assert 2 == dao.count_published_comments("/post2") assert 2 == dao.count_published_comments("/post2")
# test find published # find published
assert 0 == len(dao.find_published_comments_by_url("/")) assert 0 == len(dao.find_published_comments_by_url("/"))
assert 1 == len(dao.find_published_comments_by_url("/post1")) assert 1 == len(dao.find_published_comments_by_url("/post1"))
assert 2 == len(dao.find_published_comments_by_url("/post2")) 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): def test_dao_notified(setup_db):
# test count notified
assert 0 == len(dao.find_not_notified_comments()) assert 0 == len(dao.find_not_notified_comments())
c1 = dao.create_comment("/post1", "Yax", "", "", "Comment 1") c1 = dao.create_comment("/post1", "Yax", "", "", "Comment 1")
assert 1 == len(dao.find_not_notified_comments()) assert 1 == len(dao.find_not_notified_comments())
@ -51,3 +76,48 @@ def test_dao_notified(setup_db):
dao.notify_comment(c3) dao.notify_comment(c3)
assert 0 == len(dao.find_not_notified_comments()) 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",
]

View file

@ -6,8 +6,7 @@ import logging
import pytest import pytest
from stacosys.db import database from stacosys.db import database
from stacosys.interface import app from stacosys.interface import app, form
from stacosys.interface import form
@pytest.fixture @pytest.fixture

View file

@ -2,8 +2,10 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
import pytest import pytest
from stacosys.service import mailer from stacosys.service import mailer
def test_configure_and_check(): def test_configure_and_check():
mailer.configure_smtp("localhost", 2525, "admin", "admin") mailer.configure_smtp("localhost", 2525, "admin", "admin")
mailer.configure_destination("admin@mydomain.com") mailer.configure_destination("admin@mydomain.com")

View file

@ -3,5 +3,6 @@
from stacosys.service import rss from stacosys.service import rss
def test_configure(): def test_configure():
rss.configure("comments.xml", "blog", "http", "blog.mydomain.com") rss.configure("comments.xml", "blog", "http", "blog.mydomain.com")