Example #1
0
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()
Example #2
0
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)
Example #3
0
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)
Example #4
0
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")
Example #5
0
class Horse(sheraf.AttributeModel):
    name = sheraf.StringAttribute().index(primary=True)
    size = sheraf.IntegerAttribute()
Example #6
0
 class B(A):
     foo = sheraf.IntegerAttribute(default=33)
Example #7
0
 class Model(tests.UUIDAutoModel):
     attr = sheraf.IntegerAttribute()
Example #8
0
 class Model(tests.UUIDAutoModel):
     set = sheraf.SetAttribute(
         attribute=sheraf.IntegerAttribute(), persistent_type=persistent_type
     )
Example #9
0
 class Model(tests.UUIDAutoModel):
     status = sheraf.EnumAttribute(Enum, sheraf.IntegerAttribute())
Example #10
0
 class Model(tests.UUIDAutoModel):
     status = sheraf.EnumAttribute(Enum,
                                   sheraf.IntegerAttribute(),
                                   default=Enum.BAR).index()
Example #11
0
 class ModelInteger(tests.IntAutoModel):
     foo = sheraf.IntegerAttribute().index(noneok=True, unique=unique)
     bar = sheraf.IntegerAttribute().index(noneok=False, unique=unique)
Example #12
0
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:
Example #13
0
 class ModelWithProposeId(Model):
     table = "modelwithproposeid"
     id = sheraf.IntegerAttribute(default=lambda m: m.count()).index(primary=True)
Example #14
0
 class M(tests.UUIDAutoModel):
     id = sheraf.IntegerAttribute(default=lambda m: m.count()).index(
         primary=True)
Example #15
0
 class M(tests.UUIDAutoModel):
     foo = sheraf.IntegerAttribute()
     evens = sheraf.Index(foo, values=lambda x: {x} if x % 2 == 0 else {})
Example #16
0
 class M(sheraf.IntIndexedNamedAttributesModel):
     table = "test_int_model"
     id = sheraf.IntegerAttribute(default=mid).index(primary=True)