Пример #1
0
def test_use_schema_without_registration_raises_typeerror():
    with minidb.Store(debug=True) as db:

        class Foo(minidb.Model):
            bar = str

        Foo.query(db)
Пример #2
0
def test_group_by_with_sum():
    class Foo(minidb.Model):
        bar = str
        baz = int

    with minidb.Store(debug=True) as db:
        db.register(Foo)

        for i in range(5):
            Foo(bar='hi', baz=i).save(db)

        for i in range(6):
            Foo(bar='ho', baz=i).save(db)

        expected = {('hi', sum(range(5))), ('ho', sum(range(6)))}

        # minidb.func.sum(COLUMN)(NAME)
        result = {
            tuple(x)
            for x in Foo.query(db,
                               lambda c: c.bar // minidb.func.sum(c.baz)
                               ('sum'),
                               group_by=lambda c: c.bar)
        }
        eq_(result, expected)

        # COLUMN.sum(NAME)
        result = {
            tuple(x)
            for x in Foo.query(db,
                               lambda c: c.bar // c.baz.sum('sum'),
                               group_by=lambda c: c.bar)
        }
        eq_(result, expected)
Пример #3
0
def test_custom_converter():
    class Point(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y

    @minidb.converter_for(Point)
    def convert_point(v, serialize):
        if serialize:
            return ','.join(str(x) for x in (v.x, v.y))
        else:
            return Point(*(float(x) for x in v.split(',')))

    class Player(minidb.Model):
        name = str
        position = Point

    with minidb.Store(debug=True) as db:
        db.register(Player)
        p = Point(1.12, 5.99)
        player_id = Player(name='Foo', position=p).save(db).id
        get_value = Player.get(db, id=player_id)
        assert type(get_value.position) == Point
        assert (get_value.position.x, get_value.position.y) == (p.x, p.y)
        query_value = next(
            Player.query(db,
                         lambda c: c.position,
                         where=lambda c: c.id == player_id))
        assert type(query_value.position) == Point
        assert (query_value.position.x, query_value.position.y) == (p.x, p.y)
Пример #4
0
def test_get_with_no_value_returns_none():
    class Mod(minidb.Model):
        mod = str

    with minidb.Store(debug=True) as db:
        db.register(Mod)
        assert Mod.get(db, mod='foo') is None
Пример #5
0
def test_storing_and_retrieving_datetime():
    DT_NOW = datetime.datetime.now()
    D_TODAY = datetime.date.today()
    T_NOW = datetime.datetime.now().time()

    class DateTimeModel(minidb.Model):
        dt = datetime.datetime
        da = datetime.date
        tm = datetime.time

    with minidb.Store(debug=True) as db:
        db.register(DateTimeModel)
        datetime_id = DateTimeModel(dt=DT_NOW, da=D_TODAY,
                                    tm=T_NOW).save(db).id
        get_value = DateTimeModel.get(db, id=datetime_id)
        assert type(get_value.dt) == datetime.datetime
        assert get_value.dt == DT_NOW
        assert type(get_value.da) == datetime.date
        assert get_value.da == D_TODAY
        assert type(get_value.tm) == datetime.time
        assert get_value.tm == T_NOW
        query_value = next(
            DateTimeModel.query(db,
                                lambda c: c.dt // c.da // c.tm,
                                where=lambda c: c.id == datetime_id))
        assert type(query_value.dt) == datetime.datetime
        assert query_value.dt == DT_NOW
        assert type(query_value.da) == datetime.date
        assert query_value.da == D_TODAY
        assert type(query_value.tm) == datetime.time
        assert query_value.tm == T_NOW
Пример #6
0
def test_saving_object_stores_id():
    with minidb.Store(debug=True) as db:
        db.register(FieldTest)
        field_test = FieldTest(998)
        assert field_test.id is None
        field_test.save(db)
        assert field_test.id is not None
Пример #7
0
def test_invalid_column_raises_attribute_error():
    class HasOnlyColumnX(minidb.Model):
        x = int

    with minidb.Store(debug=True) as db:
        db.register(HasOnlyColumnX)
        HasOnlyColumnX.c.y
Пример #8
0
def cached_person_main(with_delete=None):
    if with_delete is None:
        for i in range(2):
            cached_person_main(i)
        print('=' * 77)
        return
    print('=' * 20, 'Cached Person Main, with_delete =', with_delete, '=' * 20)

    debug_object_cache, minidb.DEBUG_OBJECT_CACHE = minidb.DEBUG_OBJECT_CACHE, True

    class CachedPerson(minidb.Model):
        name = str
        age = int
        _inst = object

    with minidb.Store(debug=True) as db:
        db.register(CachedPerson)
        p = CachedPerson(name='foo', age=12)
        p._inst = 123
        p.save(db)
        p_id = p.id
        if with_delete:
            del p
        p = CachedPerson.get(db, id=p_id)
        print('p._inst =', repr(p._inst))
    minidb.DEBUG_OBJECT_CACHE = debug_object_cache
Пример #9
0
def test_distinct():
    class Foo(minidb.Model):
        bar = str
        baz = int

    with minidb.Store(debug=True) as db:
        db.register(Foo)

        for i in range(2):
            Foo(bar='hi', baz=i).save(db)

        Foo(bar='ho', baz=7).save(db)

        expected = {('hi', ), ('ho', )}

        # minidb.func.distinct(COLUMN)(NAME)
        result = {
            tuple(x)
            for x in Foo.query(db, lambda c: minidb.func.distinct(c.bar)
                               ('foo'))
        }
        eq_(result, expected)

        # COLUMN.distinct(NAME)
        result = {tuple(x) for x in Foo.query(db, Foo.c.bar.distinct('foo'))}
        eq_(result, expected)
Пример #10
0
def test_reregistering_class_raises_typeerror():
    class Foo(minidb.Model):
        bar = int

    with minidb.Store(debug=True) as db:
        db.register(Foo)
        db.register(Foo)
Пример #11
0
def test_invalid_rowproxy_access_by_key():
    class Foo(minidb.Model):
        bar = str

    with minidb.Store(debug=True) as db:
        db.register(Foo)
        Foo(bar='baz').save(db)
        next(Foo.query(db, Foo.c.bar))['baz']
Пример #12
0
def test_registering_non_subclass_of_model_fails():
    # This cannot be registered, as it's not a subclass of minidb.Model
    class Something(object):
        column = str

    with minidb.Store(debug=True) as db:
        db.register(Something)
        db.register(Something)
Пример #13
0
def test_get_with_multiple_value_raises_exception():
    class Mod(minidb.Model):
        mod = str

    with minidb.Store(debug=True) as db:
        db.register(Mod)
        Mod(mod='foo').save(db)
        Mod(mod='foo').save(db)
        Mod.get(db, mod='foo')
Пример #14
0
def test_json_field_renamed_query():
    class WithJsonField(minidb.Model):
        bar = minidb.JSON

    with minidb.Store(debug=True) as db:
        db.register(WithJsonField)
        d = {'a': [1, True, 3.9]}
        WithJsonField(bar=d).save(db)
        eq_(next(WithJsonField.c.bar('renamed').query(db)).renamed, d)
Пример #15
0
    def __init__(self, filename):
        super().__init__(filename)

        dirname = os.path.dirname(filename)
        if dirname and not os.path.isdir(dirname):
            os.makedirs(dirname)

        self.db = minidb.Store(self.filename, debug=True)
        self.db.register(CacheEntry)
Пример #16
0
def test_json_serialization():
    class WithJsonField(minidb.Model):
        foo = str
        bar = minidb.JSON

    with minidb.Store(debug=True) as db:
        db.register(WithJsonField)
        d = {'a': 1, 'b': [1, 2, 3], 'c': [True, 4.0, {'d': 'e'}]}
        WithJsonField(bar=d).save(db)
        assert WithJsonField.get(db, id=1).bar == d
Пример #17
0
def test_loading_object_returns_cached_object():
    with minidb.Store(debug=True) as db:
        db.register(FieldTest)
        field_test = FieldTest(9999)
        field_test._private1 = 4711
        assert field_test.id is None
        field_test.save(db)
        assert field_test.id is not None
        field_test_loaded = FieldTest.get(db, id=field_test.id)(9999)
        assert field_test_loaded._private1 == 4711
        assert field_test_loaded is field_test
Пример #18
0
def test_loading_objects():
    with minidb.Store(debug=True) as db:
        db.register(FieldTest)
        for i in range(100):
            FieldTest(i).save(db)

        assert next(FieldTest.c.id.count('count').query(db)).count == 100

        for field_test in FieldTest.load(db)(997):
            assert field_test.id is not None
            assert field_test._private1 == 997
Пример #19
0
def test_query_with_datetime():
    DT_NOW = datetime.datetime.now()

    class DateTimeModel(minidb.Model):
        dt = datetime.datetime

    with minidb.Store(debug=True) as db:
        db.register(DateTimeModel)
        datetime_id = DateTimeModel(dt=DT_NOW).save(db).id
        assert DateTimeModel.get(db,
                                 lambda c: c.dt == DT_NOW).id == datetime_id
Пример #20
0
def test_double_delete_without_id_raises_valueerror():
    class Foo(minidb.Model):
        bar = str

    with minidb.Store(debug=True) as db:
        db.register(Foo)
        a = Foo(bar='hello')
        a.save(db)
        assert a.id is not None
        a.delete()
        assert a.id is None
        a.delete()
Пример #21
0
def test_use_schema_with_nonidentity_class_raises_typeerror():
    with minidb.Store(debug=True) as db:

        class Foo(minidb.Model):
            bar = str

        db.register(Foo)

        class Foo(minidb.Model):
            bar = str

        Foo.query(db)
Пример #22
0
def test_delete_object():
    class Foo(minidb.Model):
        bar = int

    with minidb.Store(debug=True) as db:
        db.register(Foo)
        for i in range(3):
            Foo(bar=i).save(db)

        Foo.get(db, bar=2).delete()

        assert {0, 1} == {bar for (bar, ) in Foo.c.bar.query(db)}
Пример #23
0
def test_upgrade_schema_with_different_type_raises_typeerror():
    with minidb.Store(debug=True) as db:

        class Foo(minidb.Model):
            bar = str

        db.register(Foo)

        class Foo(minidb.Model):
            bar = int

        db.register(Foo, upgrade=True)
Пример #24
0
def test_default_values_are_set_if_none():
    class Foo(minidb.Model):
        name = str

        class __minidb_defaults__:
            name = 'Bob'

    with minidb.Store(debug=True) as db:
        f = Foo()
        eq_(f.name, 'Bob')

        f = Foo(name='John')
        eq_(f.name, 'John')
Пример #25
0
def test_upgrade_schema_with_upgrade_succeeds():
    with minidb.Store(debug=True) as db:

        class Foo(minidb.Model):
            bar = str

        db.register(Foo)

        class Foo(minidb.Model):
            bar = str
            baz = int

        db.register(Foo, upgrade=True)
Пример #26
0
def test_loading_object_returns_new_object_after_reference_drop():
    with minidb.Store(debug=True) as db:
        db.register(FieldTest)
        field_test = FieldTest(9999)
        field_test._private1 = 4711
        assert field_test.id is None
        field_test.save(db)
        assert field_test.id is not None
        field_test_id = field_test.id
        del field_test

        field_test_loaded = FieldTest.get(db, id=field_test_id)(9999)
        assert field_test_loaded._private1 == 9999
Пример #27
0
def test_delete_where():
    class DeleteWhere(minidb.Model):
        v = int

    with minidb.Store(debug=True) as db:
        db.register(DeleteWhere)

        for i in range(10):
            DeleteWhere(v=i).save(db)

        assert DeleteWhere.delete_where(db, lambda c: c.v < 2) == len({0, 1})
        assert DeleteWhere.delete_where(db, DeleteWhere.c.v > 5) == len(
            {6, 7, 8, 9})
        assert {2, 3, 4, 5} == {v for (v, ) in DeleteWhere.c.v.query(db)}
Пример #28
0
def test_storing_and_retrieving_floats():
    class FloatModel(minidb.Model):
        value = float

    with minidb.Store(debug=True) as db:
        db.register(FloatModel)
        float_id = FloatModel(value=3.1415).save(db).id
        get_value = FloatModel.get(db, id=float_id).value
        assert type(get_value) == float
        assert get_value == 3.1415
        query_value = next(
            FloatModel.c.value.query(db,
                                     where=lambda c: c.id == float_id)).value
        assert type(query_value) == float
        assert query_value == 3.1415
Пример #29
0
def test_field_conversion_query_select_star():
    with minidb.Store(debug=True) as db:
        db.register(FieldConversion)
        FieldConversion.create().save(db)
        result = next(FieldConversion.query(db, minidb.literal('*')))
        assert isinstance(result.integer, int)
        assert isinstance(result.floating, float)
        assert isinstance(result.boolean, bool)
        assert isinstance(result.string, str)
        assert isinstance(result.jsoninteger, int)
        assert isinstance(result.jsonfloating, float)
        assert isinstance(result.jsonboolean, bool)
        assert isinstance(result.jsonstring, str)
        assert isinstance(result.jsonlist, list)
        assert isinstance(result.jsondict, dict)
        assert result.jsonnone is None
Пример #30
0
def test_update_object():
    class Foo(minidb.Model):
        bar = str

    with minidb.Store(debug=True) as db:
        db.register(Foo)
        a = Foo(bar='a').save(db)
        b = Foo(bar='b').save(db)

        a.bar = 'c'
        a.save()

        b.bar = 'd'
        b.save()

        assert {'c', 'd'} == {bar for (bar, ) in Foo.c.bar.query(db)}