From 030ba05ab6e6372f10354adc8e284503b96131a5 Mon Sep 17 00:00:00 2001 From: Nikolai Date: Mon, 7 Jun 2021 16:29:16 -0700 Subject: [PATCH] membership.create() checks for joinable and player limit; factory creates joinable lexicons by default --- amanuensis/backend/membership.py | 25 +++++++++++++++++++++++++ tests/conftest.py | 5 ++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/amanuensis/backend/membership.py b/amanuensis/backend/membership.py index 7440353..470964e 100644 --- a/amanuensis/backend/membership.py +++ b/amanuensis/backend/membership.py @@ -2,6 +2,10 @@ Membership query interface """ +from sqlalchemy.sql.elements import Null +from sqlalchemy.sql.functions import current_date +from sqlalchemy.util.langhelpers import NoneType +from amanuensis.db.models import Lexicon from sqlalchemy import select, func from amanuensis.db import DbContext, Membership @@ -36,6 +40,27 @@ def create( ): 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() + + # Verify lexicon is joinable; current no Lexicons are joinable so this is commented out + if not lex.joinable: + raise ArgumentError("Can't join: Lexicon is not joinable") + + # Verify lexicon is not full + if lex.player_limit: + 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( user_id=user_id, lexicon_id=lexicon_id, diff --git a/tests/conftest.py b/tests/conftest.py index a93b93c..8e97a6c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,7 @@ pytest test fixtures """ import pytest +from sqlalchemy.orm import session from amanuensis.db import DbContext import amanuensis.backend.character as charq @@ -49,7 +50,9 @@ def make_lexicon(db: DbContext): } state["nonce"] += 1 updated_kwargs = {**default_kwargs, **kwargs} - return lexiq.create(db, **updated_kwargs) + lex = lexiq.create(db, **updated_kwargs) + lex.joinable = True + return lex return lexicon_factory