Exemple #1
0
def test_var():
    value_1 = object()
    value_2 = object()
    value_3 = object()
    var = Var([
        ('role_1', value_1),
        ('role_2', value_2),
        (not_('role_3'), value_3),
    ])
    assert len(var.values) == 3
    for matcher, value in var.values:
        assert callable(matcher)
        assert type(value) == object

    assert var.resolve('role_1') == Resolution(value_1, 'role_1')
    assert var.resolve('role_2') == Resolution(value_2, 'role_2')
    assert var.resolve('default') == Resolution(value_3, 'default')

    var = Var([
        (not_('role_3'), value_3),
        ('role_1', value_1),
        ('role_2', value_2),
    ])
    assert var.resolve('role_1') == Resolution(value_3, 'role_1')
    assert var.resolve('role_2') == Resolution(value_3, 'role_2')
    assert var.resolve('default') == Resolution(value_3, 'default')
    assert var.resolve('role_3') == Resolution(None, 'role_3')

    var = Var([
        ('role_1', value_1),
        ('role_2', value_2),
    ], propagate='role_2')
    assert callable(var.propagate)
Exemple #2
0
def test_var():
    value_1 = object()
    value_2 = object()
    value_3 = object()
    var = Var([
        ('role_1', value_1),
        ('role_2', value_2),
        (not_('role_3'), value_3),
    ])
    assert len(var.values) == 3
    for matcher, value in var.values:
        assert callable(matcher)
        assert type(value) == object

    assert var.resolve('role_1') == Resolution(value_1, 'role_1')
    assert var.resolve('role_2') == Resolution(value_2, 'role_2')
    assert var.resolve('default') == Resolution(value_3, 'default')

    var = Var([
        (not_('role_3'), value_3),
        ('role_1', value_1),
        ('role_2', value_2),
    ])
    assert var.resolve('role_1') == Resolution(value_3, 'role_1')
    assert var.resolve('role_2') == Resolution(value_3, 'role_2')
    assert var.resolve('default') == Resolution(value_3, 'default')
    assert var.resolve('role_3') == Resolution(None, 'role_3')

    var = Var([
        ('role_1', value_1),
        ('role_2', value_2),
    ], propagate='role_2')
    assert callable(var.propagate)
Exemple #3
0
def test_helpers():
    when = lambda *args: Var({
        not_(*args): False
    }, default=True)

    assert when(RESPONSE_ROLE).resolve(RESPONSE_ROLE).value
    assert not when(RESPONSE_ROLE).resolve(REQUEST_ROLE).value
Exemple #4
0
def test_helpers():
    when = lambda *args: Var({
        not_(*args): False
    }, default=True)

    assert when(RESPONSE_ROLE).resolve(RESPONSE_ROLE).value
    assert not when(RESPONSE_ROLE).resolve(REQUEST_ROLE).value
Exemple #5
0
    class User(Document):
        class Options(object):
            roles_to_propagate = not_(PARTIAL_RESPONSE_ROLE)

        with Scope(DB_ROLE) as db:
            db._id = StringField(required=True)
            db.version = StringField(required=True)
        with Scope(lambda r: r.startswith(RESPONSE_ROLE) or r == REQUEST_ROLE) as response:
            response.id = StringField(required=when_not(PARTIAL_RESPONSE_ROLE))
        with Scope(not_(REQUEST_ROLE)) as not_request:
            not_request.messages = ArrayField(DocumentField(Message), required=when_not(PARTIAL_RESPONSE_ROLE))
Exemple #6
0
def test_var():
    value_1 = object()
    value_2 = object()
    value_3 = object()
    var = Var([
        ('role_1', value_1),
        ('role_2', value_2),
        (not_('role_3'), value_3),
    ])
    assert var.resolve('role_1') == Resolution(value_1, 'role_1')
    assert var.resolve('role_2') == Resolution(value_2, 'role_2')
    assert var.resolve('default') == Resolution(value_3, 'default')

    var = Var([
        (not_('role_3'), value_3),
        ('role_1', value_1),
        ('role_2', value_2),
    ])
    assert var.resolve('role_1') == Resolution(value_3, 'role_1')
    assert var.resolve('role_2') == Resolution(value_3, 'role_2')
    assert var.resolve('default') == Resolution(value_3, 'default')
    assert var.resolve('role_3') == Resolution(None, 'role_3')
