From cca5917014a1164336fba451a1f4f645dabb3012 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 23 Sep 2026 15:03:40 +0000 Subject: [PATCH] test(nmea): add comprehensive unit tests for Znt.pretty() - Add test_znt_pretty_decoded_exact_format for exact output verification - Add test_znt_pretty_from_get_status for get_status initialization - Add test_znt_pretty_missing_param_key_error for missing keys handling --- tests/test_nmea/test_znt.py | 45 ++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/tests/test_nmea/test_znt.py b/tests/test_nmea/test_znt.py index 69d89a8..7cb7ba6 100644 --- a/tests/test_nmea/test_znt.py +++ b/tests/test_nmea/test_znt.py @@ -122,16 +122,49 @@ def test_znt_get_status(mock_ref_id_to_text): assert z.nmea_str.endswith(f"*{znt.checksum_str(z.nmea_str)}") -def test_znt_pretty(): +def test_znt_pretty_decoded_exact_format(): nmea_str = "$PNTZNT,1270567048.57,127.0.0.1,17.151.16.21,4,1270565749.41,0.000080,-20,0.117325,0.046249*14" z = znt.Znt(nmea_str=nmea_str) pretty_str = z.pretty() - assert "ZNT - NMEA Proprietary NTP status report" in pretty_str - assert "talker: NT" in pretty_str - assert "timestamp: 1270567048.57" in pretty_str - assert "stratum: 4" in pretty_str - assert "precision: -20.0" in pretty_str + expected = ( + "ZNT - NMEA Proprietary NTP status report\n\n" + " talker: NT\n" + " timestamp: 1270567048.57\n" + " host: 127.0.0.1\n" + " ref_clock: 17.151.16.21\n" + " stratum: 4\n" + " last_update: 1270565749.41\n" + " offset: 8e-05\n" + " precision: -20.0\n" + " root_delay: 0.117325\n" + " root_dispersion: 0.046249" + ) + assert pretty_str == expected + + +@mock.patch("ntplib.NTPClient", MockNTPClient) +@mock.patch("ntplib.ref_id_to_text") +def test_znt_pretty_from_get_status(mock_ref_id_to_text): + mock_ref_id_to_text.return_value = "17.151.16.21" + z = znt.Znt(hostname="127.0.0.1") + pretty_str = z.pretty() + + lines = pretty_str.split("\n") + assert lines[0] == "ZNT - NMEA Proprietary NTP status report" + assert lines[1] == "" + assert " talker: PNT" in lines[2] + assert " host: 127.0.0.1" in lines[4] + assert " ref_clock: 17.151.16.21" in lines[5] + assert " stratum: 2" in lines[6] + + +def test_znt_pretty_missing_param_key_error(): + z = znt.Znt.__new__(znt.Znt) + z.params = {"talker": "NT"} + with pytest.raises(KeyError) as exc_info: + z.pretty() + assert "timestamp" in str(exc_info.value) def test_znt_logger_will_write():