| | 1 | # vim: set ts=8 sw=4 sts=4 et ai: |
| | 2 | from __future__ import absolute_import, unicode_literals |
| | 3 | |
| | 4 | from decimal import Decimal |
| | 5 | from unittest import TestCase |
| | 6 | |
| | 7 | from .models import Book |
| | 8 | |
| | 9 | """ |
| | 10 | Note that this problem is fixed in MySQL somewhere between |
| | 11 | version 5.1.66 and 5.5.28. |
| | 12 | |
| | 13 | Test with: |
| | 14 | |
| | 15 | create table abc (value decimal(31,0)); |
| | 16 | insert into abc values (1234567890123456789012345678901); |
| | 17 | select * from abc where value = 1234567890123456789012345678901; |
| | 18 | select * from abc where value = '1234567890123456789012345678901'; |
| | 19 | drop table abc; |
| | 20 | |
| | 21 | In the broken case, you won't get a result for the second query. |
| | 22 | """ |
| | 23 | |
| | 24 | class 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]) |