Skip to content

Commit 7c94b9c

Browse files
committed
test assertion refactor
1 parent e2ff4d3 commit 7c94b9c

File tree

1 file changed

+41
-46
lines changed

1 file changed

+41
-46
lines changed

posthog/api/test/test_event_definition.py

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,16 @@ def setUpTestData(cls):
5252

5353
def test_list_event_definitions(self):
5454
response = self.client.get("/api/projects/@current/event_definitions/")
55-
self.assertEqual(response.status_code, status.HTTP_200_OK)
56-
self.assertEqual(response.json()["count"], len(self.EXPECTED_EVENT_DEFINITIONS))
57-
self.assertEqual(len(response.json()["results"]), len(self.EXPECTED_EVENT_DEFINITIONS))
55+
assert response.status_code == status.HTTP_200_OK
56+
assert response.json()["count"] == len(self.EXPECTED_EVENT_DEFINITIONS)
57+
assert len(response.json()["results"]) == len(self.EXPECTED_EVENT_DEFINITIONS)
5858

5959
for item in self.EXPECTED_EVENT_DEFINITIONS:
6060
response_item: dict[str, Any] = next(
6161
(_i for _i in response.json()["results"] if _i["name"] == item["name"]),
6262
{},
6363
)
64-
self.assertAlmostEqual(
65-
(dateutil.parser.isoparse(response_item["created_at"]) - timezone.now()).total_seconds(),
66-
0,
67-
)
64+
assert abs((dateutil.parser.isoparse(response_item["created_at"]) - timezone.now()).total_seconds()) < 1
6865

