Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit a2b0d1b

Browse files
author
peritz
committed
Add cleanup code to CRUD tests
1 parent ebc0102 commit a2b0d1b

File tree

1 file changed

+94
-81
lines changed

1 file changed

+94
-81
lines changed

tests/02-integration/entities/test_entity_crud.py

Lines changed: 94 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -15,69 +15,77 @@ def entity_class(entity):
1515
def test_entity_create(entity_class):
1616
class_data = entity_class.data()
1717
test_indicator = entity_class.own_class().create(**class_data)
18-
assert test_indicator is not None, "Response is NoneType"
19-
assert "id" in test_indicator, "No ID on object"
20-
21-
entity_class.base_class().delete(id=test_indicator["id"])
18+
try:
19+
assert test_indicator is not None, "Response is NoneType"
20+
assert "id" in test_indicator, "No ID on object"
21+
finally:
22+
entity_class.base_class().delete(id=test_indicator["id"])
2223

2324

2425
def test_read(entity_class):
2526
class_data = entity_class.data()
2627
test_indicator = entity_class.own_class().create(**class_data)
27-
assert test_indicator is not None, "Response is NoneType"
28-
assert "id" in test_indicator, "No ID on object"
29-
assert "standard_id" in test_indicator, "No standard_id (STIX ID) on object"
30-
test_indicator = entity_class.own_class().read(id=test_indicator["id"])
31-
compare_values(
32-
class_data,
33-
test_indicator,
34-
entity_class.get_compare_exception_keys(),
35-
)
28+
try:
29+
assert test_indicator is not None, "Response is NoneType"
30+
assert "id" in test_indicator, "No ID on object"
31+
assert "standard_id" in test_indicator, "No standard_id (STIX ID) on object"
32+
test_indicator = entity_class.own_class().read(id=test_indicator["id"])
33+
compare_values(
34+
class_data,
35+
test_indicator,
36+
entity_class.get_compare_exception_keys(),
37+
)
3638

37-
entity_class.base_class().delete(id=test_indicator["id"])
39+
finally:
40+
entity_class.base_class().delete(id=test_indicator["id"])
3841

3942

4043
def test_update(entity_class):
4144
class_data = entity_class.data()
4245
test_indicator = entity_class.own_class().create(**class_data)
43-
assert test_indicator is not None, "Response is NoneType"
44-
assert "id" in test_indicator, "No ID on object"
45-
46-
if len(entity_class.update_data()) > 0:
47-
function_present = getattr(
48-
entity_class.own_class(), "update_field", None)
49-
if function_present:
50-
for update_field, update_value in entity_class.update_data().items():
51-
class_data[update_field] = update_value
52-
input = [{"key": update_field, "value": update_value}]
53-
result = entity_class.own_class().update_field(
54-
id=test_indicator["id"], input=input
55-
)
46+
try:
47+
assert test_indicator is not None, "Response is NoneType"
48+
assert "id" in test_indicator, "No ID on object"
49+
50+
if len(entity_class.update_data()) > 0:
51+
function_present = getattr(
52+
entity_class.own_class(), "update_field", None)
53+
if function_present:
54+
for update_field, update_value in entity_class.update_data().items():
55+
class_data[update_field] = update_value
56+
input = [{"key": update_field, "value": update_value}]
57+
result = entity_class.own_class().update_field(
58+
id=test_indicator["id"], input=input
59+
)
60+
else:
61+
for update_field, update_value in entity_class.update_data().items():
62+
class_data[update_field] = update_value
63+
class_data["update"] = True
64+
result = entity_class.own_class().create(**class_data)
65+
66+
result = entity_class.own_class().read(id=result["id"])
67+
assert result["id"] == test_indicator["id"], "Updated SDO does not match old ID"
68+
compare_values(class_data, result,
69+
entity_class.get_compare_exception_keys())
5670
else:
57-
for update_field, update_value in entity_class.update_data().items():
58-
class_data[update_field] = update_value
59-
class_data["update"] = True
60-
result = entity_class.own_class().create(**class_data)
61-
62-
result = entity_class.own_class().read(id=result["id"])
63-
assert result["id"] == test_indicator["id"], "Updated SDO does not match old ID"
64-
compare_values(class_data, result,
65-
entity_class.get_compare_exception_keys())
66-
else:
67-
result = test_indicator
71+
result = test_indicator
6872

69-
entity_class.base_class().delete(id=result["id"])
73+
finally:
74+
entity_class.base_class().delete(id=result["id"])
7075

7176

