Skip to content

Commit

Permalink
Merge pull request #14 from jhrcook/tests
Browse files Browse the repository at this point in the history
Pytest suite
  • Loading branch information
jhrcook authored Mar 14, 2021
2 parents 90278a1 + 4b568de commit e475a5f
Show file tree
Hide file tree
Showing 5 changed files with 527 additions and 14 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: pytest
on:
push:
branches:
- master
- tests
pull_request:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10.0-alpha.6']
name: Test on Python ${{ matrix.python-version }}
steps:
- uses: actions/checkout@v2
- name: Setup python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: x64
- name: Install packages
run: |
pip install -r requirements.txt
pip install pytest pytest-cov requests
- name: Describe python environment
run: |
python3 --version
which python3
pip list
- name: Run pytest
env:
DETA_PROJECT_KEY: ${{ secrets.DETA_PROJECT_KEY }}
run: pytest
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ A simple web API for recording my coffee consumption.

[![python](https://img.shields.io/badge/Python-3.9-3776AB.svg?style=flat&logo=python&logoColor=FFDB4D)](https://www.python.org)
[![FastAPI](https://img.shields.io/badge/FastAPI-0.63.0-009688.svg?style=flat&logo=FastAPI&logoColor=white)](https://fastapi.tiangolo.com) <br>
[![pytest](https://github.com/jhrcook/coffee-counter-api/actions/workflows/CI.yml/badge.svg)](https://github.com/jhrcook/coffee-counter-api/actions/workflows/CI.yml)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)
Expand Down
35 changes: 21 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import os
import uuid
from datetime import date, datetime
from enum import Enum
Expand All @@ -12,9 +13,15 @@
from fastapi.encoders import jsonable_encoder
from passlib.context import CryptContext
from pydantic import BaseModel
from pydantic.errors import NoneIsNotAllowedError
from pydantic.fields import PrivateAttr

from keys import PROJECT_KEY
try:
from keys import PROJECT_KEY
except:
# When running on CI services.
PROJECT_KEY = os.getenv("DETA_PROJECT_KEY", default="PROJECT_KEY")


HASHED_PASSWORD = "$2b$12$VOGTaA8tXdYoAU4Js6NBXO9uL..rXITV.WMiF/g8MEmCtdoMjLkOK"
pwd_context = CryptContext(schemes=["bcrypt"])
Expand Down Expand Up @@ -152,6 +159,17 @@ def coffee_use_dict() -> Dict[str, CoffeeUse]:
return keyedlist_to_dict(uses)


def sort_coffee_bags(bags: List[CoffeeBag]):
def f(b: CoffeeBag) -> date:
if b.start is None:
return date.today()
else:
return b.start

bags.sort(key=f)
return None


#### ---- Meta DB ---- ####

META_DB_KEY = "KEY"
Expand Down Expand Up @@ -276,19 +294,8 @@ def get_bag_info(bag_id: str) -> BagResponse:
return {bag._key: bag}


def sort_coffee_bags(bags: List[CoffeeBag]):
def f(b: CoffeeBag) -> date:
if b.start is None:
return date.today()
else:
return b.start

bags.sort(key=f)
return None


@app.get("/active_bags/", response_model=BagResponse)
def get_active_bags(n_last: Optional[int] = None) -> BagResponse:
def get_active_bags(n_last: Optional[int] = Query(None, ge=1)) -> BagResponse:

n_bags = num_coffee_bags()
n_buffer = 100
Expand Down Expand Up @@ -347,7 +354,7 @@ def query_coffee_uses_db(

@app.get("/uses/", response_model=UseResponse)
def get_uses(
n_last: int = Query(100, le=10000),
n_last: int = Query(100, ge=1, le=10000),
since: Optional[datetime] = None,
bag_id: Optional[str] = None,
) -> UseResponse:
Expand Down
7 changes: 7 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[pytest]
addopts = --no-cov-on-fail --cov=main
testpaths = tests.py
markers =
getter: Tests a getter method.
setter: Tests a setter method.
dev: Tests under development.
Loading

0 comments on commit e475a5f

Please sign in to comment.