1- from unittest .mock import patch
1+ from unittest .mock import Mock , patch
22
33from kernelCI_app .queries .test import get_test_details_data , get_test_status_history
44from kernelCI_app .tests .unitTests .queries .conftest import (
55 setup_mock_cursor ,
6- setup_mock_test_queryset ,
76)
87
98
@@ -34,9 +33,25 @@ def test_get_test_details_data_empty_result(
3433
3534
3635class TestGetTestStatusHistory :
37- @patch ("kernelCI_app.queries.test.Tests" )
38- def test_get_test_status_history_with_platform (self , mock_tests_model ):
39- mock_queryset = setup_mock_test_queryset (mock_tests_model )
36+ @patch ("kernelCI_app.queries.test.transaction.atomic" )
37+ @patch ("kernelCI_app.queries.test.set_query_cache" )
38+ @patch ("kernelCI_app.queries.test.get_query_cache" )
39+ @patch ("kernelCI_app.queries.test.dict_fetchall" )
40+ @patch ("kernelCI_app.queries.test.connection" )
41+ def test_get_test_status_history_with_platform (
42+ self ,
43+ mock_connection ,
44+ mock_dict_fetchall ,
45+ mock_get_cache ,
46+ mock_set_cache ,
47+ mock_transaction ,
48+ ):
49+ mock_transaction .return_value .__enter__ = Mock (return_value = None )
50+ mock_transaction .return_value .__exit__ = Mock (return_value = None )
51+ expected_result = [{"id" : "test" , "status" : "PASS" }]
52+ mock_dict_fetchall .return_value = expected_result
53+ mock_get_cache .return_value = None
54+ mock_cursor = setup_mock_cursor (mock_connection )
4055
4156 result = get_test_status_history (
4257 path = "boot" ,
@@ -50,12 +65,34 @@ def test_get_test_status_history_with_platform(self, mock_tests_model):
5065 group_size = 10 ,
5166 )
5267
53- assert list (result ) == []
54- mock_queryset .filter .assert_called ()
55-
56- @patch ("kernelCI_app.queries.test.Tests" )
57- def test_get_test_status_history_without_platform (self , mock_tests_model ):
58- setup_mock_test_queryset (mock_tests_model )
68+ assert result == expected_result
69+ assert mock_cursor .execute .call_count > 1
70+ query_call = mock_cursor .execute .call_args_list [1 ]
71+ assert "platform" in query_call [0 ][1 ]
72+ assert query_call [0 ][1 ]["platform" ] == "x86_64"
73+ mock_get_cache .assert_called_once ()
74+ mock_set_cache .assert_called_once ()
75+ mock_transaction .assert_called_once ()
76+
77+ @patch ("kernelCI_app.queries.test.transaction.atomic" )
78+ @patch ("kernelCI_app.queries.test.set_query_cache" )
79+ @patch ("kernelCI_app.queries.test.get_query_cache" )
80+ @patch ("kernelCI_app.queries.test.dict_fetchall" )
81+ @patch ("kernelCI_app.queries.test.connection" )
82+ def test_get_test_status_history_without_platform (
83+ self ,
84+ mock_connection ,
85+ mock_dict_fetchall ,
86+ mock_get_cache ,
87+ mock_set_cache ,
88+ mock_transaction ,
89+ ):
90+ mock_transaction .return_value .__enter__ = Mock (return_value = None )
91+ mock_transaction .return_value .__exit__ = Mock (return_value = None )
92+ expected_result = []
93+ mock_dict_fetchall .return_value = expected_result
94+ mock_get_cache .return_value = None
95+ mock_cursor = setup_mock_cursor (mock_connection )
5996
6097 result = get_test_status_history (
6198 path = "boot" ,
@@ -69,13 +106,32 @@ def test_get_test_status_history_without_platform(self, mock_tests_model):
69106 group_size = 10 ,
70107 )
71108
72- assert list (result ) == []
73-
74- @patch ("kernelCI_app.queries.test.Tests" )
109+ assert result == expected_result
110+ query_call = mock_cursor .execute .call_args_list [1 ]
111+ assert "field_timestamp" in query_call [0 ][1 ]
112+ assert query_call [0 ][1 ]["field_timestamp" ] == "2025-11-11T10:00:00Z"
113+ mock_get_cache .assert_called_once ()
114+ mock_set_cache .assert_called_once ()
115+ mock_transaction .assert_called_once ()
116+
117+ @patch ("kernelCI_app.queries.test.transaction.atomic" )
118+ @patch ("kernelCI_app.queries.test.set_query_cache" )
119+ @patch ("kernelCI_app.queries.test.get_query_cache" )
120+ @patch ("kernelCI_app.queries.test.dict_fetchall" )
121+ @patch ("kernelCI_app.queries.test.connection" )
75122 def test_get_test_status_history_uses_start_time_when_provided (
76- self , mock_tests_model
123+ self ,
124+ mock_connection ,
125+ mock_dict_fetchall ,
126+ mock_get_cache ,
127+ mock_set_cache ,
128+ mock_transaction ,
77129 ):
78- mock_queryset = setup_mock_test_queryset (mock_tests_model )
130+ mock_transaction .return_value .__enter__ = Mock (return_value = None )
131+ mock_transaction .return_value .__exit__ = Mock (return_value = None )
132+ mock_dict_fetchall .return_value = []
133+ mock_get_cache .return_value = None
134+ mock_cursor = setup_mock_cursor (mock_connection )
79135
80136 get_test_status_history (
81137 path = "boot" ,
@@ -89,11 +145,32 @@ def test_get_test_status_history_uses_start_time_when_provided(
89145 group_size = 10 ,
90146 )
91147
92- mock_queryset .order_by .assert_called_with ("-start_time" )
148+ query_call = mock_cursor .execute .call_args_list [1 ]
149+ assert "test_start_time" in query_call [0 ][1 ]
150+ assert query_call [0 ][1 ]["test_start_time" ] == "2025-11-10T10:00:00Z"
151+ mock_get_cache .assert_called_once ()
152+ mock_set_cache .assert_called_once ()
153+ mock_transaction .assert_called_once ()
93154
94- @patch ("kernelCI_app.queries.test.Tests" )
95- def test_get_test_status_history_with_null_timestamps (self , mock_tests_model ):
96- mock_queryset = setup_mock_test_queryset (mock_tests_model )
155+ @patch ("kernelCI_app.queries.test.transaction.atomic" )
156+ @patch ("kernelCI_app.queries.test.set_query_cache" )
157+ @patch ("kernelCI_app.queries.test.get_query_cache" )
158+ @patch ("kernelCI_app.queries.test.dict_fetchall" )
159+ @patch ("kernelCI_app.queries.test.connection" )
160+ def test_get_test_status_history_with_null_timestamps (
161+ self ,
162+ mock_connection ,
163+ mock_dict_fetchall ,
164+ mock_get_cache ,
165+ mock_set_cache ,
166+ mock_transaction ,
167+ ):
168+ mock_transaction .return_value .__enter__ = Mock (return_value = None )
169+ mock_transaction .return_value .__exit__ = Mock (return_value = None )
170+ expected_result = []
171+ mock_dict_fetchall .return_value = expected_result
172+ mock_get_cache .return_value = None
173+ mock_cursor = setup_mock_cursor (mock_connection )
97174
98175 result = get_test_status_history (
99176 path = "boot" ,
@@ -107,7 +184,40 @@ def test_get_test_status_history_with_null_timestamps(self, mock_tests_model):
107184 group_size = 10 ,
108185 )
109186
110- assert list (result ) == []
111- filter_calls = [str (call ) for call in mock_queryset .filter .call_args_list ]
112- assert any ("start_time__isnull" in call for call in filter_calls )
113- assert any ("field_timestamp__isnull" in call for call in filter_calls )
187+ assert result == expected_result
188+ query_call = mock_cursor .execute .call_args_list [1 ]
189+ sql = query_call [0 ][0 ]
190+ assert "T.START_TIME IS NULL" in sql
191+ assert "T._TIMESTAMP IS NULL" in sql
192+ mock_get_cache .assert_called_once ()
193+ mock_set_cache .assert_called_once ()
194+ mock_transaction .assert_called_once ()
195+
196+ @patch ("kernelCI_app.queries.test.set_query_cache" )
197+ @patch ("kernelCI_app.queries.test.get_query_cache" )
198+ @patch ("kernelCI_app.queries.test.dict_fetchall" )
199+ def test_get_test_status_history_returns_cached_result (
200+ self ,
201+ mock_dict_fetchall ,
202+ mock_get_cache ,
203+ mock_set_cache ,
204+ ):
205+ cached_result = [{"id" : "cached" , "status" : "PASS" }]
206+ mock_get_cache .return_value = cached_result
207+
208+ result = get_test_status_history (
209+ path = "boot" ,
210+ origin = "maestro" ,
211+ git_repository_url = "https://my_url.com" ,
212+ git_repository_branch = "master" ,
213+ platform = "x86_64" ,
214+ test_start_time = "2025-11-11T10:00:00Z" ,
215+ config_name = "defconfig" ,
216+ field_timestamp = None ,
217+ group_size = 10 ,
218+ )
219+
220+ assert result == cached_result
221+ mock_dict_fetchall .assert_not_called ()
222+ mock_set_cache .assert_not_called ()
223+ mock_get_cache .assert_called_once ()
0 commit comments