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+ @dataclass
19+ class RemoteNamesUrl (RemoteNames ):
20+ url : str
21+
1322def remote_generator (names ):
1423 """Generates objects to be used with git.Repo.remotes call"""
1524 ret_data = []
1625 for name in names :
17- obj = type ('' , (), {})()
18- obj .name = name
19- ret_data .append (obj )
26+ ret_data .append (RemoteNames (name ))
27+ return ret_data
2028
29+ def remote_generator_url (remotes ):
30+ """Generates objects to be used with git.Repo.remotes call"""
31+ ret_data = []
32+ for name , url in remotes .items ():
33+ ret_data .append (RemoteNamesUrl (name , url ))
2134 return ret_data
2235
2336
@@ -36,6 +49,21 @@ def test_get_remotes_returns_list(mock_repo):
3649 assert expected == git_util .remote .names ()
3750
3851
52+ def test_get_remotes_returns_dict (mock_repo ):
53+ """
54+ GIVEN GitRepo is initialized with a path and repo
55+ WHEN remote.names_url_dict is called
56+ THEN a dict of remote names with its url is returned
57+ """
58+ expected = {'a' : 1 , 'b' : 2 , 'c' : 3 }
59+ attrs = {'remotes' : remote_generator_url (expected )}
60+ mock_repo .configure_mock (** attrs )
61+
62+ git_util = GitRepo ('./' , mock_repo )
63+
64+ assert expected == git_util .remote .names_url_dict ()
65+
66+
3967def test_add_remote_adds (mock_repo ):
4068 """
4169 GIVEN GitRepo initialized with a path and repo
@@ -89,6 +117,47 @@ def test_add_remote_update_fails(mock_repo):
89117 delete_mock .assert_called_once_with (remote_mock )
90118
91119
120+ def test_remove_remote_removes (mock_repo ):
121+ """
122+ GIVEN GitRepo initialized with a path and repo
123+ WHEN remote.remove is called with a name and url
124+ THEN a TRUE status is returned
125+ WITH update called
126+ """
127+ git_util = GitRepo ('./' , mock_repo )
128+
129+ assert git_util .remote .remove ('origin' ) is True
130+
131+
132+ def test_remove_remote_remote_fails (mock_repo ):
133+ """
134+ GIVEN GitRepo initialized with a path and repo
135+ WHEN remote.remove is called with a name and url
136+ AND the remote create fails with an exception
137+ THEN a False status is returned
138+ """
139+ mock_repo .remote .side_effect = ValueError
140+
141+ repo = GitRepo (repo = mock_repo )
142+ with pytest .raises (exceptions .ReferenceNotFoundException ):
143+ repo .remote .remove ("doesntExist" )
144+
145+ mock_repo .remote .assert_called_with ("doesntExist" )
146+
147+
148+ def test_remove_remote_remove_fails (mock_repo ):
149+ """
150+ GIVEN GitRepo initialized with a path and repo
151+ WHEN remote.remove is called with a name and url
152+ AND the remote create fails with an exception
153+ THEN a False status is returned
154+ """
155+ mock_repo .delete_remote .side_effect = git .CommandError ('remove' )
156+ git_util = GitRepo ('./' , mock_repo )
157+
158+ assert git_util .remote .remove ('rdo' ) is False
159+
160+
92161def test_fetch (mock_repo ):
93162 """
94163 GIVEN GitRepo is initialized with a path and repo
0 commit comments