def test_with_deferrable(self):
        """Testing serialize_to_python with Deferrable enum value"""
        if Deferrable is None:
            raise SkipTest('Not supported on this version of Django')

        self.assertEqual(
            serialize_to_python(Deferrable.DEFERRED),
            'models.Deferrable.DEFERRED')
 def test_with_deconstructable_obj(self):
     """Testing serialize_to_python with deconstructable object"""
     self.assertEqual(
         serialize_to_python(MyDeconstructableObject(1, 2, 3,
                                                     kwarg1='value1',
                                                     kwarg2='value2')),
         "MyDeconstructableObject(1, 2, 3, kwarg1='value1',"
         " kwarg2='value2')")
    def test_with_q(self):
        """Testing serialize_to_python with Q"""
        q = (~Q(field1=True) & (Q(field2='test') | Q(field3__gte=1)))

        self.assertEqual(
            serialize_to_python(q),
            "(~models.Q(field1=True) & "
            "(models.Q(field2='test') | "
            "models.Q(field3__gte=1)))")
    def test_with_ordered_dict(self):
        """Testing serialize_to_python with dict"""
        d = OrderedDict()
        d['a'] = 'value1'
        d['c'] = [1, 2, 3]
        d['b'] = None

        self.assertEqual(serialize_to_python(d),
                         "{'a': 'value1', 'c': [1, 2, 3], 'b': None}")
    def test_with_combined_expression(self):
        """Testing serialize_to_python 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_python(value),
                         "models.F('a') + models.Value(1)")
 def test_with_dict(self):
     """Testing serialize_to_python with dict"""
     self.assertEqual(
         serialize_to_python({
             'a': 'value1',
             'c': [1, 2, 3],
             'b': {
                 'nested': None,
             },
         }),
         "{'a': 'value1', 'b': {'nested': None}, 'c': [1, 2, 3]}")
Exemple #7
0
    def serialize_value(self, value):
        """Serialize a value for use in a mutation statement.

        This will attempt to represent the value as something Python can
        execute, across Python versions. The string representation of the
        value is used by default.

        See :py:func:`django_evolution.serialization.serialize_to_python`
        for details.

        Args:
            value (object):
                The value to serialize.

        Returns:
            unicode:
            The serialized string.
        """
        return serialize_to_python(value)
 def test_with_set(self):
     """Testing serialize_to_python with set"""
     self.assertEqual(serialize_to_python({1, 3, 2}),
                      "{1, 2, 3}")
 def test_with_q_empty(self):
     """Testing serialize_to_python with empty Q"""
     self.assertEqual(serialize_to_python(Q()), 'models.Q()')
 def test_with_placeholder(self):
     """Testing serialize_to_python with BasePlaceholder subclass"""
     self.assertEqual(serialize_to_python(NullFieldInitialCallback()),
                      '<<USER VALUE REQUIRED>>')
 def test_with_bool(self):
     """Testing serialize_to_python with bool"""
     self.assertEqual(serialize_to_python(True), 'True')
 def test_with_type_django_model_path(self):
     """Testing serialize_to_python with type containing django.db.models
     module path
     """
     self.assertEqual(serialize_to_python(CharField), 'models.CharField')
 def test_with_list(self):
     """Testing serialize_to_python with list"""
     self.assertEqual(serialize_to_python([1, 2, 'foo']),
                      "[1, 2, 'foo']")
 def test_with_int(self):
     """Testing serialize_to_python with int"""
     self.assertEqual(serialize_to_python(123), '123')
 def test_with_float(self):
     """Testing serialize_to_python with float"""
     self.assertEqual(serialize_to_python(1.23), '1.23')
 def test_with_tuple(self):
     """Testing serialize_to_python with tuple"""
     self.assertEqual(serialize_to_python((1, True, 'foo')),
                      "(1, True, 'foo')")
 def test_with_type(self):
     """Testing serialize_to_python with type"""
     self.assertEqual(serialize_to_python(OrderedDict), 'OrderedDict')
    def test_with_long(self):
        """Testing serialize_to_python with long"""
        if not six.PY2:
            raise SkipTest('Applicable on Python 2 only.')

        self.assertEqual(serialize_to_python(long(123)), '123L')
 def test_with_unicode_str(self):
     """Testing serialize_to_python with unicode string"""
     self.assertEqual(serialize_to_python('tést'), "'tést'")
 def test_with_none(self):
     """Testing serialize_to_python with None"""
     self.assertEqual(serialize_to_python(None), 'None')