Skip to content

Commit d9aa278

Browse files
committed
replace db and backend
1 parent 85f939e commit d9aa278

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+766
-547
lines changed

.gitignore

+2-25
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,8 @@ profile_default/
8383
ipython_config.py
8484

8585
# pyenv
86-
# For a library or package, you might want to ignore these files since the code is
87-
# intended to run in multiple environments; otherwise, check them in:
88-
# .python-version
89-
90-
# pipenv
91-
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92-
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93-
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94-
# install all needed dependencies.
95-
#Pipfile.lock
96-
97-
# poetry
98-
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99-
# This is especially recommended for binary packages to ensure reproducibility, and is more
100-
# commonly ignored for libraries.
101-
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102-
#poetry.lock
103-
104-
# pdm
105-
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106-
#pdm.lock
107-
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108-
# in version control.
109-
# https://pdm.fming.dev/#use-with-ide
86+
.python-version
87+
11088
.pdm.toml
11189

11290
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
@@ -165,4 +143,3 @@ serverless.yaml
165143

166144
*-lock.json
167145

168-
logs/

.husky/pre-commit

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
./node_modules/.bin/prettier frontend/src/**/*.js --single-quote --trailing-comma es5 --write
5-
./node_modules/.bin/eslint frontend/src --ignore-pattern 'frontend/src/public/' --fix
4+
npm run format:backend
5+
npm run lint:backend
6+
7+
npm run format:frontend
8+
npm run lint:frontend
9+
610
git add .

.husky/pre-push

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
npm run test:frontend
54
npm run test:backend
5+
npm run test:frontend
66

backend/.flake8

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[flake8]
2+
max-line-length = 88
3+
ignore =
4+
E203,
5+
W503

backend/.isort.cfg

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[settings]
2+
line_length=88
3+
multi_line_output=3
4+
include_trailing_comma=True
5+
use_parentheses=True
6+
force_grid_wrap=0

backend/Dockerfile

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
FROM python:3.11
1+
FROM python:3.8.3
22

3-
ARG API_PORT=80
3+
ARG RESERVATION_PORT=80
4+
ARG ENV
5+
ARG PG_URL
6+
ARG IS_DEBUG
7+
ARG SECRET_KEY
8+
ARG REFRESH_SECRET_KEY
49

5-
WORKDIR /app
10+
ENV RESERVATION_PORT=${RESERVATION_PORT}
11+
ENV ENV=${ENV}
12+
ENV PG_URL=${PG_URL}
13+
ENV IS_DEBUG=${IS_DEBUG}
14+
ENV SECRET_KEY=${SECRET_KEY}
15+
ENV REFRESH_SECRET_KEY=${REFRESH_SECRET_KEY}
616

17+
WORKDIR /app
718
COPY ./src/ /app/
8-
COPY ./schema.graphql /app/
9-
RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements_docker.txt
10-
19+
COPY ./schema.graphql /app/
20+
RUN mkdir logs
21+
COPY ./requirements-docker.txt ./requirements.txt
22+
RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt
23+
24+
EXPOSE ${RESERVATION_PORT}
1125
CMD ["python", "main.py"]

backend/requirements-docker.txt

Whitespace-only changes.

backend/scripts/format.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
isort --line-length 88 .
3+
black ./

backend/scripts/lint.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
set -e
3+
flake8 src/ specs/
4+
find "$(pwd)/src" -type f -name "\"*.py\"" ! -name "\"*test_*\"" \
5+
-exec python -m mypy {} +

backend/scripts/test.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export ENV=test
2+
export PYTHONDONTWRITEBYTECODE=1
3+
4+
find . -name "__pycache__" -exec rm -r {} +
5+
python -m pytest --cov=src --cov-report term

backend/specs/resolvers/__init__.py

+1-27
Original file line numberDiff line numberDiff line change
@@ -1,27 +1 @@
1-
import pytest
2-
3-
from sqlalchemy.orm import Session
4-
5-
from api.models import Reservation, Room
6-
7-
8-
@pytest.fixture
9-
def mock_db_session(mocker):
10-
return mocker.MagicMock(spec=Session)
11-
12-
13-
@pytest.fixture
14-
def patch_db_to_resolvers(mocker, mock_db_session):
15-
mocker.patch("api.resolvers.queries.DbSession", return_value=mock_db_session)
16-
mocker.patch("api.resolvers.mutations.DbSession", return_value=mock_db_session)
17-
yield mocker
18-
19-
20-
@pytest.fixture
21-
def mock_is_room_available(mocker):
22-
return mocker.patch("api.resolvers.queries.is_room_available")
23-
24-
25-
@pytest.fixture
26-
def mock_room(mocker):
27-
return mocker.MagicMock(spec=Room)
1+
MOCK_EXECUTION_CONTEXT = {"context_key": "context_value"}
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,43 @@
1+
from datetime import datetime
2+
13
import pytest
4+
from asyncpg import Pool
25

3-
from api.resolvers.queries import get_all_reservations_resolver
46
from api.models import Reservation
7+
from api.resolvers.queries import get_all_reservations_resolver
58

6-
from . import mock_db_session, patch_db_to_resolvers
9+
from . import MOCK_EXECUTION_CONTEXT
710

811

