def test_optional(self): t = typed.dict({'a': typed.int, 'b': typed.bool.optional, 'c': typed.list(typed.string).optional}) self.assertTrue(t.test({'a': 1, 'b': True, 'c': ['X', 'Y']})) self.assertTrue(t.test({'a': 1})) self.assertTrue(t.test({'a': 1, 'b': True})) self.assertTrue(t.test({'a': 1, 'c': ['X', 'Y']})) self.assertFalse(t.test({})) self.assertFalse(t.test({'a': 1, 'b': 0.01})) self.assertFalse(t.test({'a': 1, 'b': True, 'c': 'foo'})) self.assertFalse(t.test({'a': 1, 'b': True, 'c': [1, 2, 3]})) self.assertFalse(t.test({'a': 1, 'b': True, 'c': ['X', 'Y'], 'd': 'bar'}))
def test_list(self): t1 = typed.list(typed.int) self.assertEqual(t1.load([1, 2, 3]), [1, 2, 3]) self.assertEqual(t1.save([1, 2, 3]), [1, 2, 3]) self.assertRaises(ValueError, t1.load, [1, '2', None]) self.assertRaises(ValueError, t1.save, [1, '2', None]) for value in [datetime.datetime.now(), 1, 'a', True, [1.2, 2.3, 3.4], 1.2, None, {'a': 1}, self]: self.assertRaises(ValueError, t1.load, value) self.assertRaises(ValueError, t1.save, value) t2 = typed.list(typed.int | typed.string) self.assertEqual(t2.load([1, 'a', 2, 'b']), [1, 'a', 2, 'b']) self.assertEqual(t2.save([1, 'a', 2, 'b']), [1, 'a', 2, 'b']) t3 = typed.list(typed.int) | typed.list(typed.string) self.assertRaises(ValueError, t3.load, [1, 'a', 2, 'b']) self.assertRaises(ValueError, t3.save, [1, 'a', 2, 'b']) self.assertEqual(t3.load([1, 2, 3]), [1, 2, 3]) self.assertEqual(t3.save([1, 2, 3]), [1, 2, 3]) self.assertEqual(t3.load(['a', 'b', 'c']), ['a', 'b', 'c']) self.assertEqual(t3.save(['a', 'b', 'c']), ['a', 'b', 'c']) datetime_format = '%Y-%m-%d %H:%M:%S' dt_list = [datetime.datetime(2013, 9, 21, 11, 42, 33), datetime.datetime(2012, 3, 24, 9, 6, 13)] dt_str_list = map(lambda dt: dt.strftime(datetime_format), dt_list) t4 = typed.list(typed.datetime.format(datetime_format)) self.assertEqual(t4.load(list(dt_str_list)), dt_list) self.assertEqual(t4.save(list(dt_list)), dt_str_list)
def test_dict(self): self.assertRaises(TypeError, typed.dict, 1) self.assertRaises(TypeError, typed.dict, {'a', 1}) self.assertRaises(TypeError, typed.dict, {'a', int}) t = typed.dict({'a': typed.int, 'b': typed.bool, 'c': typed.list(typed.string)}) self.assertTrue(t.test({'a': 1, 'b': True, 'c': ['X', 'Y']})) self.assertFalse(t.test({})) self.assertFalse(t.test({'a': 1})) self.assertFalse(t.test({'a': 1, 'b': True})) self.assertFalse(t.test({'a': 1, 'b': True, 'c': 'foo'})) self.assertFalse(t.test({'a': 1, 'b': True, 'c': [1, 2, 3]})) self.assertFalse(t.test({'a': 1, 'b': True, 'c': ['X', 'Y'], 'd': 'bar'}))
def test_list(self): self.assertRaises(TypeError, typed.list, int) t1 = typed.list(typed.int) self.assertTrue(isinstance(t1, typed.ListType)) self.assertTrue(t1.test([1, 2, 3])) self.assertTrue(t1.test([])) self.assertFalse(t1.test([1, '2', None])) self.assertFalse(t1.test(tuple([1, 2, 3]))) for value in [datetime.datetime.now(), 1, 'a', True, [1.2, 2.3, 3.4], 1.2, None, {'a': 1}, self]: self.assertFalse(t1.test(value)) t2 = typed.list(typed.int | typed.string) self.assertTrue(t2.test([1, 'a', 2, 'b'])) t3 = typed.list(typed.int) | typed.list(typed.string) self.assertFalse(t3.test([1, 'a', 2, 'b'])) self.assertTrue(t3.test([1, 2, 3])) self.assertTrue(t3.test(['a', 'b', 'c']))
def test_format_json(self): t1 = typed.dict({ 'a': typed.datetime.format('%Y-%m-%d %H:%M:%S').optional, 'b': (typed.string | typed.none).optional, 'c': typed.bool.format({True: 1, False: 0}).default(False), }) t2 = typed.dict({ 'd': typed.int, 'e': (typed.none | typed.date.format('%d/%m/%Y')).default(None), 'f': typed.list(typed.int), 'g': typed.json(t1).default(t1.load({})), }).format(typed.json) self.assertEqual(t2.load(json.dumps({'d': 1, 'f': []})), {'d': 1, 'e': None, 'f': [], 'g': {'c': False}}) self.assertEqual(t2.load(json.dumps({'d': 0, 'e': None, 'f': [1, 2], 'g': json.dumps({'b': None, 'c': 1})})), {'d': 0, 'e': None, 'f': [1, 2], 'g': {'b': None, 'c': True}}) self.assertEqual(t2.load(json.dumps({'d': 1, 'e': '15/02/2011', 'f': [], 'g': json.dumps({'a': '2013-11-11 16:16:16', 'b': 'foo', 'c': 0})})), {'d': 1, 'e': datetime.date(2011, 02, 15), 'f': [], 'g': {'a': datetime.datetime(2013, 11, 11, 16, 16, 16), 'b': 'foo', 'c': False}})
}).format(typed.json) self.assertEqual(t2.load(json.dumps({'d': 1, 'f': []})), {'d': 1, 'e': None, 'f': [], 'g': {'c': False}}) self.assertEqual(t2.load(json.dumps({'d': 0, 'e': None, 'f': [1, 2], 'g': json.dumps({'b': None, 'c': 1})})), {'d': 0, 'e': None, 'f': [1, 2], 'g': {'b': None, 'c': True}}) self.assertEqual(t2.load(json.dumps({'d': 1, 'e': '15/02/2011', 'f': [], 'g': json.dumps({'a': '2013-11-11 16:16:16', 'b': 'foo', 'c': 0})})), {'d': 1, 'e': datetime.date(2011, 02, 15), 'f': [], 'g': {'a': datetime.datetime(2013, 11, 11, 16, 16, 16), 'b': 'foo', 'c': False}}) self.assertEqual(t2.save({'d': 1, 'f': [1, 2, 3, 4]}), json.dumps({'d': 1, 'f': [1, 2, 3, 4]})) self.assertEqual(t2.save({'d': 1, 'e': None, 'f': [1, 2, 3, 4], 'g': {'c': False}}), json.dumps({'d': 1, 'f': [1, 2, 3, 4]})) self.assertEqual(t2.save({'d': 1, 'e': datetime.date(2013, 02, 15), 'f': [], 'g': {'a': datetime.datetime(2013, 10, 10, 05, 55, 50), 'b': None, 'c': True}}), json.dumps({'d': 1, 'e': '15/02/2013', 'f': [], 'g': json.dumps({'a': '2013-10-10 05:55:50', 'b': None, 'c': 1})})) if HAS_UJSON: t3 = typed.json(typed.dict({ 'a': typed.int, 'b': typed.float, 'c': typed.list(typed.float), 'd': typed.dict({ 'e': typed.list(typed.float), }), }), double_precision=4) obj = {'a': 1, 'b': 0.0123456789, 'c': [0.00001, 0.00011, 0.00026], 'd': {'e': [10000.098765432, 123456.01001]}} obj_json = '{"a": 1, "b": 0.0123, "c": [0.0, 0.0001, 0.0003], "d": {"e": [10000.0988, 123456.01]}}' self.assertItemsEqual(t3.save(obj)[2:-1].split(',"'), obj_json.replace(' ', '')[2:-1].split(',"')) # elements might come in different order def test_tuple(self): t1 = typed.tuple(typed.int, typed.datetime.format('%Y-%m-%d %H:%M:%S'), typed.string | typed.none) self.assertEqual(t1.load(tuple([4, '2013-10-11 11:02:45', 'foo'])), (4, datetime.datetime(2013, 10, 11, 11, 02, 45), 'foo'))