diff --git a/.gitignore b/.gitignore index 778641b..0b2e12e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ spotiplayer_pi/__pycache__ +tests/__pycache__ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e2aecde..e4dad02 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,3 +6,12 @@ repos: args: ["--ignore=E711,E721"] # args: ["--fix"] # Automatically apply fixes where possible - id: ruff-format + +repos: + - repo: local + hooks: + - id: run-unittest + name: Run Unit Tests + entry: bash -c "python -m unittest discover -s tests -p '*_test.py'" + language: system + always_run: true diff --git a/tests/test_app.py b/tests/test_app.py new file mode 100644 index 0000000..974841e --- /dev/null +++ b/tests/test_app.py @@ -0,0 +1,54 @@ +import unittest +from spotiplayer_pi.main import parse_config +from spotiplayer_pi.api import Api + + +class TestApp(unittest.TestCase): + def test_config(self): + self.cfg = parse_config() + + # Assertions for config structure and values + self.assertIsNotNone(self.cfg["refresh_token"]) + self.assertIsNotNone(self.cfg["base_64"]) + self.assertIsInstance(self.cfg["api_interval"], (int, float)) + self.assertGreater(self.cfg["api_interval"], 0) + self.assertIsInstance(self.cfg["refresh_interval"], (int, float)) + self.assertGreater(self.cfg["refresh_interval"], 0) + self.assertIn("color_theme", self.cfg) + + # Check color_theme structure + color_theme = self.cfg["color_theme"] + self.assertIsInstance(color_theme, dict) + for key in ["text", "bar_outline", "bar_inside", "background"]: + self.assertIn(key, color_theme) + self.assertIsInstance(color_theme[key], tuple) + self.assertEqual(len(color_theme[key]), 3) + self.assertTrue( + all(isinstance(c, int) and 0 <= c <= 255 for c in color_theme[key]) + ) + + def test_refreshAuth(self): + self.cfg = parse_config() + self.api = Api(self.cfg["refresh_token"], self.cfg["base_64"]) + expires_in = self.api.refreshAuth() + + self.assertIsNotNone(self.api.access_token) + self.assertIsNotNone(expires_in) + self.assertGreater(expires_in, 0) + self.assertIn("Authorization", self.api.header) + + def test_getPlaying(self): + self.cfg = parse_config() + self.api = Api(self.cfg["refresh_token"], self.cfg["base_64"]) + self.api.refreshAuth() + + result = self.api.getPlaying() + if result != "not-playing": + self.assertIsNotNone(result) + self.assertIn("track", result) + self.assertIn("album", result) + self.assertIn("artists", result) + + +if __name__ == "__main__": + unittest.main()