예제 #1
0
def test_split_predicate():
    Corn = make_corn()
    filter = RequestWrapper.from_request(Corn.all.filter(c.name == 'foo'))
    # Test splitting a predicate on no property
    selfpred, other = split_predicate(filter, [])
    assert isinstance(selfpred, LiteralWrapper)
    assert selfpred.value == True
    assert other == filter.predicate
    # Test splitting a predicate on the only property
    selfpred, other = split_predicate(filter, [Corn.properties['name']])
    assert isinstance(other, LiteralWrapper)
    assert other.value == True
    assert selfpred == filter.predicate
    # Test that 'And' request are properly separated
    filter = RequestWrapper.from_request(Corn.all.filter((c.name == 'foo') & (c.lastname == 'bar')))
    selfpred, other = split_predicate(filter, [Corn.properties['name']])
    assert isinstance(selfpred, EqWrapper)
    assert isinstance(other, EqWrapper)
    assert selfpred.subject.attr_name == 'name'
    assert selfpred.other.value == 'foo'
    assert other.subject.attr_name == 'lastname'
    assert other.other.value == 'bar'
    # Test that 'Or' request are kept unchanged
    filter = RequestWrapper.from_request(Corn.all.filter((c.name == 'foo') | (c.lastname == 'bar')))
    selfpred, other = split_predicate(filter, [])
    assert isinstance(selfpred, LiteralWrapper)
    assert selfpred.value == True
    assert other == filter.predicate
    # Test that Combinations of Or and And are properly managed
    filter = RequestWrapper.from_request(Corn.all.filter(((c.lastname == 'bar') | (c.id < 4)) & (c.name == 'foo')))
    selfpred, other = split_predicate(filter, [Corn.properties['id'], Corn.properties['name']])
    assert isinstance(selfpred, EqWrapper)
    assert isinstance(other, OrWrapper)
    assert selfpred.subject.attr_name == 'name'
    assert selfpred.other.value == 'foo'
예제 #2
0
def test_used_types():
    Corn = make_corn()
    req = RequestWrapper.from_request(Corn.all)
    types = req.used_types()
    assert len(types) == 1
    assert types.keys()[0] == Corn.type
    assert list(types.values()[0]) == [req]
    req = RequestWrapper.from_request(Corn.all.filter(c.id))
    types = req.used_types()
    assert len(types) == 2
    id_type = Corn.properties['id']
    found = any((type == id_type for type in types))
    assert found
    found = any((type == Corn.type for type in types))
    assert found
예제 #3
0
def test_isolate_values():
    Corn = make_corn()
    filter = RequestWrapper.from_request(Corn.all.filter(c.lastname == 'foo'))
    context = (filter.subject.return_type().inner_type,)
    values, remainder = isolate_values(filter.predicate.wrapped_request, context)
    assert values == {'lastname': 'foo'}
    assert isinstance(remainder, LiteralRequest)
    assert RequestWrapper.from_request(remainder).value == True
    filter = RequestWrapper.from_request(Corn.all.filter((c.lastname == 'foo') & (c.id < 3)))
    context = (filter.subject.return_type().inner_type,)
    values, remainder = isolate_values(filter.predicate.wrapped_request, context)
    assert values == {'lastname': 'foo'}
    assert isinstance(remainder, LtRequest)
    assert RequestWrapper.from_request(remainder).subject.attr_name== 'id'
    assert RequestWrapper.from_request(remainder).other.value == 3
예제 #4
0
def return_type(request):
    return RequestWrapper.from_request(request).return_type()