assert scores2['operation'].ref_id == '10' * 16 assert scores2['operation'].description == 'score description' assert scores2['operation2'].op_name == 'operation2' assert scores2['operation2'].value == 20.0 assert scores2['operation2'].ref_id == '20' * 16 assert scores2['operation2'].description == 'score2 description' @pytest.mark.parametrize( 'value', [ 5, 5.0, np.int(5), np.float(5.0), NamedScore(value=5), NamedScore(value=5).proto ], ) def test_mapped_set_item(value): scores = Document().scores scores['operation'] = value assert scores['operation'].value == 5 @pytest.mark.parametrize( 'value', [ NamedScore(value=5, op_name='op', description='desc'), NamedScore(value=5, op_name='op', description='desc').proto, ],
def test_get_attr_values(): d = Document({ 'id': '123', 'text': 'document', 'feature1': 121, 'name': 'name', 'tags': { 'id': 'identity', 'a': 'b', 'c': 'd', 'e': [0, 1, { 'f': 'g' }] }, }) d.scores['metric'] = NamedScore(value=42) required_keys = [ 'id', 'text', 'tags__name', 'tags__feature1', 'scores__metric__value', 'tags__c', 'tags__id', 'tags__inexistant', 'tags__e__2__f', 'inexistant', ] res = d.get_attributes(*required_keys) assert len(res) == len(required_keys) assert res[required_keys.index('id')] == '123' assert res[required_keys.index('tags__feature1')] == 121 assert res[required_keys.index('tags__name')] == 'name' assert res[required_keys.index('text')] == 'document' assert res[required_keys.index('tags__c')] == 'd' assert res[required_keys.index('tags__id')] == 'identity' assert res[required_keys.index('scores__metric__value')] == 42 assert res[required_keys.index('tags__inexistant')] is None assert res[required_keys.index('inexistant')] is None assert res[required_keys.index('tags__e__2__f')] == 'g' required_keys_2 = ['tags', 'text'] res2 = d.get_attributes(*required_keys_2) assert len(res2) == 2 assert res2[required_keys_2.index('text')] == 'document' assert res2[required_keys_2.index('tags')] == d.tags assert res2[required_keys_2.index('tags')].dict() == d.tags.dict() d = Document({ 'id': '123', 'tags': { 'outterkey': { 'innerkey': 'real_value' } } }) required_keys_3 = ['tags__outterkey__innerkey'] res3 = d.get_attributes(*required_keys_3) assert res3 == 'real_value' d = Document(content=np.array([1, 2, 3])) res4 = np.stack(d.get_attributes(*['blob'])) np.testing.assert_equal(res4, np.array([1, 2, 3]))
import pytest from jina import Document from jina.types.arrays.chunk import ChunkArray from jina.types.arrays.match import MatchArray from jina.types.ndarray.generic import NdArray from jina.types.request import Request from jina.types.score import NamedScore @pytest.mark.parametrize( 'obj', [ Document(), Request(), NamedScore(), NdArray(), MatchArray([Document()], Document()), ChunkArray([Document()], Document()), ], ) def test_builtin_str_repr_no_content(obj): print(obj) print(f'{obj!r}') @pytest.mark.parametrize( 'obj', [ Document(content='123', chunks=[Document(content='abc')]), NamedScore(
def test_update_score_embedding(): d = Document() d_score = Document(score=NamedScore(value=42)) d.update(d_score) assert d.score.value == 42
def test_get_attr_values(): d = Document({ 'id': '123', 'text': 'document', 'feature1': 121, 'name': 'name', 'tags': { 'id': 'identity', 'a': 'b', 'c': 'd' }, }) d.score = NamedScore(value=42) required_keys = [ 'id', 'text', 'tags__name', 'tags__feature1', 'score__value', 'tags__c', 'tags__id', 'tags__inexistant', 'inexistant', ] res = d.get_attrs_values(*required_keys) assert len(res) == len(required_keys) assert res[required_keys.index('id')] == '123' assert res[required_keys.index('tags__feature1')] == 121 assert res[required_keys.index('tags__name')] == 'name' assert res[required_keys.index('text')] == 'document' assert res[required_keys.index('tags__c')] == 'd' assert res[required_keys.index('tags__id')] == 'identity' assert res[required_keys.index('score__value')] == 42 assert res[required_keys.index('tags__inexistant')] is None assert res[required_keys.index('inexistant')] is None required_keys_2 = ['tags', 'text'] res2 = d.get_attrs_values(*required_keys_2) assert len(res2) == 2 assert res2[required_keys_2.index('text')] == 'document' assert res2[required_keys_2.index('tags')] == d.tags d = Document({ 'id': '123', 'tags': { 'outterkey': { 'innerkey': 'real_value' } } }) required_keys_3 = ['tags__outterkey__innerkey'] res3 = d.get_attrs_values(*required_keys_3) assert len(res3) == 1 assert res3[required_keys_3.index( 'tags__outterkey__innerkey')] == 'real_value' d = Document(content=np.array([1, 2, 3])) res4 = d.get_attrs(*['blob']) np.testing.assert_equal(res4['blob'], np.array([1, 2, 3]))
def test_named_operands_nested_score(operands): score = NamedScore(operands=operands) assert len(score.operands) == 2 for i, operand in enumerate(score.operands): assert isinstance(operand, NamedScore) assert operand.op_name == f'operation{i + 1}'
assert score.op_name == 'operation' assert score.value == 10.0 assert score.ref_id == '10' * 16 assert score.description == 'score description' score_proto_1 = jina_pb2.NamedScoreProto() score_proto_1.op_name = 'operation1' score_proto_2 = jina_pb2.NamedScoreProto() score_proto_2.op_name = 'operation2' @pytest.mark.parametrize( 'operands', [[NamedScore(op_name='operation1'), NamedScore(op_name='operation2')], [score_proto_1, score_proto_2], [{ 'op_name': 'operation1' }, { 'op_name': 'operation2' }]]) def test_named_operands_nested_score(operands): score = NamedScore(operands=operands) assert len(score.operands) == 2 for i, operand in enumerate(score.operands): assert isinstance(operand, NamedScore) assert operand.op_name == f'operation{i + 1}' def test_named_score_wrong():