6966
@parameterized.expand(
7067
[
@@ -116,15 +113,15 @@ def test_list_event_definitions(self):
116113
)
117114
def test_list_event_definitions_ordering(self, query_params, expected_results):
118115
response = self.client.get(f"/api/projects/@current/event_definitions/?{query_params}")
119-
self.assertEqual(response.status_code, status.HTTP_200_OK)
116+
assert response.status_code == status.HTTP_200_OK
120117
assert [(r["name"], r["last_seen_at"]) for r in response.json()["results"]] == expected_results
121118

122119
@patch("posthoganalytics.capture")
123120
def test_delete_event_definition(self, mock_capture):
124121
event_definition: EventDefinition = EventDefinition.objects.create(team=self.demo_team, name="test_event")
125122
response = self.client.delete(f"/api/projects/@current/event_definitions/{event_definition.id}/")
126-
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
127-
self.assertEqual(EventDefinition.objects.filter(id=event_definition.id).count(), 0)
123+
assert response.status_code == status.HTTP_204_NO_CONTENT
124+
assert EventDefinition.objects.filter(id=event_definition.id).count() == 0
128125
mock_capture.assert_called_once_with(
129126
self.user.distinct_id,
130127
"event definition deleted",
@@ -149,11 +146,11 @@ def test_pagination_of_event_definitions(self):
149146
)
150147

151148
response = self.client.get("/api/projects/@current/event_definitions/")
152-
self.assertEqual(response.status_code, status.HTTP_200_OK)
153-
self.assertEqual(response.json()["count"], 306)
154-
self.assertEqual(len(response.json()["results"]), 100) # Default page size
155-
self.assertEqual(response.json()["results"][0]["name"], "$pageview")
156-
self.assertEqual(response.json()["results"][1]["name"], "entered_free_trial")
149+
assert response.status_code == status.HTTP_200_OK
150+
assert response.json()["count"] == 306
151+
assert len(response.json()["results"]) == 100 # Default page size
152+
assert response.json()["results"][0]["name"] == "$pageview"
153+
assert response.json()["results"][1]["name"] == "entered_free_trial"
157154

158155
event_checkpoints = [
159156
184,
@@ -164,13 +161,11 @@ def test_pagination_of_event_definitions(self):
164161

165162
for i in range(0, 3):
166163
response = self.client.get(response.json()["next"])
167-
self.assertEqual(response.status_code, status.HTTP_200_OK)
164+
assert response.status_code == status.HTTP_200_OK
168165

169-
self.assertEqual(response.json()["count"], 306)
170-
self.assertEqual(
171-
len(response.json()["results"]), 100 if i < 2 else 6
172-
) # Each page has 100 except the last one
173-
self.assertEqual(response.json()["results"][0]["name"], f"z_event_{event_checkpoints[i]}")
166+
assert response.json()["count"] == 306
167+
assert len(response.json()["results"]) == (100 if i < 2 else 6) # Each page has 100 except the last one
168+
assert response.json()["results"][0]["name"] == f"z_event_{event_checkpoints[i]}"
174169

175170
def test_cant_see_event_definitions_for_another_team(self):
176171
org = Organization.objects.create(name="Separate Org")
@@ -179,65 +174,65 @@ def test_cant_see_event_definitions_for_another_team(self):
179174
EventDefinition.objects.create(team=team, name="should_be_invisible")
180175

181176
response = self.client.get("/api/projects/@current/event_definitions/")
182-
self.assertEqual(response.status_code, status.HTTP_200_OK)
177+
assert response.status_code == status.HTTP_200_OK
183178
for item in response.json()["results"]:
184-
self.assertNotIn("should_be_invisible", item["name"])
179+
assert "should_be_invisible" not in item["name"]
185180

186181
# Also can't fetch for a team to which the user doesn't have permissions
187182
response = self.client.get(f"/api/projects/{team.pk}/event_definitions/")
188-
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
189-
self.assertEqual(response.json(), self.permission_denied_response("You don't have access to the project."))
183+
assert response.status_code == status.HTTP_403_FORBIDDEN
184+
assert response.json() == self.permission_denied_response("You don't have access to the project.")
190185

191186
def test_query_event_definitions(self):
192187
# Regular search
193188
response = self.client.get("/api/projects/@current/event_definitions/?search=app")
194-
self.assertEqual(response.status_code, status.HTTP_200_OK)
195-
self.assertEqual(response.json()["count"], 2) # rated app, installed app
189+
assert response.status_code == status.HTTP_200_OK
190+
assert response.json()["count"] == 2 # rated app, installed app
196191

197192
# Search should be case insensitive
198193
response = self.client.get("/api/projects/@current/event_definitions/?search=App")
199-
self.assertEqual(response.status_code, status.HTTP_200_OK)
200-
self.assertEqual(response.json()["count"], 2) # rated app, installed app
194+
assert response.status_code == status.HTTP_200_OK
195+
assert response.json()["count"] == 2 # rated app, installed app
201196

202197
# Fuzzy search 1
203198
response = self.client.get("/api/projects/@current/event_definitions/?search=free tri")
204-
self.assertEqual(response.status_code, status.HTTP_200_OK)
205-
self.assertEqual(response.json()["count"], 1)
199+
assert response.status_code == status.HTTP_200_OK
200+
assert response.json()["count"] == 1
206201
for item in response.json()["results"]:
207-
self.assertIn(item["name"], ["entered_free_trial"])
202+
assert item["name"] in ["entered_free_trial"]
208203

209204
# Handles URL encoding properly
210205
response = self.client.get("/api/projects/@current/event_definitions/?search=free%20tri%20")
211-
self.assertEqual(response.status_code, status.HTTP_200_OK)
212-
self.assertEqual(response.json()["count"], 1)
206+
assert response.status_code == status.HTTP_200_OK
207+
assert response.json()["count"] == 1
213208
for item in response.json()["results"]:
214-
self.assertIn(item["name"], ["entered_free_trial"])
209+
assert item["name"] in ["entered_free_trial"]
215210

216211
# Fuzzy search 2
217212
response = self.client.get("/api/projects/@current/event_definitions/?search=ed mov")
218-
self.assertEqual(response.status_code, status.HTTP_200_OK)
219-
self.assertEqual(response.json()["count"], 1)
213+
assert response.status_code == status.HTTP_200_OK
214+
assert response.json()["count"] == 1
220215
for item in response.json()["results"]:
221-
self.assertIn(item["name"], ["watched_movie"])
216+
assert item["name"] in ["watched_movie"]
222217

223218
def test_event_type_event(self):
224219
action = Action.objects.create(team=self.demo_team, name="action1_app")
225220

226221
response = self.client.get("/api/projects/@current/event_definitions/?search=app&event_type=event")
227-
self.assertEqual(response.status_code, status.HTTP_200_OK)
228-
self.assertEqual(response.json()["count"], 2)
229-
self.assertNotEqual(response.json()["results"][0]["name"], action.name)
222+
assert response.status_code == status.HTTP_200_OK
223+
assert response.json()["count"] == 2
224+
assert response.json()["results"][0]["name"] != action.name
230225

231226
def test_event_type_event_custom(self):
232227
response = self.client.get("/api/projects/@current/event_definitions/?event_type=event_custom")
233-
self.assertEqual(response.status_code, status.HTTP_200_OK)
234-
self.assertEqual(response.json()["count"], 5)
228+
assert response.status_code == status.HTTP_200_OK
229+
assert response.json()["count"] == 5
235230

236231
def test_event_type_event_posthog(self):
237232
response = self.client.get("/api/projects/@current/event_definitions/?event_type=event_posthog")
238-
self.assertEqual(response.status_code, status.HTTP_200_OK)
239-
self.assertEqual(response.json()["count"], 1)
240-
self.assertEqual(response.json()["results"][0]["name"], "$pageview")
233+
assert response.status_code == status.HTTP_200_OK
234+
assert response.json()["count"] == 1
235+
assert response.json()["results"][0]["name"] == "$pageview"
241236

242237

243238
@dataclasses.dataclass

0 commit comments

Comments
 (0)