Exemple #7
0
def test_var():
    value_1 = object()
    value_2 = object()
    value_3 = object()
    var = Var([
        ('role_1', value_1),
        ('role_2', value_2),
        (not_('role_3'), value_3),
    ])
    assert var.resolve('role_1') == Resolution(value_1, 'role_1')
    assert var.resolve('role_2') == Resolution(value_2, 'role_2')
    assert var.resolve('default') == Resolution(value_3, 'default')

    var = Var([
        (not_('role_3'), value_3),
        ('role_1', value_1),
        ('role_2', value_2),
    ])
    assert var.resolve('role_1') == Resolution(value_3, 'role_1')
    assert var.resolve('role_2') == Resolution(value_3, 'role_2')
    assert var.resolve('default') == Resolution(value_3, 'default')
    assert var.resolve('role_3') == Resolution(None, 'role_3')
Exemple #8
0
 class Options(object):
     roles_to_propagate = not_(PARTIAL_RESPONSE_ROLE)
Exemple #9
0
def test_scopes_basics():
    when_not = lambda *args: Var({not_(*args): True}, default=False)

    class Message(Document):
        with Scope(DB_ROLE) as db:
            db.uuid = StringField(required=True)
        created_at = IntField(
            required=when_not(PARTIAL_RESPONSE_ROLE, REQUEST_ROLE))
        text = StringField(required=when_not(PARTIAL_RESPONSE_ROLE))
        field_that_is_never_present = Var(
            {'NEVER': StringField(required=True)})

    class User(Document):
        class Options(object):
            roles_to_propagate = not_(PARTIAL_RESPONSE_ROLE)

        with Scope(DB_ROLE) as db:
            db._id = StringField(required=True)
            db.version = StringField(required=True)
        with Scope(lambda r: r.startswith(RESPONSE_ROLE) or r == REQUEST_ROLE
                   ) as response:
            response.id = StringField(required=when_not(PARTIAL_RESPONSE_ROLE))
        with Scope(not_(REQUEST_ROLE)) as not_request:
            not_request.messages = ArrayField(
                DocumentField(Message),
                required=when_not(PARTIAL_RESPONSE_ROLE))

    resolution = Message.resolve_field('text')
    assert resolution.value == Message.text
    assert resolution.role == DEFAULT_ROLE

    resolution = Message.resolve_field('field_that_is_never_present')
    assert resolution.value is None
    assert resolution.role == DEFAULT_ROLE

    resolution = Message.resolve_field('non-existent')
    assert resolution.value is None
    assert resolution.role == DEFAULT_ROLE

    schema = User.get_schema(role=DB_ROLE)
    expected_required = sorted(['_id', 'version', 'messages'])
    expected_properties = {
        '_id': {
            'type': 'string'
        },
        'version': {
            'type': 'string'
        },
        'messages': {
            'type': 'array',
            'items': {
                'type': 'object',
                'additionalProperties': False,
                'properties': {
                    'created_at': {
                        'type': 'integer'
                    },
                    'text': {
                        'type': 'string'
                    },
                    'uuid': {
                        'type': 'string'
                    }
                },
                'required': sorted(['uuid', 'created_at', 'text']),
            },
        },
    }
    assert sorted(schema['required']) == expected_required
    assert sort_required_keys(
        schema['properties']) == sort_required_keys(expected_properties)
    assert dict(User.resolve_and_iter_fields(DB_ROLE)) == {
        '_id': User.db._id,
        'version': User.db.version,
        'messages': User.not_request.messages,
    }

    schema = User.get_schema(role=REQUEST_ROLE)
    expected_required = sorted(['id'])
    expected_properties = {
        'id': {
            'type': 'string'
        },
    }
    assert sorted(schema['required']) == expected_required
    assert sort_required_keys(
        schema['properties']) == sort_required_keys(expected_properties)
    assert dict(User.resolve_and_iter_fields(REQUEST_ROLE)) == {
        'id': User.response.id,
    }

    schema = User.get_schema(role=RESPONSE_ROLE)
    expected_required = sorted(['id', 'messages'])
    expected_properties = {
        'id': {
            'type': 'string'
        },
        'messages': {
            'type': 'array',
            'items': {
                'type': 'object',
                'additionalProperties': False,
                'properties': {
                    'created_at': {
                        'type': 'integer'
                    },
                    'text': {
                        'type': 'string'
                    },
                },
                'required': sorted(['created_at', 'text']),
            },
        },
    }
    assert sorted(schema['required']) == expected_required
    assert sort_required_keys(
        schema['properties']) == sort_required_keys(expected_properties)
    assert dict(User.resolve_and_iter_fields(RESPONSE_ROLE)) == {
        'id': User.response.id,
        'messages': User.not_request.messages,
    }

    schema = User.get_schema(role=PARTIAL_RESPONSE_ROLE)
    expected_properties = {
        'id': {
            'type': 'string'
        },
        'messages': {
            'type': 'array',
            'items': {
                'type': 'object',
                'additionalProperties': False,
                'properties': {
                    'created_at': {
                        'type': 'integer'
                    },
                    'text': {
                        'type': 'string'
                    },
                },
                'required': sorted(['created_at', 'text']),
            },
        },
    }
    assert 'required' not in schema
    assert sort_required_keys(
        schema['properties']) == sort_required_keys(expected_properties)
    assert dict(Message.resolve_and_iter_fields(PARTIAL_RESPONSE_ROLE)) == {
        'created_at': Message.created_at,
        'text': Message.text,
    }
