44import uuid
55from unittest .mock import MagicMock , patch
66
7+ import github3
78from cleanowners import (
89 commit_changes ,
10+ get_org ,
911 get_repos_iterator ,
1012 get_usernames_from_codeowners ,
1113)
@@ -81,6 +83,38 @@ def test_get_usernames_from_codeowners(self):
8183 self .assertEqual (result , expected_usernames )
8284
8385
86+ class TestGetOrganization (unittest .TestCase ):
87+ """Test the get_org function in cleanowners.py"""
88+
89+ @patch ("github3.login" )
90+ def test_get_organization_succeeds (self , mock_github ):
91+ """Test the organization is valid."""
92+ organization = "my_organization"
93+ github_connection = mock_github .return_value
94+
95+ mock_organization = MagicMock ()
96+ github_connection .organization .return_value = mock_organization
97+
98+ result = get_org (github_connection , organization )
99+
100+ github_connection .organization .assert_called_once_with (organization )
101+ self .assertEqual (result , mock_organization )
102+
103+ @patch ("github3.login" )
104+ def test_get_organization_fails (self , mock_github ):
105+ """Test the organization is not valid."""
106+ organization = "my_organization"
107+ github_connection = mock_github .return_value
108+
109+ github_connection .organization .side_effect = github3 .exceptions .NotFoundError (
110+ resp = MagicMock (status_code = 404 )
111+ )
112+ result = get_org (github_connection , organization )
113+
114+ github_connection .organization .assert_called_once_with (organization )
115+ self .assertIsNone (result )
116+
117+
84118class TestGetReposIterator (unittest .TestCase ):
85119 """Test the get_repos_iterator function in evergreen.py"""
86120
@@ -111,7 +145,7 @@ def test_get_repos_iterator_with_organization(self, mock_github):
111145 def test_get_repos_iterator_with_repository_list (self , mock_github ):
112146 """Test the get_repos_iterator function with a repository list"""
113147 organization = None
114- repository_list = ["org/repo1" , "org /repo2" ]
148+ repository_list = ["org/repo1" , "org2 /repo2" ]
115149 github_connection = mock_github .return_value
116150
117151 mock_repository = MagicMock ()
@@ -123,7 +157,7 @@ def test_get_repos_iterator_with_repository_list(self, mock_github):
123157 # Assert that the repository method was called with the correct arguments for each repository in the list
124158 expected_calls = [
125159 unittest .mock .call ("org" , "repo1" ),
126- unittest .mock .call ("org " , "repo2" ),
160+ unittest .mock .call ("org2 " , "repo2" ),
127161 ]
128162 github_connection .repository .assert_has_calls (expected_calls )
129163
0 commit comments