From 202c8cf1112bf59ce8dbec886af02c226c0f4b3e Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Thu, 22 May 2025 13:53:08 -0400 Subject: Add TestCase to Omni.Test Also improves the Example.py a bit with a real test case and example of how to use Omni.Test. --- Omni/Bild/Example.py | 16 +++++++++++++++- Omni/Llamacpp.py | 15 +++++---------- Omni/Test.py | 2 ++ 3 files changed, 22 insertions(+), 11 deletions(-) (limited to 'Omni') diff --git a/Omni/Bild/Example.py b/Omni/Bild/Example.py index 58e941a..7d57890 100755 --- a/Omni/Bild/Example.py +++ b/Omni/Bild/Example.py @@ -8,8 +8,13 @@ Example Python file that also serves as a test case for bild. # : out example # : dep cryptography import cryptography.fernet +import Omni.App as App +import Omni.Log as Log +import Omni.Test as Test import sys +log = Log.setup() + def cryptic_hello(name: str) -> str: """ @@ -23,6 +28,7 @@ def cryptic_hello(name: str) -> str: key = cryptography.fernet.Fernet.generate_key() f = cryptography.fernet.Fernet(key) token = f.encrypt(hello(name).encode("utf-8")) + log.info("attempting decryption") ret = f.decrypt(token).decode("utf-8") if ret != hello(name): msg = "en/decryption failed!" @@ -35,8 +41,16 @@ def hello(name: str) -> str: return f"Hello {name}" +class TestExample(Test.TestCase): + """Test the Example module.""" + + def test_hello(self) -> None: + """Test `hello` function.""" + self.assertEqual("Hello Ben", hello("Ben")) + + def main() -> None: """Entrypoint.""" if "test" in sys.argv: - sys.stdout.write("testing success") + Test.run(App.Area.Test, [TestExample]) sys.stdout.write(cryptic_hello("world")) diff --git a/Omni/Llamacpp.py b/Omni/Llamacpp.py index eac08fd..bf44d5e 100755 --- a/Omni/Llamacpp.py +++ b/Omni/Llamacpp.py @@ -10,6 +10,9 @@ sure llama-cpp still works in case I need/want to switch at some point. # : out llamacpp-test # : run llama-cpp +import Omni.App as App +import Omni.Log as Log +import Omni.Test as Test import os import sys import unittest @@ -23,18 +26,10 @@ class TestLlamaCpp(unittest.TestCase): self.assertIn("llama-cpp", os.environ.get("PATH", "")) -def test() -> None: - """Run this module's test suite.""" - suite = unittest.TestSuite() - suite.addTests( - unittest.defaultTestLoader.loadTestsFromTestCase(TestLlamaCpp), - ) - unittest.TextTestRunner().run(suite) - - def main() -> None: """Entrypoint.""" if sys.argv[1] == "test": - test() + Log.setup() + Test.run(App.Area.Test, [TestLlamaCpp]) else: sys.exit(0) diff --git a/Omni/Test.py b/Omni/Test.py index 495334a..57270f9 100644 --- a/Omni/Test.py +++ b/Omni/Test.py @@ -6,6 +6,8 @@ import Omni.Log as Log import typing import unittest +TestCase = unittest.TestCase + def run(area: App.Area, tests: list[typing.Any]) -> None: """Run the given tests with loglevel determined by area.""" -- cgit v1.2.3