Skip to content

Commit

Permalink
refactor(usb_handle): improve timestamp handling and code organization
Browse files Browse the repository at this point in the history
- Move timestamp generation to start_process function
- Add timestamp to result string before creating status file
- Remove unnecessary blank lines
- Update test case to reflect new timestamp handling
  • Loading branch information
elkanamol committed Sep 8, 2024
1 parent d50f7a7 commit 0181074
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
6 changes: 2 additions & 4 deletions sierra_status/src/usb_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def send_at_command(
if "OK\r\n" in result or "ERROR\r\n" in result:
break
animate_spinner()

except serial.SerialException as e:
logging.error(f"Serial communication error: {e}")
except ValueError as e:
Expand Down Expand Up @@ -135,8 +134,6 @@ def creat_status_file(result: str, model: str) -> None:
"""
try:
time_stamp = time.strftime("%Y%m%d_%H%M%S", time.localtime())
result = f"Finished time: {time_stamp}\n" + result

file_name = STATUS_FILE_PATTERN.format(model=model, timestamp=time_stamp)
with open(file_name, "w") as f:
f.write(result)
Expand All @@ -162,7 +159,6 @@ def start_process(
None
"""
start_time = time.time()

logging.basicConfig(
level=log_level, format="%(asctime)s - %(levelname)s - %(message)s"
)
Expand All @@ -173,6 +169,8 @@ def start_process(
)
result = get_module_status(port, search, model, baudrate)
if result:
time_stamp = time.strftime("%Y%m%d_%H%M%S", time.localtime())
result = f"Finished time: {time_stamp}\n" + result
creat_status_file(result, model)
else:
logging.error("No result received from the module.")
Expand Down
7 changes: 5 additions & 2 deletions tests/test_usb_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ def test_creat_status_file(self, mock_strftime, mock_file) -> None:

@patch("sierra_status.src.usb_handle.get_module_status")
@patch("sierra_status.src.usb_handle.creat_status_file")
@patch("sierra_status.src.usb_handle.time.strftime")
def test_start_process_with_result(
self, mock_creat_status_file, mock_get_module_status
self, mock_strftime, mock_creat_status_file, mock_get_module_status
) -> None:
mock_get_module_status.return_value = "Test Status"
mock_strftime.return_value = "20230101_120000"
start_process(self.mock_port, "TestModel", logging.INFO, 0)
mock_creat_status_file.assert_called_with("Test Status", "TestModel")
expected_result = "Finished time: 20230101_120000\nTest Status"
mock_creat_status_file.assert_called_with(expected_result, "TestModel")

@patch("sierra_status.src.usb_handle.get_module_status")
@patch("sierra_status.src.usb_handle.creat_status_file")
Expand Down

0 comments on commit 0181074

Please sign in to comment.