From 3eb1b86246410fc6ee067fdb1def0b266131a949 Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Sat, 17 Jul 2021 11:42:04 +0200 Subject: [PATCH] replace pytest by unittest --- tests/test_config.py | 91 ++++++++++++++++++++-------------------- tests/test_form.py | 22 ++++++++++ tests/test_stacosys.py | 8 +++- tests/test_templater.py | 92 ++++++++++++++++++++--------------------- 4 files changed, 117 insertions(+), 96 deletions(-) create mode 100644 tests/test_form.py diff --git a/tests/test_config.py b/tests/test_config.py index 3df0502..6b4eac1 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,7 +1,8 @@ #!/usr/bin/python # -*- coding: UTF-8 -*- -import pytest +import unittest + from stacosys.conf.config import Config, ConfigParameter EXPECTED_DB_SQLITE_FILE = "db.sqlite" @@ -10,48 +11,46 @@ EXPECTED_IMAP_PORT = "5000" EXPECTED_IMAP_LOGIN = "user" -@pytest.fixture -def conf(): - conf = Config() - conf.put(ConfigParameter.DB_SQLITE_FILE, EXPECTED_DB_SQLITE_FILE) - conf.put(ConfigParameter.HTTP_PORT, EXPECTED_HTTP_PORT) - conf.put(ConfigParameter.IMAP_PORT, EXPECTED_IMAP_PORT) - conf.put(ConfigParameter.SMTP_STARTTLS, "yes") - conf.put(ConfigParameter.IMAP_SSL, "false") - return conf - - -def test_exists(conf): - assert conf is not None - assert conf.exists(ConfigParameter.DB_SQLITE_FILE) - assert not conf.exists(ConfigParameter.IMAP_HOST) - - -def test_get(conf): - assert conf is not None - 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: - conf.get_int(ConfigParameter.HTTP_PORT) - assert False - except Exception: - pass - assert conf.get_bool(ConfigParameter.SMTP_STARTTLS) - assert not conf.get_bool(ConfigParameter.IMAP_SSL) - try: - conf.get_bool(ConfigParameter.DB_URL) - assert False - except Exception: - pass - - -def test_put(conf): - assert conf is not None - assert not conf.exists(ConfigParameter.IMAP_LOGIN) - conf.put(ConfigParameter.IMAP_LOGIN, EXPECTED_IMAP_LOGIN) - assert conf.exists(ConfigParameter.IMAP_LOGIN) - assert conf.get(ConfigParameter.IMAP_LOGIN) == EXPECTED_IMAP_LOGIN +class ConfigTestCase(unittest.TestCase): + + def conf(self): + conf = Config() + conf.put(ConfigParameter.DB_SQLITE_FILE, EXPECTED_DB_SQLITE_FILE) + conf.put(ConfigParameter.HTTP_PORT, EXPECTED_HTTP_PORT) + conf.put(ConfigParameter.IMAP_PORT, EXPECTED_IMAP_PORT) + conf.put(ConfigParameter.SMTP_STARTTLS, "yes") + conf.put(ConfigParameter.IMAP_SSL, "false") + 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_get(self): + conf = self.conf() + self.assertEqual(conf.get(ConfigParameter.DB_SQLITE_FILE), EXPECTED_DB_SQLITE_FILE) + 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) + self.assertEqual(conf.get(ConfigParameter.IMAP_PORT), EXPECTED_IMAP_PORT) + self.assertEqual(conf.get_int(ConfigParameter.IMAP_PORT), int(EXPECTED_IMAP_PORT)) + try: + conf.get_int(ConfigParameter.HTTP_PORT) + self.assertTrue(False) + except Exception: + pass + self.assertTrue(conf.get_bool(ConfigParameter.SMTP_STARTTLS)) + self.assertFalse(conf.get_bool(ConfigParameter.IMAP_SSL)) + try: + conf.get_bool(ConfigParameter.DB_URL) + self.assertTrue(False) + except Exception: + pass + + def test_put(self): + conf = self.conf() + self.assertFalse(conf.exists(ConfigParameter.IMAP_LOGIN)) + conf.put(ConfigParameter.IMAP_LOGIN, EXPECTED_IMAP_LOGIN) + self.assertTrue(conf.exists(ConfigParameter.IMAP_LOGIN)) + self.assertEqual(conf.get(ConfigParameter.IMAP_LOGIN), EXPECTED_IMAP_LOGIN) diff --git a/tests/test_form.py b/tests/test_form.py new file mode 100644 index 0000000..f388c6a --- /dev/null +++ b/tests/test_form.py @@ -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() diff --git a/tests/test_stacosys.py b/tests/test_stacosys.py index 94714f0..23b2aaa 100644 --- a/tests/test_stacosys.py +++ b/tests/test_stacosys.py @@ -1,5 +1,9 @@ +import unittest + from stacosys import __version__ -def test_version(): - assert __version__ == "2.0" +class StacosysTestCase(unittest.TestCase): + + def test_version(self): + self.assertEqual("2.0", __version__) diff --git a/tests/test_templater.py b/tests/test_templater.py index aad652b..117d19d 100644 --- a/tests/test_templater.py +++ b/tests/test_templater.py @@ -2,55 +2,51 @@ # -*- coding: UTF-8 -*- import os +import unittest from stacosys.core.templater import Templater, Template -def get_template_content(lang, template_name, **kwargs): - current_path = os.path.dirname(__file__) - template_path = os.path.abspath(os.path.join(current_path, "../stacosys/templates")) - templater = Templater(template_path) - template = templater.get_template(lang, template_name) - assert template - return template.render(kwargs) - - -def test_approve_comment(): - content = get_template_content("fr", Template.APPROVE_COMMENT, original="[texte]") - assert content.startswith("Bonjour,\n\nLe commentaire sera bientôt publié.") - assert content.endswith("[texte]") - content = get_template_content("en", Template.APPROVE_COMMENT, original="[texte]") - assert content.startswith("Hi,\n\nThe comment should be published soon.") - assert content.endswith("[texte]") - - -def test_drop_comment(): - content = get_template_content("fr", Template.DROP_COMMENT, original="[texte]") - assert content.startswith("Bonjour,\n\nLe commentaire ne sera pas publié.") - assert content.endswith("[texte]") - content = get_template_content("en", Template.DROP_COMMENT, original="[texte]") - assert content.startswith("Hi,\n\nThe comment will not be published.") - assert content.endswith("[texte]") - - -def test_new_comment(): - content = get_template_content("fr", Template.NEW_COMMENT, comment="[comment]") - assert content.startswith("Bonjour,\n\nUn nouveau commentaire a été posté") - assert content.endswith("[comment]\n\n--\nStacosys") - 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" +class TemplateTestCase(unittest.TestCase): + + def get_template_content(self, lang, template_name, **kwargs): + current_path = os.path.dirname(__file__) + template_path = os.path.abspath(os.path.join(current_path, "../stacosys/templates")) + template = Templater(template_path).get_template(lang, template_name) + 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_drop_comment(self): + content = self.get_template_content("fr", Template.DROP_COMMENT, original="[texte]") + self.assertTrue(content.startswith("Bonjour,\n\nLe commentaire ne sera pas publié.")) + self.assertTrue(content.endswith("[texte]")) + content = self.get_template_content("en", Template.DROP_COMMENT, original="[texte]") + self.assertTrue(content.startswith("Hi,\n\nThe comment will not be published.")) + 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_notify_message(self): + content = self.get_template_content("fr", Template.NOTIFY_MESSAGE) + self.assertEqual("Nouveau commentaire", content) + content = self.get_template_content("en", Template.NOTIFY_MESSAGE) + self.assertEqual("New comment", content) + + def test_rss_title(self): + content = self.get_template_content("fr", Template.RSS_TITLE_MESSAGE, site="[site]") + self.assertEqual("[site] : commentaires", content) + content = self.get_template_content("en", Template.RSS_TITLE_MESSAGE, site="[site]") + self.assertEqual("[site] : comments", content)