Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the ability to create identical events #109

Merged
merged 4 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions calendar_backend/routes/event/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,35 @@ async def create_events(
result = []
for event in events:
event_dict = event.model_dump()
rooms = [Room.get(room_id, session=db.session) for room_id in event_dict.pop("room_id", [])]
lecturers = [Lecturer.get(lecturer_id, session=db.session) for lecturer_id in event_dict.pop("lecturer_id", [])]
groups = [Group.get(group_id, session=db.session) for group_id in event_dict.pop("group_id", [])]
result.append(
Event.create(
**event_dict,
room=rooms,
lecturer=lecturers,
group=groups,
session=db.session,
)
existing_events_query = (
Event.get_all(session=db.session)
.filter(Event.name == event_dict.get("name"))
.filter(Event.start_ts == event_dict.get("start_ts"))
.filter(Event.end_ts == event_dict.get("end_ts"))
)
is_unique = True
for existing_event in existing_events_query.all():
if (
{column.id for column in existing_event.group} == set(event_dict["group_id"])
and {column.id for column in existing_event.room} == set(event_dict["room_id"])
and {column.id for column in existing_event.lecturer} == set(event_dict["lecturer_id"])
):
is_unique = False
if is_unique:
rooms = [Room.get(room_id, session=db.session) for room_id in event_dict.pop("room_id", [])]
Temmmmmo marked this conversation as resolved.
Show resolved Hide resolved
lecturers = [
Lecturer.get(lecturer_id, session=db.session) for lecturer_id in event_dict.pop("lecturer_id", [])
]
groups = [Group.get(group_id, session=db.session) for group_id in event_dict.pop("group_id", [])]
result.append(
Event.create(
**event_dict,
room=rooms,
lecturer=lecturers,
group=groups,
session=db.session,
)
)
db.session.commit()
adapter = TypeAdapter(list[EventGet])
return adapter.validate_python(result)
Expand Down
46 changes: 46 additions & 0 deletions tests/event/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,52 @@ def test_create_many(client_auth: TestClient, dbsession: Session, room_factory,
assert [row.id for row in response_model.group] == request_obj[1]["group_id"]


def test_create_many_clones(client_auth: TestClient, dbsession: Session, room_factory, group_factory, lecturer_factory):
time_stamp = datetime.datetime.now()
name1 = f"name1_{time_stamp}"
room_path1 = room_factory(client_auth)
group_path1 = group_factory(client_auth)
lecturer_path1 = lecturer_factory(client_auth)
name2 = f"name2_{time_stamp}"
room_path2 = room_factory(client_auth)
group_path2 = group_factory(client_auth)
lecturer_path2 = lecturer_factory(client_auth)
room_id1 = int(room_path1.split("/")[-1])
group_id1 = int(group_path1.split("/")[-1])
lecturer_id1 = int(lecturer_path1.split("/")[-1])
room_id2 = int(room_path2.split("/")[-1])
group_id2 = int(group_path2.split("/")[-1])
lecturer_id2 = int(lecturer_path2.split("/")[-1])
request_obj = [
{
"name": name1,
"room_id": [room_id1],
"group_id": [group_id1],
"lecturer_id": [lecturer_id1],
"start_ts": "2022-08-26T22:32:38.575Z",
"end_ts": "2022-08-26T22:32:38.575Z",
},
{
"name": name2,
"room_id": [room_id2],
"group_id": [group_id2],
"lecturer_id": [lecturer_id2],
"start_ts": "2022-08-26T22:32:38.575Z",
"end_ts": "2022-08-26T22:32:38.575Z",
},
]
response = client_auth.post(f"{RESOURCE}bulk", json=request_obj)
assert response.status_code == status.HTTP_200_OK, response.json()
assert response.json()[0]["name"] == request_obj[0]["name"]
response = client_auth.post(f"{RESOURCE}bulk", json=request_obj)
assert response.status_code == status.HTTP_200_OK
assert len(response.json()) == 0
events = dbsession.query(Event).filter(Event.name == name1).all()
assert len(events) == 1
events = dbsession.query(Event).filter(Event.name == name2).all()
assert len(events) == 1


def test_delete(client_auth: TestClient, dbsession: Session, room_path, lecturer_path, group_path):
room_id = int(room_path.split("/")[-1])
group_id = int(group_path.split("/")[-1])
Expand Down
Loading