912
class DescribeAllReservationsResolver:
10-
@pytest.mark.usefixtures("patch_db_to_resolvers")
11-
def should_return_all_reservations_resolver(
12-
self,
13-
mock_db_session,
14-
):
15-
mock_reservations = [
13+
@pytest.mark.asyncio
14+
async def should_return_all_reservations(self, mocker):
15+
reservations = [
1616
Reservation(
1717
id=1,
1818
room_id="room_1",
19-
checkin_date="2023-01-01",
20-
checkout_date="2023-01-03",
19+
checkin_date=datetime(2023, 1, 1, 5, 0),
20+
checkout_date=datetime(2023, 1, 1, 5, 0),
21+
total_charge=100,
2122
),
2223
Reservation(
2324
id=2,
2425
room_id="room_2",
25-
checkin_date="2023-01-05",
26-
checkout_date="2023-01-07",
26+
checkin_date=datetime(2023, 1, 1, 5, 0),
27+
checkout_date=datetime(2023, 1, 1, 5, 0),
28+
total_charge=200,
2729
),
2830
]
2931

30-
mock_db_session.query.return_value.all.return_value = mock_reservations
31-
mock_execution_context = {"context_key": "context_value"}
32+
mock_pool = mocker.AsyncMock(spec=Pool)
33+
mocker.patch("asyncpg.create_pool", return_value=mock_pool)
34+
mocker.patch(
35+
"api.resolvers.queries.DbSession", mocker.AsyncMock(return_value=mock_pool)
36+
)
37+
mock_pool.fetch = mocker.AsyncMock(return_value=reservations)
3238

33-
result = get_all_reservations_resolver(None, mock_execution_context)
39+
result = await get_all_reservations_resolver(None, MOCK_EXECUTION_CONTEXT)
3440

3541
assert "reservations" in result
36-
assert result["reservations"] == [
37-
{
38-
"id": 1,
39-
"room_id": "room_1",
40-
"checkin_date": "2023-01-01",
41-
"checkout_date": "2023-01-03",
42-
"total_charge": None,
43-
},
44-
{
45-
"id": 2,
46-
"room_id": "room_2",
47-
"checkin_date": "2023-01-05",
48-
"checkout_date": "2023-01-07",
49-
"total_charge": None,
50-
},
51-
]
52-
53-
mock_db_session.query.assert_called_once_with(Reservation)
42+
assert len(result["reservations"]) == 2
43+
assert result["reservations"] == reservations
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,69 @@
1+
from datetime import datetime
2+
13
import pytest
4+
from asyncpg import Pool
25

3-
from api.resolvers.mutations import delete_reservation_resolver
46
from api.models import Reservation
7+
from api.resolvers.mutations import delete_reservation_resolver
58

6-
from . import mock_db_session, patch_db_to_resolvers
9+
from . import MOCK_EXECUTION_CONTEXT
710

811

912
class DescribeDeleteReservationResolver:
10-
@pytest.mark.usefixtures("patch_db_to_resolvers")
11-
def should_delete_reservation(self, mock_db_session):
12-
mock_db_session.query.return_value.all.return_value = [
13+
def find_by_id(self, reservation_id):
14+
return [
15+
reservation
16+
for reservation in self.reservations
17+
if reservation.id != reservation_id
18+
]
19+
20+
@pytest.mark.asyncio
21+
async def should_delete_existing_reservation(self, mocker):
22+
original_reservations = [
1323
Reservation(
1424
id=1,
1525
room_id="room_1",
16-
checkin_date="2023-01-01",
17-
checkout_date="2023-01-03",
18-
)
26+
checkin_date=datetime(2023, 1, 1, 5, 0),
27+
checkout_date=datetime(2023, 1, 1, 5, 0),
28+
total_charge=100,
29+
),
30+
Reservation(
31+
id=2,
32+
room_id="room_2",
33+
checkin_date=datetime(2023, 1, 1, 5, 0),
34+
checkout_date=datetime(2023, 1, 1, 5, 0),
35+
total_charge=200,
36+
),
1937
]
20-
mock_execution_context = {"context_key": "context_value"}
2138

22-
result = delete_reservation_resolver(
23-
None, mock_execution_context, reservationId=1
39+
existing_reservation_id = 1
40+
expected_reservations = [
41+
reservation
42+
for reservation in original_reservations
43+
if reservation.id != existing_reservation_id
44+
]
45+
46+
mock_pool = mocker.AsyncMock(spec=Pool)
47+
mocker.patch("asyncpg.create_pool", return_value=mock_pool)
48+
mocker.patch(
49+
"api.resolvers.mutations.DbSession",
50+
mocker.AsyncMock(return_value=mock_pool),
51+
)
52+
mock_pool.fetch = mocker.AsyncMock(return_value=expected_reservations)
53+
54+
result = await delete_reservation_resolver(
55+
None, MOCK_EXECUTION_CONTEXT, reservationId=existing_reservation_id
2456
)
2557

2658
assert result["success"] is True
27-
assert result["reservation"] is not None
59+
assert len(result["reservations"]) == len(original_reservations) - 1
60+
assert result["reservations"] == expected_reservations
61+
62+
@pytest.mark.asyncio
63+
async def test_should_give_reservation_not_found(self, mocker):
64+
result = await delete_reservation_resolver(
65+
None, MOCK_EXECUTION_CONTEXT, reservationId=999
66+
)
67+
68+
assert result["success"] is False
69+
assert "Reservation with id of 999 not found" in result["errors"]

0 commit comments

Comments
 (0)