1- import builtins
21import os
32import random
4- import unittest
3+ from unittest import TestCase
54from unittest .mock import call , patch
65
76from src .controller import PhonebookController , CommandNotFound , FieldRequired
8- from src .model import Contact , JsonStorage
7+ from src .model import JsonStorage
98from src .view import Commands
109from tests .utils import (
10+ generate_random_id ,
1111 generate_random_string ,
12- generate_random_phone ,
1312 generate_contact ,
1413 generate_contacts ,
1514)
1615
1716
18- class TestControllerGetCommand (unittest . TestCase ):
17+ class TestControllerGetCommand (TestCase ):
1918 def setUp (self ):
2019 self .controller = PhonebookController ()
2120
@@ -41,7 +40,7 @@ def test_empty_command(self):
4140 self .assertEqual (command , result )
4241
4342
44- class TestGetRequiredField (unittest . TestCase ):
43+ class TestGetRequiredField (TestCase ):
4544 def setUp (self ):
4645 self .controller = PhonebookController ()
4746
@@ -64,7 +63,7 @@ def test_empty_value(self):
6463 )
6564
6665
67- class TestAddContact (unittest . TestCase ):
66+ class TestAddContact (TestCase ):
6867 def setUp (self ):
6968 self .controller = PhonebookController ()
7069
@@ -110,7 +109,7 @@ def test_add_contact_output(self, output):
110109 )
111110
112111
113- class TestFindContact (unittest . TestCase ):
112+ class TestFindContact (TestCase ):
114113 def setUp (self ):
115114 self .controller = PhonebookController ()
116115
@@ -152,7 +151,106 @@ def test_output_multiple_contacts(self, output):
152151 self .controller ._find_contact ()
153152 calls = [
154153 call (contact_id = c .id , name = c .name , phone = c .phone , comment = c .comment )
155- for c in contacts [ 1 :]
154+ for c in contacts
156155 ]
157156 output .assert_has_calls (calls )
158157 self .assertEqual (output .call_count , len (contacts ))
158+
159+
160+ class TestGetRequiredIntegerField (TestCase ):
161+ def setUp (self ):
162+ self .controller = PhonebookController ()
163+
164+ def tearDown (self ):
165+ if os .path .isfile (JsonStorage .STORAGE ):
166+ os .remove (JsonStorage .STORAGE )
167+
168+ @patch ("src.view.InputView.ask_required_field" )
169+ def test_required_field_asked (self , view ):
170+ field = generate_random_string ()
171+ self .controller ._get_required_integer_field (field = field )
172+ view .assert_called_with (field )
173+
174+ def test_entered_integer_value (self ):
175+ expected = generate_random_id ()
176+ with patch ("src.view.InputView.ask_required_field" , return_value = str (expected )):
177+ result = self .controller ._get_required_integer_field (
178+ generate_random_string ()
179+ )
180+ self .assertEqual (result , expected )
181+
182+ def test_entered_not_integer_value (self ):
183+ with patch (
184+ "src.view.InputView.ask_required_field" ,
185+ return_value = generate_random_string ()
186+ ):
187+ self .assertRaises (
188+ ValueError ,
189+ self .controller ._get_required_integer_field ,
190+ generate_random_id (),
191+ )
192+
193+
194+ class TestEditContact (TestCase ):
195+ def setUp (self ):
196+ self .controller = PhonebookController ()
197+
198+ def tearDown (self ):
199+ if os .path .isfile (JsonStorage .STORAGE ):
200+ os .remove (JsonStorage .STORAGE )
201+
202+ @patch ("src.view.InputView.update_field" , side_effect = ["" , "" , "" ])
203+ @patch ("src.model.PhonebookModel.get" )
204+ @patch ("src.controller.PhonebookController._get_required_integer_field" )
205+ def test_contact_id_asked (self , ask_integer , phonebook_model , update_fields ):
206+ self .controller ._edit_contact ()
207+ ask_integer .assert_called_with ("ID" )
208+
209+ @patch ("src.view.InputView.update_field" , side_effect = ["" , "" , "" ])
210+ @patch ("src.model.PhonebookModel.get" )
211+ def test_get_contact_called (self , get_contact , update_fields ):
212+ contact_id = generate_random_id ()
213+ with patch (
214+ "src.controller.PhonebookController._get_required_integer_field" ,
215+ return_value = contact_id
216+ ):
217+ self .controller ._edit_contact ()
218+ get_contact .assert_called_with (contact_id )
219+
220+ # @patch("src.view.InputView.update_field")
221+ @patch ("src.model.PhonebookModel.get" )
222+ @patch ("src.controller.PhonebookController._get_required_integer_field" )
223+ def test_new_name_asked (self , ask_id , get_contact ):
224+ with patch ("src.view.InputView.update_field" ) as ask_new_values :
225+ self .controller ._edit_contact ()
226+ calls = [call ("name" ), call ("phone" ), call ("comment" )]
227+ ask_new_values .assert_has_calls (calls , any_order = True )
228+ self .assertEqual (3 , ask_new_values .call_count )
229+
230+
231+ def test_update_name_called (self ):
232+ pass
233+
234+ def test_try_clear_name (self ):
235+ pass
236+
237+ def test_new_phone_asked (self ):
238+ pass
239+
240+ def test_update_phone_called (self ):
241+ pass
242+
243+ def test_try_clear_phone (self ):
244+ pass
245+
246+ def test_new_comment_asked (self ):
247+ pass
248+
249+ def test_clear_comment_called (self ):
250+ pass
251+
252+ def test_update_comment_called (self ):
253+ pass
254+
255+ def test_return_contact (self ):
256+ pass
0 commit comments