def test_semicolon_error(self): x, y = unpack_keys(dict(x=1, y=2)) # no error assert x == 1 and y == 2 with self.assertRaises(Exception): x, y = unpack_keys( # Error caused by this line having multiple nodes # from different statements dict(x=1, y=2)); str(x)
def test_unpack_keys_extras(self): env = dict(DATABASE_USERNAME='******', DATABASE_PASSWORD='******') username, password = unpack_keys(env, prefix='DATABASE_', swapcase=True) self.assertEqual(username, 'me') self.assertEqual(password, 'secret')
def test_unpack_keys_list_comprehension(self): self.assertEqual([(y, x) for x, y in unpack_keys([ dict(x=1, y=2), dict(x=3, y=4), ])], [ (2, 1), (4, 3), ])
def test_unpack_keys_basic(self): obj = SimpleNamespace(thing=SimpleNamespace()) d = dict(foo=1, bar=3, spam=7, baz=8, x=9) out = {} foo, obj.thing.spam, obj.bar, out['baz'] = unpack_keys(d) self.assertEqual(foo, d['foo']) self.assertEqual(obj.bar, d['bar']) self.assertEqual(obj.thing.spam, d['spam']) self.assertEqual(out, {'baz': d['baz']})
def test_unpack_keys_for_loop(self): results = [] for x, y in unpack_keys([ dict(x=1, y=2), dict(x=3, z=4), dict(a=5, y=6), dict(b=7, c=8), ], default=999): results.append((x, y)) self.assertEqual(results, [ (1, 2), (3, 999), (999, 6), (999, 999), ])
def test_unpack_keys_skip_single_assigned_name(self): x, y = [int(v) for v in unpack_keys(dict(x='1', y='2'))] self.assertEqual(x, 1) self.assertEqual(y, 2)
def test_unpack_keys_bigger_expression(self): x, y = map(int, unpack_keys(dict(x='1', y='2'))) self.assertEqual(x, 1) self.assertEqual(y, 2)
def test_no_assignment(self): with self.assertRaises(TypeError): unpack_keys(dict(x=1, y=2))