You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This may be related to the questions posed in #21, but I figured it best to post a fresh topic to express how important validation functions are. Coincidentally, it's the main reason I am having to look into this package. I have a JSON field where I want to store a simple list of strings. Now that SQLite support JSON fields as of version 3.9 (thanks to JSON1) I attempted to setup a list field a few different ways, but unfortunately quickly realized the limitations with mutations on these fields.
I've searched for possible solutions to this issue, which led me to this article, which inevitably led me to this package.
models.py
fromsqlalchemy_jsonimportNestedMutableJson# because I need to store a list, not a dictfromsqlalchemy.ext.mutableimportMutableListfromsqlalchemy.ormimportvalidatesfromappimportdb# SQLAlchemy instance via flask_sqlalchemy# SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'classPortal(db.Model):
__tablename__='portal'id=db.Column(db.Integer, primary_key=True)
name=db.Column(db.String(100), nullable=False)
# ex: valid_kwargs will be json array of ['full_year']valid_kwargs=db.Column(MutableList.as_mutable(db.JSON), nullable=False, default=list())
classAccount(db.Model):
__tablename__='account'id=db.Column(db.Integer, primary_key=True)
portal_id=db.Column(db.Integer, db.ForeignKey('portal.id')) # one-to-one relationshipusername=db.Column(db.String(100), nullable=False)
password=db.Column(db.String(100), nullable=False)
# portal = accessible thanks to portal_id/portal.id ForeignKey# for extra fields, specific to each portal# ATTEMPT 1 - using sqlalchemy.ext.mutable.MutableList, no extra packageskwargs=db.Column(MutableList.as_mutable(db.JSON), nullable=False, default=list())
# ATTEMPT 2 - using NestedMutableJson from sqlalchemy_jsonkwargs=db.Column(NestedMutableJson, nullable=False, default=list())
@validates('kwargs')defvalidates_kwargs(self, key, value):
# make sure that only valid_kwargs are added to the accountifvalueandnotall([kwinself.portal.valid_kwargsforkwinvalue]):
raiseValueError('Invalid value provided in kwargs')
returnvalue
unit_tests/test_models.py
deftest_account(test_db, account):
""" GIVEN a database instance WHEN a new account is created THEN check that the username, password, kwargs, and reports fields are defined correctly """test_db.session.add(account)
test_db.session.commit()
assertAccount.query.count() ==1assertaccount.username=='mhill'assertaccount.password=='password1'assertisinstance(account.kwargs, list) andlen(account.kwargs) ==0# check that kwargs defaults to an empty listassertaccount.reports.count() ==0# check that reports relationship is presentassertisinstance(account.portal, Portal) # check that portal relationship is presentaccount.portal.valid_kwargs.append('full_year')
account.kwargs.append('full_year')
test_db.session.add(account)
test_db.session.commit()
assertlen(account.kwargs) ==1assert'full_year'inaccount.kwargs""" GIVEN an account whose associated portal has valid_kwargs WHEN kwargs are added to the account THEN check that only the kwargs in the portal's valid_kwargs are allowed to be added """# FIXME: Doesn't throw an errorwithpytest.raises(ValueError):
account.kwargs.append('illegal_kwarg')
test_db.session.add(account)
test_db.session.commit()
The text was updated successfully, but these errors were encountered:
This may be related to the questions posed in #21, but I figured it best to post a fresh topic to express how important validation functions are. Coincidentally, it's the main reason I am having to look into this package. I have a JSON field where I want to store a simple list of strings. Now that SQLite support JSON fields as of version 3.9 (thanks to JSON1) I attempted to setup a list field a few different ways, but unfortunately quickly realized the limitations with mutations on these fields.
I've searched for possible solutions to this issue, which led me to this article, which inevitably led me to this package.
models.py
unit_tests/test_models.py
The text was updated successfully, but these errors were encountered: