Skip to content

Commit

Permalink
Merge pull request #356 from LACMTA:2023-api-optimization
Browse files Browse the repository at this point in the history
Refactor endpoint and load testing scripts
  • Loading branch information
albertkun authored Nov 13, 2023
2 parents e34bf67 + ae527c8 commit 45fb7a2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
7 changes: 0 additions & 7 deletions fastapi/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,6 @@ def filter(self, record):

from sqlalchemy import Table

try:
for table_name in models.Base.metadata.tables.keys():
table = Table(table_name, models.Base.metadata, autoload_with=engine)
if not table.exists(engine):
models.Base.metadata.tables[table_name].create(bind=engine)
except SQLAlchemyError as e:
print(f"An error occurred while creating the tables: {e}")
app = FastAPI(openapi_tags=tags_metadata,docs_url="/")
# db = connect(host=''ort=0, timeout=None, source_address=None)

Expand Down
17 changes: 12 additions & 5 deletions fastapi/tests/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,23 @@ The tests will generate a log with naming in the following format:

That has details on the number of tests failed.

## Endpoint Testing
We use `pytest` to test the endpoints and it can be run using
## Running endpoint tests

`python test_endpoints.py`
We use a Python script with `pytest` to run the endpoint tests with different environments. To run the endpoint tests, use the following commands:

### To test localhost

`python test_endpoints.py --local`

### To test deployed `dev`` instance

`python test_endpoints.py --dev`

## Load Testing
We use `locust` for load testing.

> Note: Ideally, you should run on a different machine that has access to the server running our fastapi application.
We use `locust` for load testing.

> Note: Ideally, you should run on a different machine that has access to the server running our fastapi application.
To run the load tests, use the following command:
```bash
Expand Down
16 changes: 16 additions & 0 deletions fastapi/tests/run_endpoint_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
import sys

def run_tests(env):
if env == '--local':
os.system('ENV=local pytest test_endpoints.py')
elif env == '--dev':
os.system('ENV=dev pytest test_endpoints.py')
else:
raise ValueError("Invalid environment. Choose --local or --dev")

if len(sys.argv) != 2:
print("Usage: python run_tests.py [--local|--dev]")
else:
env = sys.argv[1]
run_tests(env)
34 changes: 16 additions & 18 deletions fastapi/tests/test_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import os
import requests
import pytest
# from simplified_models import TripUpdates, VehiclePositions

# agency_ids = ["LACMTA", "LACMTA_Rail"]
# trip_update_fields = [f for f in dir(TripUpdates) if not f.startswith('__') and not callable(getattr(TripUpdates, f))]
# vehicle_position_fields = [f for f in dir(VehiclePositions) if not f.startswith('__') and not callable(getattr(VehiclePositions, f))]
agency_ids = ["LACMTA", "LACMTA_Rail"] # replace with your actual agency IDs
# Get the environment variable
env = os.getenv('ENV')

# Set the URL based on the environment variable
if env == 'local':
url = 'http://localhost:80'
elif env == 'dev':
url = 'https://dev-metro-api-v2.ofhq3vd1r7une.us-west-2.cs.amazonlightsail.com/'
else:
raise ValueError("Invalid environment. Set ENV environment variable to 'local' or 'dev'")

agency_ids = ["LACMTA", "LACMTA_Rail"]

@pytest.mark.parametrize("agency_id", agency_ids)
def test_get_all_trip_updates(agency_id):
response = requests.get(f"http://localhost:80/{agency_id}/trip_updates")
response = requests.get(f"{url}/{agency_id}/trip_updates")
assert response.status_code == 200

# @pytest.mark.parametrize("agency_id,field", [(a, f) for a in agency_ids for f in trip_update_fields])
# def test_get_list_of_trip_update_field_values(agency_id, field):
# response = requests.get(f"http://localhost:80/{agency_id}/trip_updates/{field}")
# assert response.status_code == 200

@pytest.mark.parametrize("agency_id", agency_ids)
def test_get_all_vehicle_positions(agency_id):
response = requests.get(f"http://localhost:80/{agency_id}/vehicle_positions")
assert response.status_code == 200

# @pytest.mark.parametrize("agency_id,field", [(a, f) for a in agency_ids for f in vehicle_position_fields])
# def test_get_list_of_vehicle_position_field_values(agency_id, field):
# response = requests.get(f"http://localhost:80/{agency_id}/vehicle_positions/{field}")
# assert response.status_code == 200
response = requests.get(f"{url}/{agency_id}/vehicle_positions")
assert response.status_code == 200

0 comments on commit 45fb7a2

Please sign in to comment.