| 1 |  |  | import os
 | 
            
                                                        
            
                                    
            
            
                | 2 |  |  | from sources.common.BasicNeeds import BasicNeeds
 | 
            
                                                        
            
                                    
            
            
                | 3 |  |  | from sources.common.FileOperations import FileOperations
 | 
            
                                                        
            
                                    
            
            
                | 4 |  |  | # package to facilitate multiple operation system operations
 | 
            
                                                        
            
                                    
            
            
                | 5 |  |  | import unittest
 | 
            
                                                        
            
                                    
            
            
                | 6 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 7 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 8 |  |  | class TestBasicNeeds(unittest.TestCase):
 | 
            
                                                        
            
                                    
            
            
                | 9 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 10 |  |  |     def test_add_value_to_dictionary(self):
 | 
            
                                                        
            
                                    
            
            
                | 11 |  |  |         class_fo = FileOperations()
 | 
            
                                                        
            
                                    
            
            
                | 12 |  |  |         # load testing values from JSON file where all cases are grouped
 | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |         json_structure = class_fo.fn_open_file_and_get_content('dict_to_test.json')
 | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |         class_bn = BasicNeeds()
 | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |         for add_type in json_structure:
 | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |             if 'reference' not in json_structure[add_type]:
 | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |                 json_structure[add_type]['reference'] = None
 | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |             actual = class_bn.fn_add_value_to_dictionary(json_structure[add_type]['list'].copy(),
 | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |                                                          json_structure[add_type]['add'], add_type,
 | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |                                                          json_structure[add_type]['reference'])
 | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |             self.assertEqual(json_structure[add_type]['expected'].copy(), actual, 'With '
 | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |                              + 'value "' + json_structure[add_type]['add'] + '" to be added "'
 | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |                              + add_type + '" the "' + str(json_structure[add_type]['reference'])
 | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |                              + '" in the list ' + str(json_structure[add_type]['list'].copy())
 | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |                              + ' expected values were: '
 | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |                              + str(json_structure[add_type]['expected'].copy())
 | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |                              + ' but found ' + str(actual))
 | 
            
                                                        
            
                                    
            
            
                | 28 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |     def test_numbers_with_leading_zero(self):
 | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |         pair_values = [
 | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |             {
 | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |                 'given': '1',
 | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |                 'leading_zeros': 2,
 | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |                 'expected': '01'
 | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |             },
 | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |             {
 | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |                 'given': '1',
 | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |                 'leading_zeros': 10,
 | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |                 'expected': '0000000001'
 | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |             },
 | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |             {
 | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |                 'given': '1',
 | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |                 'leading_zeros': 1,
 | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |                 'expected': '1'
 | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |             }
 | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |         ]
 | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |         bn = BasicNeeds()
 | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         for current_pair in pair_values:
 | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |             
 | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |             value_to_assert = bn.fn_numbers_with_leading_zero(current_pair['given'],
 | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |                                                               current_pair['leading_zeros'])
 | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |             self.assertEqual(value_to_assert, current_pair['expected'],
 | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |                              'Provided values was "' + value_to_assert + '", Expected')
 | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         pair_failing_values = [
 | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |             {
 | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |                 'given': '1',
 | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |                 'leading_zeros': 2,
 | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |                 'expected': '02'
 | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |             },
 | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |             {
 | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |                 'given': '1',
 | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |                 'leading_zeros': 0,
 | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |                 'expected': ''
 | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |             }
 | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         ]
 | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         for current_pair in pair_failing_values:
 | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |             value_to_assert = bn.fn_numbers_with_leading_zero(current_pair['given'],
 | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |                                                               current_pair['leading_zeros'])
 | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |             self.assertNotEqual(value_to_assert, current_pair['expected'])
 | 
            
                                                        
            
                                    
            
            
                | 70 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 71 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 72 |  |  | if __name__ == '__main__':
 | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |     unittest.main()
 | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |