def test_nimp():
    Corn = make_corn()
    type = return_type(
        Corn.all.map({'name': c.name, 'grou': c.id + 5, 'id': c.id}))
    assert type == List(inner_type=Dict(mapping={
        'name': Corn.properties['name'],
        'id': Corn.properties['id'],
        'grou': Type(int)}))
    type = return_type(Corn.all.one())
    assert type == Dict(corn=Corn, mapping=Corn.properties)
    type = return_type(Corn.all.map({
        'name': c.name + c.lastname,
        'test': c.len() + 10}).one())
    assert type == Dict(mapping={
        'name': Type(unicode),
        'test': Type(int)})
    type = return_type(Corn.all.map({
        'max': Corn.all.max(),
        'min': Corn.all.min(),
        'len': Corn.all.len(),
        'sum': Corn.all.sum(),
        'distinct': Corn.all.distinct(),
        'bool': c.name == c.lastname,
        'otherbool': ~(c.id < 3),
        'andor': (c.id < 3) | (c.name.len() < 2) & (c.lastname + "test"),
        'add': Corn.all.map(c.name + c.lastname),
        'sub': Corn.all.map(c.name + c.lastname),
        'mul': Corn.all.map(c.id * c.id),
        'div': Corn.all.map(c.id / c.id),
        'index': Corn.all[10],
        'slice': Corn.all[10:20],
        'one': Corn.all.one(),
        'onedefault': Corn.all.one(None),
        'onedefaulthomogeneous': Corn.all.one(Corn.all.one()),
        'sort': Corn.all.sort(c.name, ~c.lastname),
        'groupby': Corn.all.groupby(c.name),
        'heterogeneous_list': Corn.all + ['toto', 3],
        'homogeneous_list': [u'toto', u'tata'],
        'tuple': (1, 2, 3),
        'dict + dict': Corn.all.map(c + {'name': c.id})
        }))

    assert isinstance(type, List)
    mapping = type.inner_type.mapping
    for key in ('max', 'min', 'sum', 'one', 'index'):
        assert mapping[key] == Corn.type
    for key in ('len',):
        assert mapping[key] == Type(int)
    for key in ('bool', 'otherbool', 'andor'):
        assert mapping[key] == Type(bool)
    for key in ('distinct', 'slice', 'sort'):
        assert mapping[key] == List(Corn.type)
    assert mapping['groupby'] == List(Dict(mapping={
        'key': Corn.properties['name'],
        'group': List(Corn.type)}))
    assert mapping['heterogeneous_list'] == List(Type(object))
    assert mapping['homogeneous_list'] == List(Type(unicode))
    assert mapping['tuple'] == Type(tuple)
    assert mapping['onedefaulthomogeneous'] == Corn.type
    assert mapping['onedefault'] == Type(object)
    assert isinstance(mapping['dict + dict'], List)
    assert isinstance(mapping['dict + dict'].inner_type, Dict)
    type = return_type(Corn.all.map(c + {'name': c.id}))
    assert isinstance(type, List)
    assert isinstance(type.inner_type, Dict)
Beispiel #2
0
def test_optimization(Corn):
    class NotOptimizedError(Exception):
        pass

    def error():
        raise NotOptimizedError

    Corn._all = error
    Corn.create({'id': 1, 'name': u'foo', 'lastname': u'bar'}).save()
    Corn.create({'id': 2, 'name': u'baz', 'lastname': u'bar'}).save()
    Corn.create({'id': 3, 'name': u'foo', 'lastname': u'baz'}).save()
    items = list(Corn.all.execute())
    assert len(items) == 3
    items = list(Corn.all.filter(c.name == 'foo' ).execute())
    assert len(items) == 2
    assert all(item['name'] == 'foo' for item in items)
    items = list(Corn.all.filter((c.name == 'foo' ) &
        (c.lastname == 'bar')).execute())
    assert len(items) == 1
    assert items[0]['id'] == 1
    items = list(Corn.all.filter((c.name == 'baz' ) |
        (c.lastname == 'baz')).execute())
    assert len(items) == 2
    assert 2 in (x['id'] for x in items)
    assert 3 in (x['id'] for x in items)
    items = list(Corn.all.filter(c.name == 'foo').execute())
    assert len(items) == 2
    assert all(item['name'] == 'foo' for item in items)
    items = list(Corn.all.filter((c.name == 'foo' ) &
        (c.lastname == 'bar')).execute())
    assert len(items) == 1
    assert items[0]['id'] == 1
    assert all(item.corn == Corn for item in items)
    items = list(Corn.all.filter((c.name == 'baz' ) |
        (c.lastname == 'baz')).execute())
    assert len(items) == 2
    assert 2 in (x['id'] for x in items)
    assert 3 in (x['id'] for x in items)
    assert all(item.corn == Corn for item in items)
    items = list(Corn.all.filter(c.id < 2).execute())
    assert len(items) == 1
    assert items[0]['id'] == 1
    assert all(item.corn == Corn for item in items)
    items = list(Corn.all.filter((c.id < 3) & (c.id > 1)).execute())
    assert len(items) == 1
    assert items[0]['id'] == 2
    assert all(item.corn == Corn for item in items)
    items = list(Corn.all.filter(c.id >= 2).execute())
    assert len(items) == 2
    assert 2 in (x['id'] for x in items)
    assert 3 in (x['id'] for x in items)
    assert all(item.corn == Corn for item in items)
    items = list(Corn.all.filter(c.id <= 2).execute())
    assert len(items) == 2
    assert 1 in (x['id'] for x in items)
    assert 2 in (x['id'] for x in items)
    assert all(item.corn == Corn for item in items)
    items = list(Corn.all.filter(c.id != 2).execute())
    assert len(items) == 2
    assert 1 in (x['id'] for x in items)
    assert 3 in (x['id'] for x in items)
    assert all(item.corn == Corn for item in items)
    items = list(Corn.all.map(c.name).execute())
    assert len(items) == 3
    assert all(type(item) == unicode for item in items)
    items = list(Corn.all.map({'foo': c.name}).execute())
    assert len(items) == 3
    assert all(type(item) == dict for item in items)
    assert all('foo' in item for item in items)
    items = list(Corn.all.map({'foo': c.name}).filter(c.foo == 'baz').execute())
    assert len(items) == 1
    assert all(type(item) == dict for item in items)
    assert all(item['foo'] == 'baz' for item in items)
    items = list(Corn.all.map(c + {'doubleid' : c.id}).execute())
    assert len(items) == 3
    assert all(type(item) == dict for item in items)
    assert all(item['doubleid'] == item['id'] for item in items)
    items = list(Corn.all.map(c + {'square' : c.id * c.id}).execute())
    assert len(items) == 3
    assert all(type(item) == dict for item in items)
    assert all(item['square'] == item['id'] ** 2 for item in items)
    items = list(Corn.all.map(c + c).execute())
    assert len(items) == 3
    items = list(Corn.all.map(c + Corn.all.filter(c.id == c(-1).id).map({
            'otherid': c.id, 'othername': c.name, 'otherlastname': c.lastname}).one()).execute())
    assert all(item['id'] == item['otherid'] for item in items)
    items = list(Corn.all.map(c + Corn.all.filter(c.name == c(-1).name).map({
            'otherid': c.id, 'othername': c.name, 'otherlastname': c.lastname}).one()).execute())
    assert len(items) == 5
    assert all(item['name'] == item['othername'] for item in items)
    items = list(Corn.all.map(c + {'foreign': Corn.all.filter(c.id == c(-1).id).one()}).execute())
    assert len(items) == 3
    assert all(hasattr(item['foreign'], 'corn') for item in items)
    assert all(item['foreign']['id'] == item['id'] for item in items)
    items = list(Corn.all.map(c + {'homonymes': Corn.all.filter(c.name == c(-1).name)}).execute())
    assert len(items) == 3
    assert all(all(subitem['name'] == item['name']
        for subitem in item['homonymes'])
            for item in items)
    items = list(Corn.all.sort(c.name).execute())
    assert [item['name'] for item in items] == ['baz', 'foo', 'foo']
    items = list(Corn.all.sort(-c.name).execute())
    assert [item['name'] for item in items] == ['foo', 'foo', 'baz']
    items = list(Corn.all.sort(-c.name, -c.id).execute())
    assert items[0]['name'] == 'foo' and items[0]['id'] == 3
    assert items[1]['name'] == 'foo' and items[1]['id'] == 1
    assert items[2]['name'] == 'baz' and items[2]['id'] == 2
    length = Corn.all.len().execute()
    assert length == 3
    items = list(Corn.all.groupby(c.name, c.len()).sort(c.group).execute())
    assert len(items) == 2
    assert items[0]['group'] == 1
    assert items[1]['group'] == 2
    item = Corn.all.map(c.id).sum().execute()
    assert item == 6
    item = Corn.all.map(c.id).max().execute()
    assert item == 3
    item = Corn.all.map(c.id).min().execute()
    assert item == 1
    item = Corn.all.sum(c.id).execute()
    assert item == 6
    item = Corn.all.max(c.id).execute()
    assert item == 3
    item = Corn.all.min(c.id).execute()
    assert item == 1
    items = list(Corn.all.groupby(c.name, c.sum(c.id)).sort(c.group).execute())
    assert len(items) == 2
    assert items[0]['key'] == 'baz' and items[0]['group'] == 2
    assert items[1]['key'] == 'foo' and items[1]['group'] == 4
    items = list(Corn.all.groupby(c.name, {
        'max': c.max(c.id),
        'min': c.min(c.id),
        'sum': c.sum(c.id)}).sort(c.key).execute())
    assert len(items) == 2
    assert items[0]['key'] == 'baz' and items[0]['group'] == {'max': 2, 'min': 2, 'sum': 2}
    assert items[1]['key'] == 'foo' and items[1]['group'] == {'max': 3, 'min': 1, 'sum': 4}
    items = list(Corn.all.map(c.name + ' ' + c.lastname).execute())
    assert len(items) == 3