def test_dunder_get(): assert dunder_get({'a': {'b': 5}}, 'a__b') == 5 assert dunder_get({'a': {'b': 8, 'c': {'d': 8}}}, 'a__c__d') == 8 assert dunder_get([1, 2, 3, [4, 5, [6]]], '3__1') == 5 class B: c = 5 class A: b = B assert dunder_get(A, 'b__c') == 5 d = DocumentProto() d.tags['a'] = 'hello' assert dunder_get(d, 'tags__a') == 'hello' # Error on invalid key assert dunder_get({'a': {'b': 5}}, 'a__c') is None # Error if key is too nested with pytest.raises(Exception): dunder_get({'a': {'b': 5}, 'c': 8}, 'a__b__c') # Error using str keys on list with pytest.raises(Exception): dunder_get([[1, 2], [3, 4]], 'a')
def test_data_type_builder_doc_bad(): a = DocumentProto() a.id = 'a236cbb0eda62d58' with pytest.raises(BadDocType): _new_doc_from_data(b'BREAKIT!' + a.SerializeToString(), DataInputType.DOCUMENT) with pytest.raises(BadDocType): _new_doc_from_data(MessageToJson(a) + '�', DataInputType.DOCUMENT)
def test_copy_construct(): a = DocumentProto() b = Document(a, copy=True) a.id = '1' * 16 assert b.id != '1' * 16 b.id = '2' * 16 assert a.id == '1' * 16
def test_data_type_builder_doc(builder): a = DocumentProto() a.id = 'a236cbb0eda62d58' d, t = _build_doc(builder(a), DataInputType.DOCUMENT, override_doc_id=False) assert d.id == a.id assert t == DataInputType.DOCUMENT
def input_fn(): doc1 = DocumentProto() doc2 = DocumentProto() ev1 = doc1.evaluations.add() ev1.value = 1 ev1.op_name = 'op1' ev2 = doc2.evaluations.add() ev2.value = 2 ev2.op_name = 'op2' return [doc1, doc2]
def input_fn(): doc1 = DocumentProto() doc2 = DocumentProto() # doc1 and doc2 should have the same id ev1 = doc1.evaluations.add() ev1.value = 1 ev1.op_name = 'op1' ev2 = doc2.evaluations.add() ev2.value = 2 ev2.op_name = 'op2' return [doc1, doc2]
def input_function(): doc1 = DocumentProto() doc1.modality = 'mode1' doc2 = DocumentProto() doc2.modality = 'mode2' doc3 = DocumentProto() doc3.modality = 'mode1' return [doc1, doc2, doc3]
def input_fn(): doc1 = DocumentProto() doc1.text = 'title: this is mode1 from doc1, body: this is mode2 from doc1' doc2 = DocumentProto() doc2.text = 'title: this is mode1 from doc2, body: this is mode2 from doc2' doc3 = DocumentProto() doc3.text = 'title: this is mode1 from doc3, body: this is mode2 from doc3' return [doc1, doc2, doc3]
def input_fn(): doc1 = DocumentProto() NdArray(doc1.embedding).value = e1 c = doc1.chunks.add() NdArray(c.embedding).value = e2 c.id = uid.new_doc_id(c) doc2 = DocumentProto() NdArray(doc2.embedding).value = e3 d = doc2.chunks.add() d.id = uid.new_doc_id(d) NdArray(d.embedding).value = e4 return [doc1, doc2]
def input_fn(): doc1 = DocumentProto() NdArray(doc1.embedding).value = e1 c = doc1.chunks.add() NdArray(c.embedding).value = e2 c.id = UniqueId(1) doc2 = DocumentProto() NdArray(doc2.embedding).value = e3 d = doc2.chunks.add() d.id = UniqueId(2) NdArray(d.embedding).value = e4 return [doc1, doc2]
def eval_request(): req = Request() req.request_type = 'search' # doc: 1 # doc: 2 # doc: 3 # doc: 4 # doc: 5 - will be missing from KV indexer for idx in range(5): dp = DocumentProto() dp.id = f'0{str(idx + 1)}' req.docs.append(Document(dp)) return req
def new_doc_id(doc: 'DocumentProto') -> str: """ Generate a new hexdigest based on the content of the document. .. note:: Always use it AFTER you fill in the content of the document :param doc: a non-empty document :return: the hexdigest based on :meth:`blake2b` """ d = doc if _doc_field_mask: d = DocumentProto() _doc_field_mask.MergeMessage(doc, d) return blake2b(d.SerializeToString(), digest_size=_digest_size).hexdigest()
def test_data_type_builder_doc_bad(): a = DocumentProto() a.id = 'a236cbb0eda62d58' with pytest.raises(BadDocType): _build_doc(b'BREAKIT!' + a.SerializeToString(), DataInputType.DOCUMENT, override_doc_id=False) with pytest.raises(BadDocType): _build_doc(MessageToJson(a) + '🍔', DataInputType.DOCUMENT, override_doc_id=False) with pytest.raises(BadDocType): _build_doc({'🍔': '🍔'}, DataInputType.DOCUMENT, override_doc_id=False)
def document(): d = DocumentProto() d.tags['int'] = 1 # will convert to float!!! d.tags['str'] = 'blah' d.tags['float'] = 0.1234 d.tags['bool'] = True d.tags['nested'] = {'bool': True} return d
def test_tags(document): jd = MessageToJson(document) d2 = Parse(jd, DocumentProto()) assert isinstance(d2.tags['int'], float) assert isinstance(d2.tags['str'], str) assert isinstance(d2.tags['float'], float) assert isinstance(d2.tags['bool'], bool) assert isinstance(d2.tags['nested']['bool'], bool) # can be used as a dict for _, _ in d2.tags['nested'].items(): continue
def test_tags_assign(): d = DocumentProto() d.tags.update({'int': 1, 'float': 0.1234}) with pytest.raises(AttributeError): d.tags = {'int': 1, 'float': 0.1234}
@pytest.fixture(scope='function') def flow_with_rest_api_enabled(): return Flow(rest_api=True).add() @pytest.fixture(scope='function') def test_img_1(): return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAA2ElEQVR4nADIADf/AxWcWRUeCEeBO68T3u1qLWarHqMaxDnxhAEaLh0Ssu6ZGfnKcjP4CeDLoJok3o4aOPYAJocsjktZfo4Z7Q/WR1UTgppAAdguAhR+AUm9AnqRH2jgdBZ0R+kKxAFoAME32BL7fwQbcLzhw+dXMmY9BS9K8EarXyWLH8VYK1MACkxlLTY4Eh69XfjpROqjE7P0AeBx6DGmA8/lRRlTCmPkL196pC0aWBkVs2wyjqb/LABVYL8Xgeomjl3VtEMxAeaUrGvnIawVh/oBAAD///GwU6v3yCoVAAAAAElFTkSuQmCC' @pytest.fixture(scope='function') def test_img_2(): return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAA2ElEQVR4nADIADf/AvdGjTZeOlQq07xSYPgJjlWRwfWEBx2+CgAVrPrP+O5ghhOa+a0cocoWnaMJFAsBuCQCgiJOKDBcIQTiLieOrPD/cp/6iZ/Iu4HqAh5dGzggIQVJI3WqTxwVTDjs5XJOy38AlgHoaKgY+xJEXeFTyR7FOfF7JNWjs3b8evQE6B2dTDvQZx3n3Rz6rgOtVlaZRLvR9geCAxuY3G+0mepEAhrTISES3bwPWYYi48OUrQOc//IaJeij9xZGGmDIG9kc73fNI7eA8VMBAAD//0SxXMMT90UdAAAAAElFTkSuQmCC' @pytest.mark.parametrize('input_fn', [iter([b'1234', b'45467']), iter([DocumentProto(), DocumentProto()])]) def test_check_input_success(input_fn): Client.check_input(input_fn) @pytest.mark.parametrize('input_fn', [iter([list(), list(), [12, 2, 3]]), iter([set(), set()])]) def test_check_input_fail(input_fn): with pytest.raises(BadClientInput): Client.check_input(input_fn) @pytest.mark.parametrize( 'port_expose, route, status_code', [ (random_port(), '/ready', 200), (random_port(), '/api/ass', 405)
def flow_with_rest_api_enabled(): return Flow(rest_api=True).add() @pytest.fixture(scope='function') def test_img_1(): return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAA2ElEQVR4nADIADf/AxWcWRUeCEeBO68T3u1qLWarHqMaxDnxhAEaLh0Ssu6ZGfnKcjP4CeDLoJok3o4aOPYAJocsjktZfo4Z7Q/WR1UTgppAAdguAhR+AUm9AnqRH2jgdBZ0R+kKxAFoAME32BL7fwQbcLzhw+dXMmY9BS9K8EarXyWLH8VYK1MACkxlLTY4Eh69XfjpROqjE7P0AeBx6DGmA8/lRRlTCmPkL196pC0aWBkVs2wyjqb/LABVYL8Xgeomjl3VtEMxAeaUrGvnIawVh/oBAAD///GwU6v3yCoVAAAAAElFTkSuQmCC' @pytest.fixture(scope='function') def test_img_2(): return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAA2ElEQVR4nADIADf/AvdGjTZeOlQq07xSYPgJjlWRwfWEBx2+CgAVrPrP+O5ghhOa+a0cocoWnaMJFAsBuCQCgiJOKDBcIQTiLieOrPD/cp/6iZ/Iu4HqAh5dGzggIQVJI3WqTxwVTDjs5XJOy38AlgHoaKgY+xJEXeFTyR7FOfF7JNWjs3b8evQE6B2dTDvQZx3n3Rz6rgOtVlaZRLvR9geCAxuY3G+0mepEAhrTISES3bwPWYYi48OUrQOc//IaJeij9xZGGmDIG9kc73fNI7eA8VMBAAD//0SxXMMT90UdAAAAAElFTkSuQmCC' @pytest.mark.parametrize( 'inputs', [iter([b'1234', b'45467']), iter([DocumentProto(), DocumentProto()])] ) def test_check_input_success(inputs): Client.check_input(inputs) @pytest.mark.parametrize( 'inputs', [iter([list(), list(), [12, 2, 3]]), iter([set(), set()])] ) def test_check_input_fail(inputs): with pytest.raises(BadClientInput): Client.check_input(inputs) @pytest.mark.parametrize( 'port_expose, route, status_code',
@pytest.fixture(scope='function') def test_img_1(): return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAA2ElEQVR4nADIADf/AxWcWRUeCEeBO68T3u1qLWarHqMaxDnxhAEaLh0Ssu6ZGfnKcjP4CeDLoJok3o4aOPYAJocsjktZfo4Z7Q/WR1UTgppAAdguAhR+AUm9AnqRH2jgdBZ0R+kKxAFoAME32BL7fwQbcLzhw+dXMmY9BS9K8EarXyWLH8VYK1MACkxlLTY4Eh69XfjpROqjE7P0AeBx6DGmA8/lRRlTCmPkL196pC0aWBkVs2wyjqb/LABVYL8Xgeomjl3VtEMxAeaUrGvnIawVh/oBAAD///GwU6v3yCoVAAAAAElFTkSuQmCC' @pytest.fixture(scope='function') def test_img_2(): return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAA2ElEQVR4nADIADf/AvdGjTZeOlQq07xSYPgJjlWRwfWEBx2+CgAVrPrP+O5ghhOa+a0cocoWnaMJFAsBuCQCgiJOKDBcIQTiLieOrPD/cp/6iZ/Iu4HqAh5dGzggIQVJI3WqTxwVTDjs5XJOy38AlgHoaKgY+xJEXeFTyR7FOfF7JNWjs3b8evQE6B2dTDvQZx3n3Rz6rgOtVlaZRLvR9geCAxuY3G+0mepEAhrTISES3bwPWYYi48OUrQOc//IaJeij9xZGGmDIG9kc73fNI7eA8VMBAAD//0SxXMMT90UdAAAAAElFTkSuQmCC' @pytest.mark.parametrize( 'inputs', [iter([b'1234', b'45467']), iter([DocumentProto(), DocumentProto()])]) def test_check_input_success(inputs): Client.check_input(inputs) @pytest.mark.parametrize( 'inputs', [iter([list(), list(), [12, 2, 3]]), iter([set(), set()])]) def test_check_input_fail(inputs): with pytest.raises(BadClientInput): Client.check_input(inputs) @pytest.mark.parametrize('port_expose, route, status_code', [(random_port(), '/status', 200), (random_port(), '/api/ass', 405)])
def test_data_type_builder_doc(builder): a = DocumentProto() a.id = 'a236cbb0eda62d58' d, t = _new_doc_from_data(builder(a), DataInputType.DOCUMENT) assert d.id == a.id assert t == DataInputType.DOCUMENT