Coverage for tests/utils/gen_utils.py: 95%
61 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
1from typing import Union, Any, Optional
2from abc import ABC
3from time import sleep, time
4from datetime import datetime
5from os import makedirs
6from os.path import exists as path_exists
9class GenUtils(ABC):
10 """General test utilities."""
12 @classmethod
13 def create_dir_if_not_exists(cls, dir_path: str) -> None:
14 """Creates a directory if doesn't exists.
16 :param str dir_path: path of the directory to create.
17 """
18 if path_exists(dir_path):
19 return
21 makedirs(dir_path)
23 @classmethod
24 def wait_for(cls, milliseconds: int) -> None:
25 """Waits for specified time.
27 :param int milliseconds: milliseconds to wait for.
28 """
29 sleep(milliseconds / 1000)
31 @classmethod
32 def is_empty(cls, value: Union[str, list[Any], None]) -> bool:
33 """Check if a string or list is empty.
35 :param str | list[Any] | None value: value to check.
36 :returns bool: `True` if `value` is an empty string, `False` otherwise.
37 """
38 return value == ""
40 @classmethod
41 def test_unsigned_big_integer(cls, num: Any, non_zero: Optional[bool] = None) -> None:
42 """Test number is a unsigned big integer.
44 :param Any num: number to test.
45 :param bool | None non_zero: assert number is non zero.
46 """
47 assert num is not None, "Number is None"
48 assert isinstance(num, int), f"Value is not number: {num}"
49 assert num >= 0, "Value cannot be negative"
50 if non_zero is True:
51 assert num > 0, "Number is zero"
52 elif non_zero is False:
53 assert num == 0, f"Number is not zero: {num}"
55 @classmethod
56 def current_timestamp(cls) -> int:
57 """Gets current timestamp in milliseconds.
59 :returns int: current timestamp in milliseconds.
60 """
61 return round(time() * 1000)
63 @classmethod
64 def current_timestamp_str(cls) -> str:
65 """Gets current timestamp in milliseconds.
67 :returns str: current timestamp in milliseconds.
68 """
69 return f"{cls.current_timestamp()}"
71 @classmethod
72 def current_date_time_str(cls, fmt: str = "%Y-%m-%d_%H-%M-%S") -> str:
73 """Gets current date and time formatted as string, e.g. "2026-08-19_15-50-25".
75 :param str fmt: strftime format to use.
76 :returns str: current date and time formatted as string.
77 """
78 return datetime.now().strftime(fmt)
80 @classmethod
81 def has_key(cls, key: Optional[str], dictionary: dict[str, Any]) -> bool:
82 """Check if a dictionary has a given key.
84 :param str | None key: key to look for.
85 :param dict[str, Any] dictionary: dictionary to search.
86 :returns bool: `True` if `key` is a key of `dictionary`, `False` otherwise.
87 """
88 assert key is not None, "Key is None"
89 for k in dictionary:
90 if k == key:
91 return True
92 return False
94 @classmethod
95 def count_num_instances(cls, instances: list[int]) -> dict[int, int]:
96 """Count the number of occurrences of each value in a list.
98 :param list[int] instances: values to count occurrences of.
99 :returns dict[int, int]: map of each value to its number of occurrences in `instances`.
100 """
101 height_counts: dict[int, int] = {}
102 for inst in instances:
103 count: Optional[int] = height_counts.get(inst, None)
104 height_counts[inst] = 1 if count is None else count + 1
105 return height_counts
107 @classmethod
108 def get_modes(cls, counts: dict[int, int]) -> set[int]:
109 """Get the value(s) with the highest occurrence count.
111 :param dict[int, int] counts: map of each value to its number of occurrences, as returned
112 by `count_num_instances`.
113 :returns set[int]: the value(s) sharing the highest occurrence count.
114 """
115 modes: set[int] = set()
116 max_count: Optional[int] = None
117 for cnt in counts.values():
118 if max_count is None or cnt > max_count:
119 max_count = cnt
121 for entry in counts.items():
122 if entry[1] == max_count:
123 modes.add(entry[0])
125 return modes