Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor driver test #1452

Merged
merged 6 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions jina/drivers/querylang/queryset/helper.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
from functools import partial
from typing import Callable, List, Type, Union, Iterable

from ....excepts import LookupyError

## Exceptions

class LookupyError(Exception):
"""Base exception class for all exceptions raised by lookupy"""
pass


## utility functions

def iff(precond: Callable, val: Union[int, str], f: Callable) -> bool:
"""If and only if the precond is True
Expand Down
4 changes: 4 additions & 0 deletions jina/excepts.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,7 @@ class ImageAlreadyExists(Exception):

class BadFlowYAMLVersion(Exception):
""" Exception when Flow YAML config specifies a wrong version number"""


class LookupyError(Exception):
"""Base exception class for all exceptions raised by lookupy"""
2 changes: 1 addition & 1 deletion tests/integration/hub_usage/dummyhub_abs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from helper import foo
from .helper import foo
bwanglzu marked this conversation as resolved.
Show resolved Hide resolved

from jina.executors.crafters import BaseCrafter

Expand Down
15 changes: 11 additions & 4 deletions tests/unit/drivers/querylang/queryset/test_dunderkeys.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import pytest

from jina.drivers.querylang.queryset.dunderkey import (
dunderkey,
dunder_init,
dunder_get,
dunder_partition,
undunder_keys,
dunder_truncate,
)
from jina.proto.jina_pb2 import DocumentProto
from jina import Document

def test_dunderkey():
assert dunderkey('a', 'b', 'c') == 'a__b__c'

def test_dunder_init():
assert dunder_init('a__b__c') == 'a__b'

def test_dunder_get():
assert dunder_get({'a': {'b': 5}}, 'a__b') == 5
Expand All @@ -22,9 +29,9 @@ class A:

assert dunder_get(A, 'b__c') == 5

d = DocumentProto()
d.tags['a'] = 'hello'
assert dunder_get(d, 'tags__a') == 'hello'
with Document() as d:
d.tags['a'] = 'hello'
assert dunder_get(d, 'tags__a') == 'hello'

# Error on invalid key

Expand Down
31 changes: 30 additions & 1 deletion tests/unit/drivers/querylang/queryset/test_lookup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from jina.types.document import Document
from jina.drivers.querylang.queryset.lookup import LookupLeaf
from jina.drivers.querylang.queryset.lookup import LookupLeaf, Q, QuerySet

from tests import random_docs


class MockId:
Expand Down Expand Up @@ -173,3 +175,30 @@ def test_lookup_leaf_None():
assert leaf.evaluate(mock0)
mock1 = MockId(4)
assert not leaf.evaluate(mock1)

def test_docs_filter():
s = random_docs(10)
ss = QuerySet(s).filter(tags__id__lt=5, tags__id__gt=3)
bwanglzu marked this conversation as resolved.
Show resolved Hide resolved
ssr = list(ss)
assert len(ssr) == 1
for d in ssr:
assert (3 < d.tags['id'] < 5)


def test_docs_filter_equal():
bwanglzu marked this conversation as resolved.
Show resolved Hide resolved
s = random_docs(10)
ss = QuerySet(s).filter(tags__id=4)
ssr = list(ss)
assert len(ssr) == 1
for d in ssr:
assert int(d.tags['id']) == 4
assert len(d.chunks) == 5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any specific reason to check the number of chunks here?

Suggested change
assert len(d.chunks) == 5
assert len(d.chunks) == 5



def test_nested_chunks_filter():
s = random_docs(10)
ss = QuerySet(s).filter(Q(chunks__filter=Q(tags__id__lt=35, tags__id__gt=33)))
ssr = list(ss)
assert len(ssr) == 1
for d in ssr:
assert len(d.chunks) == 5
30 changes: 0 additions & 30 deletions tests/unit/drivers/querylang/queryset/test_queryset.py

This file was deleted.

16 changes: 7 additions & 9 deletions tests/unit/drivers/test_matches2doc_rank_drivers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import numpy as np

from jina import Document
from jina.drivers.rank import Matches2DocRankDriver
from jina.executors.rankers import Match2DocRanker
from jina.proto import jina_pb2
from jina.types.sets import DocumentSet


Expand Down Expand Up @@ -41,17 +41,15 @@ def create_document_to_score():
# |- matches: (id: 3, parent_id: 1, score.value: 3),
# |- matches: (id: 4, parent_id: 1, score.value: 4),
# |- matches: (id: 5, parent_id: 1, score.value: 5),

doc = jina_pb2.DocumentProto()
doc = Document()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea herr is to see if we can refactor the logic of the test itself to be less dependant on id? I am not sure it is possible though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One problem is that this test is so dependant on document logic that any document refactor involves a lot of effort to regfactor these tests. is there a way to avoid this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the option I can see is to maintain fixtures in a separate file so that we can reuse/modify it anywhere

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

butis there so many shared code?

Copy link
Member Author

@bwanglzu bwanglzu Dec 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there could be, if we "composite" small fixtures into bigger ones, a lot of code are actually shared, from low levels.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okey that'd be great.

doc.id = '1' * 16
doc.length = 5
for match_id, match_score in [(2, 3), (3, 6), (4, 1), (5, 8)]:
match = doc.matches.add()
match.id = str(match_id) * 16
match.parent_id = '1' * 16
match.length = match_score
match.score.ref_id = doc.id
match.score.value = match_score
with Document() as match:
match.id = str(match_id) * 16
match.length = match_score
match.score.value = match_score
doc.matches.append(match)
return doc


Expand Down