Skip to content

Commit

Permalink
Fast api conversion subsemester (#553)
Browse files Browse the repository at this point in the history
* rc/api/tests/test.sh
:wq
:wq
�

* revert env variable

* updated endpoint tests

* updated second endpoint test

* corrected fastapi import to fastapi.testclient

* debig app.py

* pydantic -> str

* attempt 1 updating endpoint subsemester with dependency injection, and test subsemester with Optional param

* updated app.py and api_models.py for depends

* fixed testclient import

* added pytest mark to other tests

* refixed import after I overwrote it

* refixed import after I overwrote it

* accidentally changed connection.py

* cleaning up unnecessary imports and comments

Co-authored-by: Liam Haining <ljhaining@gmail.com>
Co-authored-by: Liam Haining <12472949+lhain08@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 25, 2022
1 parent b610a55 commit 9da2387
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/api/Updating to FastAPI
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ UPDATING YOUR ENDPOINTS
depending on your database call's response types
5. Updating pytests:
* AFTER running your pytests as before to make sure your endpoint's functionality has not changed:
* Client becomes TestClient. Replace from util import Client with from fastapi import TestClient
* Client becomes TestClient. Replace from util import Client with from fastapi.testclient import TestClient
and replace 'client: Client' with 'client: TestClient'
* Mark your pytests as updated- import pytest and add the decorator (@pytest.mark.testclient)
in front of each test case
Expand Down
3 changes: 3 additions & 0 deletions src/api/api_models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from pydantic import BaseModel
from typing import Optional


class SessionPydantic(BaseModel):
email: str
password: str

class SessionDeletePydantic(BaseModel):
sessionID: str

class SubsemesterPydantic(BaseModel):
semester: Optional[str] = None
61 changes: 33 additions & 28 deletions src/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from fastapi_cache.backends.inmemory import InMemoryBackend
from fastapi_cache.decorator import cache
from fastapi_cache.coder import PickleCoder
from fastapi import Depends

from api_models import *
import db.connection as connection
Expand Down Expand Up @@ -92,35 +93,38 @@ async def get_departments():
departments, error = class_info.get_departments()
return departments if not error else Response(content=error, status_code=500)

@app.get('/api/subsemester')
@cache(expire=Constants.HOUR_IN_SECONDS, coder=PickleCoder, namespace="API_CACHE")
async def get_subsemesters(subsemester: SubsemesterPydantic = Depends(SubsemesterPydantic)):
"""
GET /api/subsemester?semester={}
Cached: 1 Hour
Get list of departments i.e. COGS, CIVL, CSCI, BIOL
(Used in dropdown in "Course Search"
"""
if subsemester.semester:
subsemesters, error = class_info.get_subsemesters(subsemester.semester)
# for i in subsemesters:
# print(i)
db_list = [dict(r) for r in subsemesters]
return db_list if not error else Response(error, status_code=500)
# Some cases, we do want all subsemesters across all semesters like in Admin Panel
subsemesters, error = class_info.get_subsemesters()
db_list = [dict(r) for r in subsemesters]
return db_list if not error else Response(error, status_code=500)

@app.get('/api/semester')
@cache(expire=Constants.DAY_IN_SECONDS, coder=PickleCoder, namespace="API_CACHE")
async def get_semesters():
"""
GET /api/semester
Cached: 24 Hours
"""
semesters, error = class_info.get_semesters()
db_list = [dict(r) for r in semesters]
return db_list if not error else Response(error, status_code=500)

# @app.route('/api/subsemester', methods=['GET'])
# @cache.cached(timeout=Constants.HOUR_IN_SECONDS, query_string=True)
# def get_subsemesters():
# """
# GET /api/subsemester?semester={}
# Cached: 1 Hour
#
# Get list of departments i.e. COGS, CIVL, CSCI, BIOL
# (Used in dropdown in "Course Search"
# """
# semester = request.args.get("semester", default=None)
# if semester:
# subsemesters, error = class_info.get_subsemesters(semester)
# return jsonify(subsemesters) if not error else Response(error, status=500)
# # Some cases, we do want all subsemesters across all semesters like in Admin Panel
# subsemesters, error = class_info.get_subsemesters()
# return jsonify(subsemesters) if not error else Response(error, status=500)
#
# @app.route('/api/semester', methods=['GET'])
# @cache.cached(timeout=Constants.DAY_IN_SECONDS)
# def get_semesters():
# """
# GET /api/semester
# Cached: 24 Hours
# """
# semesters, error = class_info.get_semesters()
# return jsonify(semesters) if not error else Response(error, status=500)
#
@app.get('/api/semesterInfo')
def get_all_semester_info():
all_semester_info, error = class_info.get_all_semester_info()
Expand All @@ -130,6 +134,7 @@ def get_all_semester_info():
def get_defaultSemester():
semester, error = admin_info.get_semester_default()
return semester if not error else Response(error, status=500)

#
# @app.route('/api/defaultsemesterset', methods=['POST'])
# def set_defaultSemester():
Expand Down
1 change: 0 additions & 1 deletion src/api/db/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
DB_PORT = os.environ.get('DB_PORT', None)
DB_PASS = os.environ.get('DB_PASS', None)


class database():
def connect(self):
self.conn = psycopg2.connect(
Expand Down
4 changes: 1 addition & 3 deletions src/api/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ markers =
testclient: tests updated to use the FastAPI TestClient object
incompletedependency: marks tests that depend on an api that has not yet been completed
testpaths =
tests/test_root.py
tests/test_session.py
tests/test_bulk_upload.py
tests/
5 changes: 1 addition & 4 deletions src/api/tests/test.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#!/usr/bin/env bash

export DB_HOST=${DB_HOST:-localhost}
export DB_USER=${DB_USER:-yacs}
export DB_PASS=${DB_PASS:-easy_dev_pass}
export DB_NAME=${DB_NAME:-yacs}
export DB_PORT=${DB_PORT:-5432}

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

export TEST_APP_DIR=$SCRIPT_DIR/../

#pip3 install -r $TEST_APP_DIR/requirements.txt
python3 -m pytest -s -m "testclient and not incompletedependency" "${SCRIPT_DIR}"
python3 -m pytest -s -m "testclient and not incompletedependency" "${SCRIPT_DIR}"
7 changes: 4 additions & 3 deletions src/api/tests/test_semester.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
from fastapi.testclient import TestClient

from .util import Client

def test_semester(upload, client):
@pytest.mark.testclient
def test_semester(upload, client: TestClient):
"""
semester endpoint should get all of the semesters in the upload
in the case of our test data, we should be getting get 2 semesters: "SUMMER 2020" and "SPRING 2020"
Expand Down
9 changes: 6 additions & 3 deletions src/api/tests/test_subsemester.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .util import Client
import pytest
from fastapi.testclient import TestClient


def test_subsemeseter_spring2020(client, upload):
@pytest.mark.testclient
def test_subsemeseter_spring2020(client: TestClient, upload):
"""
when subsemester endpoint is given an input such as Spring 2020
it should only return data for that subsemester i.e. data where the parent semester name
Expand All @@ -14,6 +15,7 @@ def test_subsemeseter_spring2020(client, upload):
assert len(data) == 1
assert data[0]["parent_semester_name"] == "SPRING 2020"

@pytest.mark.testclient
def test_subsemester_nosemester(client, upload):
"""
when no subsemester is taken as input the subsemester endpoint should return all of the subsemesters
Expand All @@ -25,6 +27,7 @@ def test_subsemester_nosemester(client, upload):
data = r.json()
assert len(data) == 4

@pytest.mark.testclient
def test_subsemester_invalid_semester(client):
"""
invalid input to subsemester to subsemester endpoint such as "moon 2050"
Expand Down

0 comments on commit 9da2387

Please sign in to comment.