-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f132ef9
commit c7ba9f7
Showing
7 changed files
with
86 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
from flask import Blueprint, Response, current_app, request | ||
|
||
from schemes.users import User | ||
from schemes.users import User, UserRepository | ||
|
||
bp = Blueprint("api", __name__) | ||
|
||
|
||
@bp.route("/users", methods=["POST"]) | ||
def add_user() -> Response: | ||
user = User(request.get_json()["email"]) | ||
current_app.extensions["users"].append(user) | ||
users: UserRepository = current_app.extensions["users"] | ||
users.add(user) | ||
return Response(status=201) | ||
|
||
|
||
@bp.route("/users", methods=["DELETE"]) | ||
def clear_users() -> Response: | ||
current_app.extensions["users"].clear() | ||
users: UserRepository = current_app.extensions["users"] | ||
users.clear() | ||
return Response(status=204) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,27 @@ | ||
from dataclasses import dataclass | ||
from typing import List, TypeGuard | ||
|
||
|
||
@dataclass | ||
class User: | ||
email: str | ||
|
||
|
||
class UserRepository: | ||
def __init__(self) -> None: | ||
self._users: List[User] = [] | ||
|
||
def add(self, user: User) -> None: | ||
self._users.append(user) | ||
|
||
def clear(self) -> None: | ||
self._users.clear() | ||
|
||
def get(self, email: str) -> User | None: | ||
def by_email(user: User) -> TypeGuard[User]: | ||
return user.email == email | ||
|
||
return next(filter(by_email, self._users), None) | ||
|
||
def get_all(self) -> List[User]: | ||
return self._users |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pytest | ||
|
||
from schemes.users import User, UserRepository | ||
|
||
|
||
@pytest.fixture(name="user_repository") | ||
def user_repository_fixture() -> UserRepository: | ||
return UserRepository() | ||
|
||
|
||
def test_add_user(user_repository: UserRepository) -> None: | ||
user_repository.add(User("boardman@example.com")) | ||
|
||
assert user_repository.get("boardman@example.com") == User("boardman@example.com") | ||
|
||
|
||
def test_get_user(user_repository: UserRepository) -> None: | ||
user_repository.add(User("boardman@example.com")) | ||
|
||
assert user_repository.get("boardman@example.com") == User("boardman@example.com") | ||
|
||
|
||
def test_get_user_who_does_not_exist(user_repository: UserRepository) -> None: | ||
user_repository.add(User("boardman@example.com")) | ||
|
||
assert user_repository.get("obree@example.com") is None | ||
|
||
|
||
def test_get_all_users(user_repository: UserRepository) -> None: | ||
user_repository.add(User("boardman@example.com")) | ||
user_repository.add(User("obree@example.com")) | ||
|
||
user_list = user_repository.get_all() | ||
|
||
assert user_list == [User("boardman@example.com"), User("obree@example.com")] | ||
|
||
|
||
def test_clear_all_users(user_repository: UserRepository) -> None: | ||
user_repository.add(User("boardman@example.com")) | ||
user_repository.add(User("obree@example.com")) | ||
|
||
user_repository.clear() | ||
|
||
assert user_repository.get_all() == [] |