Exemple #10
0
def test_scopes_basics():
    when_not = lambda *args: Var({
        not_(*args): True
    }, default=False)

    when = lambda *args: Var({
        not_(*args): False
    }, default=True)

    class Message(Document):
        with Scope(DB_ROLE) as db:
            db.uuid = StringField(required=True)
        created_at = IntField(required=when_not(PARTIAL_RESPONSE_ROLE, REQUEST_ROLE))
        text = StringField(required=when_not(PARTIAL_RESPONSE_ROLE))

    class User(Document):
        class Options(object):
            roles_to_propagate = not_(PARTIAL_RESPONSE_ROLE)

        with Scope(DB_ROLE) as db:
            db._id = StringField(required=True)
            db.version = StringField(required=True)
        with Scope(lambda r: r.startswith(RESPONSE_ROLE) or r == REQUEST_ROLE) as response:
            response.id = StringField(required=when_not(PARTIAL_RESPONSE_ROLE))
        with Scope(not_(REQUEST_ROLE)) as request:
            request.messages = ArrayField(DocumentField(Message), required=when_not(PARTIAL_RESPONSE_ROLE))

    schema = User.get_schema(role=DB_ROLE)
    expected_required = sorted(['_id', 'version', 'messages'])
    expected_properties = {
        '_id': {'type': 'string'},
        'version': {'type': 'string'},
        'messages': {
            'type': 'array',
            'items': {
                'type': 'object',
                'additionalProperties': False,
                'properties': {
                    'created_at': {'type': 'integer'},
                    'text': {'type': 'string'},
                    'uuid': {'type': 'string'}
                },
                'required': sorted(['uuid', 'created_at', 'text']),
            },
        },
    }
    assert sorted(schema['required']) == expected_required
    assert sort_required_keys(schema['properties']) == sort_required_keys(expected_properties)

    schema = User.get_schema(role=REQUEST_ROLE)
    expected_required = sorted(['id'])
    expected_properties = {
        'id': {'type': 'string'},
    }
    assert sorted(schema['required']) == expected_required
    assert sort_required_keys(schema['properties']) == sort_required_keys(expected_properties)

    schema = User.get_schema(role=RESPONSE_ROLE)
    expected_required = sorted(['id', 'messages'])
    expected_properties = {
        'id': {'type': 'string'},
        'messages': {
            'type': 'array',
            'items': {
                'type': 'object',
                'additionalProperties': False,
                'properties': {
                    'created_at': {'type': 'integer'},
                    'text': {'type': 'string'},
                },
                'required': sorted(['created_at', 'text']),
            },
        },
    }
    assert sorted(schema['required']) == expected_required
    assert sort_required_keys(schema['properties']) == sort_required_keys(expected_properties)

    schema = User.get_schema(role=PARTIAL_RESPONSE_ROLE)
    expected_properties = {
        'id': {'type': 'string'},
        'messages': {
            'type': 'array',
            'items': {
                'type': 'object',
                'additionalProperties': False,
                'properties': {
                    'created_at': {'type': 'integer'},
                    'text': {'type': 'string'},
                },
                'required': sorted(['created_at', 'text']),
            },
        },
    }
    assert 'required' not in schema
    assert sort_required_keys(schema['properties']) == sort_required_keys(expected_properties)
