TestDictUtils.test_flip_dict()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
1
import unittest
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
3
from foil.compose import (tupleize, cartesian_product, disjoint_union,
4
                          create_quantiles, flip_dict, flip_iterable_dict)
5
6
7
class TestCartesianProduct(unittest.TestCase):
8
9
    def test_tupleize(self):
10
        elementlist = [None, 5,
11
                       'string', ('string',),
12
                       ('a', 'b', 'c')]
13
14
        expectedlist = [(None,), (5,),
15
                        ('string',), ('string',),
16
                        ('a', 'b', 'c')]
17
18
        for element, expected in zip(elementlist, expectedlist):
19
            with self.subTest(element=element):
20
                result = tupleize(element)
21
                self.assertEqual(result, expected)
22
23
    def test_carteisian_product(self):
24
        sets = (('a', 'bee', None), 'a')
25
26
        expected = [('a', 'a'), ('bee', 'a'), (None, 'a')]
27
        result = list(cartesian_product(sets))
28
29
        self.assertEqual(expected, result)
30
31
        sets = (('b', 'c'), ('x', 'y'))
32
33
        expected = [('b', 'x'), ('b', 'y'), ('c', 'x'), ('c', 'y')]
34
        result = list(cartesian_product(sets))
35
36
        self.assertEqual(expected, result)
37
38
    def test_disjoint_union(self):
39
        iter1 = iter([('a', 'a'), ('bee', 'a')])
40
        iter2 = iter([('z', None), ('zz', None)])
41
42
        expected = [('a', 'a'), ('bee', 'a'), ('z', None), ('zz', None)]
43
        result = list(disjoint_union([iter1, iter2]))
44
45
        self.assertEqual(expected, result)
46
47
48
class TestQuantiles(unittest.TestCase):
49
50
    def test_create_quantiles(self):
51
        items = ['a', 'b', 'c']
52
        lower_bound = 0.
53
        upper_bound = 6.
54
55
        expected = [('a', (0., 2.)), ('b', (2., 4.)), ('c', (4., 6.))]
56
        result = list(create_quantiles(items, lower_bound, upper_bound))
57
58
        self.assertEqual(expected, result)
59
60
61
class TestDictUtils(unittest.TestCase):
62
63
    def test_flip_dict(self):
64
        dictionary = {'a': 'aa', 'b': 'bb'}
65
66
        expected = {'aa': 'a', 'bb': 'b'}
67
        result = flip_dict(dictionary)
68
69
        self.assertEqual(expected, result)
70
71
    def test_flip_iterable_dict(self):
72
        dictionary = {'a': ['1', '2'], 'b': ['10', '20']}
73
74
        expected = {'1': 'a', '2': 'a', '10': 'b', '20': 'b'}
75
        result = flip_iterable_dict(dictionary)
76
77
        self.assertEqual(expected, result)
78