def test_with_q(self): """Testing serialize_to_signature with Q""" q = (~Q(field1=True) & (Q(field2='test') | Q(field3__gte=1))) self.assertEqual( serialize_to_signature(q), { '_deconstructed': True, 'args': [ { '_deconstructed': True, 'args': [ ('field1', True), ], 'kwargs': { '_negated': True, }, 'type': 'django.db.models.Q', }, { '_deconstructed': True, 'args': [ ('field2', 'test'), ('field3__gte', 1), ], 'kwargs': { '_connector': 'OR', }, 'type': 'django.db.models.Q' }, ], 'kwargs': {}, 'type': 'django.db.models.Q', })
def test_with_combined_expression(self): """Testing serialize_to_signature with CombinedExpression""" if not can_test_combined_expressions: raise SkipTest('Not supported on this version of Django') value = F('a') + 1 self.assertIsInstance(value, CombinedExpression) self.assertEqual( serialize_to_signature(value), { '_deconstructed': True, 'args': ( { '_deconstructed': True, 'args': ('a',), 'kwargs': {}, 'type': 'django.db.models.expressions.F', }, '+', { '_deconstructed': True, 'args': (1,), 'kwargs': {}, 'type': 'django.db.models.expressions.Value', }, ), 'kwargs': {}, 'type': 'django.db.models.expressions.CombinedExpression', })
def test_with_q_empty(self): """Testing serialize_to_signature with empty Q""" self.assertEqual( serialize_to_signature(Q()), { '_deconstructed': True, 'args': [], 'kwargs': {}, 'type': 'django.db.models.Q', })
def test_with_deferrable(self): """Testing serialize_to_signature with Deferrable enum value""" if Deferrable is None: raise SkipTest('Not supported on this version of Django') self.assertEqual( serialize_to_signature(Deferrable.DEFERRED), { '_enum': True, 'type': 'django.db.models.constraints.Deferrable', 'value': 'DEFERRED', })
def test_with_ordered_dict(self): """Testing serialize_to_signature with dict""" d = OrderedDict() d['a'] = 'value1' d['c'] = [1, 2, 3] d['b'] = None result = serialize_to_signature(d) self.assertIsInstance(result, dict) self.assertEqual( result, { 'a': 'value1', 'c': [1, 2, 3], 'b': None, })
def test_with_deconstructable_obj(self): """Testing serialize_to_signature with deconstructable object""" self.assertEqual( serialize_to_signature(MyDeconstructableObject(1, 2, 3, kwarg1='value1', kwarg2='value2')), { '_deconstructed': True, 'args': (1, 2, 3), 'kwargs': { 'kwarg1': 'value1', 'kwarg2': 'value2', }, 'type': ('django_evolution.tests.test_serialization.' 'MyDeconstructableObject'), })
def test_with_dict(self): """Testing serialize_to_signature with dict""" self.assertEqual( serialize_to_signature({ 'a': 'value1', 'c': [1, 2, 3], 'b': { 'nested': None, }, }), { 'a': 'value1', 'c': [1, 2, 3], 'b': { 'nested': None, }, })
def test_with_placeholder(self): """Testing serialize_to_signature with BasePlaceholder subclass""" with self.assertRaises(NotImplementedError): serialize_to_signature(NullFieldInitialCallback())
def test_with_none(self): """Testing serialize_to_signature with None""" self.assertIsNone(serialize_to_signature(None))
def test_with_byte_str(self): """Testing serialize_to_signature with byte string""" self.assertEqual(serialize_to_signature('test 🧸'.encode('utf-8')), 'test \U0001f9f8')
def test_with_type(self): """Testing serialize_to_signature with type""" with self.assertRaises(NotImplementedError): serialize_to_signature(CharField)
def test_with_int(self): """Testing serialize_to_signature with int""" self.assertEqual(serialize_to_signature(123), 123)
def test_with_float(self): """Testing serialize_to_signature with float""" self.assertEqual(serialize_to_signature(1.23), 1.23)
def test_with_set(self): """Testing serialize_to_signature with set""" self.assertEqual(serialize_to_signature({1, 3, 2}), {1, 2, 3})
def test_with_list(self): """Testing serialize_to_signature with list""" self.assertEqual(serialize_to_signature([1, 2, 'foo']), [1, 2, 'foo'])
def test_with_tuple(self): """Testing serialize_to_signature with tuple""" self.assertEqual(serialize_to_signature((1, True, 'foo')), (1, True, 'foo'))
def test_with_long(self): """Testing serialize_to_signature with long""" if not six.PY2: raise SkipTest('Applicable on Python 2 only.') self.assertEqual(serialize_to_signature(long(123)), long(123))
def test_with_unicode_str(self): """Testing serialize_to_signature with unicode string""" self.assertEqual(serialize_to_signature('test 🧸'), 'test 🧸')
def test_with_bool(self): """Testing serialize_to_signature with bool""" self.assertIs(serialize_to_signature(True), True)