Skip to content

Commit

Permalink
GH-4: Allow multiple users to be added via the API
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparrow0hawk committed Oct 20, 2023
1 parent 63584cc commit 7950d92
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 18 deletions.
3 changes: 1 addition & 2 deletions schemes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,4 @@ def _create_database() -> None:
def _configure_users() -> None:
users = inject.instance(UserRepository)
if not users.get_all():
users.add(User("[email protected]"))
users.add(User("[email protected]"))
users.add(User("[email protected]"), User("[email protected]"))
6 changes: 3 additions & 3 deletions schemes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

@bp.route("/users", methods=["POST"])
@inject.autoparams()
def add_user(users: UserRepository) -> Response:
user = User(request.get_json()["email"])
users.add(user)
def add_users(users: UserRepository) -> Response:
json = request.get_json()
users.add(*[User(element["email"]) for element in json])
return Response(status=201)


Expand Down
7 changes: 4 additions & 3 deletions schemes/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class User:


class UserRepository:
def add(self, user: User) -> None:
def add(self, *users: User) -> None:
raise NotImplementedError()

def clear(self) -> None:
Expand All @@ -38,9 +38,10 @@ class DatabaseUserRepository(UserRepository):
def __init__(self, engine: Engine):
self._engine = engine

def add(self, user: User) -> None:
def add(self, *users: User) -> None:
with self._engine.begin() as connection:
connection.execute(text("INSERT INTO users (email) VALUES (:email)"), {"email": user.email})
for user in users:
connection.execute(text("INSERT INTO users (email) VALUES (:email)"), {"email": user.email})

def clear(self) -> None:
with self._engine.begin() as connection:
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/app_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def __init__(self, url: str):
self._url = url

def add_user(self, email: str) -> None:
user = {"email": email}
response = requests.post(f"{self._url}/api/users", json=user, timeout=self.DEFAULT_TIMEOUT)
users = [{"email": email}]
response = requests.post(f"{self._url}/api/users", json=users, timeout=self.DEFAULT_TIMEOUT)
assert response.status_code == 201

def clear_users(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class MemoryUserRepository(UserRepository):
def __init__(self) -> None:
self._users: List[User] = []

def add(self, user: User) -> None:
self._users.append(user)
def add(self, *users: User) -> None:
self._users.extend(users)

def clear(self) -> None:
self._users.clear()
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def users_fixture() -> UserRepository:
return inject.instance(UserRepository)


def test_add_user(users: UserRepository, client: FlaskClient) -> None:
response = client.post("/api/users", json={"email": "[email protected]"})
def test_add_users(users: UserRepository, client: FlaskClient) -> None:
response = client.post("/api/users", json=[{"email": "[email protected]"}, {"email": "[email protected]"}])

assert response.status_code == 201
assert users.get_by_email("[email protected]")
assert users.get_all() == [User("[email protected]"), User("[email protected]")]


def test_clear_users(users: UserRepository, client: FlaskClient) -> None:
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def users_fixture() -> DatabaseUserRepository:
return repository


def test_add_user(users: DatabaseUserRepository) -> None:
users.add(User("[email protected]"))
def test_add_users(users: DatabaseUserRepository) -> None:
users.add(User("[email protected]"), User("[email protected]"))

assert users.get_by_email("[email protected]") == User("boardman@example.com")
assert users.get_all() == [User("[email protected]"), User("obree@example.com")]


def test_get_user_by_email(users: DatabaseUserRepository) -> None:
Expand Down

0 comments on commit 7950d92

Please sign in to comment.