Add test for device metadata file creation and fixes until test passed.
This commit is contained in:
parent
a21312ee61
commit
266a669e5e
@ -2,6 +2,7 @@ import logging
|
|||||||
import sys
|
import sys
|
||||||
from logging.handlers import RotatingFileHandler
|
from logging.handlers import RotatingFileHandler
|
||||||
|
|
||||||
|
|
||||||
def setup_logging():
|
def setup_logging():
|
||||||
logger_obj = logging.getLogger('iottbLogger')
|
logger_obj = logging.getLogger('iottbLogger')
|
||||||
logger_obj.setLevel(logging.DEBUG)
|
logger_obj.setLevel(logging.DEBUG)
|
||||||
|
|||||||
@ -19,26 +19,36 @@ def setup_init_device_root_parser(subparsers):
|
|||||||
def handle_add(args):
|
def handle_add(args):
|
||||||
logger.info(f"Add device handler called with args {args}")
|
logger.info(f"Add device handler called with args {args}")
|
||||||
|
|
||||||
|
args.root_dir.mkdir(parents=True, exist_ok=True) # else metadata.save_to_file will fail TODO: unclear what to assume
|
||||||
|
|
||||||
if args.guided:
|
if args.guided:
|
||||||
logger.debug("Guided setup")
|
logger.debug("begin guided setup")
|
||||||
metadata = guided_setup(args.root_dir)
|
metadata = guided_setup(args.root_dir)
|
||||||
|
logger.debug("guided setup complete")
|
||||||
else:
|
else:
|
||||||
logger.debug("Setup through passed args: setup")
|
logger.debug("Setup through passed args: setup")
|
||||||
device_name = args.name
|
if not args.name:
|
||||||
args.root_dir.mkdir(parents=True, exist_ok=True)
|
logger.error("No device name specified with unguided setup.")
|
||||||
metadata = DeviceMetadata(device_name, args.root_dir)
|
return ReturnCodes.ERROR
|
||||||
|
metadata = DeviceMetadata(args.name, args.root_dir)
|
||||||
|
|
||||||
file_path = args.root_dir / DEVICE_METADATA_FILE
|
file_path = args.root_dir / DEVICE_METADATA_FILE
|
||||||
|
if file_path.exists():
|
||||||
response = input(f"Confirm device metadata: {metadata.to_json()} [y/N]")
|
print("Directory already contains a metadata file. Aborting.")
|
||||||
if response.lower() not in definitions.AFFIRMATIVE_USER_RESPONSE.add(""):
|
|
||||||
configure_metadata()
|
|
||||||
assert False, "TODO implement dynamic setup"
|
|
||||||
args.root_dir.mkdir(parents=True, exist_ok=True) # else metadata.save_to_file will fail TODO: unclear
|
|
||||||
if metadata.save_to_json(file_path) == ReturnCodes.FILE_ALREADY_EXISTS:
|
|
||||||
print("Directory already contains a device metadata file. Aborting operation.")
|
|
||||||
return ReturnCodes.ABORTED
|
return ReturnCodes.ABORTED
|
||||||
assert Path(file_path).exists(), f"{file_path} does not exist"
|
serialized_metadata = metadata.to_json()
|
||||||
|
response = input(f"Confirm device metadata: {serialized_metadata} [y/N]")
|
||||||
|
logger.debug(f"response: {response}")
|
||||||
|
if response not in definitions.AFFIRMATIVE_USER_RESPONSE:
|
||||||
|
print("Adding device aborted by user.")
|
||||||
|
return ReturnCodes.ABORTED
|
||||||
|
|
||||||
|
logger.debug(f"Device metadata file {file_path}")
|
||||||
|
if metadata.save_to_json(file_path) == ReturnCodes.FILE_ALREADY_EXISTS:
|
||||||
|
logger.error("File exists after checking, which should not happen.")
|
||||||
|
return ReturnCodes.ABORTED
|
||||||
|
|
||||||
|
print("Device metadata successfully created.")
|
||||||
return ReturnCodes.SUCCESS
|
return ReturnCodes.SUCCESS
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
37
code/tests/subcommands/test_add_device.py
Normal file
37
code/tests/subcommands/test_add_device.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
from io import StringIO
|
||||||
|
from unittest.mock import patch, MagicMock
|
||||||
|
from pathlib import Path
|
||||||
|
from iottb.definitions import DEVICE_METADATA_FILE
|
||||||
|
import shutil
|
||||||
|
from iottb.__main__ import main
|
||||||
|
|
||||||
|
|
||||||
|
class TestDeviceSetup(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.test_dir = Path("/tmp/iottbtest/test_add_device")
|
||||||
|
self.test_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
# self.captured_output = StringIO()
|
||||||
|
# sys.stdout = self.captured_output
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
# shutil.rmtree(str(self.test_dir))
|
||||||
|
for item in self.test_dir.iterdir():
|
||||||
|
if item.is_dir():
|
||||||
|
item.rmdir()
|
||||||
|
else:
|
||||||
|
item.unlink()
|
||||||
|
self.test_dir.rmdir()
|
||||||
|
# sys.stdout = sys.__stdout__
|
||||||
|
|
||||||
|
@patch("builtins.input", side_effect=["iPhone 14", "y", "y"])
|
||||||
|
def test_guided_device_setup(self, mock_input):
|
||||||
|
sys.argv = ['__main__.py', 'add', '--root_dir', str(self.test_dir), '--guided']
|
||||||
|
main()
|
||||||
|
expected_file = self.test_dir / DEVICE_METADATA_FILE
|
||||||
|
self.assertTrue(expected_file.exists()), f"Expected file not created: {expected_file}"
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Loading…
x
Reference in New Issue
Block a user