@@ -66,6 +66,18 @@ def init_moving_window(
6666    return  window , lm_tx 
6767
6868
69+ def  dt (i : int ) ->  datetime :  # pylint: disable=invalid-name 
70+     """Create datetime objects from indices. 
71+ 
72+     Args: 
73+         i: Index to create datetime from. 
74+ 
75+     Returns: 
76+         Datetime object. 
77+     """ 
78+     return  datetime .fromtimestamp (i , tz = timezone .utc )
79+ 
80+ 
6981async  def  test_access_window_by_index () ->  None :
7082    """Test indexing a window by integer index.""" 
7183    window , sender  =  init_moving_window (timedelta (seconds = 1 ))
@@ -92,7 +104,8 @@ async def test_access_window_by_int_slice() -> None:
92104    async  with  window :
93105        await  push_logical_meter_data (sender , range (0 , 5 ))
94106        assert  np .array_equal (window [3 :5 ], np .array ([3.0 , 4.0 ]))
95- 
107+         with  pytest .raises (IndexError ):
108+             window .window (3 , 5 )  # type: ignore 
96109        data  =  [1 , 2 , 2.5 , 1 , 1 , 1 , 2 , 2 , 1 , 1 , 1 , 1 , 1 , 1 ]
97110        await  push_logical_meter_data (sender , data )
98111        assert  np .array_equal (window [5 :14 ], np .array (data [5 :14 ]))
@@ -106,6 +119,15 @@ async def test_access_window_by_ts_slice() -> None:
106119        time_start  =  UNIX_EPOCH  +  timedelta (seconds = 3 )
107120        time_end  =  time_start  +  timedelta (seconds = 2 )
108121        assert  np .array_equal (window [time_start :time_end ], np .array ([3.0 , 4.0 ]))  # type: ignore 
122+         assert  np .array_equal (window .window (dt (3 ), dt (5 )), np .array ([3.0 , 4.0 ]))
123+         assert  np .array_equal (window .window (dt (3 ), dt (3 )), np .array ([]))
124+         # Window only supports slicing with ascending indices within allowed range 
125+         with  pytest .raises (IndexError ):
126+             window .window (dt (3 ), dt (1 ))
127+         with  pytest .raises (IndexError ):
128+             window .window (dt (3 ), dt (6 ))
129+         with  pytest .raises (IndexError ):
130+             window .window (dt (- 1 ), dt (5 ))
109131
110132
111133async  def  test_access_empty_window () ->  None :
0 commit comments