This repository has been archived on 2022-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
pyurbantz/urbantz/tests/test_utils.py

83 lines
2.6 KiB
Python

from unittest import TestCase
from urbantz.utils import to_camel_case, DictObject
class TestUtils(TestCase):
test_data = {
'first_name': 'Chuck',
'lastName': 'Norris',
'nested_dict': {
'key': 'value',
},
'nested_list': ['a', 'b', 'c'],
}
def test_to_camel_case(self):
self.assertEqual(to_camel_case('abcdef'), 'abcdef')
self.assertEqual(to_camel_case('some_thing'), 'someThing')
self.assertEqual(to_camel_case('a_b_c_d_e_f'), 'aBCDEF')
self.assertEqual(to_camel_case('hEllO_wOrLd'), 'hEllOWOrLd')
def test_dictobject_is_dict(self):
"""
Test the DictObject at least acts like a dict
"""
d = DictObject(self.test_data)
self.assertIsInstance(d, dict)
self.assertTrue(d)
# Dict comparison
self.assertEqual(d, self.test_data)
self.assertEqual(dict(d), d)
# Get, set, delete items
self.assertEqual(d['first_name'], 'Chuck')
self.assertIn('first_name', d)
del d['first_name']
self.assertNotIn('first_name', d)
d['first_name'] = 'Clutch'
self.assertEqual(d['first_name'], 'Clutch')
self.assertListEqual(
list(d.keys()),
['lastName', 'nested_dict', 'nested_list', 'first_name'],
)
self.assertListEqual(
list(d.values()),
['Norris', {'key': 'value'}, ['a', 'b', 'c'], 'Clutch'],
)
self.assertListEqual(list(d.items()), [
('lastName', 'Norris'),
('nested_dict', {'key': 'value'}),
('nested_list', ['a', 'b', 'c']),
('first_name', 'Clutch'),
])
def test_dictobject_attributes(self):
"""
Test DictObject translates attributes to items
"""
d = DictObject(self.test_data)
self.assertEqual(d.first_name, 'Chuck')
self.assertEqual(d.lastName, 'Norris')
del d.first_name
self.assertNotIn('first_name', d)
with self.assertRaises(AttributeError):
d.nope
def test_dictobject_camel_case(self):
"""
Test DictObject turns snake_cased attribute or item names into
camelCased names and tries again when not found
"""
d = DictObject(self.test_data)
self.assertEqual(d['lastName'], 'Norris')
self.assertEqual(d['last_name'], 'Norris')
self.assertEqual(d.lastName, 'Norris')
self.assertEqual(d.last_name, 'Norris')
self.assertIn('lastName', d)
self.assertIn('last_name', d)
del d.last_name
self.assertNotIn('lastName', d)