Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast api conversion subsemester #553

Merged
merged 18 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions src/api/api_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from pydantic import BaseModel
from typing import Optional


class SessionPydantic(BaseModel):
email: str
Expand All @@ -7,3 +9,5 @@ class SessionPydantic(BaseModel):
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 @@ -93,34 +94,38 @@ async def get_departments():
return departments if not error else Response(content=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/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/semesterInfo', methods=['GET'])
# def get_all_semester_info():
# all_semester_info, error = class_info.get_all_semester_info()
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