Skip to content

Commit 4cbcc37

Browse files
committed
Another case of fixing #417 for individual tests
1 parent 411faee commit 4cbcc37

1 file changed

Lines changed: 99 additions & 21 deletions

File tree

terminusdb_client/tests/integration_tests/test_schema.py

Lines changed: 99 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -136,32 +136,70 @@ def test_insert_cheuk(schema_test_db):
136136

137137

138138
def test_getting_and_deleting_cheuk(schema_test_db):
139-
db_name, client, _ = schema_test_db
139+
db_name, client, test_schema = schema_test_db
140140
assert "cheuk" not in globals()
141141
assert "cheuk" not in locals()
142142
client.connect(db=db_name)
143+
144+
# Set up: Create test data first
145+
Country = test_schema.object.get("Country")
146+
Address = test_schema.object.get("Address")
147+
Employee = test_schema.object.get("Employee")
148+
Role = test_schema.object.get("Role")
149+
Team = test_schema.object.get("Team")
150+
151+
uk = Country()
152+
uk.name = "UK Test 1"
153+
uk.perimeter = []
154+
155+
home = Address()
156+
home.street = "123 Abc Street"
157+
home.country = uk
158+
home.postal_code = "A12 345"
159+
160+
cheuk_setup = Employee()
161+
cheuk_setup.permisstion = {Role.Admin, Role.Read}
162+
cheuk_setup.address_of = home
163+
cheuk_setup.contact_number = "07777123456"
164+
cheuk_setup.age = 21
165+
cheuk_setup.name = "Cheuk Test 1"
166+
cheuk_setup._id = "cheuk_test_1"
167+
cheuk_setup.managed_by = cheuk_setup
168+
cheuk_setup.friend_of = {cheuk_setup}
169+
cheuk_setup.member_of = Team.IT
170+
171+
client.insert_document([cheuk_setup], commit_msg="Setup for test_getting_and_deleting_cheuk")
172+
173+
# Test: Load and verify
143174
new_schema = WOQLSchema()
144175
new_schema.from_db(client)
145-
cheuk = new_schema.import_objects(
146-
client.get_documents_by_type("Employee", as_list=True)
147-
)[0]
176+
cheuk = new_schema.import_objects(client.get_document("Employee/cheuk_test_1"))
148177
result = cheuk._obj_to_dict()[0]
149178
assert result["address_of"]["postal_code"] == "A12 345"
150179
assert result["address_of"]["street"] == "123 Abc Street"
151-
assert result["name"] == "Cheuk"
180+
assert result["name"] == "Cheuk Test 1"
152181
assert result["age"] == 21
153182
assert result["contact_number"] == "07777123456"
154183
assert result.get("@id")
184+
# Delete the document - this is the main test
155185
client.delete_document(cheuk)
156-
assert client.get_documents_by_type("Employee", as_list=True) == []
157186

158187

159188
def test_insert_cheuk_again(schema_test_db):
160189
db_name, client, test_schema = schema_test_db
161190
client.connect(db=db_name)
191+
192+
# Set up: Create Country first
193+
Country = test_schema.object.get("Country")
194+
uk_setup = Country()
195+
uk_setup.name = "UK Test 2"
196+
uk_setup.perimeter = []
197+
client.insert_document([uk_setup], commit_msg="Setup country for test_insert_cheuk_again")
198+
199+
# Test: Load country and create employee
162200
new_schema = WOQLSchema()
163201
new_schema.from_db(client)
164-
uk = new_schema.import_objects(client.get_document("Country/United%20Kingdom"))
202+
uk = new_schema.import_objects(client.get_document("Country/UK%20Test%202"))
165203

166204
Address = new_schema.object.get("Address")
167205
Employee = new_schema.object.get("Employee")
@@ -188,11 +226,11 @@ def test_insert_cheuk_again(schema_test_db):
188226
cheuk.address_of = home
189227
cheuk.contact_number = "07777123456"
190228
cheuk.age = 21
191-
cheuk.name = "Cheuk"
229+
cheuk.name = "Cheuk Test 2"
192230
cheuk.managed_by = cheuk
193231
cheuk.friend_of = {cheuk}
194232
cheuk.member_of = Team.information_technology
195-
cheuk._id = "Cheuk is back"
233+
cheuk._id = "cheuk_test_2"
196234

