From 7b394bcb68e93dd6a6d657a0bd1eb2e7dec1613f Mon Sep 17 00:00:00 2001 From: PThorpe92 Date: Fri, 9 Aug 2024 22:57:12 -0400 Subject: [PATCH] feat: add test python script to build against --- .gitignore | 1 + README.md | 7 ++++ runtests.py | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 runtests.py diff --git a/.gitignore b/.gitignore index 784fd4f..7a52573 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ pyvenv.cfg /lib64 /include +/test_implementation diff --git a/README.md b/README.md index ca8922e..b6a1d70 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,13 @@ The timezones specified in the JSON file adhere to the current version of the tz Assume that rates in this file will never overlap +# **Test cases** + +If you would like to build against a small test suite, I have included a python +script that will run tests against your API. This assumes you are serving it on +port 8080, and also assumes that both endpoints return 200 status codes, +as well as the expected JSON response. + ## Sample result Datetime ranges must be specified in ISO-8601 format. A rate must completely encapsulate a datetime range for it to be available. diff --git a/runtests.py b/runtests.py new file mode 100644 index 0000000..11bfddf --- /dev/null +++ b/runtests.py @@ -0,0 +1,114 @@ +import unittest +import requests + + +class TestParkingAPI(unittest.TestCase): + def setUp(self): + self.base_url = "http://localhost:8080" + self.sample_rates = { + "rates": [ + { + "days": "mon,tues,thurs", + "times": "0900-2100", + "tz": "America/Chicago", + "price": 1500, + }, + { + "days": "fri,sat,sun", + "times": "0900-2100", + "tz": "America/Chicago", + "price": 2000, + }, + { + "days": "wed", + "times": "0600-1800", + "tz": "America/Chicago", + "price": 1750, + }, + { + "days": "mon,wed,sat", + "times": "0100-0500", + "tz": "America/Chicago", + "price": 1000, + }, + { + "days": "sun,tues", + "times": "0100-0700", + "tz": "America/Chicago", + "price": 925, + }, + ] + } + + response = requests.put(f"{self.base_url}/rates", json=self.sample_rates) + self.assertEqual(response.status_code, 200, "Failed to load initial rates") + + def test_price_available(self): + response = requests.get( + f"{self.base_url}/price?start=2015-07-01T07:00:00-05:00&end=2015-07-01T12:00:00-05:00" + ) + self.assertEqual(response.status_code, 200) + price_data = response.json() + self.assertEqual( + price_data["price"], 1750, "Price does not match expected value" + ) + + def test_price_unavailable_multiple_days(self): + response = requests.get( + f"{self.base_url}/price?start=2015-07-01T07:00:00-05:00&end=2015-07-02T12:00:00-05:00" + ) + self.assertEqual(response.status_code, 200) + price_data = response.json() + self.assertEqual( + price_data["price"], + "unavailable", + "Expected 'unavailable' for multiple days", + ) + + def test_price_unavailable_multiple_rates(self): + response = requests.get( + f"{self.base_url}/price?start=2015-07-01T07:00:00-05:00&end=2015-07-01T19:00:00-05:00" + ) + self.assertEqual(response.status_code, 200) + price_data = response.json() + self.assertEqual( + price_data["price"], + "unavailable", + "Expected 'unavailable' for multiple rates", + ) + + def test_price_unavailable_no_matching_rate(self): + response = requests.get( + f"{self.base_url}/price?start=2015-07-01T00:00:00-05:00&end=2015-07-01T01:00:00-05:00", + ) + self.assertEqual(response.status_code, 200) + price_data = response.json() + self.assertEqual( + price_data["price"], + "unavailable", + "Expected 'unavailable' for no matching rate", + ) + + def test_update_rates(self): + new_rates = { + "rates": [ + { + "days": "mon", + "times": "0000-2359", + "tz": "America/New_York", + "price": 5000, + } + ] + } + response = requests.put(f"{self.base_url}/rates", json=new_rates) + self.assertEqual(response.status_code, 200) + response = requests.get( + f"{self.base_url}/price?start=2024-08-05T00:00:00-04:00&end=2024-08-05T23:59:00-04:00", + ) + self.assertEqual(response.status_code, 200) + price_data = response.json() + self.assertEqual(price_data["price"], 5000, "Price does not match updated rate") + + +if __name__ == "__main__": + unittest.main()