1- import platform
21from datetime import datetime , timedelta , timezone
32from importlib import reload
43from types import ModuleType
4+ from typing import NoReturn
55from unittest .mock import patch
66
77import pytest
88
99
10- @pytest .fixture (params = ["windows" , "emscripten" , "linux" ])
10+ @pytest .fixture (params = [True , False ])
1111def imported_ts (request : pytest .FixtureRequest ) -> ModuleType :
12- with patch .object (platform , "system" , return_value = request .param ):
13- from dissect .util import ts
12+ from dissect .util import ts
13+
14+ if request .param :
15+
16+ class MockDatetime (datetime ):
17+ @classmethod
18+ def fromtimestamp (cls , * args , ** kwargs ) -> NoReturn :
19+ raise OverflowError ("Mock overflow error" )
1420
15- return reload (ts )
21+ with patch ("datetime.datetime" , MockDatetime ):
22+ return reload (ts )
23+
24+ return reload (ts )
1625
1726
1827@pytest .fixture
@@ -32,32 +41,32 @@ def test_now(ts: ModuleType) -> None:
3241 assert ts_now .tzinfo == timezone .utc
3342
3443
35- def test_unix_now (imported_ts : ModuleType ) -> None :
36- timestamp = imported_ts .unix_now ()
44+ def test_unix_now (ts : ModuleType ) -> None :
45+ timestamp = ts .unix_now ()
3746
3847 assert isinstance (timestamp , int )
3948 assert datetime .fromtimestamp (timestamp , tz = timezone .utc ).microsecond == 0
4049
4150
42- def test_unix_now_ms (imported_ts : ModuleType ) -> None :
43- timestamp = imported_ts .unix_now_ms ()
51+ def test_unix_now_ms (ts : ModuleType ) -> None :
52+ timestamp = ts .unix_now_ms ()
4453
4554 assert isinstance (timestamp , int )
46- assert imported_ts .from_unix_ms (timestamp ).microsecond == (timestamp % 1e3 ) * 1000
55+ assert ts .from_unix_ms (timestamp ).microsecond == (timestamp % 1e3 ) * 1000
4756
4857
49- def test_unix_now_us (imported_ts : ModuleType ) -> None :
50- timestamp = imported_ts .unix_now_us ()
58+ def test_unix_now_us (ts : ModuleType ) -> None :
59+ timestamp = ts .unix_now_us ()
5160
5261 assert isinstance (timestamp , int )
53- assert imported_ts .from_unix_us (timestamp ).microsecond == timestamp % 1e6
62+ assert ts .from_unix_us (timestamp ).microsecond == timestamp % 1e6
5463
5564
56- def test_unix_now_ns (imported_ts : ModuleType ) -> None :
57- timestamp = imported_ts .unix_now_ns ()
65+ def test_unix_now_ns (ts : ModuleType ) -> None :
66+ timestamp = ts .unix_now_ns ()
5867
5968 assert isinstance (timestamp , int )
60- assert imported_ts .from_unix_ns (timestamp ).microsecond == int ((timestamp // 1000 ) % 1e6 )
69+ assert ts .from_unix_ns (timestamp ).microsecond == int ((timestamp // 1000 ) % 1e6 )
6170
6271
6372def test_to_unix (ts : ModuleType ) -> None :
0 commit comments