Ejemplo n.º 1
0
def test_document_field():
    class B(Document):
        name = Var(
            response=StringField(required=True),
            request=StringField()
        )

    class A(Document):
        id = Var(response=StringField(required=True))
        b = DocumentField(B)

    field = DocumentField(A)

    # test iter_fields method
    assert list(field.iter_fields()) == [A.b]
    assert (sorted(list(field.iter_fields(role='response')), key=id) ==
            sorted([A.b, A.id.values['response']], key=id))

    # test walk method
    w = list(field.walk())
    assert w == [field]

    w = list(field.walk(through_document_fields=True))
    assert w == [field, A.b]

    w = list(field.walk(through_document_fields=True, role='response'))
    assert (sorted(w, key=id) ==
            sorted([field, A.b, A.id.values['response'], B.name.values['response']], key=id))

    w = list(field.walk(through_document_fields=True, role='request'))
    assert sorted(w, key=id) == sorted([field, A.b, B.name.values['request']], key=id)

    class X(Document):
        pass
    class Y(Document):
        pass
    field = DocumentField(Var({
        'role_1': X,
        'role_2': Y
    }))
    assert field.get_document_cls() is None
    assert field.get_document_cls(role='role_1') == X
    assert field.get_document_cls(role='role_2') == Y
Ejemplo n.º 2
0
def test_document_field():
    class A(Document):
        a = a
        b = b

    field = DocumentField(A)
    assert set(field.iter_fields()) == set([a, b])

    class B(Document):
        field = Var({
            'a': a,
            'b': b
        })
        b = c

    field = DocumentField(B)
    assert set(field.iter_fields()) == set([a, b, c])

    class C(Document):
        pass

    field = DocumentField(C)
    assert set(field.iter_fields()) == set([])