Skip to content

Commit 287b621

Browse files
Merge pull request #440 from jakob-bagterp/fix/flaky-test-for-count-minutes-to-seconds
Fix: Flaky tests for rounding of floating point numbers
2 parents fbe4572 + 2da3139 commit 287b621

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

test/helper/time_fractions/count_minutes_to_seconds_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import math
2+
13
from _helper.time_fractions import random_minutes_as_ns
24

35
from timer.model.time_fractions import TimeFractions
@@ -9,4 +11,4 @@ def test_count_minutes_to_seconds() -> None:
911
mock_elapsed_time_ns = random_minutes_as_ns()
1012
rounded_seconds = round(TimeFractions(mock_elapsed_time_ns).count_minutes_to_seconds(), float_precision)
1113
rounded_mock_seconds = round(mock_elapsed_time_ns / 1000 / 1000 / 1000, float_precision)
12-
assert rounded_seconds == rounded_mock_seconds
14+
assert math.isclose(rounded_seconds, rounded_mock_seconds, rel_tol=1e-6)

test/helper/time_fractions/count_x_to_float_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import math
2+
13
from _helper.time_fractions import (random_microseconds_as_ns,
24
random_milliseconds_as_ns,
35
random_seconds_as_ns)
@@ -12,7 +14,7 @@ def test_count_microseconds_to_float() -> None:
1214
rounded_microseconds_to_float = round(TimeFractions(
1315
mock_elapsed_time_ns).count_microseconds_to_float(), float_precision)
1416
rounded_mock_microseconds_to_float = round(mock_elapsed_time_ns / 1000, float_precision)
15-
assert rounded_microseconds_to_float == rounded_mock_microseconds_to_float
17+
assert math.isclose(rounded_microseconds_to_float, rounded_mock_microseconds_to_float, rel_tol=1e-6)
1618

1719

1820
def test_count_milliseconds_to_float() -> None:
@@ -22,7 +24,7 @@ def test_count_milliseconds_to_float() -> None:
2224
rounded_milliseconds_to_float = round(TimeFractions(
2325
mock_elapsed_time_ns).count_milliseconds_to_float(), float_precision)
2426
rounded_mock_milliseconds_to_float = round(mock_elapsed_time_ns / 1000 / 1000, float_precision)
25-
assert rounded_milliseconds_to_float == rounded_mock_milliseconds_to_float
27+
assert math.isclose(rounded_milliseconds_to_float, rounded_mock_milliseconds_to_float, rel_tol=1e-9)
2628

2729

2830
def test_count_seconds_to_float() -> None:
@@ -31,4 +33,4 @@ def test_count_seconds_to_float() -> None:
3133
mock_elapsed_time_ns = random_seconds_as_ns()
3234
rounded_seconds_to_float = round(TimeFractions(mock_elapsed_time_ns).count_seconds_to_float(), float_precision)
3335
rounded_mock_seconds_to_float = round(mock_elapsed_time_ns / 1000 / 1000 / 1000, float_precision)
34-
assert rounded_seconds_to_float == rounded_mock_seconds_to_float
36+
assert math.isclose(rounded_seconds_to_float, rounded_mock_seconds_to_float, rel_tol=1e-12)

test/helper/time_fractions/seconds_rounded_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import math
2+
13
from _helper.time_fractions import random_seconds_as_ns
24

35
from timer.model.time_fractions import TimeFractions
@@ -8,4 +10,4 @@ def test_seconds_rounded() -> None:
810
mock_elapsed_time_ns = random_seconds_as_ns()
911
round_seconds = TimeFractions(mock_elapsed_time_ns).seconds_rounded()
1012
rounded_mock_seconds = round(mock_elapsed_time_ns / 1000 / 1000 / 1000, 0)
11-
assert round_seconds == rounded_mock_seconds
13+
assert math.isclose(round_seconds, rounded_mock_seconds, rel_tol=1e-9)

0 commit comments

Comments
 (0)