File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -33,6 +33,10 @@ def _log_request_exception(e: req.RequestException):
3333 pass
3434
3535
36+ def _dt_is_tzaware (dt : datetime ) -> bool :
37+ return dt .tzinfo is not None and dt .tzinfo .utcoffset (dt ) is not None
38+
39+
3640def always_raise_for_request_errors (f : Callable [..., req .Response ]):
3741 @functools .wraps (f )
3842 def g (* args , ** kwargs ):
@@ -301,6 +305,14 @@ def query(
301305 params ["name" ] = name
302306 params ["cache" ] = int (cache )
303307
308+ # Check that datetimes have timezone information
309+ for start , stop in timeperiods :
310+ try :
311+ assert _dt_is_tzaware (start )
312+ assert _dt_is_tzaware (stop )
313+ except AssertionError :
314+ raise ValueError ("start/stop needs to have a timezone set" )
315+
304316 data = {
305317 "timeperiods" : [
306318 "/" .join ([start .isoformat (), end .isoformat ()])
Original file line number Diff line number Diff line change 11#!/usr/bin/env python3
22import time
3-
43from random import random
54from datetime import datetime , timedelta , timezone
65from requests .exceptions import HTTPError
76
7+ import pytest
8+
89from aw_core .models import Event
910from aw_client import ActivityWatchClient
1011
@@ -66,11 +67,19 @@ def test_full():
6667 assert eventcount == len (events )
6768
6869 result = client .query (
69- f" RETURN = query_bucket(' { bucket_name } ');" ,
70+ f' RETURN = query_bucket(" { bucket_name } ");' ,
7071 timeperiods = [(now - timedelta (hours = 1 ), now + timedelta (hours = 1 ))],
7172 )
7273 assert len (result ) == 1
7374 assert len (result [0 ]) == 3
7475
76+ # Test exception raising
77+ with pytest .raises (ValueError ):
78+ # timeperiod end time does not have a timezone set
79+ result = client .query (
80+ f'RETURN = query_bucket("{ bucket_name } ");' ,
81+ timeperiods = [(now - timedelta (hours = 1 ), datetime .now ())],
82+ )
83+
7584 # Delete bucket
7685 client .delete_bucket (bucket_name )
You can’t perform that action at this time.
0 commit comments