def test_is_a_partnership_is_not_forgotten():
    from xoutil.compat import zip
    with bubbling() as current:
        next((person, partner)
             for person, partner in zip(this('person'),
                                        this('partner'))
             for rel in this('relation')
             if rel.type == 'partnership'
             if (rel.subject == person) & (rel.object == partner))

        parts = current.bubble.parts
        tokens = current.bubble.tokens
    assert len(parts) ==  2  # Only the 2 filters

    tokens = [tk.expression for tk in tokens]
    def ok(x):
        assert str(x) == str(parts.pop(-1))
    person = this('person')
    partner = this('partner')
    rel = this('relation')
    assert person in tokens
    assert partner in tokens
    assert rel in tokens
    ok((rel.subject == person) & (rel.object == partner))
    ok(rel.type == 'partnership')
    with pytest.raises(IndexError):
        ok(None)
def test_worst_case_must_have_3_filters_and_3_tokens():
    from xoutil.compat import zip
    with bubbling() as current:
        next(person
             for person, partner in zip(this('person'),
                                        this('partner'))
             for rel in this('relation')
             if rel.type == 'partnership'
             if rel.subject == person
             if rel.object == partner
             if partner.age > 32)

        parts = current.bubble.parts
        tokens = current.bubble.tokens
    assert len(parts) == 4

    tokens = [tk.expression for tk in tokens]
    def ok(x):
        assert str(x) == str(parts.pop(-1))
    person = this('person')
    partner = this('partner')
    rel = this('relation')
    assert person in tokens
    assert partner in tokens
    assert rel in tokens
    ok(partner.age > 32)
    ok(rel.object == partner)
    ok(rel.subject == person)
    ok(rel.type == 'partnership')
    with pytest.raises(IndexError):
        ok(None)
def test_cotraverse_expression():
    from xoutil.compat import zip
    from xotl.ql.expressions import is_a
    from xotl.ql.translation import cotraverse_expression

    @thesefy
    class Person(object):
        pass

    @thesefy
    class Partnership(object):
        pass

    query = these((person, partner)
                  for person, partner in zip(Person, Person)
                  for rel in Partnership
                  if (rel.subject == person) & (rel.obj == partner))
    filters = list(query.filters)
    person, partner = query.selection
    person_is_a_person = is_a(person, Person)
    partner_is_a_person = is_a(partner, Person)
    rel_token = query.tokens[-1]
    rel_subject = rel_token.expression.subject
    rel_obj = rel_token.expression.obj
    with context(UNPROXIFING_CONTEXT):
        assert person != partner
        assert person_is_a_person in filters
        assert partner_is_a_person in filters
        expected_terms_order = [person, partner, rel_token.expression, rel_subject, person, rel_obj, partner]
        assert expected_terms_order == list(cotraverse_expression(query))

    assert UNPROXIFING_CONTEXT not in context