Skip to content

Commit

Permalink
Python tests: fix compare_time, add test (#27457)
Browse files Browse the repository at this point in the history
* Python tests: fix compare_time, add test

* Restyled by isort

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Sep 7, 2023
1 parent 84331fe commit 1397272
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
33 changes: 30 additions & 3 deletions src/python_testing/TestMatterTestingSupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import chip.clusters as Clusters
from chip.clusters.Types import Nullable, NullValue
from chip.tlv import uint
from matter_testing_support import (MatterBaseTest, async_test_body, default_matter_test_main, parse_pics, type_matches,
utc_time_in_matter_epoch)
from mobly import asserts
from matter_testing_support import (MatterBaseTest, async_test_body, compare_time, default_matter_test_main, parse_pics,
type_matches, utc_time_in_matter_epoch)
from mobly import asserts, signals


def get_raw_type_list():
Expand Down Expand Up @@ -157,6 +157,33 @@ async def test_pics_support(self):
except ValueError:
pass

def test_time_compare_function(self):
# only offset, exact match
compare_time(received=1000, offset=timedelta(microseconds=1000), utc=0, tolerance=timedelta())
# only utc, exact match
compare_time(received=1000, offset=timedelta(), utc=1000, tolerance=timedelta())
# both, exact match
compare_time(received=2000, offset=timedelta(microseconds=1000), utc=1000, tolerance=timedelta())
# both, negative offset
compare_time(received=0, offset=timedelta(microseconds=-1000), utc=1000, tolerance=timedelta())

# Exact match, within delta, both
compare_time(received=2000, offset=timedelta(microseconds=1000), utc=1000, tolerance=timedelta(seconds=5))

# Just inside tolerance
compare_time(received=1001, offset=timedelta(), utc=2000, tolerance=timedelta(microseconds=1000))

# Just outside tolerance
try:
compare_time(received=999, offset=timedelta(), utc=2000, tolerance=timedelta(microseconds=1000))
asserts.fail("Expected failure case for time just outside of the tolerance failed")
except signals.TestFailure:
pass

# everything in the seconds range
compare_time(received=timedelta(seconds=3600).total_seconds() * 1000000,
offset=timedelta(seconds=3605), utc=0, tolerance=timedelta(seconds=5))


if __name__ == "__main__":
default_matter_test_main()
5 changes: 3 additions & 2 deletions src/python_testing/matter_testing_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,11 @@ def compare_time(received: int, offset: timedelta = timedelta(), utc: int = None
if utc is None:
utc = utc_time_in_matter_epoch()

expected = utc + offset.microseconds
# total seconds includes fractional for microseconds
expected = utc + offset.total_seconds()*1000000
delta_us = abs(expected - received)
delta = timedelta(microseconds=delta_us)
asserts.assert_less(delta, tolerance, "Received time is out of tolerance")
asserts.assert_less_equal(delta, tolerance, "Received time is out of tolerance")


@dataclass
Expand Down

0 comments on commit 1397272

Please sign in to comment.