예제 #1
0
파일: tests.py 프로젝트: Zemanta/typed
	def test_format_dict(self):
		t1 = typed.dict({
				'a': typed.int.format({1: 'one', 2: 'two'}),
				'b': typed.bool.format({True: 1, False: 0}),
			})

		self.assertEqual(t1.load({'a': 1, 'b': True}), {'a': 1, 'b': True})
		self.assertEqual(t1.load({'a': 'one', 'b': 1}), {'a': 1, 'b': True})
		self.assertEqual(t1.load({'a': 'two', 'b': 0}), {'a': 2, 'b': False})
		self.assertEqual(t1.load({'a': 3, 'b': True}), {'a': 3, 'b': True})

		self.assertEqual(t1.save({'a': 1, 'b': True}), {'a': 'one', 'b': 1})
		self.assertEqual(t1.save({'a': 2, 'b': False}), {'a': 'two', 'b': 0})
		self.assertEqual(t1.save({'a': 3, 'b': True}), {'a': 3, 'b': 1})

		self.assertRaises(ValueError, t1.load, {'a': 'three'})

		t2 = (typed.int | typed.null).format({1: 'one', None: 'nothing'})

		self.assertEqual(t2.load('one'), 1)
		self.assertEqual(t2.load(1), 1)
		self.assertEqual(t2.load(2), 2)
		self.assertEqual(t2.load('nothing'), None)

		self.assertEqual(t2.save(1), 'one')
		self.assertEqual(t2.save(None), 'nothing')
		self.assertEqual(t2.save(2), 2)

		t3 = typed.bool.format({True: 1, False: 0}) | typed.int.format({0: 'zero', 1: 'one'})

		self.assertEqual(t3.load('one'), 1)
		self.assertEqual(t3.load('zero'), 0)
		self.assertEqual(t3.load(3), 3)
		self.assertEqual(t3.load(0), False)
		self.assertEqual(t3.load(1), True)

		self.assertEqual(t3.save(1), 'one')
		self.assertEqual(t3.save(2), 2)
		self.assertEqual(t3.save(0), 'zero')
		self.assertEqual(t3.save(False), 0)
		self.assertEqual(t3.save(True), 1)

		t4 = typed.dict({
				'a': typed.int.optional.format({1: 'one', 2: 'two'}),
				'b': typed.bool.default(False).format({False: 'wrong', True: 'right'}),
				'c': typed.float.format({1.0: '100%', 0.0: '0%'}).default(0.0),
				'd': (typed.bool | typed.none).format({None: 'nothing'}).optional,
				'e': typed.datetime.format('%Y-%m-%d %H:%M:%S').format({'1970-01-01 00:00:00': 'forever'}).optional,
			})

		self.assertEqual(t4.load({}), {'b': False, 'c': 0.0})
		self.assertEqual(t4.load({'a': 3, 'b': True, 'c': 0.5, 'd': None, 'e': '2013-10-11 11:02:45'}), {'a': 3, 'b': True, 'c': 0.5, 'd': None, 'e': datetime.datetime(2013, 10, 11, 11, 02, 45)})
		self.assertEqual(t4.load({'a': 'one', 'b': 'right', 'c': '0%', 'd': 'nothing', 'e': 'forever'}), {'a': 1, 'b': True, 'c': 0.0, 'd': None, 'e': datetime.datetime(1970, 01, 01, 00, 00, 00)})

		self.assertEqual(t4.save({}), {})
		self.assertEqual(t4.save({'a': 2, 'b': True, 'c': 1.0, 'd': True, 'e': datetime.datetime(2014, 12, 15, 16, 10, 32)}), {'a': 'two', 'b': 'right', 'c': '100%', 'd': True, 'e': '2014-12-15 16:10:32'})
		self.assertEqual(t4.save({'a': 3, 'b': False, 'c': 0.0, 'd': None, 'e': datetime.datetime(1970, 01, 01, 00, 00, 00)}), {'a': 3, 'd': 'nothing', 'e': 'forever'})
