Ticket #19625: django19625-master.mysql-decimalfield-lookup-fail.tests.patch

File django19625-master.mysql-decimalfield-lookup-fail.tests.patch, 4.8 KB (added by wdoekes, 3 years ago)

Tests.

  • models.py

    diff -uNrw null/models.py test_long_decimal/models.py
    old new  
     1# -*- coding: utf-8 -*-
     2"""
     312. Using a decimal field in lookups
     4
     5MySQL does not convert strings to Decimals that well. Make sure inserting
     6and filtering works properly.
     7"""
     8
     9from __future__ import absolute_import
     10
     11from django.db import models
     12
     13try:
     14    from django.utils.encoding import python_2_unicode_compatible
     15except ImportError:
     16    python_2_unicode_compatible = None
     17
     18
     19class Book(models.Model):
     20    isbn = models.DecimalField(max_digits=31, decimal_places=0, db_index=True)
     21    name = models.CharField(max_length=63)
     22
     23    def __str__(self):
     24        return "%s %s" % (self.isbn, self.name)
     25
     26
     27if python_2_unicode_compatible:
     28    Book = python_2_unicode_compatible(Book)
  • tests.py

    diff -uNrw null/tests.py test_long_decimal/tests.py
    old new  
     1# vim: set ts=8 sw=4 sts=4 et ai:
     2from __future__ import absolute_import, unicode_literals
     3
     4from decimal import Decimal
     5from unittest import TestCase
     6
     7from .models import Book
     8
     9"""
     10Note that this problem is fixed in MySQL somewhere between
     11version 5.1.66 and 5.5.28.
     12
     13Test with:
     14
     15create table abc (value decimal(31,0));
     16insert into abc values (1234567890123456789012345678901);
     17select * from abc where value = 1234567890123456789012345678901;
     18select * from abc where value = '1234567890123456789012345678901';
     19drop table abc;
     20
     21In the broken case, you won't get a result for the second query.
     22"""
     23
     24class DecimalTests(TestCase):
     25    def help_decimal_field(self, elements, iterable):
     26        try:
     27            self.help_decimal_field_setup(elements, iterable)
     28            self.help_decimal_field_test(elements, iterable)
     29        finally:
     30            Book.objects.all().delete()
     31
     32    def help_decimal_field_values(self, elements, size):
     33        value = ''.join([elements[(i % len(elements))] for i in range(size)])
     34        isbn = Decimal(value)
     35        name = 'ISBN:%s' % (value,)
     36        return isbn, name
     37
     38    def help_decimal_field_setup(self, elements, iterable):
     39        for size in iterable:
     40            isbn, name = self.help_decimal_field_values(elements, size)
     41            Book.objects.create(isbn=isbn, name=name)
     42
     43    def help_decimal_field_test(self, elements, iterable):
     44        for size in iterable:
     45            isbn, name = self.help_decimal_field_values(elements, size)
     46            try:
     47                book = Book.objects.get(isbn=isbn)
     48            except Book.DoesNotExist:
     49                self.assertFalse(True, 'book with isbn %s was not found' % (
     50                                 isbn,))
     51            except:
     52                # Certain fixes can produce an unexpected MySQL Warning..
     53                import sys
     54                print >>sys.stderr, '( failure when getting', isbn, ')'
     55                raise
     56            else:
     57                self.assertEqual(book.isbn, isbn)
     58                self.assertEqual(book.name, name)
     59
     60    def test_baseline(self):
     61        """
     62        This should always work. The point was that larger decimals get
     63        cast to a lossy float. For small values, there is no loss.
     64        """
     65        try:
     66            Book.objects.create(isbn=Decimal('987'), name='baseline')
     67            baseline = Book.objects.get(isbn=Decimal('987'))
     68            self.assertEquals(baseline.name, 'baseline')
     69        finally:
     70            Book.objects.all().delete()
     71
     72    def test_decimal_field_works1(self):
     73        """
     74        For some reason, the test doesn't fail when we iterate with this
     75        test from 1 to 31.
     76        """
     77        self.help_decimal_field('1234567890', range(1, 32))
     78
     79    def test_decimal_field_works2(self):
     80        """
     81        The original test reversed. Works too.
     82        """
     83        self.help_decimal_field('1234567890', range(31, 0, -1))
     84
     85    def test_decimal_field_broken1(self):
     86        """
     87        Testing with just one element. This fails nicely.
     88
     89        This fails on:
     90        * MySQL 5.1.66
     91
     92        Doesn't fail on:
     93        * MySQL 5.5.28
     94        """
     95        self.help_decimal_field('1234567890', [31])
     96
     97    def test_decimal_field_broken2(self):
     98        """
     99        Testing with just one element. This fails nicely.
     100
     101        This fails on:
     102        * MySQL 5.1.66
     103
     104        Doesn't fail on:
     105        * MySQL 5.5.28
     106        """
     107        self.help_decimal_field('1234567890', [22])
     108
     109    def test_decimal_field_broken3(self):
     110        """
     111        Testing with just one element. This fails nicely.
     112
     113        This fails on:
     114        * MySQL 5.1.66
     115
     116        Doesn't fail on:
     117        * MySQL 5.5.28
     118        """
     119        self.help_decimal_field('1234567890', [18])
Back to Top