2021-05-31 19:13:37 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
|
|
|
from amanuensis.db import *
|
|
|
|
from amanuensis.errors import ArgumentError
|
2021-05-30 00:38:14 +00:00
|
|
|
import amanuensis.backend.membership as memq
|
|
|
|
|
|
|
|
|
2021-06-02 01:46:19 +00:00
|
|
|
def test_create_membership(db: DbContext, make):
|
2021-05-30 00:38:14 +00:00
|
|
|
"""Test joining a game."""
|
|
|
|
# Set up a user and a lexicon
|
2021-06-03 04:31:42 +00:00
|
|
|
new_user: User = make.user()
|
2021-06-03 03:10:34 +00:00
|
|
|
assert new_user.id, "Failed to create user"
|
2021-06-03 04:31:42 +00:00
|
|
|
new_lexicon: Lexicon = make.lexicon()
|
2021-06-03 03:10:34 +00:00
|
|
|
assert new_lexicon.id, "Failed to create lexicon"
|
2021-05-30 00:38:14 +00:00
|
|
|
|
|
|
|
# Add the user to the lexicon as an editor
|
2021-06-03 04:31:42 +00:00
|
|
|
mem: Membership = memq.create(db, new_user.id, new_lexicon.id, True)
|
2021-06-03 03:10:34 +00:00
|
|
|
assert mem, "Failed to create membership"
|
2021-05-30 00:38:14 +00:00
|
|
|
|
|
|
|
# Check that the user and lexicon are mutually visible in the ORM relationships
|
2021-05-31 19:13:37 +00:00
|
|
|
assert any(map(lambda mem: mem.lexicon == new_lexicon, new_user.memberships))
|
|
|
|
assert any(map(lambda mem: mem.user == new_user, new_lexicon.memberships))
|
2021-05-30 00:38:14 +00:00
|
|
|
|
|
|
|
# Check that the editor flag was set properly
|
2021-06-03 04:31:42 +00:00
|
|
|
editor: User = db(
|
2021-05-31 19:13:37 +00:00
|
|
|
select(User)
|
|
|
|
.join(User.memberships)
|
|
|
|
.join(Membership.lexicon)
|
|
|
|
.where(Lexicon.id == new_lexicon.id)
|
|
|
|
.where(Membership.is_editor == True)
|
|
|
|
).scalar_one()
|
|
|
|
assert editor is not None
|
|
|
|
assert isinstance(editor, User)
|
|
|
|
assert editor.id == new_user.id
|
|
|
|
|
|
|
|
# Check that joining twice is not allowed
|
|
|
|
with pytest.raises(ArgumentError):
|
2021-06-03 04:31:42 +00:00
|
|
|
memq.create(db, new_user.id, new_lexicon.id, False)
|