Skip to content

Commit

Permalink
Merge pull request #58 from grillazz/57-update-poetry-to-version-171
Browse files Browse the repository at this point in the history
57 update poetry to version 171
  • Loading branch information
grillazz committed Feb 19, 2024
2 parents 278f33c + cf7ef84 commit 0595871
Show file tree
Hide file tree
Showing 6 changed files with 366 additions and 364 deletions.
52 changes: 27 additions & 25 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
name: Unit Tests
name: CI

on: [pull_request]
on:
pull_request:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.11"]
fail-fast: false
poetry-version: [ "1.7.1" ]

env:
PYTHONDONTWRITEBYTECODE: 1
PYTHONUNBUFFERED: 1
ENVIRONMENT: test
TESTING: 0
UP: up
DOWN: down
WEB_SERVER: web_server
MONGO_HOST: mongodb
MONGO_PORT: 27017
MONGO_USER: farmer
MONGO_PASS: tractor
MONGO_DB: greenhouse
MONGO_COLLECTION: greens
MONGO_TEST_DB: farmland
MONGO_URL: mongodb://farmer:[email protected]:27017/?retryWrites=true&w=majority

services:
mongodb:
Expand All @@ -27,31 +49,11 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
uses: abatilo/actions-poetry@v2
with:
path: .venv
key: venv-${{ runner.os }}-3.11-${{ hashFiles('**/poetry.lock') }}
poetry-version: ${{ matrix.poetry-version }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root
- name: Test Code
run: poetry run pytest
env:
PYTHONDONTWRITEBYTECODE: 1
PYTHONUNBUFFERED: 1
ENVIRONMENT: test
TESTING: 0
UP: up
DOWN: down
WEB_SERVER: web_server
MONGO_HOST: mongodb
MONGO_PORT: 27017
MONGO_USER: farmer
MONGO_PASS: tractor
MONGO_DB: greenhouse
MONGO_COLLECTION: greens
MONGO_TEST_DB: farmland
MONGO_URL: mongodb://farmer:[email protected]:27017/?retryWrites=true&w=majority
4 changes: 2 additions & 2 deletions greens/routers/v1/vegs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"",
status_code=HTTP_201_CREATED,
response_description="Document created",
# response_model=DocumentResponse,
response_model=DocumentResponse,
)
async def add_document(payload: Document):
"""
Expand Down Expand Up @@ -45,7 +45,7 @@ async def get_document(object_id: ObjectIdField):
"""
try:
return await retrieve_document(object_id, collection)
except ValueError as exception:
except (ValueError, TypeError) as exception:
raise NotFoundHTTPException(msg=str(exception)) from exception


Expand Down
37 changes: 28 additions & 9 deletions greens/schemas/vegs.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
from bson import ObjectId as _ObjectId
from bson.errors import InvalidId
from pydantic import BaseModel, Field, ConfigDict, AfterValidator
from pydantic import BaseModel, Field, ConfigDict, AfterValidator, BeforeValidator
from typing_extensions import Annotated


def check_object_id(value: str) -> str:
# def check_object_id(value: str) -> str:
# if not _ObjectId.is_valid(value):
# raise ValueError('Invalid ObjectId')
# return value


def check_object_id(value: _ObjectId) -> str:
"""
Checks if the given _ObjectId is valid and returns it as a string.
Args:
value: The _ObjectId to be checked.
Returns:
str: The _ObjectId as a string.
Raises:
ValueError: If the _ObjectId is invalid.
"""

if not _ObjectId.is_valid(value):
raise ValueError('Invalid ObjectId')
return value
raise ValueError("Invalid ObjectId")
return str(value)


ObjectIdField = Annotated[str, AfterValidator(check_object_id)]
ObjectIdField = Annotated[str, BeforeValidator(check_object_id)]


config = ConfigDict(arbitrary_types_allowed=True, populate_by_name=True)


class Document(BaseModel):
# model_config = config
model_config = config

name: str = Field(...)
desc: str = Field(...)
name: str
desc: str


class DocumentResponse(BaseModel):
id: ObjectIdField = Field(...)
id: ObjectIdField
Loading

0 comments on commit 0595871

Please sign in to comment.