Skip to content

Commit

Permalink
Merge pull request #10 from freud-digital/9-implement-cache
Browse files Browse the repository at this point in the history
cache implemented #5
  • Loading branch information
csae8092 authored Aug 4, 2022
2 parents 06d6c60 + 2c197e2 commit 8c8e59e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
10 changes: 10 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import requests
from typing import Union
from fastapi import FastAPI, Request
from fastapi_cache import FastAPICache
from fastapi_cache.backends.inmemory import InMemoryBackend
from fastapi_cache.decorator import cache
from app.config import (
BASEROW_TABLE_MAPPING,
BASEROW_API,
Expand All @@ -18,6 +21,7 @@


@app.get("/")
@cache(expire=60 * 60)
async def root(request: Request):
endpoints = [
{
Expand All @@ -39,6 +43,7 @@ async def root(request: Request):


@app.get("/{entity_type}")
@cache(expire=60 * 60)
async def fetch_entitiy(
entity_type: str,
q: str,
Expand Down Expand Up @@ -75,3 +80,8 @@ async def fetch_entitiy(
data = r.json()
result = populate_baserow_response(data, format=format)
return result


@app.on_event("startup")
async def startup():
FastAPICache.init(InMemoryBackend())
62 changes: 37 additions & 25 deletions app/test_main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from fastapi.testclient import TestClient
from fastapi_cache import FastAPICache
from fastapi_cache.backends.inmemory import InMemoryBackend

from .main import app
from .config import BASEROW_TABLE_MAPPING, BASEROW_TOKEN, MINIMAL_CHARS_ERROR
Expand All @@ -8,45 +10,55 @@
client = TestClient(app)


@app.on_event("startup")
async def startup():
FastAPICache.init(InMemoryBackend())


def test_read_main():
response = client.get("/")
assert response.status_code == 200
with TestClient(app) as client:
response = client.get("/")
assert response.status_code == 200


def test_correct_endpoint_number():
response = client.get("/")
endpoints = response.json()['endpoints']
assert len(endpoints) == len(BASEROW_TABLE_MAPPING.keys()) + 1
with TestClient(app) as client:
response = client.get("/")
endpoints = response.json()['endpoints']
assert len(endpoints) == len(BASEROW_TABLE_MAPPING.keys()) + 1


def test_for_token():
assert BASEROW_TOKEN


def test_entity_endpoints():
response = client.get("/")
endpoints = response.json()['endpoints']
for x in endpoints:
response = client.get(f"{x['endpoint']}")
assert response.status_code == 422
response = client.get(f"{x['endpoint']}?q=ha")
assert response.status_code == 200
assert response.json()['data'] == MINIMAL_CHARS_ERROR
with TestClient(app) as client:
response = client.get("/")
endpoints = response.json()['endpoints']
for x in endpoints:
response = client.get(f"{x['endpoint']}")
assert response.status_code == 422
response = client.get(f"{x['endpoint']}?q=ha")
assert response.status_code == 200
assert response.json()['data'] == MINIMAL_CHARS_ERROR


def test_minimal_chars():
response = client.get("/")
endpoints = response.json()['endpoints']
for x in endpoints:
response = client.get(f"{x['endpoint']}?q=ha")
assert response.status_code == 200
assert response.json()['data'] == MINIMAL_CHARS_ERROR
with TestClient(app) as client:
response = client.get("/")
endpoints = response.json()['endpoints']
for x in endpoints:
response = client.get(f"{x['endpoint']}?q=ha")
assert response.status_code == 200
assert response.json()['data'] == MINIMAL_CHARS_ERROR


def test_no_match():
response = client.get("/")
endpoints = response.json()['endpoints']
for x in endpoints:
response = client.get(f"{x['endpoint']}?q={NO_MATCH_STRING}")
assert response.status_code == 200
assert 'tc:suggestion' in response.json().keys()
with TestClient(app) as client:
response = client.get("/")
endpoints = response.json()['endpoints']
for x in endpoints:
response = client.get(f"{x['endpoint']}?q={NO_MATCH_STRING}")
assert response.status_code == 200
assert 'tc:suggestion' in response.json().keys()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
coverage
fastapi[all]
fastapi-cache2==0.1.8
flake8
pytest

0 comments on commit 8c8e59e

Please sign in to comment.