From 469b8215fdb0edb9179ef4e899e506b318202dfe Mon Sep 17 00:00:00 2001
From: David Hoese <david.hoese@ssec.wisc.edu>
Date: Thu, 2 Mar 2023 15:54:35 -0600
Subject: [PATCH] Add more tests for error conditions in data API

---
 metobsapi/data_api.py            | 15 ++-------------
 metobsapi/tests/test_data_api.py | 30 ++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/metobsapi/data_api.py b/metobsapi/data_api.py
index fea0677..52ca64a 100644
--- a/metobsapi/data_api.py
+++ b/metobsapi/data_api.py
@@ -29,10 +29,6 @@ ROUNDING = {
 }
 
 
-def round_value(value, symbol):
-    return np.round(value, ROUNDING.get(symbol, 1))
-
-
 def handle_date(date):
     try:
         date_len = len(date)
@@ -68,7 +64,6 @@ def handle_symbols(symbols: list[str]):
 
         try:
             site, inst, s = symbol.split(".")
-            si = (site, inst)
         except ValueError:
             raise ValueError("Symbols must have 3 period-separated parts: {}".format(symbol))
 
@@ -83,10 +78,10 @@ def handle_symbols(symbols: list[str]):
         influx_symbols.setdefault((site, inst), []).append(influx_name)
 
     # Add the symbols needed to compute the wind_speed and wind_direction
-    for si in add_winds:
+    for site, inst in add_winds:
         influx_to_requested_symbols[f"{site}.{inst}.wind_east"] = None
         influx_to_requested_symbols[f"{site}.{inst}.wind_north"] = None
-        influx_symbols[si].extend(("wind_east", "wind_north"))
+        influx_symbols[(site, inst)].extend(("wind_east", "wind_north"))
 
     return influx_to_requested_symbols, influx_symbols
 
@@ -165,12 +160,6 @@ def calc_num_records(begin, end, interval):
     return diff / data_responses.INTERVALS[interval]
 
 
-def calc_file_size(num_records, num_streams):
-    """Get number of bytes returned for a text based format."""
-    # estimate about 7 bytes (overhead + data characters) per data point
-    return num_records * num_streams * 7.0
-
-
 def handle_csv(frame, epoch, sep=",", message="", code=200, status="success", **kwargs):
     output = """# status: {status}
 # code: {code:d}
diff --git a/metobsapi/tests/test_data_api.py b/metobsapi/tests/test_data_api.py
index 7e6f2ef..744a6ef 100644
--- a/metobsapi/tests/test_data_api.py
+++ b/metobsapi/tests/test_data_api.py
@@ -234,6 +234,36 @@ class TestDataAPI:
         assert res["status"] == "error"
         assert "'symbols'" in res["message"]
 
+    def test_nonexistent_symbol(self, client):
+        res = client.get("/api/data.json?begin=-05:00:00&symbols=aoss.tower.air_temp:aoss.tower.noexist")
+        res = json.loads(res.data.decode())
+        assert res["code"] == 400
+        assert res["status"] == "error"
+        assert "Unknown symbol" in res["message"]
+
+    def test_bad_symbol_site_inst(self, client):
+        res = client.get("/api/data.json?begin=-05:00:00&symbols=aoss.tower.something.air_temp")
+        res = json.loads(res.data.decode())
+        assert res["code"] == 400
+        assert res["status"] == "error"
+        assert "3 period-separated parts" in res["message"]
+
+    def test_symbol_unknown_site_inst(self, client):
+        res = client.get("/api/data.json?begin=-05:00:00&symbols=aoss2.tower.air_temp")
+        res = json.loads(res.data.decode())
+        assert res["code"] == 400
+        assert res["status"] == "error"
+        assert "Unknown site/instrument" in res["message"]
+
+    def test_multiple_symbol(self, client):
+        res = client.get(
+            "/api/data.json?begin=-05:00:00&symbols=aoss.tower.air_temp:aoss.tower.wind_speed:aoss.tower.air_temp"
+        )
+        res = json.loads(res.data.decode())
+        assert res["code"] == 400
+        assert res["status"] == "error"
+        assert "multiple times" in res["message"]
+
     def test_too_many_points(self, client):
         res = client.get("/api/data.json?symbols=aoss.tower.air_temp&begin=1970-01-01T00:00:00")
         assert res.status_code == 413
-- 
GitLab