Skip to content

Commit 8116f49

Browse files
test: add unit tests for HardwareDetailsCommitHistoryView
1 parent cb5588d commit 8116f49

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import json
2+
from unittest.mock import patch
3+
from django.test import SimpleTestCase
4+
from kernelCI_app.constants.localization import ClientStrings
5+
from kernelCI_app.views.hardwareDetailsCommitHistoryView import (
6+
HardwareDetailsCommitHistoryView,
7+
)
8+
from rest_framework.test import APIRequestFactory
9+
from datetime import datetime, timezone
10+
11+
12+
class TestHardwareDetailsCommitHistoryView(SimpleTestCase):
13+
def setUp(self):
14+
self.factory = APIRequestFactory()
15+
self.view = HardwareDetailsCommitHistoryView()
16+
self.url = "/hardware/1/commit-history"
17+
18+
@patch(
19+
"kernelCI_app.views.hardwareDetailsCommitHistoryView.get_hardware_commit_history"
20+
)
21+
def test_get_hardware_details_commit_history_success(
22+
self, mock_get_hardware_commit_history
23+
):
24+
mock_get_hardware_commit_history.return_value = [
25+
("tree1", "repo1", "branch1", "commit1", "tag1", "name1", "hash1", "time1"),
26+
]
27+
28+
request = self.factory.post(
29+
self.url,
30+
data=json.dumps(
31+
{
32+
"origin": "maestro",
33+
"startTimestampInSeconds": 1737487800,
34+
"endTimestampInSeconds": 1737574200,
35+
"commitHeads": [],
36+
}
37+
),
38+
content_type="application/json",
39+
)
40+
response = self.view.post(request, hardware_id="1")
41+
42+
self.assertEqual(response.status_code, 200)
43+
self.assertEqual(response.data, {"commit_history_table": {}})
44+
45+
def test_get_hardware_details_commit_history_ok_with_error(self):
46+
request = self.factory.post(
47+
self.url,
48+
data=json.dumps(
49+
{
50+
"origin": "maestro",
51+
"startTimestampInSeconds": 1737487800,
52+
"endTimestampInSeconds": 1737574200,
53+
"commitHeads": [],
54+
}
55+
),
56+
content_type="application/json",
57+
)
58+
response = self.view.post(request, hardware_id="1")
59+
60+
self.assertEqual(response.status_code, 200)
61+
self.assertEqual(
62+
response.data, {"error": ClientStrings.HARDWARE_COMMIT_HISTORY_NOT_FOUND}
63+
)
64+
65+
def test_get_hardware_details_commit_history_invalid_json_body(self):
66+
request = self.factory.post(
67+
self.url,
68+
data="invalid json",
69+
content_type="application/json",
70+
)
71+
response = self.view.post(request, hardware_id="1")
72+
73+
self.assertEqual(response.status_code, 400)
74+
self.assertEqual(response.data, {"error": ClientStrings.INVALID_JSON_BODY})
75+
76+
def test_get_hardware_details_commit_history_invalid_timestamp(self):
77+
request = self.factory.post(
78+
self.url,
79+
data=json.dumps(
80+
{
81+
"origin": "maestro",
82+
"startTimestampInSeconds": "invalid",
83+
"endTimestampInSeconds": "invalid",
84+
"commitHeads": [],
85+
}
86+
),
87+
content_type="application/json",
88+
)
89+
response = self.view.post(request, hardware_id="1")
90+
91+
self.assertEqual(response.status_code, 400)
92+
self.assertEqual(response.data, {"error": ClientStrings.INVALID_TIMESTAMP})
93+
94+
@patch(
95+
"kernelCI_app.views.hardwareDetailsCommitHistoryView.get_hardware_commit_history"
96+
)
97+
def test_get_hardware_details_commit_history_returns_table_when_rows_valid(
98+
self, mock_get_hardware_commit_history
99+
):
100+
start_time = datetime(2025, 1, 1, tzinfo=timezone.utc)
101+
mock_get_hardware_commit_history.return_value = [
102+
(
103+
"tree1",
104+
"repo1",
105+
"branch1",
106+
["tag1"],
107+
"name1",
108+
"hash1",
109+
start_time,
110+
),
111+
]
112+
113+
request = self.factory.post(
114+
self.url,
115+
data=json.dumps(
116+
{
117+
"origin": "maestro",
118+
"startTimestampInSeconds": 1737487800,
119+
"endTimestampInSeconds": 1737574200,
120+
"commitHeads": [],
121+
}
122+
),
123+
content_type="application/json",
124+
)
125+
response = self.view.post(request, hardware_id="1")
126+
127+
self.assertEqual(response.status_code, 200)
128+
self.assertIn("commit_history_table", response.data)
129+
self.assertIn("tree1-repo1-branch1", response.data["commit_history_table"])
130+
self.assertEqual(
131+
response.data["commit_history_table"]["tree1-repo1-branch1"][0][
132+
"git_commit_hash"
133+
],
134+
"hash1",
135+
)
136+
self.assertEqual(
137+
response.data["commit_history_table"]["tree1-repo1-branch1"][0][
138+
"start_time"
139+
],
140+
start_time,
141+
)
142+
143+
def test_get_hardware_details_commit_history_invalid_body_schema(self):
144+
request = self.factory.post(
145+
self.url,
146+
data=json.dumps(
147+
{
148+
"origin": "maestro",
149+
"startTimestampInSeconds": 1737487800,
150+
"endTimestampInSeconds": 1737574200,
151+
}
152+
),
153+
content_type="application/json",
154+
)
155+
response = self.view.post(request, hardware_id="1")
156+
157+
self.assertEqual(response.status_code, 400)
158+
self.assertIsInstance(response.data, str)
159+
self.assertIn("commitHeads", response.data)
160+
161+
@patch.object(HardwareDetailsCommitHistoryView, "_sanitize_checkouts")
162+
@patch(
163+
"kernelCI_app.views.hardwareDetailsCommitHistoryView.get_hardware_commit_history"
164+
)
165+
def test_get_hardware_details_commit_history_response_validation_error(
166+
self,
167+
mock_get_hardware_commit_history,
168+
mock_sanitize_checkouts,
169+
):
170+
mock_get_hardware_commit_history.return_value = [
171+
("tree1", "repo1", "branch1", ["tag1"], "name1", "hash1", "time1"),
172+
]
173+
mock_sanitize_checkouts.return_value = {"bad-key": [{"start_time": "invalid"}]}
174+
175+
request = self.factory.post(
176+
self.url,
177+
data=json.dumps(
178+
{
179+
"origin": "maestro",
180+
"startTimestampInSeconds": 1737487800,
181+
"endTimestampInSeconds": 1737574200,
182+
"commitHeads": [],
183+
}
184+
),
185+
content_type="application/json",
186+
)
187+
response = self.view.post(request, hardware_id="1")
188+
189+
self.assertEqual(response.status_code, 500)
190+
self.assertIsInstance(response.data, str)

0 commit comments

Comments
 (0)