Skip to content

Commit

Permalink
Webapp changes to handle the geometry location columns, unit test fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dajtxx committed Jun 21, 2024
1 parent 08520be commit c0ab05b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/python/api/client/DAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,9 +1062,9 @@ def get_physical_timeseries_message(start: datetime | None = None, end: datetime


def _msg_tuple_to_obj(ts: datetime, arg2: datetime | dict | None = None, arg3: datetime | dict | None = None) -> dict:
print(ts, arg2, arg3)
#print(ts, arg2, arg3)
if arg2 is None and arg3 is None:
return {BrokerConstants.TIMESTAMP_KEY: ts}
return {BrokerConstants.TIMESTAMP_KEY: ts.isoformat()}

if arg2 is not None and arg3 is None:
if isinstance(arg2, datetime):
Expand Down
6 changes: 5 additions & 1 deletion src/www/app/utils/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json, logging, os
import json, os
from typing import List
import requests
from datetime import datetime, timezone
Expand Down Expand Up @@ -254,6 +254,10 @@ def create_logical_device(physical_device: PhysicalDevice, token: str) ->str:
"location": physical_device.location,
}

# This is to work around a problem that turned up after the change to PostGIS.
if logicalJson['location'] is not None and (logicalJson['location'].lat is None or logicalJson['location'].long is None):
logicalJson.pop('location')

response = requests.post(f'{end_point}/broker/api/logical/devices/', json=logicalJson, headers=headers)
response.raise_for_status()

Expand Down
12 changes: 6 additions & 6 deletions test/python/test_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,15 +644,15 @@ def test_get_physical_timeseries_messages(self):
msg_ts.append(dateutil.parser.isoparse(msg[BrokerConstants.TIMESTAMP_KEY]))

msgs = dao.get_physical_timeseries_message(None, None, 1, only_timestamp=False, include_received_at=False, p_uid=new_pdev.uid)
print(msgs)
self.assertEqual(len(msgs), 1)
self.assertEqual(msgs[0]['ts'], msg_ts[0])
self.assertEqual(dateutil.parser.isoparse(msgs[0][BrokerConstants.TIMESTAMP_KEY]), msg_ts[0])

msgs = dao.get_physical_timeseries_message(None, None, None, only_timestamp=True, p_uid=new_pdev.uid)

self.assertEqual(len(msgs), len(msg_list))
for i, msg in enumerate(msgs):
self.assertEqual(msg['ts'], msg_ts[i])
print(msg)
self.assertEqual(dateutil.parser.isoparse(msg[BrokerConstants.TIMESTAMP_KEY]), msg_ts[i])

_, new_ldev = self._create_default_logical_device()
mapping = PhysicalToLogicalMapping(pd=new_pdev, ld=new_ldev, start_time=_now())
Expand All @@ -679,18 +679,18 @@ def test_get_physical_timeseries_messages(self):
# batch should be returned.
msgs = dao.get_physical_timeseries_message(only_timestamp=True, l_uid=new_ldev.uid)
self.assertEqual(len(msgs), 1)
self.assertEqual(msgs[0]['ts'], msg_ts[-1])
self.assertEqual(dateutil.parser.isoparse(msgs[0][BrokerConstants.TIMESTAMP_KEY]), msg_ts[-1])

# This will return all the messages because 'end' has been set past the latest message timestamp.
msgs = dao.get_physical_timeseries_message(end=now + datetime.timedelta(days=1), only_timestamp=True, l_uid=new_ldev.uid)
self.assertEqual(len(msgs), len(msg_list))
for i, msg in enumerate(msgs):
self.assertEqual(msg['ts'], msg_ts[i])
self.assertEqual(dateutil.parser.isoparse(msg[BrokerConstants.TIMESTAMP_KEY]), msg_ts[i])

