Skip to content

Commit

Permalink
Test: Add tests for /locations/{id} endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fjugipe committed Feb 7, 2024
1 parent 334c49e commit 78744e8
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions api/test/test_edr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import json
from unittest.mock import patch

import datastore_pb2 as dstore
from fastapi.testclient import TestClient
from google.protobuf.json_format import Parse
from main import app


client = TestClient(app)


def test_get_locations_id_with_single_parameter_query_without_format():
with patch("routers.edr.getObsRequest") as mock_getObsRequest:
test_data = load_json("test/test_data/test_single_proto.json")
compare_data = load_json("test/test_data/test_single_covjson.json")

mock_getObsRequest.return_value = create_mock_obs_response(test_data)

response = client.get(
"/collections/observations/locations/06260?"
+ "parameter-name=ff&datetime=2022-12-31T00:00:00Z/2022-12-31T01:00:00Z"
)

# Check that getObsRequest gets called with correct arguments given in query
mock_getObsRequest.assert_called_once()
assert "ff" in mock_getObsRequest.call_args[0][0].filter["instrument"].values
assert "06260" in mock_getObsRequest.call_args[0][0].filter["platform"].values

assert response.status_code == 200
assert response.json()["type"] == "Coverage"
assert response.json() == compare_data


def test_get_locations_id_without_parameter_names_query():
with patch("routers.edr.getObsRequest") as mock_getObsRequest:
test_data = load_json("test/test_data/test_multiple_proto.json")
compare_data = load_json("test/test_data/test_multiple_covjson.json")

mock_getObsRequest.return_value = create_mock_obs_response(test_data)

response = client.get("/collections/observations/locations/06260?f=covjson")

# Check that getObsRequest gets called with correct arguments when no parameter names are given
# in query
mock_getObsRequest.assert_called_once()
assert "*" in mock_getObsRequest.call_args[0][0].filter["instrument"].values

assert response.status_code == 200
assert response.json() == compare_data


def test_get_locations_id_with_incorrect_datetime_format():
response = client.get("/collections/observations/locations/06260?datetime=20221231T000000Z/20221231T010000Z")

assert response.status_code == 422
assert response.json() == {"detail": {"datetime": "Invalid format: 20221231T000000Z/20221231T010000Z"}}


def test_get_locations_id_with_incorrect_datetime_range():
response = client.get(
"/collections/observations/locations/06260?datetime=2024-12-31T00:00:00Z/2022-12-31T01:00:00Z"
)

assert response.status_code == 422
assert response.json() == {"detail": {"datetime": "Invalid range: 2024-12-31T00:00:00Z > 2022-12-31T01:00:00Z"}}


def create_mock_obs_response(json_data):
response = dstore.GetObsResponse()
Parse(json.dumps(json_data), response)
return response


def load_json(file_path):
with open(file_path, "r") as file:
return json.load(file)

1 comment on commit 78744e8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API Unit Test Coverage Report
FileStmtsMissCoverMissing
\_\_init\_\_.py00100% 
datastore_pb2.py584621%23–68
datastore_pb2_grpc.py43430%2–225
dependencies.py31310%1–49
grpc_getter.py880%1–16
locustfile.py15150%1–31
main.py22220%3–51
metadata_endpoints.py19190%1–70
formatters
   \_\_init\_\_.py12650%16–32
   base_formatter.py7186%18
   covjson.py522944%32–33, 37–87, 92–101
routers
   \_\_init\_\_.py00100% 
   edr.py47470%2–126
   records.py00100% 
test
   test_covjson.py604623%13–38, 42–64, 68–89, 93–102, 106–108, 112–113
   test_edr.py44409%6–77
TOTAL41835316% 

API Unit Test Coverage Summary

Tests Skipped Failures Errors Time
1 0 💤 0 ❌ 1 🔥 1.501s ⏱️

Please sign in to comment.