class Cowboy(sheraf.models.IntOrderedNamedAttributesModel): table = "my_model_queryset" age = sheraf.IntegerAttribute() name = sheraf.StringAttribute(default="John Doe") email = sheraf.StringAttribute().index(unique=True) genre = sheraf.StringAttribute(default="M") size = sheraf.IntegerAttribute().index()
class FarmB(tests.IntAutoModel): owner = sheraf.ReverseModelAttribute("CowboyB", "farms") size = sheraf.IntegerAttribute() @size.on_edition def update_size(self, new, old): attr = self.owner.attributes["farms"] old_values = self.owner.before_index_edition(attr) yield self.owner.after_index_edition(attr, old_values)
class FarmD(tests.IntAutoModel): owners = sheraf.ReverseModelAttribute("CowboyD", "farms") size = sheraf.IntegerAttribute() @size.on_edition def update_size(self, new, old): attr = CowboyD.attributes["farms"] old_values = { owner: owner.before_index_edition(attr) for owner in self.owners } yield for owner, old_value in old_values.items(): owner.after_index_edition(attr, old_value)
import pytest import sheraf import tests @pytest.mark.parametrize( "persistent_type", [sheraf.types.SmallDict, sheraf.types.LargeDict, dict]) @pytest.mark.parametrize("subattribute", [None, sheraf.IntegerAttribute()]) def test_dict_attribute(sheraf_connection, persistent_type, subattribute): class Model(tests.UUIDAutoModel): dict = sheraf.DictAttribute(attribute=subattribute, persistent_type=persistent_type) m = Model.create() assert not m.dict assert 0 == len(m.dict) m.dict.update({"a": 0, "b": 1}) assert 0 == m.dict["a"] assert 1 == m.dict["b"] assert 2 == len(m.dict) assert m.dict assert "a" in m.dict.keys() assert 1 in m.dict.values() for k, v in m.dict.items(): assert (k, v) in [("a", 0), ("b", 1)] for k in m.dict: assert k in ("a", "b")
class Horse(sheraf.AttributeModel): name = sheraf.StringAttribute().index(primary=True) size = sheraf.IntegerAttribute()
class B(A): foo = sheraf.IntegerAttribute(default=33)
class Model(tests.UUIDAutoModel): attr = sheraf.IntegerAttribute()
class Model(tests.UUIDAutoModel): set = sheraf.SetAttribute( attribute=sheraf.IntegerAttribute(), persistent_type=persistent_type )
class Model(tests.UUIDAutoModel): status = sheraf.EnumAttribute(Enum, sheraf.IntegerAttribute())
class Model(tests.UUIDAutoModel): status = sheraf.EnumAttribute(Enum, sheraf.IntegerAttribute(), default=Enum.BAR).index()
class ModelInteger(tests.IntAutoModel): foo = sheraf.IntegerAttribute().index(noneok=True, unique=unique) bar = sheraf.IntegerAttribute().index(noneok=False, unique=unique)
import pytest import sheraf import sheraf.exceptions import tests import warnings # ---------------------------------------------------------------------------- # Types # ---------------------------------------------------------------------------- @pytest.mark.parametrize( "instance, mapping", [ (sheraf.IntegerAttribute().index(unique=True), BTrees.LOBTree.LOBTree), # ( sheraf.IntegerAttribute().index(unique=True, key="my_int"), BTrees.IOBTree.IOBTree) ( sheraf.SimpleAttribute(default=int).index(unique=True), BTrees.OOBTree.OOBTree, ), ], ) def test_integer_unique_index_creation(sheraf_database, instance, mapping): class Model(tests.IntAutoModel): my_attribute = instance with sheraf.connection(commit=True): mfoo = Model.create(my_attribute=22) with sheraf.connection() as conn:
class ModelWithProposeId(Model): table = "modelwithproposeid" id = sheraf.IntegerAttribute(default=lambda m: m.count()).index(primary=True)
class M(tests.UUIDAutoModel): id = sheraf.IntegerAttribute(default=lambda m: m.count()).index( primary=True)
class M(tests.UUIDAutoModel): foo = sheraf.IntegerAttribute() evens = sheraf.Index(foo, values=lambda x: {x} if x % 2 == 0 else {})
class M(sheraf.IntIndexedNamedAttributesModel): table = "test_int_model" id = sheraf.IntegerAttribute(default=mid).index(primary=True)