Skip to content

Commit

Permalink
add and change unittest for the new weaviate version (v1.0.0) for the…
Browse files Browse the repository at this point in the history
… schema module
  • Loading branch information
StefanBogdan committed Jan 11, 2021
1 parent 34a4e1b commit 746de2e
Show file tree
Hide file tree
Showing 4 changed files with 555 additions and 762 deletions.
125 changes: 44 additions & 81 deletions test/schema/schema_company.json
Original file line number Diff line number Diff line change
@@ -1,83 +1,46 @@
{
"actions": {
"classes": [],
"type": "action"
},
"things": {
"@context": "",
"version": "0.2.0",
"type": "thing",
"name": "company",
"maintainer": "[email protected]",
"classes": [
{
"class": "Company",
"description": "A business that acts in the market",
"keywords": [],
"properties": [
{
"name": "name",
"description": "The name under which the company is known",
"dataType": [
"text"
],
"cardinality": "atMostOne",
"keywords": []
},
{
"name": "legalBody",
"description": "The legal body under which the company maintains its business",
"dataType": [
"text"
],
"cardinality": "atMostOne",
"keywords": []
},
{
"name": "hasEmployee",
"description": "The employees of the company",
"dataType": [
"Employee"
],
"cardinality": "many",
"keywords": []
}
]
},
{
"class": "Employee",
"description": "An employee of the company",
"keywords": [],
"properties": [
{
"name": "name",
"description": "The name of the employee",
"dataType": [
"text"
],
"cardinality": "atMostOne",
"keywords": []
},
{
"name": "job",
"description": "the job description of the employee",
"dataType": [
"text"
],
"cardinality": "atMostOne",
"keywords": []
},
{
"name": "yearsInTheCompany",
"description": "The number of years this employee has worked in the company",
"dataType": [
"int"
],
"cardinality": "atMostOne",
"keywords": []
}
]
}
]
}
"classes": [
{
"class": "Company",
"description": "A business that acts in the market",
"properties": [
{
"name": "name",
"description": "The name under which the company is known",
"dataType": ["text"]
},
{
"name": "legalBody",
"description": "The legal body under which the company maintains its business",
"dataType": ["text"]
},
{
"name": "hasEmployee",
"description": "The employees of the company",
"dataType": ["Employee"]
}
]
},
{
"class": "Employee",
"description": "An employee of the company",
"properties": [
{
"name": "name",
"description": "The name of the employee",
"dataType": ["text"]
},
{
"name": "job",
"description": "the job description of the employee",
"dataType": ["text"]
},
{
"name": "yearsInTheCompany",
"description": "The number of years this employee has worked in the company",
"dataType": ["int"]
}
]
}
]
}
111 changes: 52 additions & 59 deletions test/schema/test_properties.py
Original file line number Diff line number Diff line change
@@ -1,107 +1,100 @@
import unittest
import weaviate
from weaviate import SEMANTIC_TYPE_ACTIONS
from weaviate.connect import REST_METHOD_POST, REST_METHOD_DELETE
from unittest.mock import Mock
from test.testing_util import replace_connection, add_run_rest_to_mock
from test.testing_util import replace_connection, add_run_rest_to_mock, Mock


class TestCRUDProperty(unittest.TestCase):

def test_create_bad_input(self):
w = weaviate.Client("http://localhorst:8080")
"""
Test create property exceptions.
"""

client = weaviate.Client("http://localhorst:8080")
test_prop = {
"dataType": ["string"],
"cardinality": "atMostOne",
"description": "my Property",
"vectorizePropertyName": True,
"moduleConfig" : {
"text2vec-contextionary": {
"vectorizePropertyName": True
}
},
# "name": "superProp", missing name
"index": True
"indexInverted": True
}
try:
w.schema.property.create("Class", test_prop)
self.fail("No error")
except weaviate.SchemaValidationException:
pass

