TestBasicNeeds.test_add_value_to_dictionary()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 18
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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