Fix day filter
[cds-indico.git] / indico / util / signals_test.py
blob157661ba65633e24deab3f17e6e4a015ee33a0ba
1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
17 import pytest
18 from mock import MagicMock
20 from indico.util.signals import values_from_signal, named_objects_from_signal
23 def _make_signal_response(objects):
24 return [(None, obj) for obj in objects]
27 def _make_gen(*values):
28 for value in values:
29 yield value
32 @pytest.mark.parametrize(('retvals', 'expected'), (
33 (('a', 'b', 'c'), {'a', 'b', 'c'}),
34 (('a', _make_gen('b', 'c')), {'a', 'b', 'c'}),
36 def test_values_from_signal(retvals, expected):
37 signal_response = _make_signal_response(retvals)
38 assert values_from_signal(signal_response) == expected
41 def test_values_from_signal_single_value():
42 vals = ('a', _make_gen('b', 'c'))
43 signal_response = _make_signal_response(vals)
44 assert values_from_signal(signal_response, single_value=True) == set(vals)
45 assert values_from_signal(signal_response) == {'a', 'b', 'c'}
48 def test_values_from_signal_skip_none():
49 vals = ('a', None, 'b', 'c')
50 signal_response = _make_signal_response(vals)
51 assert values_from_signal(signal_response, skip_none=False) == set(vals)
52 assert values_from_signal(signal_response) == set(vals) - {None}
55 def test_values_from_signal_as_list():
56 vals = ('a', 'b', 'c')
57 signal_response = _make_signal_response(vals)
58 assert values_from_signal(signal_response, as_list=True) == list(vals)
59 assert values_from_signal(signal_response) == set(vals)
62 def test_values_from_signal_multi_value_types():
63 vals = ('a', ['b', 'c'])
64 signal_response = _make_signal_response(vals)
65 with pytest.raises(TypeError):
66 # list is unhashable, can't be added to a set
67 values_from_signal(signal_response)
68 assert values_from_signal(signal_response, as_list=True) == list(vals)
69 assert values_from_signal(signal_response, multi_value_types=list) == {'a', 'b', 'c'}
72 def test_values_from_signal_return_plugins():
73 vals = ('a', 'b', 'c')
74 signal_response = _make_signal_response(vals) + [(MagicMock(indico_plugin='foo'), 'd')]
75 assert values_from_signal(signal_response, return_plugins=True) == set(zip([None] * 3, vals) + [('foo', 'd')])
76 assert values_from_signal(signal_response) == set(vals + ('d',))
79 @pytest.mark.parametrize('name_attr', ('name', 'foobar'))
80 def test_named_objects_from_signal(name_attr):
81 objects = [type('Dummy', (object,), {name_attr: name}) for name in ('a', 'b')]
82 signal_response = _make_signal_response(objects)
83 assert named_objects_from_signal(signal_response, name_attr=name_attr) == {'a': objects[0], 'b': objects[1]}
86 def test_named_objects_from_signal_plugin_attr():
87 objects = [type('Dummy', (object,), {'name': name}) for name in ('a', 'b')]
88 signal_response = _make_signal_response(objects)
89 signal_response[-1] = (MagicMock(indico_plugin='foo'), signal_response[-1][1])
90 rv = named_objects_from_signal(signal_response, plugin_attr='plugin')
91 assert rv == {'a': objects[0], 'b': objects[1]}
92 assert rv['a'].plugin is None
93 assert rv['b'].plugin == 'foo'
96 def test_named_objects_from_signal_duplicate():
97 objects = [type('Dummy', (object,), {'name': name}) for name in ('a', 'a', 'b', 'c', 'c')]
98 signal_response = _make_signal_response(objects)
99 with pytest.raises(RuntimeError) as exc_info:
100 named_objects_from_signal(signal_response)
101 assert str(exc_info.value) == 'Non-unique object names: a, c'