diff --git a/tests/test_metadata.py b/tests/test_metadata.py new file mode 100644 index 00000000..23896706 --- /dev/null +++ b/tests/test_metadata.py @@ -0,0 +1,56 @@ +import os +import sys +import unittest +import json + +class MetadataTest(unittest.TestCase): + + def get_installed_lib_version(self, lib_name:str): + dirs = [f for f in os.listdir(os.path.join(sys.exec_prefix, 'Lib', 'site-packages')) if f.startswith(lib_name.replace('-', '_')+'-')] + if len(dirs) == 1: + metadata_file = os.path.join(sys.exec_prefix, 'Lib', 'site-packages', dirs[0], 'METADATA') + with open(metadata_file, 'r') as f: + for l in f.readlines(): + if l.startswith('Version: '): + return l.replace('Version: ', '').strip() + return None + return None + + def get_version_of_installed_eltako14bus(self): + return self.get_installed_lib_version('eltako14bus') + + def get_manifest(self): + manifest_filename = os.path.join(os.getcwd(), 'custom_components', 'eltako', 'manifest.json') + with open(manifest_filename, 'r') as f: + return json.loads( f.read() ) + + def get_version_of_required_eltako14bus(self): + manifest = self.get_manifest() + + for r in manifest['requirements']: + if r.startswith('eltako14bus'): + return r.split('==')[1].strip() + return None + + def test_check_all_installed_dependencies(self): + manifest = self.get_manifest() + + for r in manifest['requirements']: + if '==' in r: + lib_name = r.split('==')[0].strip() + required_version = r.split('==')[1].strip() + installed_version = self.get_installed_lib_version(lib_name) + + # if this test fails install specified libraries in manifest.json + self.assertEqual(required_version, installed_version) + + + return None + + def test_eltako14bus_required_and_installed_is_the_same(self): + installed = self.get_version_of_installed_eltako14bus() + required = self.get_version_of_required_eltako14bus() + + self.assertIsNotNone(installed) + self.assertIsNotNone(required) + self.assertEqual(installed, required) \ No newline at end of file diff --git a/tests/test_send_message_service.py b/tests/test_send_message_service.py index a7745aa5..35d5f06c 100644 --- a/tests/test_send_message_service.py +++ b/tests/test_send_message_service.py @@ -7,6 +7,8 @@ import os import json +from tests.test_metadata import MetadataTest + dispatcher.dispatcher_send = mock.Mock(return_value=None) class TestSendMessageService(unittest.IsolatedAsyncioTestCase): @@ -48,38 +50,13 @@ async def test_send_message(self): await g.async_service_send_message(event, True) - def get_version_of_installed_eltako14bus(self): - dirname = [f for f in os.listdir(os.path.join(sys.exec_prefix, 'Lib', 'site-packages')) if f.startswith('eltako14bus')][0] - metadata_file = os.path.join(sys.exec_prefix, 'Lib', 'site-packages', dirname, 'METADATA') - with open(metadata_file, 'r') as f: - for l in f.readlines(): - if l.startswith('Version: '): - return l.replace('Version: ', '').strip() - return None - - def get_version_of_required_eltako14bus(self): - manifest_filename = os.path.join(os.getcwd(), 'custom_components', 'eltako', 'manifest.json') - with open(manifest_filename, 'r') as f: - manifest = json.loads( f.read() ) - - for r in manifest['requirements']: - if r.startswith('eltako14bus'): - return r.split('==')[1].strip() - return None - - async def test_eltako14bus_required_and_installed_is_the_same(self): - installed = self.get_version_of_installed_eltako14bus() - required = self.get_version_of_required_eltako14bus() - - self.assertIsNotNone(installed) - self.assertIsNotNone(required) - self.assertEqual(installed, required) async def test_write_eep_params_to_docs_file(self): text = '# Paramters for EEPs in Send Message Events \n' + eltakobus_version = MetadataTest().get_version_of_installed_eltako14bus() text += f'(This file was auto-generated by using [eltako14bus library](https://github.com/grimmpp/eltako14bus/blob/master/eltakobus/eep.py) ' - text += f'in [version {self.get_version_of_installed_eltako14bus()}](https://pypi.org/project/eltako14bus/).)\n' + text += f'in [version {eltakobus_version}](https://pypi.org/project/eltako14bus/).)\n' text += '\n' text += "## Not Supported EEPs \n"