Skip to content

Commit

Permalink
feat: update replenishment options (#38)
Browse files Browse the repository at this point in the history
* feat: add ReplenishmentUpdate schema

* feat: add time field to update on replenishment

* test: update tests
  • Loading branch information
RezenkovD authored Feb 9, 2024
1 parent c619765 commit 395990a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
9 changes: 7 additions & 2 deletions src/routers/replenishment.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
Page,
)
from models import User
from schemas import ReplenishmentCreate, ReplenishmentModel, UserReplenishment
from schemas import (
ReplenishmentCreate,
ReplenishmentUpdate,
ReplenishmentModel,
UserReplenishment,
)

router = APIRouter(
prefix="/replenishments",
Expand All @@ -39,7 +44,7 @@ def update_replenishment(
*,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
replenishment: ReplenishmentCreate,
replenishment: ReplenishmentUpdate,
replenishment_id: int,
) -> ReplenishmentModel:
return services.update_replenishment(
Expand Down
1 change: 1 addition & 0 deletions src/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from .expense import ExpenseCreate, ExpenseUpdate, ExpenseModel, UserExpense
from .replenishment import (
ReplenishmentCreate,
ReplenishmentUpdate,
UserBalance,
ReplenishmentModel,
UserReplenishment,
Expand Down
4 changes: 4 additions & 0 deletions src/schemas/replenishment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class ReplenishmentCreate(BaseModel):
descriptions: str


class ReplenishmentUpdate(ReplenishmentCreate):
time: datetime.datetime


class ReplenishmentModel(ReplenishmentCreate):
id: int
time: datetime.datetime
Expand Down
11 changes: 8 additions & 3 deletions src/services/replenishment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
from starlette.exceptions import HTTPException

from models import Replenishment
from schemas import ReplenishmentCreate, ReplenishmentModel, UserReplenishment
from schemas import (
ReplenishmentCreate,
ReplenishmentUpdate,
ReplenishmentModel,
UserReplenishment,
)


def create_replenishment(
Expand All @@ -31,8 +36,8 @@ def create_replenishment(


def update_replenishment(
db: Session, user_id: int, replenishment: ReplenishmentCreate, replenishment_id: int
):
db: Session, user_id: int, replenishment: ReplenishmentUpdate, replenishment_id: int
) -> ReplenishmentModel:
try:
db.query(Replenishment).filter_by(id=replenishment_id, user_id=user_id).one()
except exc.NoResultFound:
Expand Down
12 changes: 8 additions & 4 deletions tests/test_endpoints/test_replenishment_e.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest.mock import Mock

from dependencies import oauth
from schemas import ReplenishmentCreate
from schemas import ReplenishmentCreate, ReplenishmentUpdate
from tests.conftest import async_return, client
from tests.factories import ReplenishmentFactory, UserFactory

Expand Down Expand Up @@ -45,21 +45,25 @@ def test_create_replenishment(self) -> None:

def test_update_replenishment(self) -> None:
replenishment = ReplenishmentFactory(user_id=self.user.id)
date_update_replenishment = ReplenishmentCreate(
descriptions="descriptions", amount=999.9
time = "2018-08-03T10:51:42"
date_update_replenishment = ReplenishmentUpdate(
descriptions="descriptions",
amount=999.9,
time=time,
)
data = client.put(
f"/replenishments/{replenishment.id}/",
json={
"descriptions": date_update_replenishment.descriptions,
"amount": date_update_replenishment.amount,
"time": time,
},
)
replenishments_data = {
"id": data.json()["id"],
"descriptions": date_update_replenishment.descriptions,
"amount": date_update_replenishment.amount,
"time": data.json()["time"],
"time": time,
"user": {"id": self.user.id, "login": self.user.login},
}
assert data.status_code == 200
Expand Down
11 changes: 5 additions & 6 deletions tests/test_services/test_replenishment_s.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from starlette.exceptions import HTTPException

from models import Replenishment
from schemas import ReplenishmentCreate
from schemas import ReplenishmentCreate, ReplenishmentUpdate
from services import (
create_replenishment,
read_replenishments,
Expand All @@ -31,17 +31,16 @@ def test_create_replenishment(session) -> None:
def test_update_replenishment(session) -> None:
user = UserFactory()
replenishment = ReplenishmentFactory(user_id=user.id)
date_update_replenishment = ReplenishmentCreate(
descriptions="descriptions", amount=999.9
time = datetime.datetime.now()
date_update_replenishment = ReplenishmentUpdate(
descriptions="descriptions", amount=999.9, time=time
)
data = update_replenishment(
session, user.id, date_update_replenishment, replenishment.id
)
assert data.descriptions == date_update_replenishment.descriptions
assert float(data.amount) == date_update_replenishment.amount
assert data.time.strftime("%Y-%m-%d %H:%M") == datetime.datetime.utcnow().strftime(
"%Y-%m-%d %H:%M"
)
assert data.time == time
assert data.user.id == user.id


Expand Down

0 comments on commit 395990a

Please sign in to comment.