# Should return only the latest message.
msgs = dao.get_physical_timeseries_message(end=now + datetime.timedelta(days=1), only_timestamp=True, count=1, l_uid=new_ldev.uid)
self.assertEqual(len(msgs), 1)
self.assertEqual(msgs[0]['ts'], msg_ts[0])
self.assertEqual(dateutil.parser.isoparse(msgs[0][BrokerConstants.TIMESTAMP_KEY]), msg_ts[0])

self.assertRaises(ValueError, dao.get_physical_timeseries_message)
self.assertRaises(TypeError, dao.get_physical_timeseries_message, p_uid='x')
Expand Down
19 changes: 10 additions & 9 deletions test/python/test_get_physical_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest
from fastapi.testclient import TestClient

import BrokerConstants
import test_utils as tu
import api.client.DAO as dao
from pdmodels.Models import PhysicalDevice
Expand Down Expand Up @@ -43,7 +44,7 @@ def create_msgs():
with open('/tmp/msgs.json', 'w') as f:
for i in range(0, max_msgs + 1):
timestamps.append(ts)
msg = {'ts': ts.isoformat(), 'i': i}
msg = {BrokerConstants.TIMESTAMP_KEY: ts.isoformat(), 'i': i}
msgs.append(msg)
s = f'{pd.uid}\t{ts.isoformat()}\t{json.dumps(msg)}'
print(s, file=f)
Expand Down Expand Up @@ -77,7 +78,7 @@ def test_no_params(test_client):
response = test_client.get(f'/broker/api/messages', params={'p_uid': pd.uid})
assert response.status_code == 200
for a, b in zip(response.json(), msgs[:-1]):
assert a['msg'] == b
assert a == b


def test_no_params_ts(test_client):
Expand All @@ -88,7 +89,7 @@ def test_no_params_ts(test_client):
if a is None:
break

assert dateutil.parser.isoparse(a['ts']) == b
assert dateutil.parser.isoparse(a[BrokerConstants.TIMESTAMP_KEY]) == b


def test_count(test_client):
Expand All @@ -97,7 +98,7 @@ def test_count(test_client):
assert response.status_code == 200
#assert response.json() == msgs[:50]
for a, b in zip(response.json(), msgs[:50]):
assert a['msg'] == b
assert a == b


def test_count_ts(test_client):
Expand All @@ -107,7 +108,7 @@ def test_count_ts(test_client):
if a is None:
break

assert dateutil.parser.isoparse(a['ts']) == b
assert dateutil.parser.isoparse(a[BrokerConstants.TIMESTAMP_KEY]) == b


def test_start_after_end(test_client):
Expand All @@ -125,7 +126,7 @@ def test_start_gives_gt(test_client):
# Confirm start time parameter gets the next message greater than, not greater than equal to.
response = test_client.get(f'/broker/api/messages', params={'p_uid': pd.uid, 'start': timestamps[1]})
assert response.status_code == 200
assert response.json()[0]['msg'] == msgs[0]
assert response.json()[0] == msgs[0]


def test_invalid_count(test_client):
Expand All @@ -148,19 +149,19 @@ def test_end(test_client):

response = test_client.get(f'/broker/api/messages', params={'p_uid': pd.uid, 'end': timestamps[-1]})
assert response.status_code == 200
assert response.json()[0]['msg'] == msgs[-1]
assert response.json()[0] == msgs[-1]

response = test_client.get(f'/broker/api/messages', params={'p_uid': pd.uid, 'end': timestamps[-9]})
assert response.status_code == 200
for a, b in zip(response.json(), msgs[-9:]):
assert a['msg'] == b
assert a == b


def test_start_end(test_client):
response = test_client.get(f'/broker/api/messages/', params={'p_uid': pd.uid, 'start': timestamps[9], 'end': timestamps[5]})
assert response.status_code == 200
for a, b in zip(response.json(), msgs[5:9]):
assert a['msg'] == b
assert a == b


def test_invalid_start_end(test_client):
Expand Down

0 comments on commit c0ab05b

Please sign in to comment.