Пример #1
0
def test_ordering():
    from xotl.ql.translation.py import naive_translation

    @thesefy
    class Universe(int):
        pass
    Universe.this_instances = [Universe(i) for i in range(2, 10)]

    query = these((which for which in Universe),
                   ordering=lambda which: -which)
    plan = naive_translation(query)
    assert list(plan()) == list(reversed(range(2, 10)))

    query = these((which for which in Universe),
                   ordering=lambda which: +which)
    plan = naive_translation(query)
    assert list(plan()) == list(range(2, 10))  #XXX: Py3k list()

    query = these((person for person in Person),
                  ordering=lambda person: -person.age)
    plan = naive_translation(query)
    results = list(plan())
    assert manolito == results[-1]
    assert elsa == results[0]

    query = these((person for person in Person if person.children))
    plan = naive_translation(query)
    results = list(plan())
    parents = (manu, yade, pedro, papi, elsa, ppp, denia)
    for who in parents:
        assert who in results
    assert len(results) == len(parents)

    from xotl.ql.expressions import sum_
    query = these((person for person in Person if person.children),
                  ordering=lambda person: (-sum_(child.age for child in person.children), -person.age))
    plan = naive_translation(query)
    results = list(plan())
    assert pedro == results[0]
Пример #2
0
def test_all_pred(**kwargs):
    from xoutil.iterators import dict_update_new
    from xotl.ql.expressions import all_, sum_
    from xotl.ql.translation.py import naive_translation
    query = these(parent
                  for parent in Person
                  if parent.children
                  if all_((30 < child.age) & (child.age < 36) for child in parent.children))
    dict_update_new(kwargs, dict(only='test_translate.*'))
    plan = naive_translation(query, **kwargs)
    result = list(plan())
    assert elsa in result
    assert papi in result
    assert len(result) == 2

    query = these(parent
                  for parent in Person
                  if parent.children
                  if all_(parent.name.startswith('Manu'), parent.age > 30))

    dict_update_new(kwargs, dict(only='test_translate.*'))
    plan = naive_translation(query, **kwargs)
    with pytest.raises(SyntaxError):
        result = list(plan())


    query = these(parent
                  for parent in Person
                  if parent.children
                  if sum_(child.age for child in parent.children) > 60)

    dict_update_new(kwargs, dict(only='test_translate.*'))
    plan = naive_translation(query, **kwargs)
    result = list(plan())
    assert denia in result
    assert pedro in result
    assert len(result) == 2