TestAttributeFilter.setUp()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
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
from collections import namedtuple
3
from foil.filters import AttributeFilter, create_key_filter, create_indexer
4
5
6
TeamRank = namedtuple('TeamRank', ['sport', 'team', 'rank'])
7
8
9
class TestAttributeFilter(unittest.TestCase):
10
11
    def setUp(self):
12
        self.teams = [TeamRank('baseball', 'cubs', 2),
13
                      TeamRank('basketball', 'bulls', 1),
14
                      TeamRank('baseball', 'mets', 1),
15
                      TeamRank('basketball', 'lakers', 2),
16
                      TeamRank('basketball', 'knicks', 3),
17
                      TeamRank('basketball', 'bulls', 2)]
18
19
    def test_include_attributes(self):
20
        keys = ('sport', 'team')
21
        include = [('basketball', 'bulls'),
22
                   ('basketball', 'knicks')]
23
24
        expected = [TeamRank('basketball', 'bulls', 1),
25
                    TeamRank('basketball', 'knicks', 3),
26
                    TeamRank('basketball', 'bulls', 2)]
27
        result = list(AttributeFilter(keys, predicates=include).including(self.teams))
28
29
        self.assertEqual(expected, result)
30
31
    def test_exclude_attributes(self):
32
        keys = ('sport', 'team', 'rank')
33
        remove = [('basketball', 'bulls', 2),
34
                  ('baseball', 'mets', 1),
35
                  ('basketball', 'lakers', 3)]
36
37
        expected = [
38
            TeamRank('baseball', 'cubs', 2),
39
            TeamRank('basketball', 'bulls', 1),
40
            TeamRank('basketball', 'lakers', 2),
41
            TeamRank('basketball', 'knicks', 3)]
42
        result = list(AttributeFilter(keys, predicates=remove).excluding(self.teams))
43
44
        self.assertEqual(expected, result)
45
46
    def test_create_key_filter(self):
47
        properties = {'sports': ['baseball', 'basketball'],
48
                      'teams': ['bulls', 'knicks', 'lakers']}
49
50
        expected = set([('sports', 'baseball'),
51
                        ('sports', 'basketball'),
52
                        ('teams', 'bulls'),
53
                        ('teams', 'knicks'),
54
                        ('teams', 'lakers')])
55
        result = set(create_key_filter(properties))
56
57
        self.assertSetEqual(result, expected)
58
59
60
class TestCreateIndexer(unittest.TestCase):
61
62
    def setUp(self):
63
        self.record = [0, 10, 20]
64
65
    def test_single_indexer(self):
66
        indexer = create_indexer([1])
67
68
        expected = (10,)
69
        result = indexer(self.record)
70
71
        self.assertEqual(expected, result)
72
73
    def test_multi_indexer(self):
74
        indexer = create_indexer([2, 0])
75
76
        expected = (20, 0)
77
        result = indexer(self.record)
78
79
        self.assertEqual(expected, result)
80