mt5-manager/tests/test_services_ini.py
77 lines
3.7 KiB
Python
'''Проверки регистрации сервиса без удаления других сервисов терминала.'''
import tempfile
import unittest
from pathlib import Path
from deploy_service import services_ini_update, write_services_ini
class ServicesIniTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.root = Path(self.temp.name)
self.template = Path(__file__).resolve().parent.parent / 'static/services.ini'
def tearDown(self):
self.temp.cleanup()
def test_missing_configuration_is_created_as_utf16_and_is_idempotent(self):
target, text, changed = services_ini_update(self.root, self.template, 'custom.sqlite')
self.assertTrue(changed)
write_services_ini(target, text)
self.assertTrue(target.read_bytes().startswith(b'\xff\xfe'))
self.assertIn('enabled=1', text)
self.assertIn('path=Services\\Shared Projects\\mt5-manager\\ServiceStateWriter.ex5', text)
self.assertIn('InpDatabase=custom.sqlite', text)
self.assertNotIn('\r\r\n', target.read_bytes().decode('utf-16'))
self.assertFalse(services_ini_update(self.root, self.template, 'custom.sqlite')[2])
def test_repair_preserves_other_services_and_custom_parameters(self):
other = '<service>\nname=OtherService\npath=Services\\Other.ex5\nenabled=1\n</service>'
ours = self.template.read_text(encoding='utf-16').strip()
ours = ours.replace('enabled=1', 'enabled=0').replace('InpIntervalSec=2', 'InpIntervalSec=30')
ours = ours.replace('path=Services\\Shared Projects\\mt5-manager\\ServiceStateWriter.ex5',
'path=Services\\old-location.ex5')
target = self.root / 'config/services.ini'
target.parent.mkdir()
target.write_text(other + '\n' + ours + '\n', encoding='utf-16')
_, text, changed = services_ini_update(self.root, self.template, 'updated.sqlite')
self.assertTrue(changed)
self.assertIn(other, text)
self.assertIn('InpIntervalSec=30', text)
self.assertIn('InpDatabase=updated.sqlite', text)
self.assertIn('path=Services\\Shared Projects\\mt5-manager\\ServiceStateWriter.ex5', text)
self.assertNotIn('enabled=0', text)
def test_existing_unrelated_service_is_kept_when_adding_ours(self):
target = self.root / 'config/services.ini'
target.parent.mkdir()
other = '<service>\nname=Other\npath=Services\\Other.ex5\nenabled=1\n</service>\n'
target.write_text(other, encoding='utf-16')
_, text, changed = services_ini_update(self.root, self.template, 'custom.sqlite')
self.assertTrue(changed)
self.assertIn(other, text)
self.assertEqual(text.count('<service>'), 2)
def test_malformed_configuration_is_not_overwritten(self):
target = self.root / 'config/services.ini'
target.parent.mkdir()
target.write_text('<service>\nname=Other\n', encoding='utf-16')
original = target.read_bytes()
with self.assertRaises(ValueError):
services_ini_update(self.root, self.template, 'custom.sqlite')
self.assertEqual(target.read_bytes(), original)
def test_malformed_inputs_are_not_overwritten(self):
target = self.root / 'config/services.ini'
target.parent.mkdir()
target.write_text('<service>\nname=ServiceStateWriter\n<inputs>\n</service>', encoding='utf-16')
original = target.read_bytes()
with self.assertRaises(ValueError):
services_ini_update(self.root, self.template, 'custom.sqlite')
self.assertEqual(target.read_bytes(), original)
if __name__ == '__main__':
unittest.main()