7277
def test_delete(entity_class):
7378
class_data = entity_class.data()
7479
test_indicator = entity_class.own_class().create(**class_data)
75-
assert test_indicator is not None, "Response is NoneType"
76-
assert "id" in test_indicator, "No ID on object"
77-
result = entity_class.base_class().delete(id=test_indicator["id"])
78-
assert result is None, f"Delete returned value '{result}'"
79-
result = entity_class.own_class().read(id=test_indicator["id"])
80-
assert result is None, f"Read returned value '{result}' after delete"
80+
try:
81+
assert test_indicator is not None, "Response is NoneType"
82+
assert "id" in test_indicator, "No ID on object"
83+
result = entity_class.base_class().delete(id=test_indicator["id"])
84+
assert result is None, f"Delete returned value '{result}'"
85+
result = entity_class.own_class().read(id=test_indicator["id"])
86+
assert result is None, f"Read returned value '{result}' after delete"
87+
except AssertionError:
88+
entity_class.base_class().delete(id=test_indicator["id"])
8189

8290

8391
def test_filter(entity_class):
@@ -86,16 +94,18 @@ def test_filter(entity_class):
8694

8795
class_data = entity_class.data()
8896
test_indicator = entity_class.own_class().create(**class_data)
89-
assert test_indicator is not None, "Response is NoneType"
90-
assert "id" in test_indicator, "No ID on object"
91-
test_indicator = entity_class.own_class().read(filters=entity_class.get_filter())
92-
compare_values(
93-
class_data,
94-
test_indicator,
95-
entity_class.get_compare_exception_keys(),
96-
)
97-
98-
entity_class.base_class().delete(id=test_indicator["id"])
97+
try:
98+
assert test_indicator is not None, "Response is NoneType"
99+
assert "id" in test_indicator, "No ID on object"
100+
test_indicator = entity_class.own_class().read(
101+
filters=entity_class.get_filter())
102+
compare_values(
103+
class_data,
104+
test_indicator,
105+
entity_class.get_compare_exception_keys(),
106+
)
107+
finally:
108+
entity_class.base_class().delete(id=test_indicator["id"])
99109

100110

101111
def test_search(entity_class):
@@ -104,16 +114,17 @@ def test_search(entity_class):
104114

105115
class_data = entity_class.data()
106116
test_indicator = entity_class.own_class().create(**class_data)
107-
assert test_indicator is not None, "Response is NoneType"
108-
assert "id" in test_indicator, "No ID on object"
109-
test_indicator = entity_class.own_class().read(search=entity_class.get_search())
110-
compare_values(
111-
class_data,
112-
test_indicator,
113-
entity_class.get_compare_exception_keys(),
114-
)
115-
116-
entity_class.base_class().delete(id=test_indicator["id"])
117+
try:
118+
assert test_indicator is not None, "Response is NoneType"
119+
assert "id" in test_indicator, "No ID on object"
120+
test_indicator = entity_class.own_class().read(search=entity_class.get_search())
121+
compare_values(
122+
class_data,
123+
test_indicator,
124+
entity_class.get_compare_exception_keys(),
125+
)
126+
finally:
127+
entity_class.base_class().delete(id=test_indicator["id"])
117128

118129

119130
def test_relation(entity_class):
@@ -123,19 +134,21 @@ def test_relation(entity_class):
123134
class_data2 = entity_class.relation_test()
124135
test_indicator = entity_class.own_class().create(**class_data)
125136
test_indicator2 = entity_class.own_class().create(**class_data2)
126-
assert test_indicator is not None, "Response is NoneType"
127-
assert "id" in test_indicator, "No ID on object"
128-
entity_class.own_class().add_stix_object_or_stix_relationship(
129-
id=test_indicator["id"],
130-
stixObjectOrStixRelationshipId=test_indicator2["id"],
131-
)
132-
result = entity_class.own_class().read(id=test_indicator["id"])
133-
assert result["objectsIds"][0] == test_indicator2["id"]
134-
entity_class.own_class().remove_stix_object_or_stix_relationship(
135-
id=test_indicator["id"],
136-
stixObjectOrStixRelationshipId=test_indicator2["id"],
137-
)
138-
result = entity_class.own_class().read(id=test_indicator["id"])
139-
assert len(result["objectsIds"]) == 0
140-
entity_class.base_class().delete(id=test_indicator["id"])
141-
entity_class.base_class().delete(id=test_indicator2["id"])
137+
try:
138+
assert test_indicator is not None, "Response is NoneType"
139+
assert "id" in test_indicator, "No ID on object"
140+
entity_class.own_class().add_stix_object_or_stix_relationship(
141+
id=test_indicator["id"],
142+
stixObjectOrStixRelationshipId=test_indicator2["id"],
143+
)
144+
result = entity_class.own_class().read(id=test_indicator["id"])
145+
assert result["objectsIds"][0] == test_indicator2["id"]
146+
entity_class.own_class().remove_stix_object_or_stix_relationship(
147+
id=test_indicator["id"],
148+
stixObjectOrStixRelationshipId=test_indicator2["id"],
149+
)
150+
result = entity_class.own_class().read(id=test_indicator["id"])
151+
assert len(result["objectsIds"]) == 0
152+
finally:
153+
entity_class.base_class().delete(id=test_indicator["id"])
154+
entity_class.base_class().delete(id=test_indicator2["id"])

0 commit comments

Comments
 (0)