Compare commits
8 Commits
00738b5a45
...
f7069f233f
Author | SHA1 | Date |
---|---|---|
Tim Van Baak | f7069f233f | |
Tim Van Baak | 64a6147025 | |
Tim Van Baak | d29a5741e0 | |
Tim Van Baak | bcfdbf0c32 | |
Tim Van Baak | 142ea1a9ba | |
Tim Van Baak | 40b031e198 | |
Tim Van Baak | 80bb7a3d28 | |
Tim Van Baak | d9cdd4372a |
|
@ -5,7 +5,6 @@ Membership query interface
|
||||||
from sqlalchemy import select, func
|
from sqlalchemy import select, func
|
||||||
|
|
||||||
from amanuensis.db import DbContext, Membership
|
from amanuensis.db import DbContext, Membership
|
||||||
from amanuensis.db.models import Lexicon
|
|
||||||
from amanuensis.errors import ArgumentError
|
from amanuensis.errors import ArgumentError
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,25 +36,6 @@ def create(
|
||||||
):
|
):
|
||||||
raise ArgumentError("User is already a member of lexicon")
|
raise ArgumentError("User is already a member of lexicon")
|
||||||
|
|
||||||
# get reference to lexicon for next few checks
|
|
||||||
lex: Lexicon = db(
|
|
||||||
select(Lexicon).where(Lexicon.id == lexicon_id)
|
|
||||||
).scalar_one_or_none()
|
|
||||||
if not lex:
|
|
||||||
raise ArgumentError("could not find lexicon")
|
|
||||||
|
|
||||||
# Verify lexicon is joinable
|
|
||||||
if not lex.joinable:
|
|
||||||
raise ArgumentError("Can't join: Lexicon is not joinable")
|
|
||||||
|
|
||||||
# Verify lexicon is not full
|
|
||||||
if lex.player_limit is not None:
|
|
||||||
if (
|
|
||||||
db(select(func.count()).where(Membership.lexicon_id == lexicon_id)).scalar()
|
|
||||||
>= lex.player_limit
|
|
||||||
):
|
|
||||||
raise ArgumentError("Can't join: Lexicon is full")
|
|
||||||
|
|
||||||
new_membership = Membership(
|
new_membership = Membership(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
lexicon_id=lexicon_id,
|
lexicon_id=lexicon_id,
|
||||||
|
|
|
@ -4,10 +4,9 @@ Post query interface
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select, func
|
||||||
|
|
||||||
from amanuensis.db import DbContext, Post
|
from amanuensis.db import DbContext, Post
|
||||||
from amanuensis.db.models import Lexicon
|
|
||||||
from amanuensis.errors import ArgumentError
|
from amanuensis.errors import ArgumentError
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,14 +34,6 @@ def create(
|
||||||
if not body.strip():
|
if not body.strip():
|
||||||
raise ArgumentError("Post body cannot be empty.")
|
raise ArgumentError("Post body cannot be empty.")
|
||||||
|
|
||||||
# Check that the lexicon allows posting
|
|
||||||
if not (
|
|
||||||
db(select(Lexicon).where(Lexicon.id == lexicon_id))
|
|
||||||
.scalar_one_or_none()
|
|
||||||
.allow_post
|
|
||||||
):
|
|
||||||
raise ArgumentError("Lexicon does not allow posting.")
|
|
||||||
|
|
||||||
new_post = Post(lexicon_id=lexicon_id, user_id=user_id, body=body)
|
new_post = Post(lexicon_id=lexicon_id, user_id=user_id, body=body)
|
||||||
db.session.add(new_post)
|
db.session.add(new_post)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
|
@ -49,10 +49,7 @@ def make_lexicon(db: DbContext):
|
||||||
}
|
}
|
||||||
state["nonce"] += 1
|
state["nonce"] += 1
|
||||||
updated_kwargs = {**default_kwargs, **kwargs}
|
updated_kwargs = {**default_kwargs, **kwargs}
|
||||||
lex = lexiq.create(db, **updated_kwargs)
|
return lexiq.create(db, **updated_kwargs)
|
||||||
lex.joinable = True
|
|
||||||
db.session.commit()
|
|
||||||
return lex
|
|
||||||
|
|
||||||
return lexicon_factory
|
return lexicon_factory
|
||||||
|
|
||||||
|
|
|
@ -15,15 +15,7 @@ def test_create_membership(db: DbContext, make):
|
||||||
new_lexicon: Lexicon = make.lexicon()
|
new_lexicon: Lexicon = make.lexicon()
|
||||||
assert new_lexicon.id, "Failed to create lexicon"
|
assert new_lexicon.id, "Failed to create lexicon"
|
||||||
|
|
||||||
# Joining doesn't work when joinable is false
|
# Add the user to the lexicon as an editor
|
||||||
new_lexicon.joinable = False
|
|
||||||
db.session.commit()
|
|
||||||
with pytest.raises(ArgumentError):
|
|
||||||
memq.create(db, new_user.id, new_lexicon.id, True)
|
|
||||||
|
|
||||||
# Joining works when joinable is true
|
|
||||||
new_lexicon.joinable = True
|
|
||||||
db.session.commit()
|
|
||||||
mem: Membership = memq.create(db, new_user.id, new_lexicon.id, True)
|
mem: Membership = memq.create(db, new_user.id, new_lexicon.id, True)
|
||||||
assert mem, "Failed to create membership"
|
assert mem, "Failed to create membership"
|
||||||
|
|
||||||
|
@ -46,16 +38,3 @@ def test_create_membership(db: DbContext, make):
|
||||||
# Check that joining twice is not allowed
|
# Check that joining twice is not allowed
|
||||||
with pytest.raises(ArgumentError):
|
with pytest.raises(ArgumentError):
|
||||||
memq.create(db, new_user.id, new_lexicon.id, False)
|
memq.create(db, new_user.id, new_lexicon.id, False)
|
||||||
|
|
||||||
# Check that joining full lexicon not allowed
|
|
||||||
new_lexicon.player_limit = 1
|
|
||||||
db.session.commit()
|
|
||||||
two_user: User = make.user()
|
|
||||||
|
|
||||||
with pytest.raises(ArgumentError):
|
|
||||||
memq.create(db, two_user.id, new_lexicon.id, False)
|
|
||||||
|
|
||||||
new_lexicon.player_limit = 2
|
|
||||||
db.session.commit()
|
|
||||||
mem2: Membership = memq.create(db, two_user.id, new_lexicon.id, False)
|
|
||||||
assert mem2, "Failed to join lexicon with open player slots"
|
|
||||||
|
|
Loading…
Reference in New Issue