@@ -104,11 +104,52 @@ async def test_access_window_by_int_slice() -> None:
104104    async  with  window :
105105        await  push_logical_meter_data (sender , range (0 , 5 ))
106106        assert  np .array_equal (window [3 :5 ], np .array ([3.0 , 4.0 ]))
107-         with   pytest . raises ( IndexError ): 
108-              window . window ( 3 ,  5 )   # type: ignore 
107+         assert   np . array_equal ( window . window ( 3 ,  5 ),  np . array ([ 3.0 ,  4.0 ])) 
108+ 
109109        data  =  [1 , 2 , 2.5 , 1 , 1 , 1 , 2 , 2 , 1 , 1 , 1 , 1 , 1 , 1 ]
110110        await  push_logical_meter_data (sender , data )
111111        assert  np .array_equal (window [5 :14 ], np .array (data [5 :14 ]))
112+         assert  np .array_equal (window .window (5 , 14 ), np .array (data [5 :14 ]))
113+ 
114+     window , sender  =  init_moving_window (timedelta (seconds = 5 ))
115+ 
116+     def  test_eq (expected : list [float ], start : int  |  None , end : int  |  None ) ->  None :
117+         assert  np .allclose (
118+             window .window (start , end ), np .array (expected ), equal_nan = True 
119+         )
120+ 
121+     async  with  window :
122+         test_eq ([], 0 , 1 )
123+ 
124+         # Incomplete window 
125+         await  push_logical_meter_data (sender , [0.0 , 1.0 ])
126+         test_eq ([0.0 , 1.0 ], 0 , 2 )
127+         test_eq ([0.0 , 1.0 ], 0 , 9 )
128+         test_eq ([0.0 , 1.0 ], 0 , None )
129+         test_eq ([0.0 , 1.0 ], - 9 , None )
130+         test_eq ([0.0 , 1.0 ], None , None )
131+         test_eq ([0.0 ], - 2 , - 1 )
132+         test_eq ([1.0 ], - 1 , None )
133+ 
134+         # Incomplete window with gap 
135+         await  push_logical_meter_data (
136+             sender , [3.0 ], start_ts = UNIX_EPOCH  +  timedelta (seconds = 3 )
137+         )
138+         test_eq ([0.0 , 1.0 ], 0 , 2 )
139+         # gap fill not supported yet: 
140+         # test_eq([0.0, 1.0, np.nan, 3.0], 0, None) 
141+         # test_eq([0.0, 1.0, np.nan, 3.0], -9, None) 
142+         # test_eq([np.nan, 3.0], -2, None) 
143+ 
144+         # Complete window 
145+         await  push_logical_meter_data (sender , [0.0 , 1.0 , 2.0 , 3.0 , 4.0 ])
146+         test_eq ([0.0 , 1.0 ], 0 , 2 )
147+         test_eq ([3.0 , 4.0 ], - 2 , None )
148+ 
149+         # Complete window with nan 
150+         await  push_logical_meter_data (sender , [0.0 , 1.0 , np .nan ])
151+         test_eq ([0.0 , 1.0 , np .nan ], 0 , 3 )
152+         test_eq ([np .nan , 3.0 , 4.0 ], - 3 , None )
112153
113154
114155async  def  test_access_window_by_ts_slice () ->  None :
0 commit comments