def test_get_open_id_configuration(self): """ Test the `get_open_id_configuration` method. """ client = Client("http://localhost:8080") # Request to weaviate returns 200 connection_mock = mock_connection_method('get', return_json="OK!") client._connection = connection_mock self.assertEqual(client.get_open_id_configuration(), "OK!") connection_mock.get.assert_called_with( path="/.well-known/openid-configuration") # Request to weaviate returns 404 connection_mock = mock_connection_method('get', status_code=404) client._connection = connection_mock self.assertIsNone(client.get_open_id_configuration()) connection_mock.get.assert_called_with( path="/.well-known/openid-configuration") # Request to weaviate returns 204 connection_mock = mock_connection_method('get', status_code=204) client._connection = connection_mock with self.assertRaises(UnexpectedStatusCodeException) as error: client.get_open_id_configuration() error_message = f"Meta endpoint! Unexpected status code: 204, with response body: None" check_error_message(self, error, error_message) connection_mock.get.assert_called_with( path="/.well-known/openid-configuration")
def test_get_concept_vector(self): """ Test `get_concept_vector` method. """ # test valid call connection_mock = mock_connection_method('get', return_json={"A": "B"}) contextionary = Contextionary(connection_mock) self.assertEqual("B", contextionary.get_concept_vector("sauce")["A"]) connection_mock.get.assert_called_with( path="/modules/text2vec-contextionary/concepts/sauce", ) # test exceptions # error messages requests_error_message = 'text2vec-contextionary vector was not retrieved.' unexpected_exception_error_message = "text2vec-contextionary vector" ## test UnexpectedStatusCodeException contextionary = Contextionary( mock_connection_method('get', status_code=404)) with self.assertRaises(UnexpectedStatusCodeException) as error: contextionary.get_concept_vector("Palantir") check_startswith_error_message(self, error, unexpected_exception_error_message) ## test requests error contextionary = Contextionary( mock_connection_method( 'get', side_effect=RequestsConnectionError("Test!"))) with self.assertRaises(RequestsConnectionError) as error: contextionary.get_concept_vector("Palantir") check_error_message(self, error, requests_error_message)
def test_get(self, mock_get_params): """ Test the `get` method. """ # error messages requests_error_message = 'Could not get object/s.' unexpected_error_message = "Get object/s" # test exceptions data_object = DataObject( mock_connection_method('get', side_effect=RequestsConnectionError("Test!")) ) with self.assertRaises(RequestsConnectionError) as error: data_object.get() check_error_message(self, error, requests_error_message) data_object = DataObject( mock_connection_method('get', status_code=204) ) with self.assertRaises(UnexpectedStatusCodeException) as error: data_object.get() check_startswith_error_message(self, error, unexpected_error_message) # test valid calls return_value_get = {"my_key": 12341} mock_get_params.return_value = {'include': "test1,test2"} connection_mock = mock_connection_method('get', return_json=return_value_get, status_code=200) data_object = DataObject(connection_mock) result = data_object.get() self.assertEqual(result, return_value_get) connection_mock.get.assert_called_with( path="/objects", params={'include': "test1,test2"} ) return_value_get = {"my_key": '12341'} mock_get_params.return_value = {'include': "test1,test2"} connection_mock = mock_connection_method('get', return_json=return_value_get, status_code=200) data_object = DataObject(connection_mock) result = data_object.get(uuid="1d420c9c98cb11ec9db61e008a366d49") self.assertEqual(result, return_value_get) connection_mock.get.assert_called_with( path="/objects/1d420c9c-98cb-11ec-9db6-1e008a366d49", params={'include': "test1,test2"} ) return_value_get = {"my_key": '12341'} mock_get_params.return_value = {'include': "test1,test2"} connection_mock = mock_connection_method('get', return_json=return_value_get, status_code=404) data_object = DataObject(connection_mock) result = data_object.get(uuid="1d420c9c-98cb-11ec-9db6-1e008a366d49") self.assertIsNone(result) connection_mock.get.assert_called_with( path="/objects/1d420c9c-98cb-11ec-9db6-1e008a366d49", params={'include': "test1,test2"} )
def test_contains(self): """ Test the `contains` method. """ # If a schema is present it should return true otherwise false # 1. test schema is present: schema = Schema( mock_connection_method('get', return_json=persons_return_test_schema)) self.assertTrue(schema.contains()) # 2. test no schema is present: schema = Schema( mock_connection_method('get', return_json={"classes": []})) self.assertFalse(schema.contains()) # 3. test with 'schema' argument ## Test weaviate.schema.contains specific schema. schema = Schema( mock_connection_method('get', return_json=persons_return_test_schema)) self.assertFalse(schema.contains(company_test_schema)) subset_schema = { "classes": [{ "class": "Person", "description": "", "properties": [{ "dataType": ["text"], "description": "", "name": "name" }] }] } self.assertTrue(schema.contains(subset_schema)) ## Test weaviate.schema.contains schema from file. schema = Schema( mock_connection_method('get', return_json=persons_return_test_schema)) schema_json_file = os.path.join(os.path.dirname(__file__), "schema_company.json") self.assertFalse(schema.contains(schema_json_file)) schema = Schema( mock_connection_method('get', return_json=company_test_schema)) self.assertTrue(schema.contains(schema_json_file))
def test_delete_everything(self): """ Test the `delete_all` method. """ mock_connection = mock_connection_method( 'get', return_json=company_test_schema) mock_connection = mock_connection_method( 'delete', connection_mock=mock_connection) schema = Schema(mock_connection) schema.delete_all() self.assertEqual(mock_connection.get.call_count, 1) self.assertEqual(mock_connection.delete.call_count, 2)
def test_is_live(self): """ Test the `is_live` method. """ client = Client("http://localhost:8080") # Request to weaviate returns 200 connection_mock = mock_connection_method('get') client._connection = connection_mock self.assertTrue(client.is_live()) # Should be true connection_mock.get.assert_called_with(path="/.well-known/live") # Request to weaviate returns 404 connection_mock = mock_connection_method('get', status_code=404) client._connection = connection_mock self.assertFalse(client.is_live()) # Should be false connection_mock.get.assert_called_with(path="/.well-known/live")
def helper_test(test_class, test_class_call): mock_rest = mock_connection_method('post') schema = Schema(mock_rest) schema._create_class_with_premitives(test_class) self.assertEqual(mock_rest.post.call_count, 1) mock_rest.post.assert_called_with( path="/schema", weaviate_object=test_class_call, )
def test_delete(self): """ Test the `delete` method. """ data_object = DataObject(Mock()) # error messages uuid_type_error_message = lambda dt: f"'uuid' must be of type str or uuid.UUID, but was: {dt}" uuid_value_error_message = "Not valid 'uuid' or 'uuid' can not be extracted from value" requests_error_message = 'Object could not be deleted.' unexpected_error_message = "Delete object" with self.assertRaises(TypeError) as error: data_object.delete(4) check_error_message(self, error, uuid_type_error_message(int)) with self.assertRaises(ValueError) as error: data_object.delete("Hallo World") check_error_message(self, error, uuid_value_error_message) connection_mock = mock_connection_method('delete', side_effect=RequestsConnectionError('Test!')) data_object = DataObject(connection_mock) with self.assertRaises(RequestsConnectionError) as error: data_object.delete("b36268d4-a6b5-5274-985f-45f13ce0c642") check_error_message(self, error, requests_error_message) connection_mock = mock_connection_method('delete', status_code=404) data_object = DataObject(connection_mock) with self.assertRaises(UnexpectedStatusCodeException) as error: data_object.delete("b36268d4-a6b5-5274-985f-45f13ce0c642") check_startswith_error_message(self, error, unexpected_error_message) # 1. Successfully delete something connection_mock = mock_connection_method('delete', status_code=204) data_object = DataObject(connection_mock) object_id = "b36268d4-a6b5-5274-985f-45f13ce0c642" data_object.delete(object_id) connection_mock.delete.assert_called_with( path="/objects/" + object_id )
def test_get(self): """ Test the `get` method. """ # invalid calls requests_error_message = 'Schema could not be retrieved.' unexpected_error_msg = "Get schema" type_error_msg = lambda dt: f"'class_name' argument must be of type `str`! Given type: {dt}" mock_conn = mock_connection_method( 'get', side_effect=RequestsConnectionError("Test!")) schema = Schema(mock_conn) with self.assertRaises(RequestsConnectionError) as error: schema.get() check_error_message(self, error, requests_error_message) mock_conn = mock_connection_method('get', status_code=404) schema = Schema(mock_conn) with self.assertRaises(UnexpectedStatusCodeException) as error: schema.get() check_startswith_error_message(self, error, unexpected_error_msg) connection_mock_file = mock_connection_method( 'get', status_code=200, return_json={'Test': 'OK!'}) schema = Schema(connection_mock_file) with self.assertRaises(TypeError) as error: schema.get(1234) check_error_message(self, error, type_error_msg(int)) # valid calls self.assertEqual(schema.get(), {'Test': 'OK!'}) connection_mock_file.get.assert_called_with(path="/schema", ) self.assertEqual(schema.get("Artist"), {'Test': 'OK!'}) connection_mock_file.get.assert_called_with(path="/schema/Artist") # with uncapitalized class_name self.assertEqual(schema.get("artist"), {'Test': 'OK!'}) connection_mock_file.get.assert_called_with(path="/schema/Artist")
def test_get(self): """ Test the `get` method. """ # error messages uuid_type_error = lambda dt: f"'uuid' must be of type str or uuid.UUID, but was: {dt}" value_error = "Not valid 'uuid' or 'uuid' can not be extracted from value" requests_error_message = 'Classification status could not be retrieved.' unexpected_error_message = "Get classification status" # invalid calls with self.assertRaises(TypeError) as error: Classification(None).get(123) check_error_message(self, error, uuid_type_error(int)) with self.assertRaises(ValueError) as error: Classification(None).get('123') check_error_message(self, error, value_error) mock_conn = mock_connection_method( 'get', side_effect=RequestsConnectionError('Test!')) with self.assertRaises(RequestsConnectionError) as error: Classification(mock_conn).get( "d087b7c6-a115-5c89-8cb2-f25bdeb9bf92") check_error_message(self, error, requests_error_message) mock_conn = mock_connection_method('get', status_code=404) with self.assertRaises(UnexpectedStatusCodeException) as error: Classification(mock_conn).get( "d087b7c6-a115-5c89-8cb2-f25bdeb9bf92") check_startswith_error_message(self, error, unexpected_error_message) # valid calls mock_conn = mock_connection_method('get', return_json='OK!', status_code=200) result = Classification(mock_conn).get( "d087b7c6-a115-5c89-8cb2-f25bdeb9bf92") self.assertEqual(result, 'OK!')
def test__start(self): """ Test the `_start` method. """ # error messages requests_error_message = 'Classification may not started.' unexpected_error_message = "Start classification" # invalid calls mock_conn = mock_connection_method( 'post', side_effect=RequestsConnectionError('Test!')) config = ConfigBuilder(mock_conn, None) with self.assertRaises(RequestsConnectionError) as error: config._start() check_error_message(self, error, requests_error_message) mock_conn.post.assert_called_with(path="/classifications", weaviate_object={}) mock_conn = mock_connection_method('post', status_code=200) config = ConfigBuilder(mock_conn, None).with_class_name('Test!') with self.assertRaises(UnexpectedStatusCodeException) as error: config._start() check_startswith_error_message(self, error, unexpected_error_message) mock_conn.post.assert_called_with(path="/classifications", weaviate_object={'class': 'Test!'}) # valid calls mock_conn = mock_connection_method('post', status_code=201, return_json='OK!') config = ConfigBuilder( mock_conn, None).with_class_name('TestClass').with_type('TestType') self.assertEqual(config._start(), 'OK!') mock_conn.post.assert_called_with(path="/classifications", weaviate_object={ 'class': 'TestClass', 'type': 'TestType' })
def test_do(self): """ Test the `do` method. """ # test exceptions requests_error_message = 'Query was not successful.' # requests.exceptions.ConnectionError mock_obj = mock_connection_method( 'post', side_effect=RequestsConnectionError("Test")) self.aggregate._connection = mock_obj with self.assertRaises(RequestsConnectionError) as error: self.aggregate.do() check_error_message(self, error, requests_error_message) # weaviate.UnexpectedStatusCodeException mock_obj = mock_connection_method('post', status_code=204) self.aggregate._connection = mock_obj with self.assertRaises(UnexpectedStatusCodeException) as error: self.aggregate.do() check_startswith_error_message(self, error, "Query was not successful") filter = {"path": ["name"], "operator": "Equal", "valueString": "B"} self.aggregate \ .with_group_by_filter(["name"]) \ .with_fields("groupedBy { value }") \ .with_fields("name { count }") \ .with_where(filter) expected_gql_clause = '{Aggregate{Object(where: {path: ["name"] operator: Equal valueString: "B"} groupBy: ["name"]){groupedBy { value }name { count }}}}' mock_obj = mock_connection_method('post', status_code=200, return_json={"status": "OK!"}) self.aggregate._connection = mock_obj self.assertEqual(self.aggregate.do(), {"status": "OK!"}) mock_obj.post.assert_called_with( path="/graphql", weaviate_object={'query': expected_gql_clause})
def test_delete_class_input(self): """ Test the 'delete_class` method. """ schema = Schema(Mock()) # invalid calls type_error_message = lambda t: f"Class name was {t} instead of str" requests_error_message = 'Deletion of class.' with self.assertRaises(TypeError) as error: schema.delete_class(1) check_error_message(self, error, type_error_message(int)) schema = Schema( mock_connection_method( 'delete', side_effect=RequestsConnectionError('Test!'))) with self.assertRaises(RequestsConnectionError) as error: schema.delete_class("uuid") check_error_message(self, error, requests_error_message) schema = Schema(mock_connection_method('delete', status_code=404)) with self.assertRaises(UnexpectedStatusCodeException) as error: schema.delete_class("uuid") check_startswith_error_message(self, error, "Delete class from schema") # valid calls mock_conn = mock_connection_method('delete', status_code=200) schema = Schema(mock_conn) schema.delete_class("Test") mock_conn.delete.assert_called_with(path="/schema/Test") # with uncapitalized class_name mock_conn = mock_connection_method('delete', status_code=200) schema = Schema(mock_conn) schema.delete_class("test") mock_conn.delete.assert_called_with(path="/schema/Test")
def helper_test(nr_calls=1): mock_rest = mock_connection_method('post') schema = Schema(mock_rest) schema._create_complex_properties_from_class(properties) self.assertEqual(mock_rest.post.call_count, nr_calls) properties_copy = deepcopy(properties['properties']) for prop in properties_copy: prop['dataType'] = [ _capitalize_first_letter(dt) for dt in prop['dataType'] ] mock_rest.post.assert_called_with( path="/schema/" + _capitalize_first_letter(properties["class"]) + "/properties", weaviate_object=properties_copy[0])
def test_raw(self): """ Test the `raw` method. """ # valid calls connection_mock = mock_connection_method('post') query = Query(connection_mock) gql_query = "{Get {Group {name Members {... on Person {name}}}}}" query.raw(gql_query) connection_mock.post.assert_called_with( path="/graphql", weaviate_object={"query": gql_query}) # invalid calls type_error_message = "Query is expected to be a string" requests_error_message = 'Query not executed.' query_error_message = "GQL query failed" with self.assertRaises(TypeError) as error: query.raw(["TestQuery"]) check_error_message(self, error, type_error_message) query = Query( mock_connection_method( 'post', side_effect=RequestsConnectionError("Test!"))) with self.assertRaises(RequestsConnectionError) as error: query.raw("TestQuery") check_error_message(self, error, requests_error_message) query = Query(mock_connection_method('post', status_code=404)) with self.assertRaises(UnexpectedStatusCodeException) as error: query.raw("TestQuery") check_startswith_error_message(self, error, query_error_message)
def test_is_ready(self): """ Test the `is_ready` method. """ client = Client("http://localhost:8080") # Request to weaviate returns 200 connection_mock = mock_connection_method('get') client._connection = connection_mock self.assertTrue(client.is_ready()) # Should be true connection_mock.get.assert_called_with(path="/.well-known/ready") # Request to weaviate returns 404 connection_mock = mock_connection_method('get', status_code=404) client._connection = connection_mock self.assertFalse(client.is_ready()) # Should be false connection_mock.get.assert_called_with(path="/.well-known/ready") # Test exception in connect connection_mock = mock_connection_method( 'get', side_effect=RequestsConnectionError("Test")) client._connection = connection_mock self.assertFalse(client.is_ready()) connection_mock.get.assert_called_with(path="/.well-known/ready")
def test_delete(self): """ Test `delete` method`. """ reference = Reference(Mock()) # error messages unexpected_error_msg = 'Delete property reference to object' connection_error_msg = 'Reference was not deleted.' # invalid calls with self.assertRaises(TypeError) as error: reference.delete(1, "myProperty", self.uuid_2) check_error_message(self, error, self.uuid_error_message) with self.assertRaises(TypeError) as error: reference.delete(self.uuid_1, "myProperty", 2) check_error_message(self, error, self.uuid_error_message) with self.assertRaises(TypeError) as error: reference.delete(self.uuid_1, 3, self.uuid_2) check_error_message(self, error, self.name_error_message(int)) with self.assertRaises(ValueError) as error: reference.delete("str", "myProperty", self.uuid_2) check_error_message(self, error, self.valid_uuid_error_message) with self.assertRaises(ValueError) as error: reference.delete(self.uuid_1, "myProperty", "str") check_error_message(self, error, self.valid_uuid_error_message) mock_obj = mock_connection_method('delete', status_code=200) reference = Reference(mock_obj) with self.assertRaises(UnexpectedStatusCodeException) as error: reference.delete(self.uuid_1, "myProperty", self.uuid_2) check_startswith_error_message(self, error, unexpected_error_msg) mock_obj = mock_connection_method('delete', side_effect=RequestsConnectionError("Test!")) reference = Reference(mock_obj) with self.assertRaises(RequestsConnectionError) as error: reference.delete(self.uuid_1, "myProperty", self.uuid_2) check_error_message(self, error, connection_error_msg) # test valid calls connection_mock = mock_connection_method('delete', status_code=204) reference = Reference(connection_mock) reference.delete( self.uuid_1, "myProperty", self.uuid_2 ) connection_mock.delete.assert_called_with( path=f"/objects/{self.uuid_1}/references/myProperty", weaviate_object={"beacon": f"weaviate://localhost/{self.uuid_2}"}, ) reference.delete( self.uuid_1, "hasItem", f"http://localhost:8080/v1/objects/{self.uuid_2}" ) connection_mock.delete.assert_called_with( path=f"/objects/{self.uuid_1}/references/hasItem", weaviate_object={"beacon": f"weaviate://localhost/{self.uuid_2}"}, )
def test__create_complex_properties_from_class(self): """ Test the `_create_complex_properties_from_class` method. """ # valid calls def helper_test(nr_calls=1): mock_rest = mock_connection_method('post') schema = Schema(mock_rest) schema._create_complex_properties_from_class(properties) self.assertEqual(mock_rest.post.call_count, nr_calls) properties_copy = deepcopy(properties['properties']) for prop in properties_copy: prop['dataType'] = [ _capitalize_first_letter(dt) for dt in prop['dataType'] ] mock_rest.post.assert_called_with( path="/schema/" + _capitalize_first_letter(properties["class"]) + "/properties", weaviate_object=properties_copy[0]) # no `properties` key mock_rest = mock_connection_method('post') schema = Schema(mock_rest) schema._create_complex_properties_from_class({}) self.assertEqual(mock_rest.run_rest.call_count, 0) # no COMPLEX properties properties = {'properties': [{'dataType': ["text"]}]} schema._create_complex_properties_from_class(properties) self.assertEqual(mock_rest.post.call_count, 0) properties = { 'properties': [{ 'dataType': ["text"] }, { 'dataType': ['string'] }] } schema._create_complex_properties_from_class(properties) self.assertEqual(mock_rest.post.call_count, 0) # COMPLEX properties properties = { 'class': 'TestClass', 'properties': [ { 'dataType': ["Test"], 'description': "test description", 'name': 'test_prop' }, ] } mock_rest = mock_connection_method('post') schema = Schema(mock_rest) schema._create_complex_properties_from_class(properties) self.assertEqual(mock_rest.post.call_count, 1) properties = { 'class': 'TestClass', 'properties': [ { 'dataType': ["Test"], 'description': "test description", 'name': 'test_prop' }, ] } helper_test() properties['properties'][0]['indexInverted'] = True helper_test() properties['properties'][0]['moduleConfig'] = {'test': 'ok!'} helper_test() properties['properties'].append( properties['properties'][0]) # add another property properties['properties'].append( properties['properties'][0]) # add another property helper_test(3) # with uncapitalized class_name properties['class'] = 'testClass' helper_test(3) properties = { 'class': 'testClass', 'properties': [ { 'dataType': ["test", 'myTest'], 'description': "test description", 'name': 'test_prop' }, ] } # invalid calls requests_error_message = 'Property may not have been created properly.' mock_rest = mock_connection_method( 'post', side_effect=RequestsConnectionError('TEST1')) schema = Schema(mock_rest) with self.assertRaises(RequestsConnectionError) as error: schema._create_complex_properties_from_class(properties) check_error_message(self, error, requests_error_message) mock_rest = mock_connection_method('post', status_code=404) schema = Schema(mock_rest) with self.assertRaises(UnexpectedStatusCodeException) as error: schema._create_complex_properties_from_class(properties) check_startswith_error_message(self, error, "Add properties to classes")
def test_create(self): """ Test `create` method. """ property = Property(Mock()) # invalid calls error_message = "Class name must be of type str but is " check_property_error_message = 'Property does not contain "dataType"' requests_error_message = 'Property was created properly.' with self.assertRaises(TypeError) as error: property.create(35, {}) check_error_message(self, error, error_message + str(int)) # test if `check_property` is called in `create` with self.assertRaises(SchemaValidationException) as error: property.create("Class", {}) check_error_message(self, error, check_property_error_message) property = Property( mock_connection_method( 'post', side_effect=RequestsConnectionError('Test!'))) with self.assertRaises(RequestsConnectionError) as error: property.create("Class", { "name": 'test', 'dataType': ["test_type"] }) check_error_message(self, error, requests_error_message) property = Property(mock_connection_method('post', status_code=404)) with self.assertRaises(UnexpectedStatusCodeException) as error: property.create("Class", { "name": 'test', 'dataType': ["test_type"] }) check_startswith_error_message(self, error, "Add property to class") # valid calls connection_mock = mock_connection_method( 'post') # Mock calling weaviate property = Property(connection_mock) test_prop = { "dataType": ["string"], "description": "my Property", "moduleConfig": { "text2vec-contextionary": { "vectorizePropertyName": True } }, "name": "superProp", "indexInverted": True } property.create("TestThing", test_prop) connection_mock.post.assert_called_with( path="/schema/TestThing/properties", weaviate_object=test_prop, ) property.create("testThing", test_prop) connection_mock.post.assert_called_with( path="/schema/TestThing/properties", weaviate_object=test_prop, )
def test__create_class_with_premitives(self): """ Test the `_create_class_with_premitives` method. """ # valid calls def helper_test(test_class, test_class_call): mock_rest = mock_connection_method('post') schema = Schema(mock_rest) schema._create_class_with_premitives(test_class) self.assertEqual(mock_rest.post.call_count, 1) mock_rest.post.assert_called_with( path="/schema", weaviate_object=test_class_call, ) test_class = { "class": "TestClass", "properties": [{ 'dataType': ['int'], 'name': 'test_prop', 'description': 'None' }, { 'dataType': ['Test'], 'name': 'test_prop', 'description': 'None' }] } test_class_call = { "class": "TestClass", "properties": [ { 'dataType': ['int'], 'name': 'test_prop', 'description': 'None' }, ] } helper_test(test_class, test_class_call) test_class['description'] = 'description' test_class_call['description'] = 'description' helper_test(test_class, test_class_call) test_class['description'] = 'description' test_class_call['description'] = 'description' helper_test(test_class, test_class_call) test_class['vectorIndexType'] = 'vectorIndexType' test_class_call['vectorIndexType'] = 'vectorIndexType' helper_test(test_class, test_class_call) test_class['vectorIndexConfig'] = { 'vectorIndexConfig': 'vectorIndexConfig' } test_class_call['vectorIndexConfig'] = { 'vectorIndexConfig': 'vectorIndexConfig' } helper_test(test_class, test_class_call) test_class['vectorizer'] = 'test_vectorizer' test_class_call['vectorizer'] = 'test_vectorizer' helper_test(test_class, test_class_call) test_class['moduleConfig'] = {'moduleConfig': 'moduleConfig'} test_class_call['moduleConfig'] = {'moduleConfig': 'moduleConfig'} helper_test(test_class, test_class_call) test_class['shardingConfig'] = {'shardingConfig': 'shardingConfig'} test_class_call['shardingConfig'] = { 'shardingConfig': 'shardingConfig' } helper_test(test_class, test_class_call) # multiple properties do not imply multiple `run_rest` calls test_class['properties'].append( test_class['properties'][0]) # add another property test_class['properties'].append( test_class['properties'][0]) # add another property test_class_call['properties'].append( test_class['properties'][0]) # add another property test_class_call['properties'].append( test_class['properties'][0]) # add another property helper_test(test_class, test_class_call) # with uncapitalized class_name test_class['class'] = 'testClass' helper_test(test_class, test_class_call) # invalid calls requests_error_message = 'Class may not have been created properly.' mock_rest = mock_connection_method( 'post', side_effect=RequestsConnectionError('TEST1')) schema = Schema(mock_rest) with self.assertRaises(RequestsConnectionError) as error: schema._create_class_with_premitives(test_class) check_error_message(self, error, requests_error_message) mock_rest = mock_connection_method('post', status_code=404) schema = Schema(mock_rest) with self.assertRaises(UnexpectedStatusCodeException) as error: schema._create_class_with_premitives(test_class) check_startswith_error_message(self, error, "Create class")
def test_extend(self): """ Test `extend` method. """ contextionary = Contextionary(Mock()) some_concept = { "concept": "lsd", "definition": "In probability and statistics, the logarithmic series distribution is a discrete probability distribution derived from the Maclaurin series expansion" } # error messages concept_type_error_message = "Concept must be string" definition_type_error_message = "Definition must be string" weight_type_error_message = "Weight must be float" weight_value_error_message = "Weight out of limits 0.0 <= weight <= 1.0" requests_error_message = 'text2vec-contextionary could not be extended.' unexpected_error_message = "Extend text2vec-contextionary" ## test exceptions with self.assertRaises(TypeError) as error: contextionary.extend(concept=None, definition=some_concept["definition"], weight=1.0) check_error_message(self, error, concept_type_error_message) with self.assertRaises(TypeError) as error: contextionary.extend(concept=some_concept["concept"], definition=None, weight=1.0) check_error_message(self, error, definition_type_error_message) with self.assertRaises(TypeError) as error: contextionary.extend(**some_concept, weight=None) check_error_message(self, error, weight_type_error_message) with self.assertRaises(ValueError) as error: contextionary.extend(**some_concept, weight=1.1) check_error_message(self, error, weight_value_error_message) with self.assertRaises(ValueError) as error: contextionary.extend(**some_concept, weight=-1.0) check_error_message(self, error, weight_value_error_message) ## test UnexpectedStatusCodeException contextionary = Contextionary( mock_connection_method('post', status_code=404)) with self.assertRaises(UnexpectedStatusCodeException) as error: contextionary.extend(**some_concept) check_startswith_error_message(self, error, unexpected_error_message) ## test requests error contextionary = Contextionary( mock_connection_method( 'post', side_effect=RequestsConnectionError("Test!"))) with self.assertRaises(RequestsConnectionError) as error: contextionary.extend(**some_concept) check_error_message(self, error, requests_error_message) ## test valid call without specifying 'weight' some_concept["weight"] = 1.0 connection_mock = mock_connection_method('post', status_code=200) contextionary = Contextionary(connection_mock) contextionary.extend(**some_concept) connection_mock.post.assert_called_with( path="/modules/text2vec-contextionary/extensions", weaviate_object=some_concept, ) ## test valid call with specifying 'weight as error' connection_mock = mock_connection_method('post', status_code=200) contextionary = Contextionary(connection_mock) # add weight to 'some_concept' some_concept["weight"] = .1234 contextionary.extend(**some_concept) connection_mock.post.assert_called_with( path="/modules/text2vec-contextionary/extensions", weaviate_object=some_concept, )
def test_validate(self, mock_get_vector, mock_get_dict_from_object): """ Test the `validate` method. """ data_object = DataObject(Mock()) # error messages uuid_type_error_message = lambda dt: f"'uuid' must be of type str or uuid.UUID, but was: {dt}" class_name_error_message = lambda dt: f"Expected class_name of type `str` but was: {dt}" requests_error_message = 'Object was not validated against weaviate.' unexpected_error_message = "Validate object" # test exceptions with self.assertRaises(TypeError) as error: data_object.validate({}, "Name", 1) check_error_message(self, error, uuid_type_error_message(int)) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() with self.assertRaises(TypeError) as error: data_object.validate({}, ["Name"], "73802305-c0da-427e-b21c-d6779a22f35f") check_error_message(self, error, class_name_error_message(list)) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() data_object = DataObject( mock_connection_method('post', side_effect=RequestsConnectionError("Test!")) ) with self.assertRaises(RequestsConnectionError) as error: data_object.validate({"name": "Alan Greenspan"}, "CoolestPersonEver", "73802305-c0da-427e-b21c-d6779a22f35f") check_error_message(self, error, requests_error_message) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() data_object = DataObject( mock_connection_method('post', status_code=204, return_json={}) ) with self.assertRaises(UnexpectedStatusCodeException) as error: data_object.validate({"name": "Alan Greenspan"}, "CoolestPersonEver", "73802305-c0da-427e-b21c-d6779a22f35f") check_startswith_error_message(self, error, unexpected_error_message) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() # test valid calls # test for status_code 200 without vector argument connection_mock = mock_connection_method('post', status_code=200) data_object = DataObject(connection_mock) response = data_object.validate({"A": 2}, "Hero", "27be9d8d-1da1-4d52-821f-bc7e2a25247d") self.assertEqual(response, {'error': None, 'valid': True}) weaviate_obj = { "id": "27be9d8d-1da1-4d52-821f-bc7e2a25247d", "class": "Hero", "properties": {"A": 2} } connection_mock.post.assert_called_with( path="/objects/validate", weaviate_object=weaviate_obj ) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() ### with uncapitalized class_name connection_mock = mock_connection_method('post', status_code=200) data_object = DataObject(connection_mock) response = data_object.validate({"A": 2}, "hero", "27be9d8d-1da1-4d52-821f-bc7e2a25247d") self.assertEqual(response, {'error': None, 'valid': True}) weaviate_obj = { "id": "27be9d8d-1da1-4d52-821f-bc7e2a25247d", "class": "Hero", "properties": {"A": 2} } connection_mock.post.assert_called_with( path="/objects/validate", weaviate_object=weaviate_obj ) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() # test for status_code 422 connection_mock = mock_connection_method('post', status_code=422, return_json={"error": "Not OK!"}) data_object = DataObject(connection_mock) response = data_object.validate({"A": 2}, "Hero", "27be9d8d-1da1-4d52-821f-bc7e2a25247d") self.assertEqual(response, {'error': "Not OK!", 'valid': False}) weaviate_obj = { "id": "27be9d8d-1da1-4d52-821f-bc7e2a25247d", "class": "Hero", "properties": {"A": 2} } connection_mock.post.assert_called_with( path="/objects/validate", weaviate_object=weaviate_obj ) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() # test for status_code 200 with vector argument connection_mock = mock_connection_method('post', status_code=200) data_object = DataObject(connection_mock) response = data_object.validate({"A": 2}, "Hero", "27be9d8d-1da1-4d52-821f-bc7e2a25247d", vector=[-9.8, 6.66]) self.assertEqual(response, {'error': None, 'valid': True}) weaviate_obj = { "id": "27be9d8d-1da1-4d52-821f-bc7e2a25247d", "class": "Hero", "properties": {"A": 2}, "vector": [-9.8, 6.66] } connection_mock.post.assert_called_with( path="/objects/validate", weaviate_object=weaviate_obj ) mock_get_dict_from_object.assert_called() mock_get_vector.assert_called()
def test_update(self): """ Test the `update` method. """ reference = Reference(Mock()) # error messages unexpected_error_msg = 'Update property reference to object' connection_error_msg = 'Reference was not updated.' # test exceptions with self.assertRaises(TypeError) as error: reference.update(1, "prop", [self.uuid_1]) check_error_message(self, error, self.uuid_error_message) with self.assertRaises(TypeError) as error: reference.update(self.uuid_1, 1, [self.uuid_2]) check_error_message(self, error, self.name_error_message(int)) with self.assertRaises(TypeError) as error: reference.update(self.uuid_1, "prop", 1) check_error_message(self, error, self.uuid_error_message) with self.assertRaises(TypeError) as error: reference.update(self.uuid_1, "prop", [1]) check_error_message(self, error, self.uuid_error_message) with self.assertRaises(ValueError) as error: reference.update("my UUID", "prop", self.uuid_2) check_error_message(self, error, self.valid_uuid_error_message) with self.assertRaises(ValueError) as error: reference.update(self.uuid_1, "prop", "my uuid") check_error_message(self, error, self.valid_uuid_error_message) with self.assertRaises(ValueError) as error: reference.update(self.uuid_1, "prop", ["my uuid"]) check_error_message(self, error, self.valid_uuid_error_message) with self.assertRaises(ValueError) as error: reference.update(f"http://localhost:8080/v1/objects/{self.uuid_1}", "prop", "http://localhost:8080/v1/objects/MY_UUID") check_error_message(self, error, self.valid_uuid_error_message) with self.assertRaises(ValueError) as error: reference.update("http://localhost:8080/v1/objects/My-UUID", "prop", f"http://localhost:8080/v1/objects/{self.uuid_2}") check_error_message(self, error, self.valid_uuid_error_message) mock_obj = mock_connection_method('put', status_code=204) reference = Reference(mock_obj) with self.assertRaises(UnexpectedStatusCodeException) as error: reference.update(self.uuid_1, "myProperty", self.uuid_2) check_startswith_error_message(self, error, unexpected_error_msg) mock_obj = mock_connection_method('put', side_effect=RequestsConnectionError("Test!")) reference = Reference(mock_obj) with self.assertRaises(RequestsConnectionError) as error: reference.update(self.uuid_1, "myProperty", self.uuid_2) check_error_message(self, error, connection_error_msg) # test valid calls connection_mock = mock_connection_method('put') reference = Reference(connection_mock) reference.update( "de998e81-fa66-440e-a1de-2a2013667e77", "hasAwards", "fc041624-4ddf-4b76-8e09-a5b0b9f9f832" ) connection_mock.put.assert_called_with( path="/objects/de998e81-fa66-440e-a1de-2a2013667e77/references/hasAwards", weaviate_object=[{'beacon': 'weaviate://localhost/fc041624-4ddf-4b76-8e09-a5b0b9f9f832'}], ) reference.update( "4e44db9b-7f9c-4cf4-a3a0-b57024eefed0", "hasAwards", [ "17ee17bd-a09a-49ff-adeb-d242f25f390d", "f8c25386-707c-40c0-b7b9-26cc0e9b2bd1", "d671dc52-dce4-46e7-8731-b722f19420c8" ] ) connection_mock.put.assert_called_with( path="/objects/4e44db9b-7f9c-4cf4-a3a0-b57024eefed0/references/hasAwards", weaviate_object=[ {'beacon': 'weaviate://localhost/17ee17bd-a09a-49ff-adeb-d242f25f390d'}, {'beacon': 'weaviate://localhost/f8c25386-707c-40c0-b7b9-26cc0e9b2bd1'}, {'beacon': 'weaviate://localhost/d671dc52-dce4-46e7-8731-b722f19420c8'} ], )
def test_replace(self, mock_get_vector, mock_get_dict_from_object): """ Test the `replace` method. """ # error messages requests_error_message = 'Object was not replaced.' unexpected_error_message = "Replace object" # test exceptions mock_obj = mock_connection_method('put', side_effect=RequestsConnectionError("Test!")) data_object = DataObject(mock_obj) with self.assertRaises(RequestsConnectionError) as error: data_object.replace( {"name": "Alan Greenspan"}, "CoolestPersonEver", "27be9d8d-1da1-4d52-821f-bc7e2a25247d") check_error_message(self, error, requests_error_message) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() mock_obj = mock_connection_method('put', status_code=204, return_json={}) data_object = DataObject(mock_obj) with self.assertRaises(UnexpectedStatusCodeException) as error: data_object.replace( {"name": "Alan Greenspan"}, "CoolestPersonEver", "27be9d8d-1da1-4d52-821f-bc7e2a25247d") check_startswith_error_message(self, error, unexpected_error_message) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() # test valid calls ## without vector argument connection_mock = mock_connection_method('put') data_object = DataObject(connection_mock) data_object.replace({"A": 2}, "Hero", "27be9d8d-1da1-4d52-821f-bc7e2a25247d") weaviate_obj = { "id": "27be9d8d-1da1-4d52-821f-bc7e2a25247d", "class": "Hero", "properties": {"A": 2} } connection_mock.put.assert_called_with( path="/objects/27be9d8d-1da1-4d52-821f-bc7e2a25247d", weaviate_object=weaviate_obj ) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() ### with uncapitalized class_name connection_mock = mock_connection_method('put') data_object = DataObject(connection_mock) data_object.replace({"A": 2}, "hero", "27be9d8d-1da1-4d52-821f-bc7e2a25247d") weaviate_obj = { "id": "27be9d8d-1da1-4d52-821f-bc7e2a25247d", "class": "Hero", "properties": {"A": 2} } connection_mock.put.assert_called_with( path="/objects/27be9d8d-1da1-4d52-821f-bc7e2a25247d", weaviate_object=weaviate_obj ) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() # with vector argument connection_mock = mock_connection_method('put') data_object = DataObject(connection_mock) data_object.replace({"A": 2}, "Hero", "27be9d8d-1da1-4d52-821f-bc7e2a25247d", vector=[3.,5, 7]) weaviate_obj = { "id": "27be9d8d-1da1-4d52-821f-bc7e2a25247d", "class": "Hero", "properties": {"A": 2}, "vector": [3.,5, 7] } connection_mock.put.assert_called_with( path="/objects/27be9d8d-1da1-4d52-821f-bc7e2a25247d", weaviate_object=weaviate_obj ) mock_get_dict_from_object.assert_called() mock_get_vector.assert_called()
def test_update(self, mock_get_vector, mock_get_dict_from_object): """ Test the `update` method. """ data_object = DataObject(Mock()) # error messages class_type_error_message = "Class must be type str" uuid_type_error_message = lambda dt: f"'uuid' must be of type str or uuid.UUID, but was: {dt}" uuid_value_error_message = "Not valid 'uuid' or 'uuid' can not be extracted from value" requests_error_message = 'Object was not updated.' unexpected_error_message = "Update of the object not successful" with self.assertRaises(TypeError) as error: data_object.update({"A": "B"}, 35, "ae6d51d6-b4ea-5a03-a808-6aae990bdebf") check_error_message(self, error, class_type_error_message) mock_get_dict_from_object.assert_not_called() mock_get_vector.assert_not_called() with self.assertRaises(TypeError) as error: data_object.update({"A": "B"}, "Class", 1238234) check_error_message(self, error, uuid_type_error_message(int)) mock_get_dict_from_object.assert_not_called() mock_get_vector.assert_not_called() with self.assertRaises(ValueError) as error: data_object.update({"A": "B"}, "Class", "NOT-A-valid-uuid") check_error_message(self, error, uuid_value_error_message) mock_get_dict_from_object.assert_not_called() mock_get_vector.assert_not_called() mock_obj = mock_connection_method('patch', side_effect=RequestsConnectionError("Test!")) data_object = DataObject(mock_obj) with self.assertRaises(RequestsConnectionError) as error: data_object.update( {"name": "Alan Greenspan"}, "CoolestPersonEver", "27be9d8d-1da1-4d52-821f-bc7e2a25247d") check_error_message(self, error, requests_error_message) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() with self.assertRaises(UnexpectedStatusCodeException) as error: mock_obj = mock_connection_method('patch', status_code=200, return_json={}) data_object = DataObject(mock_obj) data_object.update( {"name": "Alan Greenspan"}, "CoolestPersonEver", "27be9d8d-1da1-4d52-821f-bc7e2a25247d") check_startswith_error_message(self, error, unexpected_error_message) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() # test valid calls ## without vector argument connection_mock = mock_connection_method('patch', status_code=204) data_object = DataObject(connection_mock) data_object.update({"A": "B"}, "Class", "ae6d51d6-b4ea-5a03-a808-6aae990bdebf") weaviate_obj = { "id": "ae6d51d6-b4ea-5a03-a808-6aae990bdebf", "class": "Class", "properties": {"A": "B"} } connection_mock.patch.assert_called_with( path="/objects/ae6d51d6-b4ea-5a03-a808-6aae990bdebf", weaviate_object=weaviate_obj ) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() ### with uncapitalized class_name connection_mock = mock_connection_method('patch', status_code=204) data_object = DataObject(connection_mock) data_object.update({"A": "B"}, "class", "ae6d51d6-b4ea-5a03-a808-6aae990bdebf") weaviate_obj = { "id": "ae6d51d6-b4ea-5a03-a808-6aae990bdebf", "class": "Class", "properties": {"A": "B"} } connection_mock.patch.assert_called_with( path="/objects/ae6d51d6-b4ea-5a03-a808-6aae990bdebf", weaviate_object=weaviate_obj ) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() ## with vector argument connection_mock = mock_connection_method('patch', status_code=204) data_object = DataObject(connection_mock) data_object.update({"A": "B"}, "Class", "ae6d51d6-b4ea-5a03-a808-6aae990bdebf", vector=[2., 4.]) weaviate_obj = { "id": "ae6d51d6-b4ea-5a03-a808-6aae990bdebf", "class": "Class", "properties": {"A": "B"}, "vector": [2., 4.] } connection_mock.patch.assert_called_with( path="/objects/ae6d51d6-b4ea-5a03-a808-6aae990bdebf", weaviate_object=weaviate_obj ) mock_get_dict_from_object.assert_called() mock_get_vector.assert_called()
def test_create(self, mock_get_vector, mock_get_valid_uuid, mock_get_dict_from_object): """ Test the `create` method. """ def reset(): """ Reset patched objects """ mock_get_valid_uuid.reset_mock() # reset called mock_get_vector.reset_mock() # reset called mock_get_dict_from_object.reset_mock() # reset_called data_object = DataObject(Mock()) # invalid calls class_name_error_message = lambda dt: f"Expected class_name of type str but was: {dt}" requests_error_message = 'Object was not added to Weaviate.' # tests with self.assertRaises(TypeError) as error: data_object.create({'name': 'Optimus Prime'}, ["Transformer"]) check_error_message(self, error, class_name_error_message(list)) mock_get_dict_from_object.assert_not_called() mock_get_vector.assert_not_called() mock_get_valid_uuid.assert_not_called() reset() mock_obj = mock_connection_method('post', side_effect=RequestsConnectionError("Test!")) data_object = DataObject(mock_obj) with self.assertRaises(RequestsConnectionError) as error: data_object.create({"name": "Alan Greenspan"}, "CoolestPersonEver") check_error_message(self, error, requests_error_message) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() mock_get_valid_uuid.assert_not_called() reset() mock_obj = mock_connection_method('post', status_code=204, return_json={}) data_object = DataObject(mock_obj) with self.assertRaises(UnexpectedStatusCodeException) as error: data_object.create({"name": "Alan Greenspan"}, "CoolestPersonEver") check_startswith_error_message(self, error, "Creating object") mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() mock_get_valid_uuid.assert_not_called() reset() mock_obj = mock_connection_method('post', status_code=204, return_json={"error" : [{"message" : "already exists"}]}) data_object = DataObject(mock_obj) with self.assertRaises(ObjectAlreadyExistsException) as error: data_object.create({"name": "Alan Greenspan"}, "CoolestPersonEver") check_error_message(self, error, "None") mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() mock_get_valid_uuid.assert_not_called() reset() mock_obj = mock_connection_method('post', status_code=204, return_json={}) data_object = DataObject(mock_obj) with self.assertRaises(UnexpectedStatusCodeException) as error: data_object.create({"name": "Alan Greenspan"}, "CoolestPersonEver") check_startswith_error_message(self, error, "Creating object") mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() mock_get_valid_uuid.assert_not_called() # # test valid calls ## without vector argument connection_mock = mock_connection_method('post', return_json={"id": 0}, status_code=200) data_object = DataObject(connection_mock) object_ = {"lyrics": "da da dadadada dada, da da dadadada da, da da dadadada da, da da dadadada da Tequila"} class_name = "KaraokeSongs" vector = [1., 2.] id_ = "ae6d51d6-b4ea-5a03-a808-6aae990bdebf" rest_object = { "class": class_name, "properties": object_, "id": id_ } reset() uuid = data_object.create(object_, class_name, id_) self.assertEqual(uuid, "0") connection_mock.post.assert_called_with( path="/objects", weaviate_object=rest_object ) mock_get_dict_from_object.assert_called() mock_get_vector.assert_not_called() mock_get_valid_uuid.assert_called() ## with vector argument connection_mock = mock_connection_method('post', return_json={"id": 0}, status_code=200) data_object = DataObject(connection_mock) object_ = {"lyrics": "da da dadadada dada, da da dadadada da, da da dadadada da, da da dadadada da Tequila"} class_name = "KaraokeSongs" vector = [1., 2.] id_ = "ae6d51d6-b4ea-5a03-a808-6aae990bdebf" rest_object = { "class": class_name, "properties": object_, "vector": vector, "id": id_ } reset() uuid = data_object.create(object_, class_name, id_, vector) self.assertEqual(uuid, "0") connection_mock.post.assert_called_with( path="/objects", weaviate_object=rest_object ) mock_get_dict_from_object.assert_called() mock_get_vector.assert_called() mock_get_valid_uuid.assert_called() reset() # uncapitalized class_names should be capitalized uuid = data_object.create(object_, "karaokeSongs", id_, vector) self.assertEqual(uuid, "0") connection_mock.post.assert_called_with( path="/objects", weaviate_object=rest_object ) mock_get_dict_from_object.assert_called() mock_get_vector.assert_called() mock_get_valid_uuid.assert_called()
def test_create_data(self): """ Test the `_create_data` method. """ ####################################################################### # test status_code == 200, timeout_retries = 0 mock_connection = mock_connection_method('post', status_code=200) batch = Batch(mock_connection) batch._create_data('references', ReferenceBatchRequest()) mock_connection.post.assert_called_with( path="/batch/references", weaviate_object=[], ) self.assertEqual(mock_connection.post.call_count, 1) ####################################################################### # timeout_retries = 2, and no exception raised mock_connection = mock_connection_method('post', status_code=200) batch = Batch(mock_connection) batch.timeout_retries = 2 batch._create_data('references', ReferenceBatchRequest()) mock_connection.post.assert_called_with( path="/batch/references", weaviate_object=[], ) self.assertEqual(mock_connection.post.call_count, 1) ####################################################################### # test errors ####################################################################### ## error messages requests_error_message = 'Batch was not added to weaviate.' read_timeout_error_message = lambda data_type: ( f"The '{data_type}' creation was cancelled because it took " "longer than the configured timeout of 100s. " "Try reducing the batch size (currently 0) to a lower value. " "Aim to on average complete batch request within less than 10s") unexpected_error_message = lambda data: f"Create {data} in batch" ####################################################################### ## test RequestsConnectionError mock_connection = mock_connection_method( 'post', side_effect=RequestsConnectionError('Test!')) batch = Batch(mock_connection) with self.assertRaises(RequestsConnectionError) as error: batch._create_data('objects', ObjectsBatchRequest()) check_error_message(self, error, requests_error_message) mock_connection.post.assert_called_with( path="/batch/objects", weaviate_object={ "fields": ["ALL"], "objects": [] }, ) ## test ReadTimeout, timeout_retries = 0 mock_connection = mock_connection_method( 'post', side_effect=ReadTimeout('Test!')) mock_connection.timeout_config = (2, 100) batch = Batch(mock_connection) with self.assertRaises(ReadTimeout) as error: batch._create_data('references', ReferenceBatchRequest()) check_startswith_error_message( self, error, read_timeout_error_message('references')) mock_connection.post.assert_called_with( path="/batch/references", weaviate_object=[], ) self.assertEqual(mock_connection.post.call_count, 1) ## test ReadTimeout, timeout_retries = 3 mock_connection = mock_connection_method( 'post', side_effect=ReadTimeout('Test!')) mock_connection.timeout_config = (2, 100) batch = Batch(mock_connection) batch.timeout_retries = 3 with self.assertRaises(ReadTimeout) as error: batch._create_data('objects', ObjectsBatchRequest()) check_startswith_error_message(self, error, read_timeout_error_message('objects')) mock_connection.post.assert_called_with( path="/batch/objects", weaviate_object={ 'fields': ['ALL'], 'objects': [] }, ) self.assertEqual(mock_connection.post.call_count, 4) ## test status_code != 200 mock_connection = mock_connection_method('post', status_code=204) batch = Batch(mock_connection) with self.assertRaises(UnexpectedStatusCodeException) as error: batch._create_data('references', ReferenceBatchRequest()) check_startswith_error_message(self, error, unexpected_error_message('references')) mock_connection.post.assert_called_with( path="/batch/references", weaviate_object=[], )
def test_add(self): """ Test the `add` method. """ reference = Reference(Mock()) # error messages unexpected_error_msg = 'Add property reference to object' connection_error_msg = 'Reference was not added.' # test exceptions with self.assertRaises(TypeError) as error: reference.add(1, "prop", self.uuid_1) check_error_message(self, error, self.uuid_error_message) with self.assertRaises(TypeError) as error: reference.add(self.uuid_1, 1, self.uuid_2) check_error_message(self, error, self.name_error_message(int)) with self.assertRaises(TypeError) as error: reference.add(self.uuid_1, "prop", 1) check_error_message(self, error, self.uuid_error_message) with self.assertRaises(ValueError) as error: reference.add("my UUID", "prop", self.uuid_2) check_error_message(self, error, self.valid_uuid_error_message) with self.assertRaises(ValueError) as error: reference.add(self.uuid_1, "prop", "my uuid") check_error_message(self, error, self.valid_uuid_error_message) with self.assertRaises(ValueError) as error: reference.add(f"http://localhost:8080/v1/objects/{self.uuid_1}", "prop", "http://localhost:8080/v1/objects/MY_UUID") check_error_message(self, error, self.valid_uuid_error_message) with self.assertRaises(ValueError) as error: reference.add("http://localhost:8080/v1/objects/My-UUID", "prop", f"http://localhost:8080/v1/objects/{self.uuid_2}") check_error_message(self, error, self.valid_uuid_error_message) mock_obj = mock_connection_method('post', status_code=204) reference = Reference(mock_obj) with self.assertRaises(UnexpectedStatusCodeException) as error: reference.add(self.uuid_1, "myProperty", self.uuid_2) check_startswith_error_message(self, error, unexpected_error_msg) mock_obj = mock_connection_method('post', side_effect=RequestsConnectionError("Test!")) reference = Reference(mock_obj) with self.assertRaises(RequestsConnectionError) as error: reference.add(self.uuid_1, "myProperty", self.uuid_2) check_error_message(self, error, connection_error_msg) # test valid calls connection_mock = mock_connection_method('post') reference = Reference(connection_mock) # 1. Plain reference.add( "3250b0b8-eaf7-499b-ac68-9084c9c82d0f", "hasItem", "99725f35-f12a-4f36-a2e2-0d41501f4e0e" ) connection_mock.post.assert_called_with( path="/objects/3250b0b8-eaf7-499b-ac68-9084c9c82d0f/references/hasItem", weaviate_object={'beacon': 'weaviate://localhost/99725f35-f12a-4f36-a2e2-0d41501f4e0e'}, ) # 2. using url reference.add( "http://localhost:8080/v1/objects/7591be77-5959-4386-9828-423fc5096e87", "hasItem", "http://localhost:8080/v1/objects/1cd80c11-29f0-453f-823c-21547b1511f0" ) connection_mock.post.assert_called_with( path="/objects/7591be77-5959-4386-9828-423fc5096e87/references/hasItem", weaviate_object={'beacon': 'weaviate://localhost/1cd80c11-29f0-453f-823c-21547b1511f0'}, ) # 3. using weaviate url reference.add( "weaviate://localhost/f8def983-87e7-4e21-bf10-e32e2de3efcf", "hasItem", "weaviate://localhost/e40aaef5-d3e5-44f1-8ec4-3eafc8475078" ) connection_mock.post.assert_called_with( path="/objects/f8def983-87e7-4e21-bf10-e32e2de3efcf/references/hasItem", weaviate_object={'beacon': 'weaviate://localhost/e40aaef5-d3e5-44f1-8ec4-3eafc8475078'}, )
def test_update_config(self, mock_schema): """ Test the `update_config` method. """ # invalid calls requests_error_message = 'Class schema configuration could not be updated.' unexpected_error_msg = 'Update class schema configuration' mock_schema.return_value = { 'class': 'Test', 'vectorIndexConfig': { 'test1': 'Test1', 'test2': 2 } } mock_conn = mock_connection_method( 'put', side_effect=RequestsConnectionError("Test!")) schema = Schema(mock_conn) with self.assertRaises(RequestsConnectionError) as error: schema.update_config("Test", {'vectorIndexConfig': { 'test2': 'Test2' }}) check_error_message(self, error, requests_error_message) mock_conn.put.assert_called_with(path="/schema/Test", weaviate_object={ 'class': 'Test', 'vectorIndexConfig': { 'test1': 'Test1', 'test2': 'Test2' } }) mock_schema.return_value = { 'class': 'Test', 'vectorIndexConfig': { 'test1': 'Test1', 'test2': 2 } } mock_conn = mock_connection_method('put', status_code=404) schema = Schema(mock_conn) with self.assertRaises(UnexpectedStatusCodeException) as error: schema.update_config("Test", {'vectorIndexConfig': { 'test3': True }}) check_startswith_error_message(self, error, unexpected_error_msg) mock_conn.put.assert_called_with(path="/schema/Test", weaviate_object={ 'class': 'Test', 'vectorIndexConfig': { 'test1': 'Test1', 'test2': 2, 'test3': True } }) # valid calls mock_schema.return_value = { 'class': 'Test', 'vectorIndexConfig': { 'test1': 'Test1', 'test2': 2 } } mock_conn = mock_connection_method('put') schema = Schema(mock_conn) schema.update_config("Test", {}) mock_conn.put.assert_called_with(path="/schema/Test", weaviate_object={ 'class': 'Test', 'vectorIndexConfig': { 'test1': 'Test1', 'test2': 2 } }) # with uncapitalized class_name mock_schema.return_value = { 'class': 'Test', 'vectorIndexConfig': { 'test1': 'Test1', 'test2': 2 } } mock_conn = mock_connection_method('put') schema = Schema(mock_conn) schema.update_config("test", {}) mock_conn.put.assert_called_with(path="/schema/Test", weaviate_object={ 'class': 'Test', 'vectorIndexConfig': { 'test1': 'Test1', 'test2': 2 } })