test_basic.py (2356B)
1 # standard imports 2 import copy 3 import unittest 4 5 # local imports 6 from potaahto.symbols import snake_and_camel 7 from potaahto.symbols import ensure_key 8 from potaahto.symbols import mimic_key 9 10 11 class TestFauna(unittest.TestCase): 12 13 def test_snake_and_camel(self): 14 k_stupid = { 15 'fooBarBaz': 1, 16 'barBazFoo': 2, 17 'baz_foo_bar': 3, 18 } 19 20 k = snake_and_camel(k_stupid) 21 22 self.assertEqual(k['fooBarBaz'], 1) 23 self.assertEqual(k['barBazFoo'], 2) 24 self.assertEqual(k['bazFooBar'], 3) 25 26 self.assertEqual(k['foo_bar_baz'], 1) 27 self.assertEqual(k['bar_baz_foo'], 2) 28 self.assertEqual(k['baz_foo_bar'], 3) 29 30 31 def test_ensure(self): 32 k_stupid = { 33 'fooBarBaz': 1, 34 'barBazFoo': 2, 35 } 36 37 k = copy.copy(k_stupid) 38 k = ensure_key(k, 'barBazFoo', 42) 39 self.assertDictEqual(k_stupid, k) 40 self.assertEqual(k['barBazFoo'], 2) 41 42 k = copy.copy(k_stupid) 43 ensure_key(k, 'barBazFoo', 42) 44 self.assertEqual(k['barBazFoo'], 2) 45 46 k = copy.copy(k_stupid) 47 k = ensure_key(k, 'bazFooBar', 42) 48 try: 49 self.assertDictEqual(k_stupid, k) 50 except AssertionError: 51 self.assertEqual(k['bazFooBar'], 42) 52 53 k = copy.copy(k_stupid) 54 k = ensure_key(k, 'baz_foo_bar', 42) 55 self.assertEqual(k['baz_foo_bar'], 42) 56 57 k = copy.copy(k_stupid) 58 k = ensure_key(k, 'foo_bar_baz', 42) 59 self.assertEqual(k['fooBarBaz'], 1) 60 61 k = copy.copy(k_stupid) 62 ensure_key(k, 'baz_foo_bar', 42) 63 self.assertEqual(k['baz_foo_bar'], 42) 64 65 66 def test_mimic(self): 67 k_stupid = { 68 'fooBarBaz': 1, 69 'barBazFoo': 2, 70 } 71 72 k = copy.copy(k_stupid) 73 k = mimic_key(k, 'barBazFoo', 'xyzzy', 42) 74 self.assertEqual(k['xyzzy'], 2) 75 76 k = copy.copy(k_stupid) 77 k = mimic_key(k, 'baz_baz_baz', 'inkyPinky') 78 self.assertNotIn('baz_baz_baz', k.keys()) 79 self.assertNotIn('inkyPinky', k.keys()) 80 81 k = copy.copy(k_stupid) 82 k = mimic_key(k, 'baz_baz_baz', 'inkyPinky', default_value=42) 83 self.assertEqual(k['baz_baz_baz'], 42) 84 self.assertEqual(k['inkyPinky'], 42) 85 86 87 if __name__ == '__main__': 88 unittest.main()