@@ -256,20 +256,38 @@ def extractall(*args, **kwargs):
256256 mock_create_instance .assert_called_once ()
257257 mock_wait_for_operation .assert_called_once ()
258258
259+ @mock .patch ('github.Github.get_repo' )
259260 @mock .patch ('mod_ci.controllers.start_test' )
260261 @mock .patch ('mod_ci.controllers.get_compute_service_object' )
261262 @mock .patch ('mod_ci.controllers.g' )
262- def test_gcp_instance (self , mock_g , mock_get_compute_service_object , mock_start_test ):
263+ def test_gcp_instance (self , mock_g , mock_get_compute_service_object , mock_start_test , mock_repo ):
263264 """Test gcp_instance function."""
264265 from mod_ci .controllers import gcp_instance
265266
266- # Making a sample test invalid
267- test = Test .query .get (1 )
268- test .pr_nr = 0
267+ repo = mock_repo ()
268+
269+ # Making test with id 1 invalid with pr_nr = 0
270+ test_1 = Test .query .get (1 )
271+ test_1 .pr_nr = 0
272+
273+ # Making pr of test with id 2 already updated
274+ test_2 = Test .query .get (2 )
275+ pr_head_sha = test_2 .commit + 'f'
276+ repo .get_pull .return_value .head .sha = pr_head_sha
277+ repo .get_pull .return_value .state = 'open'
278+
279+ # Creating a test with pull_request type that is valid
280+ test_3 = Test (TestPlatform .linux , TestType .pull_request , 1 , "pull_request" , pr_head_sha , 2 )
281+ g .db .add (test_3 )
282+
283+ # Creating a test with commit type that is valid
284+ test_4 = Test (TestPlatform .linux , TestType .commit , 1 , "master" , pr_head_sha )
285+ g .db .add (test_4 )
269286 g .db .commit ()
270- gcp_instance (self .app , mock_g .db , TestPlatform .linux , mock .ANY , None )
271287
272- mock_start_test .assert_called_once ()
288+ gcp_instance (self .app , mock_g .db , TestPlatform .linux , repo , None )
289+
290+ self .assertEqual (mock_start_test .call_count , 2 )
273291 mock_get_compute_service_object .assert_called_once ()
274292
275293 def test_get_compute_service_object (self ):
@@ -459,9 +477,10 @@ def test_customizedtest_added_to_queue(self):
459477 self .assertIn (2 , customized_test )
460478 self .assertNotIn (1 , customized_test )
461479
480+ @mock .patch ('flask.g.log.error' )
462481 @mock .patch ('mailer.Mailer' )
463482 @mock .patch ('mod_ci.controllers.get_html_issue_body' )
464- def test_inform_mailing_list (self , mock_get_html_issue_body , mock_email ):
483+ def test_inform_mailing_list (self , mock_get_html_issue_body , mock_email , mock_log_error ):
465484 """Test the inform_mailing_list function."""
466485 from mod_ci .controllers import inform_mailing_list
467486
@@ -482,6 +501,11 @@ def test_inform_mailing_list(self, mock_get_html_issue_body, mock_email):
482501 }
483502 )
484503 mock_get_html_issue_body .assert_called_once ()
504+ mock_log_error .assert_not_called ()
505+
506+ mock_email .send_simple_message .return_value = False
507+ inform_mailing_list (mock_email , "matejmecka" , "2430" , "Some random string" , "Lorem Ipsum sit dolor amet..." )
508+ mock_log_error .assert_called_once ()
485509
486510 @staticmethod
487511 @mock .patch ('mod_ci.controllers.markdown' )
@@ -741,34 +765,6 @@ def __init__(self):
741765
742766 mock_test .query .filter .assert_called_once ()
743767
744- @mock .patch ('github.Github.get_repo' )
745- @mock .patch ('mod_ci.controllers.Test' )
746- @mock .patch ('requests.get' , side_effect = mock_api_request_github )
747- def test_webhook_pr_converted_to_draft (self , mock_requests , mock_test , mock_repo ):
748- """Test webhook triggered with pull_request event with converted_to_draft action."""
749- platform_name = "platform"
750-
751- class MockTest :
752- def __init__ (self ):
753- self .id = 1
754- self .progress = []
755- self .platform = MockPlatform (platform_name )
756- self .commit = "test"
757-
758- mock_test .query .filter .return_value .all .return_value = [MockTest ()]
759- mock_repo .return_value .get_commit .return_value .get_statuses .return_value = [
760- {"context" : f"CI - { platform_name } " }]
761-
762- data = {'action' : 'converted_to_draft' ,
763- 'pull_request' : {'number' : 1234 , 'draft' : False }}
764- # one of ip address from GitHub web hook
765- with self .app .test_client () as c :
766- response = c .post (
767- '/start-ci' , environ_overrides = WSGI_ENVIRONMENT ,
768- data = json .dumps (data ), headers = self .generate_header (data , 'pull_request' ))
769-
770- mock_test .query .filter .assert_called_once ()
771-
772768 @mock .patch ('mod_ci.controllers.BlockedUsers' )
773769 @mock .patch ('github.Github.get_repo' )
774770 @mock .patch ('requests.get' , side_effect = mock_api_request_github )
@@ -803,25 +799,6 @@ def test_webhook_pr_opened(self, mock_request, mock_add_test_entry, mock_repo, m
803799 mock_blocked .query .filter .assert_called_once_with (mock_blocked .user_id == 'test' )
804800 mock_add_test_entry .assert_called_once ()
805801
806- @mock .patch ('mod_ci.controllers.BlockedUsers' )
807- @mock .patch ('github.Github.get_repo' )
808- @mock .patch ('mod_ci.controllers.add_test_entry' )
809- @mock .patch ('requests.get' , side_effect = mock_api_request_github )
810- def test_webhook_pr_ready_for_review (self , mock_request , mock_add_test_entry , mock_repo , mock_blocked ):
811- """Test webhook triggered with pull_request event with ready_for_review action."""
812- mock_blocked .query .filter .return_value .first .return_value = None
813-
814- data = {'action' : 'ready_for_review' ,
815- 'pull_request' : {'number' : 1234 , 'head' : {'sha' : 'abcd1234' }, 'user' : {'id' : 'test' }, 'draft' : False }}
816- with self .app .test_client () as c :
817- response = c .post (
818- '/start-ci' , environ_overrides = WSGI_ENVIRONMENT ,
819- data = json .dumps (data ), headers = self .generate_header (data , 'pull_request' ))
820-
821- self .assertEqual (response .data , b'{"msg": "EOL"}' )
822- mock_blocked .query .filter .assert_called_once_with (mock_blocked .user_id == 'test' )
823- mock_add_test_entry .assert_called_once ()
824-
825802 @mock .patch ('mod_ci.controllers.BlockedUsers' )
826803 @mock .patch ('github.Github.get_repo' )
827804 @mock .patch ('requests.get' , side_effect = mock_api_request_github )
@@ -1292,10 +1269,8 @@ def test_github_api_error(self, mock_critical, mock_github):
12921269 schedule_test (github_status )
12931270 mock_critical .assert_called ()
12941271 mock_critical .reset_mock ()
1295- deschedule_test (github_status , 1 , TestType .commit , TestPlatform .linux )
1296- mock_critical .assert_called ()
1297- mock_critical .reset_mock ()
1298- deschedule_test (github_status , 1 , TestType .commit , TestPlatform .windows )
1272+ test = Test .query .first ()
1273+ deschedule_test (github_status , test = test , db = g .db )
12991274 mock_critical .assert_called ()
13001275
13011276 @mock .patch ('mod_ci.controllers.is_main_repo' )
0 commit comments