class CustomSearchModelA(tests.IntAutoModel): foo = sheraf.SimpleAttribute().index( unique=True, values=lambda string: {string.lower()}, search=lambda string: [string.lower()[::-1], string.lower()], ) bar = sheraf.SimpleAttribute().index()
class Model(tests.IntAutoModel): foo = sheraf.SimpleAttribute() bar = sheraf.SimpleAttribute() theindex = sheraf.Index(foo, bar) @theindex.values def values(self, value): return {value.lower(), value.upper()}
class CommonValuesComputedTogetherModelB(tests.IntAutoModel): foo = sheraf.SimpleAttribute() bar = sheraf.SimpleAttribute() theindex = sheraf.Index(foo, bar) @theindex.values(foo, bar) def theindex_values(self, foo_, bar_): return {f"{foo_} {bar_}"}
class CommonValuesComputedSeparatelyModelA(tests.IntAutoModel): foo = sheraf.SimpleAttribute() bar = sheraf.SimpleAttribute() theindex = sheraf.Index(foo, bar) @theindex.values(foo) @theindex.values(bar) def values(self, x): return {x.upper()}
class CustomIndexationModelF(tests.IntAutoModel): foo = sheraf.SimpleAttribute() bar = sheraf.SimpleAttribute() fooindex = sheraf.Index(foo, key="foo", unique=True) barindex = sheraf.Index(bar, key="bar") @fooindex.values def fooindex_values(self, value): return value.lower() if value else None
class CustomIndexationModelB(tests.IntAutoModel): foo = sheraf.SimpleAttribute() bar = sheraf.SimpleAttribute() fooindex = sheraf.Index( "foo", key="foo", unique=True, values=lambda string: {string.lower() if string else None}, ) barindex = sheraf.Index("bar", key="bar")
class CustomSearchModelB(tests.IntAutoModel): foo = sheraf.SimpleAttribute() bar = sheraf.SimpleAttribute() fooindex = sheraf.Index( "foo", key="foo", unique=True, values=lambda string: {string.lower()}, search=lambda string: [string.lower()[::-1], string.lower()], ) barindex = sheraf.Index("bar", key="bar")
class Model1(sheraf.Model): table = "model1_table" simple = sheraf.SimpleAttribute(lazy=False) my_inline_model = sheraf.InlineModelAttribute(InlineModel, lazy=False) my_blob = sheraf.BlobAttribute(lazy=False) counter = sheraf.CounterAttribute(lazy=False)
class Model(tests.UUIDAutoModel): counter = sheraf.CounterAttribute(default=0) useless = sheraf.InlineModelAttribute( sheraf.InlineModel(foo=sheraf.SimpleAttribute(default="bar", lazy=False), ), lazy=False, )
class CustomSearchModelE(tests.IntAutoModel): foo = sheraf.SimpleAttribute() bar = sheraf.SimpleAttribute() fooindex = sheraf.Index( foo, key="foo", unique=True, values=lambda string: {string.lower()}, ) barindex = sheraf.Index(bar, key="bar") @fooindex.search def search_foo(self, value): return [value.lower()[::-1], value.lower()]
class Model(tests.IntAutoModel): deleted = False foo = sheraf.SimpleAttribute() @foo.on_deletion def foo_deletion(self, old): self.deleted = True
class Model(tests.IntAutoModel): edited = False foo = sheraf.SimpleAttribute() @foo.on_edition def foo_update(self, new, old): if new == "whatever": self.edited = True
class Model(tests.IntAutoModel): created = False foo = sheraf.SimpleAttribute() @foo.on_creation def foo_creation(self, new): if new == "whatever": self.created = True
def test_index(): attribute = sheraf.SimpleAttribute() primary = Index(attribute, key="primary", primary=True) unique = Index(attribute, key="unique", unique=True) multiple = Index(attribute, key="multiple", unique=False) assert "<Index key=primary unique=True primary>" == repr(primary) assert "<Index key=unique unique=True>" == repr(unique) assert "<Index key=multiple unique=False>" == repr(multiple)
class CommonModelDefaultValuesMethodsA(tests.IntAutoModel): def lower(self, value): return {value.lower()} foo = sheraf.StringAttribute() bar = sheraf.SimpleAttribute() theindex = sheraf.Index(foo, bar, values=lower) @theindex.values(bar) def upper(self, bar): return {bar.upper()}
class CommonModelDifferentValuesMethodsB(tests.IntAutoModel): foo = sheraf.StringAttribute() bar = sheraf.SimpleAttribute() theindex = sheraf.Index(foo, bar) @theindex.values(foo) def lower(self, foo): return {foo.lower()} @theindex.values(bar) def upper(self, bar): return {bar.upper()}
class Child(sheraf.InlineModel): nom = sheraf.SimpleAttribute() a1 = sheraf.SimpleAttribute() a2 = sheraf.SimpleAttribute() a3 = sheraf.SimpleAttribute() @classmethod def create(cls, val): model = super().create() model.nom = val model.a1 = val model.a2 = val model.a3 = val model.check(val) return model def check(self, key): assert key == self.nom == self.a1 == self.a2 == self.a3 def __eq__(self, other): return (self.nom == other.nom and self.a1 == other.a1 and self.a2 == other.a2 and self.a3 == other.a3)
class FileStorable(tests.UUIDAutoModel): attr = sheraf.SimpleAttribute() file = sheraf.FileAttribute()
class FileStorable(tests.UUIDAutoModel): a = sheraf.SimpleAttribute() b = sheraf.SimpleAttribute() c = sheraf.SmallListAttribute() d = sheraf.LargeDictAttribute()
class HorseWithNoName(sheraf.AttributeModel): foo = sheraf.SimpleAttribute() bar = sheraf.SimpleAttribute()
class BarModel(tests.UUIDAutoModel): my_attribute = sheraf.SimpleAttribute(default="hell yeah")
class M(tests.UUIDAutoModel): foo = sheraf.SimpleAttribute(default=lambda: None)
class B(A, sheraf.DatedNamedAttributesModel): bar = sheraf.SimpleAttribute(default="bar")
class A(sheraf.BaseModel): foo = sheraf.SimpleAttribute(default="foo")
class A(tests.UUIDAutoModel): foo = sheraf.SimpleAttribute(default="foo_A") bar = sheraf.SimpleAttribute(default="bar_A")
class Model(tests.UUIDAutoModel): foo = sheraf.SimpleAttribute() mylist = sheraf.LargeListAttribute()
class FoobarModel(tests.UUIDAutoModel): my_attribute = sheraf.SimpleAttribute(default="hell yeah", lazy=False)
class BadParent(tests.UUIDAutoModel): child = sheraf.SetAttribute(sheraf.SimpleAttribute()).index()
class Model(tests.UUIDAutoModel): field = sheraf.SimpleAttribute()
class BTreesLengthModel(sheraf.Model): table = "model_test_simple_attribute_replacement" unique_table_name = False counter = sheraf.SimpleAttribute(default=BTrees.Length.Length)