11#! /usr/bin/env python
22"""Tests for GitRemote"""
33
4+ from dataclasses import dataclass
45from mock import Mock
56
67import git
1011from git_wrapper .repo import GitRepo
1112
1213
14+ @dataclass
15+ class RemoteNames :
16+ name : str
17+
18+
19+ @dataclass
20+ class RemoteNamesUrl (RemoteNames ):
21+ url : str
22+
23+
1324def remote_generator (names ):
1425 """Generates objects to be used with git.Repo.remotes call"""
1526 ret_data = []
1627 for name in names :
17- obj = type ( '' , (), {})( )
18- obj . name = name
19- ret_data . append ( obj )
28+ ret_data . append ( RemoteNames ( name ) )
29+ return ret_data
30+
2031
32+ def remote_generator_url (remotes ):
33+ """Generates objects to be used with git.Repo.remotes call"""
34+ ret_data = []
35+ for name , url in remotes .items ():
36+ ret_data .append (RemoteNamesUrl (name , url ))
2137 return ret_data
2238
2339
@@ -36,6 +52,21 @@ def test_get_remotes_returns_list(mock_repo):
3652 assert expected == git_util .remote .names ()
3753
3854
55+ def test_get_remotes_returns_dict (mock_repo ):
56+ """
57+ GIVEN GitRepo is initialized with a path and repo
58+ WHEN remote.names_url_dict is called
59+ THEN a dict of remote names with its url is returned
60+ """
61+ expected = {'a' : 1 , 'b' : 2 , 'c' : 3 }
62+ attrs = {'remotes' : remote_generator_url (expected )}
63+ mock_repo .configure_mock (** attrs )
64+
65+ git_util = GitRepo ('./' , mock_repo )
66+
67+ assert expected == git_util .remote .names_url_dict ()
68+
69+
3970def test_add_remote_adds (mock_repo ):
4071 """
4172 GIVEN GitRepo initialized with a path and repo
@@ -89,6 +120,46 @@ def test_add_remote_update_fails(mock_repo):
89120 delete_mock .assert_called_once_with (remote_mock )
90121
91122
123+ def test_remove_remote_removes (mock_repo ):
124+ """
125+ GIVEN GitRepo initialized with a path and repo
126+ WHEN remote.remove is called with a name and url
127+ THEN a TRUE status is returned
128+ """
129+ git_util = GitRepo ('./' , mock_repo )
130+
131+ assert git_util .remote .remove ('origin' ) is True
132+
133+
134+ def test_remove_remote_remote_fails (mock_repo ):
135+ """
136+ GIVEN GitRepo initialized with a path and repo
137+ WHEN remote.remove is called with a name and url
138+ AND the repo.remote fails with an exception
139+ THEN a ReferenceNotFoundException is raised
140+ """
141+ mock_repo .remote .side_effect = ValueError
142+
143+ repo = GitRepo (repo = mock_repo )
144+ with pytest .raises (exceptions .ReferenceNotFoundException ):
145+ repo .remote .remove ("doesntExist" )
146+
147+ mock_repo .remote .assert_called_with ("doesntExist" )
148+
149+
150+ def test_remove_remote_remove_fails (mock_repo ):
151+ """
152+ GIVEN GitRepo initialized with a path and repo
153+ WHEN remote.remove is called with a name and url
154+ AND the repo.delete_remote fails with an exception
155+ THEN a False status is returned
156+ """
157+ mock_repo .delete_remote .side_effect = git .CommandError ('remove' )
158+ git_util = GitRepo ('./' , mock_repo )
159+
160+ assert git_util .remote .remove ('rdo' ) is False
161+
162+
92163def test_fetch (mock_repo ):
93164 """
94165 GIVEN GitRepo is initialized with a path and repo
0 commit comments