replace pytest by unittest

This commit is contained in:
Yax 2021-07-17 11:42:04 +02:00
parent 92e6a8965d
commit 3905ef4bbd
4 changed files with 107 additions and 86 deletions

View file

@ -1,7 +1,8 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
import pytest import unittest
from stacosys.conf.config import Config, ConfigParameter from stacosys.conf.config import Config, ConfigParameter
EXPECTED_DB_SQLITE_FILE = "db.sqlite" EXPECTED_DB_SQLITE_FILE = "db.sqlite"
@ -10,8 +11,9 @@ EXPECTED_IMAP_PORT = "5000"
EXPECTED_IMAP_LOGIN = "user" EXPECTED_IMAP_LOGIN = "user"
@pytest.fixture class ConfigTestCase(unittest.TestCase):
def conf():
def conf(self):
conf = Config() conf = Config()
conf.put(ConfigParameter.DB_SQLITE_FILE, EXPECTED_DB_SQLITE_FILE) conf.put(ConfigParameter.DB_SQLITE_FILE, EXPECTED_DB_SQLITE_FILE)
conf.put(ConfigParameter.HTTP_PORT, EXPECTED_HTTP_PORT) conf.put(ConfigParameter.HTTP_PORT, EXPECTED_HTTP_PORT)
@ -20,38 +22,35 @@ def conf():
conf.put(ConfigParameter.IMAP_SSL, "false") conf.put(ConfigParameter.IMAP_SSL, "false")
return conf return conf
def test_exists(self):
conf = self.conf()
self.assertTrue(conf.exists(ConfigParameter.DB_SQLITE_FILE))
self.assertFalse(conf.exists(ConfigParameter.IMAP_HOST))
def test_exists(conf): def test_get(self):
assert conf is not None conf = self.conf()
assert conf.exists(ConfigParameter.DB_SQLITE_FILE) self.assertEqual(conf.get(ConfigParameter.DB_SQLITE_FILE), EXPECTED_DB_SQLITE_FILE)
assert not conf.exists(ConfigParameter.IMAP_HOST) self.assertEqual(conf.get(ConfigParameter.HTTP_PORT), EXPECTED_HTTP_PORT)
self.assertIsNone(conf.get(ConfigParameter.HTTP_HOST))
self.assertEqual(conf.get(ConfigParameter.HTTP_PORT), EXPECTED_HTTP_PORT)
def test_get(conf): self.assertEqual(conf.get(ConfigParameter.IMAP_PORT), EXPECTED_IMAP_PORT)
assert conf is not None self.assertEqual(conf.get_int(ConfigParameter.IMAP_PORT), int(EXPECTED_IMAP_PORT))
assert conf.get(ConfigParameter.DB_SQLITE_FILE) == EXPECTED_DB_SQLITE_FILE
assert conf.get(ConfigParameter.HTTP_PORT) == EXPECTED_HTTP_PORT
assert conf.get(ConfigParameter.HTTP_HOST) is None
assert conf.get(ConfigParameter.HTTP_PORT) == EXPECTED_HTTP_PORT
assert conf.get(ConfigParameter.IMAP_PORT) == EXPECTED_IMAP_PORT
assert conf.get_int(ConfigParameter.IMAP_PORT) == int(EXPECTED_IMAP_PORT)
try: try:
conf.get_int(ConfigParameter.HTTP_PORT) conf.get_int(ConfigParameter.HTTP_PORT)
assert False self.assertTrue(False)
except Exception: except Exception:
pass pass
assert conf.get_bool(ConfigParameter.SMTP_STARTTLS) self.assertTrue(conf.get_bool(ConfigParameter.SMTP_STARTTLS))
assert not conf.get_bool(ConfigParameter.IMAP_SSL) self.assertFalse(conf.get_bool(ConfigParameter.IMAP_SSL))
try: try:
conf.get_bool(ConfigParameter.DB_URL) conf.get_bool(ConfigParameter.DB_URL)
assert False self.assertTrue(False)
except Exception: except Exception:
pass pass
def test_put(self):
def test_put(conf): conf = self.conf()
assert conf is not None self.assertFalse(conf.exists(ConfigParameter.IMAP_LOGIN))
assert not conf.exists(ConfigParameter.IMAP_LOGIN)
conf.put(ConfigParameter.IMAP_LOGIN, EXPECTED_IMAP_LOGIN) conf.put(ConfigParameter.IMAP_LOGIN, EXPECTED_IMAP_LOGIN)
assert conf.exists(ConfigParameter.IMAP_LOGIN) self.assertTrue(conf.exists(ConfigParameter.IMAP_LOGIN))
assert conf.get(ConfigParameter.IMAP_LOGIN) == EXPECTED_IMAP_LOGIN self.assertEqual(conf.get(ConfigParameter.IMAP_LOGIN), EXPECTED_IMAP_LOGIN)

22
tests/test_form.py Normal file
View file

@ -0,0 +1,22 @@
import unittest
from stacosys.interface import form
class FormInterfaceTestCase(unittest.TestCase):
def test_check_form_data_ok(self):
d = {"url": "/", "message": "", "site": "", "remarque": "", "author": "", "token": "", "email": ""}
self.assertTrue(form.check_form_data(d))
d = {"url": "/"}
self.assertTrue(form.check_form_data(d))
d = {}
self.assertTrue(form.check_form_data(d))
def test_check_form_data_ko(self):
d = {"url": "/", "message": "", "site": "", "remarque": "", "author": "", "token": "", "email": "", "bonus": ""}
self.assertFalse(form.check_form_data(d))
if __name__ == '__main__':
unittest.main()

