Ejemplo n.º 1
0
def test_ql_constructors_from_proto(source):
    ql = SliceQL(start=3, end=5, priority=999)
    q = QueryLang(ql).as_pb_object

    qlr = QueryLang(source(q))
    assert qlr.name == 'SliceQL'
    assert qlr.parameters['start'] == 3
    assert qlr.parameters['end'] == 5
    assert qlr.priority == 999
Ejemplo n.º 2
0
def test_ql_constructors_from_proto(source):
    q = QueryLang({
        'name': 'SliceQL',
        'parameters': {
            'start': 3,
            'end': 5
        },
        'priority': 999
    }).as_pb_object

    qlr = QueryLang(source(q))
    assert qlr.name == 'SliceQL'
    assert qlr.parameters['start'] == 3
    assert qlr.parameters['end'] == 5
    assert qlr.priority == 999
Ejemplo n.º 3
0
def test_read_from_req(mocker):
    def validate1(req):
        assert len(req.docs) == 5

    def validate2(req):
        assert len(req.docs) == 3

    response_mock = mocker.Mock(wrap=validate1)
    response_mock_2 = mocker.Mock(wrap=validate2)
    response_mock_3 = mocker.Mock(wrap=validate1)

    qs = QueryLang(SliceQL(start=1, end=4, priority=1))

    f = Flow(callback_on='body').add(uses='- !SliceQL | {start: 0, end: 5}')

    # without queryset
    with f:
        f.index(random_docs(10), on_done=response_mock)

    response_mock.assert_called()
    # with queryset
    with f:
        f.index(random_docs(10), queryset=qs, on_done=response_mock_2)

    response_mock_2.assert_called()

    qs.priority = -1
    # with queryset, but priority is no larger than driver's default
    with f:
        f.index(random_docs(10), queryset=qs, on_done=response_mock_3)

    response_mock_3.assert_called()
Ejemplo n.º 4
0
def test_read_from_req(mocker):
    def validate1(req):
        assert len(req.docs) == 5

    def validate2(req):
        assert len(req.docs) == 3

    response_mock = mocker.Mock()
    response_mock_2 = mocker.Mock()
    response_mock_3 = mocker.Mock()

    qs = QueryLang(
        {'name': 'SliceQL', 'priority': 1, 'parameters': {'start': 1, 'end': 4}}
    )

    f = Flow().add(uses='- !SliceQL | {start: 0, end: 5}')

    # without queryset
    with f:
        f.index(random_docs(10), on_done=response_mock)

    validate_callback(response_mock, validate1)
    # with queryset
    with f:
        f.index(random_docs(10), queryset=qs, on_done=response_mock_2)

    validate_callback(response_mock_2, validate2)

    qs.priority = -1
    # with queryset, but priority is no larger than driver's default
    with f:
        f.index(random_docs(10), queryset=qs, on_done=response_mock_3)

    validate_callback(response_mock_3, validate1)
Ejemplo n.º 5
0
def test_querylang_request():
    qs = QueryLang({
        'name': 'SliceQL',
        'parameters': {
            'start': 1,
            'end': 4
        },
        'priority': 1
    })
    Client.check_input(random_docs(10), queryset=qs)
Ejemplo n.º 6
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     ql = QueryLang({
         'name': 'MockExcludeQL',
         'parameters': {
             'fields': ['updated_field1', 'updated_field2']
         },
         'priority': 3
     })
     self.qset = QueryLangSet([ql.proto])
Ejemplo n.º 7
0
def test_ql_priority():
    qs = QueryLang(SliceQL(start=1, end=4, priority=1))
    assert qs.priority == 1
    qs._querylang.priority = -1
    assert qs._querylang.priority == -1
    assert qs.priority == -1

    qs.priority = -2
    assert qs._querylang.priority == -2
    assert qs.priority == -2
Ejemplo n.º 8
0
def test_ql_priority():
    qs = QueryLang({
        'name': 'SliceQL',
        'parameters': {
            'start': 1,
            'end': 4
        },
        'priority': 1
    })
    assert qs.priority == 1
    qs._querylang.priority = -1
    assert qs._querylang.priority == -1
    assert qs.priority == -1

    qs.priority = -2
    assert qs._querylang.priority == -2
    assert qs.priority == -2

    qs2 = QueryLang({'name': 'SliceQL', 'parameters': {'start': 1, 'end': 4}})
    assert qs2.priority == 0
