Compare commits

..

5 Commits

Author SHA1 Message Date
John Lancaster
81da8be655 added another test for the child being modified 2024-08-14 00:36:05 -05:00
John Lancaster
a0add54445 refactored to use new function 2024-08-14 00:35:55 -05:00
John Lancaster
6c50657e5b added some utils functions 2024-08-14 00:35:41 -05:00
John Lancaster
7bb5d1debe removed change 2024-08-14 00:12:33 -05:00
John Lancaster
4547668c2b added family folder 2024-08-14 00:11:53 -05:00
8 changed files with 84 additions and 4 deletions

View File

@@ -5,3 +5,5 @@ hello_world:
simple_app: simple_app:
module: simple module: simple
class: SimpleApp class: SimpleApp
dependencies:
- hello_world

View File

@@ -0,0 +1,15 @@
child:
module: child
class: Child
parent:
module: parent
class: Parent
dependencies:
- child
grand-parent:
module: grand_parent
class: GrandParent
dependencies:
- parent

View File

@@ -0,0 +1,5 @@
from appdaemon.adapi import ADAPI
class Child(ADAPI):
def initialize(self):
self.log(f"{self.__class__.__name__} Initialized")

View File

@@ -0,0 +1,5 @@
from appdaemon.adapi import ADAPI
class GrandParent(ADAPI):
def initialize(self):
self.log(f"{self.__class__.__name__} Initialized")

View File

@@ -0,0 +1,5 @@
from appdaemon.adapi import ADAPI
class Parent(ADAPI):
def initialize(self):
self.log(f"{self.__class__.__name__} Initialized")

View File

@@ -3,12 +3,15 @@ import logging
import re import re
from pathlib import Path from pathlib import Path
# import child
import food.menu import food.menu
import pytest import pytest
from appdaemon.appdaemon import AppDaemon from appdaemon.appdaemon import AppDaemon
from git import Repo from git import Repo
from .utils import get_load_order from .utils import count_error_lines, get_app_orders, get_load_order, get_loaded_apps
INDENT = ' ' * 4
def reset_file(repo: Repo, changed: Path): def reset_file(repo: Repo, changed: Path):
@@ -67,3 +70,25 @@ def test_file_with_error(ad: AppDaemon, caplog: pytest.LogCaptureFixture, config
assert "Started 'my_restaurant'" in caplog.text assert "Started 'my_restaurant'" in caplog.text
finally: finally:
reset_file(config_repo, module_file) reset_file(config_repo, module_file)
def test_modification_child(ad: AppDaemon, caplog: pytest.LogCaptureFixture, config_repo: Repo):
assert isinstance(ad, AppDaemon)
module_file = Path(__file__).parents[2] / 'conf/apps/family/child.py'
file_content = module_file.read_text()
file_content += (INDENT * 2) + "self.log(f'Modified {self.__class__.__name__}')\n"
module_file.write_text(file_content)
try:
with caplog.at_level(logging.DEBUG):
ad.loop.run_until_complete(asyncio.sleep(1.0))
assert count_error_lines(caplog) == 0
assert get_loaded_apps(caplog) == {'child'}
assert get_app_orders(caplog, 'stop') == ['child', 'parent', 'grand-parent']
assert get_app_orders(caplog, 'start') == ['child', 'parent', 'grand-parent']
finally:
reset_file(config_repo, module_file)
ad.loop.run_until_complete(asyncio.sleep(1.0))

View File

@@ -5,7 +5,7 @@ from typing import List
import pytest import pytest
from appdaemon.appdaemon import AppDaemon from appdaemon.appdaemon import AppDaemon
from .utils import get_load_order from .utils import get_load_order, count_error_lines
def validate_app_dependencies(ad: AppDaemon): def validate_app_dependencies(ad: AppDaemon):
@@ -41,8 +41,7 @@ def test_startup(ad: AppDaemon, caplog: pytest.LogCaptureFixture):
with caplog.at_level(logging.DEBUG): with caplog.at_level(logging.DEBUG):
ad.loop.run_until_complete(asyncio.sleep(2.0)) ad.loop.run_until_complete(asyncio.sleep(2.0))
error_log_lines = [msg for name, lvl, msg in caplog.record_tuples if name.startswith('Error')] assert count_error_lines(caplog) == 0
assert len(error_log_lines) == 0
assert "Started 'hello_world'" in caplog.text assert "Started 'hello_world'" in caplog.text
assert 'App initialization complete' in caplog.text assert 'App initialization complete' in caplog.text

View File

@@ -1,4 +1,5 @@
import asyncio import asyncio
import re
from typing import List from typing import List
import pytest import pytest
@@ -15,3 +16,26 @@ def get_load_order(caplog: pytest.LogCaptureFixture) -> List[str]:
if record.name == 'AppDaemon._app_management': if record.name == 'AppDaemon._app_management':
if 'Determined module load order' in record.msg: if 'Determined module load order' in record.msg:
return record.args[0] return record.args[0]
def get_logger_records(caplog: pytest.LogCaptureFixture, name: str):
for record in caplog.records:
if record.name == name:
yield record
def get_app_orders(caplog: pytest.LogCaptureFixture, phase: str) -> List[str]:
for record in get_logger_records(caplog, 'AppDaemon._app_management'):
if re.search(f'App {phase} order', record.msg):
return record.args[0]
def get_loaded_apps(caplog: pytest.LogCaptureFixture):
for record in get_logger_records(caplog, 'AppDaemon._app_management'):
if re.search('Apps to be loaded', record.msg):
return record.args[0]
def count_error_lines(caplog: pytest.LogCaptureFixture) -> int:
error_log_lines = [msg for name, lvl, msg in caplog.record_tuples if name.startswith('Error')]
return len(error_log_lines)