View file

@ -1,5 +1,9 @@
import unittest
from stacosys import __version__ from stacosys import __version__
def test_version(): class StacosysTestCase(unittest.TestCase):
assert __version__ == "2.0"
def test_version(self):
self.assertEqual("2.0", __version__)

View file

@ -2,55 +2,51 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
import os import os
import unittest
from stacosys.core.templater import Templater, Template from stacosys.core.templater import Templater, Template
def get_template_content(lang, template_name, **kwargs): class TemplateTestCase(unittest.TestCase):
def get_template_content(self, lang, template_name, **kwargs):
current_path = os.path.dirname(__file__) current_path = os.path.dirname(__file__)
template_path = os.path.abspath(os.path.join(current_path, "../stacosys/templates")) template_path = os.path.abspath(os.path.join(current_path, "../stacosys/templates"))
templater = Templater(template_path) template = Templater(template_path).get_template(lang, template_name)
template = templater.get_template(lang, template_name)
assert template
return template.render(kwargs) return template.render(kwargs)
def test_approve_comment(self):
content = self.get_template_content("fr", Template.APPROVE_COMMENT, original="[texte]")
self.assertTrue(content.startswith("Bonjour,\n\nLe commentaire sera bientôt publié."))
self.assertTrue(content.endswith("[texte]"))
content = self.get_template_content("en", Template.APPROVE_COMMENT, original="[texte]")
self.assertTrue(content.startswith("Hi,\n\nThe comment should be published soon."))
self.assertTrue(content.endswith("[texte]"))
def test_approve_comment(): def test_drop_comment(self):
content = get_template_content("fr", Template.APPROVE_COMMENT, original="[texte]") content = self.get_template_content("fr", Template.DROP_COMMENT, original="[texte]")
assert content.startswith("Bonjour,\n\nLe commentaire sera bientôt publié.") self.assertTrue(content.startswith("Bonjour,\n\nLe commentaire ne sera pas publié."))
assert content.endswith("[texte]") self.assertTrue(content.endswith("[texte]"))
content = get_template_content("en", Template.APPROVE_COMMENT, original="[texte]") content = self.get_template_content("en", Template.DROP_COMMENT, original="[texte]")
assert content.startswith("Hi,\n\nThe comment should be published soon.") self.assertTrue(content.startswith("Hi,\n\nThe comment will not be published."))
assert content.endswith("[texte]") self.assertTrue(content.endswith("[texte]"))
def test_new_comment(self):
content = self.get_template_content("fr", Template.NEW_COMMENT, comment="[comment]")
self.assertTrue(content.startswith("Bonjour,\n\nUn nouveau commentaire a été posté"))
self.assertTrue(content.endswith("[comment]\n\n--\nStacosys"))
content = self.get_template_content("en", Template.NEW_COMMENT, comment="[comment]")
self.assertTrue(content.startswith("Hi,\n\nA new comment has been submitted"))
self.assertTrue(content.endswith("[comment]\n\n--\nStacosys"))
def test_drop_comment(): def test_notify_message(self):
content = get_template_content("fr", Template.DROP_COMMENT, original="[texte]") content = self.get_template_content("fr", Template.NOTIFY_MESSAGE)
assert content.startswith("Bonjour,\n\nLe commentaire ne sera pas publié.") self.assertEqual("Nouveau commentaire", content)
assert content.endswith("[texte]") content = self.get_template_content("en", Template.NOTIFY_MESSAGE)
content = get_template_content("en", Template.DROP_COMMENT, original="[texte]") self.assertEqual("New comment", content)
assert content.startswith("Hi,\n\nThe comment will not be published.")
assert content.endswith("[texte]")
def test_rss_title(self):
def test_new_comment(): content = self.get_template_content("fr", Template.RSS_TITLE_MESSAGE, site="[site]")
content = get_template_content("fr", Template.NEW_COMMENT, comment="[comment]") self.assertEqual("[site] : commentaires", content)
assert content.startswith("Bonjour,\n\nUn nouveau commentaire a été posté") content = self.get_template_content("en", Template.RSS_TITLE_MESSAGE, site="[site]")
assert content.endswith("[comment]\n\n--\nStacosys") self.assertEqual("[site] : comments", content)
content = get_template_content("en", Template.NEW_COMMENT, comment="[comment]")
assert content.startswith("Hi,\n\nA new comment has been submitted")
assert content.endswith("[comment]\n\n--\nStacosys")
def test_notify_message():
content = get_template_content("fr", Template.NOTIFY_MESSAGE)
assert content == "Nouveau commentaire"
content = get_template_content("en", Template.NOTIFY_MESSAGE)
assert content == "New comment"
def test_rss_title():
content = get_template_content("fr", Template.RSS_TITLE_MESSAGE, site="[site]")
assert content == "[site] : commentaires"
content = get_template_content("en", Template.RSS_TITLE_MESSAGE, site="[site]")
assert content == "[site] : comments"