Ejemplo n.º 9
0
def test_ql_constructors_from_driver():
    ql = SliceQL(start=3, end=5, priority=999)
    q = QueryLang(ql)
    qb = q.as_pb_object
    assert q.name == 'SliceQL'
    assert q.parameters['start'] == 3
    assert q.parameters['end'] == 5
    assert q.priority == 999

    assert qb.name == 'SliceQL'
    assert qb.parameters['start'] == 3
    assert qb.parameters['end'] == 5
    assert qb.priority == 999

    assert isinstance(q.as_driver_object, SliceQL)
    assert ql.start == q.as_driver_object.start
    assert ql.end == q.as_driver_object.end
    assert ql._priority == q.as_driver_object._priority
Ejemplo n.º 10
0
def test_ql_constructors_from_driver_info():
    q = QueryLang({
        'name': 'SliceQL',
        'parameters': {
            'start': 3,
            'end': 5
        },
        'priority': 999
    })
    qb = q.as_pb_object
    assert q.name == 'SliceQL'
    assert q.parameters['start'] == 3
    assert q.parameters['end'] == 5
    assert q.priority == 999

    assert qb.name == 'SliceQL'
    assert qb.parameters['start'] == 3
    assert qb.parameters['end'] == 5
    assert qb.priority == 999
Ejemplo n.º 11
0
def test_read_from_req():
    def validate1(req):
        assert len(req.docs) == 5

    def validate2(req):
        assert len(req.docs) == 3

    qs = QueryLang(SliceQL(start=1, end=4, priority=1))

    f = Flow(callback_on='body').add(uses='- !SliceQL | {start: 0, end: 5}')

    # without queryset
    with f:
        f.index(random_docs(10), output_fn=validate1)

    # with queryset
    with f:
        f.index(random_docs(10), queryset=qs, output_fn=validate2)

    qs.priority = -1
    # with queryset, but priority is no larger than driver's default
    with f:
        f.index(random_docs(10), queryset=qs, output_fn=validate1)
Ejemplo n.º 12
0
def test_querylang_request():
    qs = QueryLang(SliceQL(start=1, end=4, priority=1))
    Client.check_input(random_docs(10), queryset=qs)
Ejemplo n.º 13
0
def querylang_instance():
    """:returns: An instance of :class:`QueryLang`."""
    query_lang = QueryLang()
    query_lang.name = 'test'
    query_lang.priority = 5
    return query_lang
Ejemplo n.º 14
0
    del querylang_set[0]
    assert len(querylang_set) == 2


def test_length(querylang_set):
    """Test :meth:`__len__`, should return the length of :class:`QueryLangSet`."""
    assert len(querylang_set) == 3


def test_iter(querylang_set):
    """Test :meth:`__iter__`, should yield an instance of :class:`QueryLang`."""
    for querylang in querylang_set:
        assert isinstance(querylang, QueryLang)


@pytest.mark.parametrize('querylang_item', [QueryLangProto(), QueryLang()])
def test_append_success(querylang_set, querylang_item):
    """Test :meth:`append`. Expect test three cases depends on the type of :attr:`value`.
    Such as :class:`BaseDriver`, :class:`QueryLangProto` and :class:`QueryLang`.

    .. note::
            Please parameterize this test with pytest.mark.parameterize.
    """
    querylang_set.append(querylang_item)
    assert len(querylang_set) == 4


def test_append_fail(querylang_set):
    """Test :meth:`append` with an invalid input.

    .. note::
Ejemplo n.º 15
0

def test_length(querylang_set):
    """Test :meth:`__len__`, should return the length of :class:`QueryLangSet`."""
    assert len(querylang_set) == 3


def test_iter(querylang_set):
    """Test :meth:`__iter__`, should yield an instance of :class:`QueryLang`."""
    for querylang in querylang_set:
        assert isinstance(querylang, QueryLang)


@pytest.mark.parametrize('querylang_item', [
    QueryLangProto(),
    QueryLang(), {
        'name': 'Driver',
        'parameters': {
            'key': 'value'
        }
    }
])
def test_append_success_proto(querylang_set, querylang_item):
    """Test :meth:`append`. Expect test three cases depends on the type of :attr:`value`.
    Such as :class:`BaseDriver`, :class:`QueryLangProto` and :class:`QueryLang`.

    .. note::
            Please parameterize this test with pytest.mark.parameterize.
    """
    querylang_set.append(querylang_item)
    assert len(querylang_set) == 4