-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_github_clone.py
More file actions
67 lines (51 loc) · 2.46 KB
/
Copy pathtest_github_clone.py
File metadata and controls
67 lines (51 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import unittest
from unittest.mock import patch, MagicMock
import os
import sys
# Add the root directory to the Python path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from github_clone import get_starred_repos, clone_repo
class TestGitHubClone(unittest.TestCase):
@patch('github_clone.requests.get')
def test_get_starred_repos_success(self, mock_get):
# Mock the API response
mock_response = MagicMock()
mock_response.json.return_value = [{'name': 'repo1'}, {'name': 'repo2'}]
mock_response.raise_for_status = MagicMock()
mock_get.return_value = mock_response
repos = get_starred_repos('testuser', 'testtoken')
self.assertEqual(len(repos), 2)
self.assertEqual(repos[0]['name'], 'repo1')
mock_get.assert_called_with('https://api.github.com/users/testuser/starred', auth=('testuser', 'testtoken'))
@patch('github_clone.requests.get')
def test_get_starred_repos_failure(self, mock_get):
# Mock a failed API response
mock_response = MagicMock()
mock_response.raise_for_status.side_effect = Exception("API Error")
mock_get.return_value = mock_response
with self.assertRaises(Exception):
get_starred_repos('testuser', 'testtoken')
@patch('github_clone.git.Repo.clone_from')
@patch('github_clone.os.path.exists')
def test_clone_repo_new(self, mock_exists, mock_clone_from):
# Mock that the repo does not exist
mock_exists.return_value = False
repo_info = {'name': 'new_repo', 'clone_url': 'http://example.com/new_repo.git'}
clone_dir = 'test_dir'
clone_repo(repo_info, clone_dir)
repo_dir = os.path.join(clone_dir, repo_info['name'])
mock_exists.assert_called_with(repo_dir)
mock_clone_from.assert_called_with(repo_info['clone_url'], repo_dir)
@patch('github_clone.git.Repo.clone_from')
@patch('github_clone.os.path.exists')
def test_clone_repo_exists(self, mock_exists, mock_clone_from):
# Mock that the repo already exists
mock_exists.return_value = True
repo_info = {'name': 'existing_repo', 'clone_url': 'http://example.com/existing_repo.git'}
clone_dir = 'test_dir'
clone_repo(repo_info, clone_dir)
repo_dir = os.path.join(clone_dir, repo_info['name'])
mock_exists.assert_called_with(repo_dir)
mock_clone_from.assert_not_called()
if __name__ == '__main__':
unittest.main()