예제 #2
0
파일: tests.py 프로젝트: Zemanta/typed
	def test_trimmed_dict(self):
		t = typed.dict({
				'a': typed.string,
				'b': typed.int.optional,
				'c': typed.bool.default(False)
			}).trimmed


		self.assertEqual(t.load({'a': ''}), {'a': '', 'c': False})
		self.assertEqual(t.load({'a': '', 'b': 2}), {'a': '', 'b': 2, 'c': False})
		self.assertEqual(t.load({'a': '', 'c': True}), {'a': '', 'c': True})
		self.assertEqual(t.load({'a': '', 'b': 2, 'c': True}), {'a': '', 'b': 2, 'c': True})

		self.assertEqual(t.load({'a': '', 'd': 1, 'e': 0}), {'a': '', 'c': False})
		self.assertEqual(t.load({'a': '', 'b': 2, 'd': 1, 'e': 0, 'f': -1}), {'a': '', 'b': 2, 'c': False})
		self.assertEqual(t.load({'a': '', 'c': True, 'd': 1, 'e': 0, 'f': -1}), {'a': '', 'c': True})
		self.assertEqual(t.load({'a': '', 'b': 2, 'c': True, 'd': 1, 'e': 0, 'f': -1}), {'a': '', 'b': 2, 'c': True})

		self.assertEqual(t.save({'a': ''}), {'a': ''})
		self.assertEqual(t.save({'a': '', 'c': False}), {'a': ''})
		self.assertEqual(t.save({'a': '', 'c': True}), {'a': '', 'c': True})
		self.assertEqual(t.save({'a': '', 'b': 2}), {'a': '', 'b': 2})
		self.assertEqual(t.save({'a': '', 'b': 2, 'c': True}), {'a': '', 'b': 2, 'c': True})

		self.assertEqual(t.save({'a': '', 'd': 1, 'e': 0}), {'a': ''})
		self.assertEqual(t.save({'a': '', 'c': False, 'd': 1, 'e': 0, 'f': -1}), {'a': ''})
		self.assertEqual(t.save({'a': '', 'b': 2, 'd': 1, 'e': 0, 'f': -1}), {'a': '', 'b': 2})
		self.assertEqual(t.save({'a': '', 'b': 2, 'c': False, 'd': 1, 'e': 0, 'f': -1}), {'a': '', 'b': 2})
		self.assertEqual(t.save({'a': '', 'b': 2, 'c': True, 'd': 1, 'e': 0, 'f': -1}), {'a': '', 'b': 2, 'c': True})

		self.assertRaises(ValueError, t.load, {})
		self.assertRaises(ValueError, t.load, {'a': '', 'b': 'foo'})
		self.assertRaises(ValueError, t.load, {'a': '', 'c': 2})
예제 #3
0
파일: tests.py 프로젝트: Zemanta/typed
	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}})
예제 #4
0
파일: tests.py 프로젝트: Zemanta/typed
	def test_trimmed_dict(self):
		t = typed.dict({'a': typed.int, 'b': typed.bool}).trimmed

		self.assertTrue(t.test({'a': 1, 'b': True}))
		self.assertTrue(t.test({'a': 1, 'b': True, 'c': "string"}))
		self.assertTrue(t.test({'a': 1, 'b': True, 'c': 0, 'd': 0, 'e': 0, 'f': 0}))

		self.assertFalse(t.test({}))
		self.assertFalse(t.test({'a': 1, 'b': 0.01}))
		self.assertFalse(t.test({'a': 1}))
		self.assertFalse(t.test({'a': 1, 'c': 0, 'd': 0, 'e': 0, 'f': 0}))
예제 #5
0
파일: tests.py 프로젝트: Zemanta/typed
	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'}))
예제 #6
0
파일: tests.py 프로젝트: Zemanta/typed
	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'}))
