Skip to content

Commit

Permalink
fix: fixed tests for union_no_overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Jun 8, 2021
1 parent 6b08d43 commit 4ad9920
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 12 additions & 0 deletions aw_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def _log_request_exception(e: req.RequestException):
pass


def _dt_is_tzaware(dt: datetime) -> bool:
return dt.tzinfo is not None and dt.tzinfo.utcoffset(dt) is not None


def always_raise_for_request_errors(f: Callable[..., req.Response]):
@functools.wraps(f)
def g(*args, **kwargs):
Expand Down Expand Up @@ -301,6 +305,14 @@ def query(
params["name"] = name
params["cache"] = int(cache)

# Check that datetimes have timezone information
for start, stop in timeperiods:
try:
assert _dt_is_tzaware(start)
assert _dt_is_tzaware(stop)
except AssertionError:
raise ValueError("start/stop needs to have a timezone set")

data = {
"timeperiods": [
"/".join([start.isoformat(), end.isoformat()])
Expand Down
13 changes: 11 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python3
import time

from random import random
from datetime import datetime, timedelta, timezone
from requests.exceptions import HTTPError

import pytest

from aw_core.models import Event
from aw_client import ActivityWatchClient

Expand Down Expand Up @@ -66,11 +67,19 @@ def test_full():
assert eventcount == len(events)

result = client.query(
f"RETURN = query_bucket('{bucket_name}');",
f'RETURN = query_bucket("{bucket_name}");',
timeperiods=[(now - timedelta(hours=1), now + timedelta(hours=1))],
)
assert len(result) == 1
assert len(result[0]) == 3

# Test exception raising
with pytest.raises(ValueError):
# timeperiod end time does not have a timezone set
result = client.query(
f'RETURN = query_bucket("{bucket_name}");',
timeperiods=[(now - timedelta(hours=1), datetime.now())],
)

# Delete bucket
client.delete_bucket(bucket_name)

0 comments on commit 4ad9920

Please sign in to comment.