Skip to content

Commit

Permalink
feat: add test python script to build against
Browse files Browse the repository at this point in the history
  • Loading branch information
PThorpe92 committed Aug 10, 2024
1 parent 521bc5c commit 7b394bc
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
pyvenv.cfg
/lib64
/include
/test_implementation
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
114 changes: 114 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 7b394bc

Please sign in to comment.