Exemple #11
0
def test_scopes_basics():
    when_not = lambda *args: Var({
        not_(*args): True
    }, default=False)

    class Message(Document):
        with Scope(DB_ROLE) as db:
            db.uuid = StringField(required=True)
        created_at = IntField(required=when_not(PARTIAL_RESPONSE_ROLE, REQUEST_ROLE))
        text = StringField(required=when_not(PARTIAL_RESPONSE_ROLE))
        field_that_is_never_present = Var({
            'NEVER': StringField(required=True)
        })

    class User(Document):
        class Options(object):
            roles_to_propagate = not_(PARTIAL_RESPONSE_ROLE)

        with Scope(DB_ROLE) as db:
            db._id = StringField(required=True)
            db.version = StringField(required=True)
        with Scope(lambda r: r.startswith(RESPONSE_ROLE) or r == REQUEST_ROLE) as response:
            response.id = StringField(required=when_not(PARTIAL_RESPONSE_ROLE))
        with Scope(not_(REQUEST_ROLE)) as not_request:
            not_request.messages = ArrayField(DocumentField(Message), required=when_not(PARTIAL_RESPONSE_ROLE))

    resolution = Message.resolve_field('text')
    assert resolution.value == Message.text
    assert resolution.role == DEFAULT_ROLE

    resolution = Message.resolve_field('field_that_is_never_present')
    assert resolution.value is None
    assert resolution.role == DEFAULT_ROLE

    resolution = Message.resolve_field('non-existent')
    assert resolution.value is None
    assert resolution.role == DEFAULT_ROLE

    schema = User.get_schema(role=DB_ROLE)
    expected_required = sorted(['_id', 'version', 'messages'])
    expected_properties = {
        '_id': {'type': 'string'},
        'version': {'type': 'string'},
        'messages': {
            'type': 'array',
            'items': {
                'type': 'object',
                'additionalProperties': False,
                'properties': {
                    'created_at': {'type': 'integer'},
                    'text': {'type': 'string'},
                    'uuid': {'type': 'string'}
                },
                'required': sorted(['uuid', 'created_at', 'text']),
            },
        },
    }
    assert sorted(schema['required']) == expected_required
    assert sort_required_keys(schema['properties']) == sort_required_keys(expected_properties)
    assert dict(User.resolve_and_iter_fields(DB_ROLE)) == {
        '_id': User.db._id,
        'version': User.db.version,
        'messages': User.not_request.messages,
    }

    schema = User.get_schema(role=REQUEST_ROLE)
    expected_required = sorted(['id'])
    expected_properties = {
        'id': {'type': 'string'},
    }
    assert sorted(schema['required']) == expected_required
    assert sort_required_keys(schema['properties']) == sort_required_keys(expected_properties)
    assert dict(User.resolve_and_iter_fields(REQUEST_ROLE)) == {
        'id': User.response.id,
    }

    schema = User.get_schema(role=RESPONSE_ROLE)
    expected_required = sorted(['id', 'messages'])
    expected_properties = {
        'id': {'type': 'string'},
        'messages': {
            'type': 'array',
            'items': {
                'type': 'object',
                'additionalProperties': False,
                'properties': {
                    'created_at': {'type': 'integer'},
                    'text': {'type': 'string'},
                },
                'required': sorted(['created_at', 'text']),
            },
        },
    }
    assert sorted(schema['required']) == expected_required
    assert sort_required_keys(schema['properties']) == sort_required_keys(expected_properties)
    assert dict(User.resolve_and_iter_fields(RESPONSE_ROLE)) == {
        'id': User.response.id,
        'messages': User.not_request.messages,
    }

    schema = User.get_schema(role=PARTIAL_RESPONSE_ROLE)
    expected_properties = {
        'id': {'type': 'string'},
        'messages': {
            'type': 'array',
            'items': {
                'type': 'object',
                'additionalProperties': False,
                'properties': {
                    'created_at': {'type': 'integer'},
                    'text': {'type': 'string'},
                },
                'required': sorted(['created_at', 'text']),
            },
        },
    }
    assert 'required' not in schema
    assert sort_required_keys(schema['properties']) == sort_required_keys(expected_properties)
    assert dict(Message.resolve_and_iter_fields(PARTIAL_RESPONSE_ROLE)) == {
        'created_at': Message.created_at,
        'text': Message.text,
    }