예제 #7
0
파일: tests.py 프로젝트: Zemanta/typed
	def test_dict(self):
		datetime_format = '%Y-%m-%d %H:%M:%S'
		dt = datetime.datetime(2013, 9, 21, 11, 42, 33)
		dt_str = dt.strftime(datetime_format)

		t = typed.dict({
				'a': typed.string,
				'b': typed.int.optional,
				'c': typed.bool.default(False),
				'd': typed.set(0, 1).default(0),
				'e': typed.datetime.format(datetime_format).optional,
				'f': typed.optional,
				'g': typed.default(None),
				'h': (typed.int | typed.none).default(None)
			})

		self.assertEqual(t.load({'a': ''}), {'a': '', 'c': False, 'd': 0, 'g': None, 'h': None})
		self.assertEqual(t.load({'a': '', 'f': 1, 'g': 'foo'}), {'a': '', 'c': False, 'd': 0, 'f': 1, 'g': 'foo', 'h': None})
		self.assertEqual(t.load({'a': '', 'f': True, 'g': [1, 2, 3]}), {'a': '', 'c': False, 'd': 0, 'f': True, 'g': [1, 2, 3], 'h': None})
		self.assertEqual(t.load({'a': '', 'b': 2, 'c': True, 'd': 1}), {'a': '', 'b': 2, 'c': True, 'd': 1, 'g': None, 'h': None})
		self.assertEqual(t.load({'a': '', 'e': dt_str, 'h': 1}), {'a': '', 'c': False, 'd': 0, 'e': dt, 'g': None, 'h': 1})
		self.assertEqual(t.load({'a': '', 'h': None}), {'a': '', 'c': False, 'd': 0, 'g': None, 'h': None})

		self.assertEqual(t.save({'a': ''}), {'a': ''})
		self.assertEqual(t.save({'a': '', 'd': 0, 'h': None}), {'a': ''})
		self.assertEqual(t.save({'a': '', 'c': False, 'd': 0, 'g': None, 'h': None}), {'a': ''})
		self.assertEqual(t.save({'a': '', 'c': False, 'd': 0, 'f': 1, 'g': [1, 2, 3], 'h': None}), {'a': '', 'f': 1, 'g': [1, 2, 3]})
		self.assertEqual(t.save({'a': '', 'c': False, 'd': 0, 'f': True, 'g': None, 'h': None}), {'a': '', 'f': True})
		self.assertEqual(t.save({'a': '', 'b': 2, 'c': True, 'd': 1, 'g': None, 'h': None}), {'a': '', 'b': 2, 'c': True, 'd': 1})
		self.assertEqual(t.save({'a': '', 'c': False, 'd': 0, 'e': dt, 'g': None, 'h': 1}), {'a': '', 'e': dt_str, 'h': 1})

		self.assertRaises(ValueError, t.load, {})
		self.assertRaises(ValueError, t.load, {'a': '', 'b': 'foo'})
		self.assertRaises(ValueError, t.load, {'a': '', 'd': 2})
		self.assertRaises(ValueError, t.load, {'a': '', 'e': '2012-12-12T12:12:12'})
		self.assertRaises(ValueError, t.load, {'a': '', 'h': 'foo'})

		self.assertRaises(ValueError, t.save, {})
		self.assertRaises(ValueError, t.save, {'a': '', 'b': 'foo'})
		self.assertRaises(ValueError, t.save, {'a': '', 'd': 2})
		self.assertRaises(ValueError, t.save, {'a': '', 'e': '2012-12-12T12:12:12'})
		self.assertRaises(ValueError, t.save, {'a': '', 'h': 'foo'})
		self.assertRaises(ValueError, t.save, {'a': '', 'c': False, 'd': 0, 'g': None, 'h': 1, 't': 0, 'u': 0, 'v': 0})
예제 #8
0
파일: tests.py 프로젝트: Zemanta/typed
			}).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'))
		self.assertEqual(t1.load(tuple([4, '2013-10-11 11:02:45', None])), (4, datetime.datetime(2013, 10, 11, 11, 02, 45), None))