Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
6
Indexable
Never
import unittest
import requests
from unittest.mock import patch
from io import StringIO
import sys
from git_fork_per import get_project_list

class TestGitLabAPI(unittest.TestCase):
    def setUp(self):
        self.username = "testuser"
        self.projects_data = [
            {
                "id": 1,
                "name": "personal_project",
                "owner": {"id": "testuser"}
            },
            {
                "id": 2,
                "name": "forked_project",
                "owner": {"id": "otheruser"},
                "forked_from_project": {"id": 3}
            },
            {
                "id": 3,
                "name": "original_project",
                "owner": {"id": "otheruser"}
            }
        ]
        self.expected_output = [
            "Personal project: personal_project",
            "Forked project: forked_project",
            "Personal project: original_project"
        ]
        
    @patch('sys.stdout', new_callable=StringIO)
    def test_get_project_list(self, mock_stdout):
        with patch.object(requests, 'get') as mock_get:
            mock_get.return_value.status_code = 200
            mock_get.return_value.json.return_value = self.projects_data
            get_project_list(self.username)
            output = mock_stdout.getvalue().strip().split('\n')
            self.assertEqual(output, self.expected_output)
            
if __name__ == '__main__':
    unittest.main()