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 = Document() d.tags['a'] = 'hello' assert dunder_get(d, 'tags__a') == 'hello' # Error on invalid key with pytest.raises(Exception): dunder_get({'a': {'b': 5}}, 'a__c') # 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_check_input(self): input_fn = iter([b'1234', b'45467']) PyClient.check_input(input_fn) input_fn = iter([Document(), Document()]) PyClient.check_input(input_fn, in_proto=True) bad_input_fn = iter([b'1234', '45467']) self.assertRaises(TypeError, PyClient.check_input, bad_input_fn) bad_input_fn = iter([Document()]) self.assertRaises(TypeError, PyClient.check_input, bad_input_fn)
def test_check_input(self): input_fn = iter([b'1234', b'45467']) PyClient.check_input(input_fn) input_fn = iter([Document(), Document()]) PyClient.check_input(input_fn) bad_input_fn = iter([b'1234', '45467', [12, 2, 3]]) self.assertRaises(TypeError, PyClient.check_input, bad_input_fn) bad_input_fn = iter([Document(), None]) self.assertRaises(TypeError, PyClient.check_input, bad_input_fn)
def test_check_input(self): input_fn = iter([b'1234', b'45467']) PyClient.check_input(input_fn) input_fn = iter([Document(), Document()]) PyClient.check_input(input_fn, input_type=ClientInputType.PROTOBUF) bad_input_fn = iter([b'1234', '45467']) self.assertRaises(TypeError, PyClient.check_input, bad_input_fn) bad_input_fn = iter([Document()]) self.assertRaises(TypeError, PyClient.check_input, bad_input_fn)
def test_check_input(self): input_fn = iter([b'1234', b'45467']) PyClient.check_input(input_fn) input_fn = iter([Document(), Document()]) PyClient.check_input(input_fn, input_type=ClientInputType.PROTOBUF) # bad_input_fn = iter([b'1234', '45467']) this is invalid as we convert str to binary # self.assertRaises(TypeError, PyClient.check_input, bad_input_fn) bad_input_fn = iter([Document()]) self.assertRaises(TypeError, PyClient.check_input, bad_input_fn)
def input_fn(): doc1 = Document() doc2 = Document() 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 test_check_input(): input_fn = iter([b'1234', b'45467']) PyClient.check_input(input_fn) input_fn = iter([Document(), Document()]) PyClient.check_input(input_fn) bad_input_fn_1 = iter([b'1234', '45467', [12, 2, 3]]) with pytest.raises(TypeError): PyClient.check_input(bad_input_fn_1) bad_input_fn_2 = iter([Document(), None]) with pytest.raises(TypeError): PyClient.check_input(bad_input_fn_2)
def input_fn(): doc1 = Document() doc1.modality = 'mode1' doc2 = Document() doc2.modality = 'mode2' doc3 = Document() doc3.modality = 'mode3' return [doc1, doc2, doc3]
def input_fn(): doc1 = Document() doc1.text = 'title: this is mode1 from doc1, body: this is mode2 from doc1' doc2 = Document() doc2.text = 'title: this is mode1 from doc2, body: this is mode2 from doc2' doc3 = Document() doc3.text = 'title: this is mode1 from doc3, body: this is mode2 from doc3' return [doc1, doc2, doc3]
def input_fn(): doc1 = Document() GenericNdArray(doc1.embedding).value = e1 c = doc1.chunks.add() GenericNdArray(c.embedding).value = e2 c.id = uid.new_doc_id(c) doc2 = Document() GenericNdArray(doc2.embedding).value = e3 d = doc2.chunks.add() d.id = uid.new_doc_id(d) GenericNdArray(d.embedding).value = e4 return [doc1, doc2]
def input_fn(): doc1 = Document() doc1.embedding.CopyFrom(array2pb(e1)) c = doc1.chunks.add() c.embedding.CopyFrom(array2pb(e2)) c.id = uid.new_doc_id(c) doc2 = Document() doc2.embedding.CopyFrom(array2pb(e3)) d = doc2.chunks.add() d.id = uid.new_doc_id(d) d.embedding.CopyFrom(array2pb(e4)) return [doc1, doc2]
def document(): d = Document() 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, Document()) 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(self): d = Document() 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} jd = MessageToJson(d) d2 = Parse(jd, Document()) self.assertTrue(isinstance(d2.tags['int'], float)) self.assertTrue(isinstance(d2.tags['str'], str)) self.assertTrue(isinstance(d2.tags['float'], float)) self.assertTrue(isinstance(d2.tags['bool'], bool)) self.assertTrue(isinstance(d2.tags['nested']['bool'], bool)) # can be used as a dict for k, v in d2.tags['nested'].items(): print(f'{k}:{v}')
def input_fn(): d = Document() d.mime_type = 'text/plain' c = d.chunks.add() c.blob.CopyFrom(array2pb(np.random.random(7))) yield d d = Document() d.mime_type = 'image/png' c = d.chunks.add() c.blob.CopyFrom(array2pb(np.random.random(5))) yield d
def input_fn(): doc1 = Document() doc1.id = 1 doc1.embedding.CopyFrom(array2pb(np.random.random([7]))) c = doc1.chunks.add() c.id = 3 c.embedding.CopyFrom(array2pb(np.random.random([5]))) doc2 = Document() doc2.id = 2 doc2.embedding.CopyFrom(array2pb(np.random.random([3]))) d = doc2.chunks.add() d.id = 4 d.embedding.CopyFrom(array2pb(np.random.random([9]))) return [doc1, doc2]
def input_fn(): doc1 = Document() doc1.id = 1 doc1.embedding.CopyFrom(array2pb(e1)) c = doc1.chunks.add() c.id = 3 c.embedding.CopyFrom(array2pb(e2)) doc2 = Document() doc2.id = 2 doc2.embedding.CopyFrom(array2pb(e3)) d = doc2.chunks.add() d.id = 4 d.embedding.CopyFrom(array2pb(e4)) return [doc1, doc2]
@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' def test_client(flow): with flow: py_client(port_expose=flow.port_expose).call_unary( b'a1234', mode=ClientMode.INDEX) @pytest.mark.parametrize( 'input_fn', [iter([b'1234', b'45467']), iter([Document(), Document()])]) def test_check_input_success(input_fn): PyClient.check_input(input_fn) @pytest.mark.parametrize( 'input_fn', [iter([b'1234', '45467', [12, 2, 3]]), iter([Document(), None])]) def test_check_input_fail(input_fn): with pytest.raises(TypeError): PyClient.check_input(input_fn) @pytest.mark.parametrize('port_expose, route, status_code', [(random_port(), '/ready', 200),