]> git.ipfire.org Git - pakfire.git/blobdiff - tests/python/ctx.py
tests: Add Python tests for context
[pakfire.git] / tests / python / ctx.py
diff --git a/tests/python/ctx.py b/tests/python/ctx.py
new file mode 100755 (executable)
index 0000000..b7e1126
--- /dev/null
@@ -0,0 +1,78 @@
+#!/usr/bin/python3
+###############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2024 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+import logging
+import pakfire
+
+import tests
+
+class CtxTests(tests.TestCase):
+       """
+               This tests the pakfire context
+       """
+       def test_create(self):
+               """
+                       Create a context without any further ado
+               """
+               ctx = self.setup_ctx()
+
+               self.assertIsInstance(ctx, pakfire.Ctx)
+
+       # Logging
+
+       def test_log_default(self):
+               """
+                       Tests if there is any log output if the default logger is being used
+               """
+               logger = logging.getLogger("pakfire")
+
+               with self.assertLogs(logger, logging.DEBUG):
+                       self.setup_pakfire()
+
+       def test_log_custom(self):
+               """
+                       Tests if log messages can be received using a custom logger
+               """
+               # A simple logger that stores everything it receives in a buffer
+               class Logger(object):
+                       def __init__(self):
+                               self.buffer = []
+
+                       def log(self, *args):
+                               self.buffer.append(args)
+
+               logger = Logger()
+
+               # Setup a new context
+               ctx = self.setup_ctx()
+
+               # Configure the logger
+               ctx.set_logger(logger)
+
+               # Create a new Pakfire instance to create some log messages
+               self.setup_pakfire(ctx=ctx)
+
+               # Check if we have received anything
+               self.assertTrue(logger.buffer)
+
+
+if __name__ == "__main__":
+       tests.main()