From 9dd8a247238d573550a6fafcca899276cd723806 Mon Sep 17 00:00:00 2001 From: Dominik Krenn Date: Tue, 1 Jul 2025 09:39:51 +0200 Subject: [PATCH] test: add unit tests for Environment class and remove unused test file --- tests/env-test.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++ tests/test.py | 2 -- 2 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 tests/env-test.py delete mode 100644 tests/test.py diff --git a/tests/env-test.py b/tests/env-test.py new file mode 100644 index 0000000..82104eb --- /dev/null +++ b/tests/env-test.py @@ -0,0 +1,75 @@ +import unittest +import tempfile +from pathlib import Path +from multinut.env import Environment, Modes + +class TestEnvironment(unittest.TestCase): + def setUp(self): + # Create a temporary directory and file + self.temp_dir = tempfile.TemporaryDirectory() + self.base_path = Path(self.temp_dir.name) + self.default_file = self.base_path / ".env" + self.mode_file = self.base_path / ".env.testing" + self.named_file = self.base_path / "custom.env" + self.named_mode_file = self.base_path / "custom.production" + + for f in [self.default_file, self.mode_file, self.named_file, self.named_mode_file]: + f.write_text("KEY=value\n") + + def tearDown(self): + self.temp_dir.cleanup() + + def reset_environment_singleton(self): + Environment._instance = None + + def test_default_env(self): + print("here") + self.reset_environment_singleton() + env = Environment(env_file_path=str(self.base_path)) + self.assertTrue(str(self.default_file) in env.env_file_path) + + def test_with_named_file(self): + print("here") + self.reset_environment_singleton() + env = Environment(env_file_name="custom.env", env_file_path=str(self.base_path)) + self.assertTrue(str(self.named_file) in env.env_file_path) + + def test_with_mode_enum(self): + print("here") + self.reset_environment_singleton() + env = Environment(mode=Modes.TESTING, env_file_path=str(self.base_path)) + self.assertTrue(str(self.mode_file) in env.env_file_path) + + def test_with_mode_string(self): + print("here") + self.reset_environment_singleton() + env = Environment(mode="testing", env_file_path=str(self.base_path)) + self.assertTrue(str(self.mode_file) in env.env_file_path) + + def test_with_named_file_and_mode_enum(self): + print("here") + self.reset_environment_singleton() + env = Environment(env_file_name="custom", mode=Modes.PRODUCTION, env_file_path=str(self.base_path)) + self.assertTrue(str(self.named_mode_file) in env.env_file_path) + + def test_missing_file_raises(self): + self.reset_environment_singleton() + with self.assertRaises(FileNotFoundError): + Environment(env_file_name="notfound.env", env_file_path=str(self.base_path)) + + def test_missing_file_suppressed(self): + print("here") + self.reset_environment_singleton() + env = Environment(env_file_name="notfound.env", env_file_path=str(self.base_path), suppress_file_not_found=True) + self.assertIn("notfound.env", env.env_file_path) + + def test_singleton_behavior(self): + self.reset_environment_singleton() + a = Environment(env_file_path=str(self.base_path), suppress_file_not_found=True) + b = Environment(env_file_path="should/be/ignored", suppress_file_not_found=True) + self.assertIs(a, b) + self.assertEqual(a.env_file_path, b.env_file_path) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test.py b/tests/test.py deleted file mode 100644 index 04607df..0000000 --- a/tests/test.py +++ /dev/null @@ -1,2 +0,0 @@ -def say(): - print("lol")