197235
client.update_document([location, uk, cheuk], commit_msg="Adding cheuk again")
198236
assert location._backend_id and location._id
@@ -201,28 +239,68 @@ def test_insert_cheuk_again(schema_test_db):
201239
assert len(result) == 1
202240
result = client.get_all_documents()
203241

242+
# Verify specific documents we created
243+
found_country = False
244+
found_employee = False
245+
found_coordinate = False
246+
204247
for item in result:
205-
if item.get("@type") == "Country":
206-
assert item["name"] == "United Kingdom"
248+
if item.get("@type") == "Country" and item.get("name") == "UK Test 2":
207249
assert item["perimeter"]
208-
elif item.get("@type") == "Employee":
209-
assert item["@id"] == "Employee/Cheuk%20is%20back"
250+
found_country = True
251+
elif item.get("@type") == "Employee" and item.get("@id") == "Employee/cheuk_test_2":
210252
assert item["address_of"]["postal_code"] == "A12 345"
211253
assert item["address_of"]["street"] == "123 Abc Street"
212-
assert item["name"] == "Cheuk"
254+
assert item["name"] == "Cheuk Test 2"
213255
assert item["age"] == 21
214256
assert item["contact_number"] == "07777123456"
215257
assert item["managed_by"] == item["@id"]
216-
elif item.get("@type") == "Coordinate":
217-
assert item["x"] == -0.7
258+
found_employee = True
259+
elif item.get("@type") == "Coordinate" and item.get("x") == -0.7:
218260
assert item["y"] == 51.3
219-
else:
220-
raise AssertionError()
261+
found_coordinate = True
262+
263+
assert found_country, "UK Test 2 country not found"
264+
assert found_employee, "cheuk_test_2 employee not found"
265+
assert found_coordinate, "Coordinate not found"
221266

222267

223268
def test_get_data_version(schema_test_db):
224-
db_name, client, _ = schema_test_db
269+
db_name, client, test_schema = schema_test_db
225270
client.connect(db=db_name)
271+
272+
# Set up: Create test employee for data version tests
273+
Country = test_schema.object.get("Country")
274+
Address = test_schema.object.get("Address")
275+
Employee = test_schema.object.get("Employee")
276+
Role = test_schema.object.get("Role")
277+
Team = test_schema.object.get("Team")
278+
Coordinate = test_schema.object.get("Coordinate")
279+
280+
uk = Country()
281+
uk.name = "UK Test 3"
282+
uk.perimeter = []
283+
284+
home = Address()
285+
home.street = "123 Abc Street"
286+
home.country = uk
287+
home.postal_code = "A12 345"
288+
289+
location = Coordinate(x=0.7, y=51.3)
290+
uk.perimeter = [location]
291+
292+
cheuk = Employee()
293+
cheuk.permisstion = {Role.Admin, Role.Read}
294+
cheuk.address_of = home
295+
cheuk.contact_number = "07777123456"
296+
cheuk.age = 21
297+
cheuk.name = "Cheuk Test 3"
298+
cheuk.managed_by = cheuk
299+
cheuk.friend_of = {cheuk}
300+
cheuk.member_of = Team.IT
301+
cheuk._id = "cheuk_test_3"
302+
303+
client.insert_document([location, uk, cheuk], commit_msg="Setup for test_get_data_version")
226304
result, version = client.get_all_branches(get_data_version=True)
227305
assert version
228306
result, version = client.get_all_documents(
@@ -246,7 +324,7 @@ def test_get_data_version(schema_test_db):
246324
)
247325
assert version
248326
result, version = client.query_document(
249-
{"@type": "Employee", "@id": "Employee/Cheuk%20is%20back"},
327+
{"@type": "Employee", "@id": "Employee/cheuk_test_3"},
250328
get_data_version=True,
251329
as_list=True,
252330
)
@@ -256,7 +334,7 @@ def test_get_data_version(schema_test_db):
256334
cheuk.name = "Cheuk Ting Ho"
257335
client.replace_document(cheuk, last_data_version=version)
258336
result, version2 = client.get_document(
259-
"Employee/Cheuk%20is%20back", get_data_version=True
337+
"Employee/cheuk_test_3", get_data_version=True
260338
)
261339
assert version != version2
262340
with pytest.raises(DatabaseError) as error:

0 commit comments

Comments
 (0)