Exemplo n.º 1
0
    def test_init_exceptions(self):
        with self.assertRaises(TypeError):
            ProxyGroup(123)

        with self.assertRaises(ValueError):
            ProxyGroup('abc')
Exemplo n.º 2
0
 def test__getattr__(self):
     number = complex(2, 3)
     group = ProxyGroup([number, number])
     group = group.imag  # <- Gets `imag` attribute.
     self.assertEqual(group._objs, (3, 3))
Exemplo n.º 3
0
 def test_init_mapping(self):
     data = {'a': 1, 'b': 2, 'c': 3}
     group = ProxyGroup({'a': 1, 'b': 2, 'c': 3})
     self.assertEqual(group._keys, tuple(data.keys()))
     self.assertEqual(group._objs, tuple(data.values()))
Exemplo n.º 4
0
    def test_expand_args_kwds(self):
        argsgroup = ProxyGroup([2, 4])

        kwdsgroup = ProxyGroup([2, 4])
        kwdsgroup._keys = ['foo', 'bar']

        # Unwrap ProxyGroup.
        result = argsgroup._expand_args_kwds(ProxyGroup([5, 6]))
        expected = [
            ((5, ), {}),
            ((6, ), {}),
        ]
        self.assertEqual(result, expected)

        # Expand int and unwrap ProxyGroup.
        result = argsgroup._expand_args_kwds(1, ProxyGroup([5, 6]))
        expected = [
            ((1, 5), {}),
            ((1, 6), {}),
        ]
        self.assertEqual(result, expected)

        # Unwrap two ProxyGroups.
        result = argsgroup._expand_args_kwds(x=ProxyGroup([5, 6]),
                                             y=ProxyGroup([7, 9]))
        expected = [
            ((), {
                'x': 5,
                'y': 7
            }),
            ((), {
                'x': 6,
                'y': 9
            }),
        ]
        self.assertEqual(result, expected)

        # Kwdsgroup expansion.
        kwdgrp2 = ProxyGroup([5, 6])
        kwdgrp2._keys = ['foo', 'bar']

        # Unwrap keyed ProxyGroup.
        result = kwdsgroup._expand_args_kwds(kwdgrp2)
        expected = [
            ((5, ), {}),
            ((6, ), {}),
        ]
        self.assertEqual(result, expected)

        # Unwrap keyed ProxyGroup with keys in different order.
        kwdgrp_reverse = ProxyGroup([6, 5])
        kwdgrp_reverse._keys = ['bar', 'foo']
        result = kwdsgroup._expand_args_kwds(kwdgrp_reverse)
        expected = [
            ((5, ), {}),
            ((6, ), {}),
        ]
        self.assertEqual(result, expected)

        # Expand int and unwrap keyed ProxyGroup.
        result = kwdsgroup._expand_args_kwds(1, kwdgrp2)
        expected = [
            ((1, 5), {}),
            ((1, 6), {}),
        ]
        self.assertEqual(result, expected)

        # Sanity-check/quick integration test (all combinations).
        result = kwdsgroup._expand_args_kwds('a',
                                             ProxyGroup({
                                                 'foo': 'b',
                                                 'bar': 'c'
                                             }),
                                             x=1,
                                             y=ProxyGroup({
                                                 'bar': 4,
                                                 'foo': 2
                                             }))
        expected = [
            (('a', 'b'), {
                'x': 1,
                'y': 2
            }),
            (('a', 'c'), {
                'x': 1,
                'y': 4
            }),
        ]
        self.assertEqual(result, expected)
Exemplo n.º 5
0
 def test_init_sequence(self):
     group = ProxyGroup([1, 2, 3])
     self.assertEqual(group._keys, ())
     self.assertEqual(group._objs, (1, 2, 3))
Exemplo n.º 6
0
    def test_normalize_value(self):
        group = ProxyGroup([2, 4])

        result = group._normalize_value(5)
        self.assertEqual(
            result,
            (5, 5),
            msg='value is expanded to match number of _objs',
        )

        result = group._normalize_value(ProxyGroup([5, 6]))
        self.assertEqual(
            result,
            (5, 6),
            msg='compatible ProxyGroups are unwrapped rather than expanded',
        )

        other = ProxyGroup([5, 6, 7])
        result = group._normalize_value(other)
        self.assertIsInstance(
            result,
            tuple,
            msg='incompatible ProxyGroups are expanded like other values',
        )
        self.assertEqual(len(result), 2)
        equals_other = super(other.__class__, other).__eq__
        self.assertTrue(equals_other(result[0]))
        self.assertTrue(equals_other(result[1]))

        group = ProxyGroup([2, 4])
        group._keys = ['foo', 'bar']
        other = ProxyGroup([8, 6])
        other._keys = ['bar', 'foo']  # <- keys in different order
        result = group._normalize_value(other)
        self.assertEqual(
            result,
            (6, 8),  # <- reordered to match `group`
            msg='result order should match key names, not _obj position',
        )