Skip to content

Commit

Permalink
added test to check lib versions for development and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
grimmpp committed Mar 28, 2024
1 parent a7a2788 commit 516b320
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 27 deletions.
56 changes: 56 additions & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
@@ -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)
31 changes: 4 additions & 27 deletions tests/test_send_message_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 516b320

Please sign in to comment.