with self.assertRaises(weaviate.SchemaValidationException):
client.schema.property.create("Class", test_prop)
test_prop["name"] = "someName"
try:
w.schema.property.create(35, test_prop)
self.fail("No error")
except TypeError:
pass
try:
w.schema.property.create("Class", ["wrong", "type"])
self.fail("No error")
except TypeError:
pass
with self.assertRaises(TypeError):
client.schema.property.create(35, test_prop)
with self.assertRaises(TypeError):
client.schema.property.create("Class", ["wrong", "type"])

def test_create(self):
w = weaviate.Client("http://localhorst:8080")
"""
Test create.
"""

client = weaviate.Client("http://localhorst:8080")

connection_mock = Mock() # Mock calling weaviate
add_run_rest_to_mock(connection_mock)
replace_connection(w, connection_mock)
replace_connection(client, connection_mock)

test_prop = {
"dataType": ["string"],
"cardinality": "atMostOne",
"description": "my Property",
"vectorizePropertyName": True,
"moduleConfig" : {
"text2vec-contextionary": {
"vectorizePropertyName": True
}
},
"name": "superProp",
"index": True
"indexInverted": True
}

w.schema.property.create("TestThing", test_prop)
w.schema.property.create("TestAction", test_prop, SEMANTIC_TYPE_ACTIONS)
client.schema.property.create("TestThing", test_prop)

connection_mock.run_rest.assert_called()

call_args_list = connection_mock.run_rest.call_args_list
call_args, call_kwargs = call_args_list[0]
call_args = call_args_list[0][0]

self.assertEqual("/schema/things/TestThing/properties", call_args[0])
self.assertEqual("/schema/TestThing/properties", call_args[0])
self.assertEqual(REST_METHOD_POST, call_args[1])
self.assertEqual(test_prop, call_args[2])

call_args, call_kwargs = call_args_list[1]
def test_delete_bad_input(self):
"""
Test create with bad input.
"""

self.assertEqual("/schema/actions/TestAction/properties", call_args[0])
self.assertEqual(REST_METHOD_POST, call_args[1])
self.assertEqual(test_prop, call_args[2])
client = weaviate.Client("http://localhorst:8080")

def test_delete_bad_input(self):
w = weaviate.Client("http://localhorst:8080")
try:
w.schema.property._delete("Class", 4)
self.fail("No error")
except TypeError:
pass
try:
w.schema.property._delete(35, "prop")
self.fail("No error")
except TypeError:
pass
with self.assertRaises(TypeError):
client.schema.property._delete("Class", 4)
with self.assertRaises(TypeError):
client.schema.property._delete(35, "prop")

def test_delete(self):
w = weaviate.Client("http://localhorst:8080")
"""
Test delete property. (currently not available)
"""

client = weaviate.Client("http://localhorst:8080")

connection_mock = Mock() # Mock calling weaviate
add_run_rest_to_mock(connection_mock)
replace_connection(w, connection_mock)
replace_connection(client, connection_mock)

w.schema.property._delete("ThingClass", "propUno")
w.schema.property._delete("ActionClass", "propDos", SEMANTIC_TYPE_ACTIONS)
client.schema.property._delete("ThingClass", "propUno")

connection_mock.run_rest.assert_called()

call_args_list = connection_mock.run_rest.call_args_list
call_args, call_kwargs = call_args_list[0]

self.assertEqual("/schema/things/ThingClass/properties/propUno", call_args[0])
self.assertEqual(REST_METHOD_DELETE, call_args[1])

call_args, call_kwargs = call_args_list[1]
call_args = call_args_list[0][0]

self.assertEqual("/schema/actions/ActionClass/properties/propDos", call_args[0])
self.assertEqual("/schema/ThingClass/properties/propUno", call_args[0])
self.assertEqual(REST_METHOD_DELETE, call_args[1])

Loading

0 comments on commit 746de2e

Please sign in to comment.