Reorganized backends tests.

This commit is contained in:
Mariusz Felisiak 2017-02-11 21:37:49 +01:00 committed by Tim Graham
parent 0f91ba1adc
commit 8cb1b1fd8e
21 changed files with 731 additions and 710 deletions

View file

@ -1,22 +1,11 @@
import unittest
"""Tests for django.db.backends.utils"""
from decimal import Decimal, Rounded
from django.core.exceptions import ImproperlyConfigured
from django.db import connection
from django.db.backends.utils import truncate_name
from django.db.utils import ProgrammingError, load_backend
from django.test import SimpleTestCase, TestCase
from django.db.backends.utils import format_number, truncate_name
from django.test import SimpleTestCase
class TestLoadBackend(SimpleTestCase):
def test_load_backend_invalid_name(self):
msg = (
"'foo' isn't an available database backend.\n"
"Try using 'django.db.backends.XXX', where XXX is one of:\n"
" 'mysql', 'oracle', 'postgresql', 'sqlite3'"
)
with self.assertRaisesMessage(ImproperlyConfigured, msg) as cm:
load_backend('foo')
self.assertEqual(str(cm.exception.__cause__), "No module named 'foo'")
class TestUtils(SimpleTestCase):
def test_truncate_name(self):
self.assertEqual(truncate_name('some_table', 10), 'some_table')
@ -28,15 +17,31 @@ class TestLoadBackend(SimpleTestCase):
self.assertEqual(truncate_name('username"."some_long_table', 10), 'username"."some_la38a')
self.assertEqual(truncate_name('username"."some_long_table', 10, 3), 'username"."some_loa38')
def test_format_number(self):
def equal(value, max_d, places, result):
self.assertEqual(format_number(Decimal(value), max_d, places), result)
@unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific tests')
class TestDatabaseErrorWrapper(TestCase):
def test_reraising_backend_specific_database_exception(self):
cursor = connection.cursor()
msg = 'table "X" does not exist'
with self.assertRaisesMessage(ProgrammingError, msg) as cm:
cursor.execute('DROP TABLE "X"')
self.assertNotEqual(type(cm.exception), type(cm.exception.__cause__))
self.assertIsNotNone(cm.exception.__cause__)
self.assertIsNotNone(cm.exception.__cause__.pgcode)
self.assertIsNotNone(cm.exception.__cause__.pgerror)
equal('0', 12, 3, '0.000')
equal('0', 12, 8, '0.00000000')
equal('1', 12, 9, '1.000000000')
equal('0.00000000', 12, 8, '0.00000000')
equal('0.000000004', 12, 8, '0.00000000')
equal('0.000000008', 12, 8, '0.00000001')
equal('0.000000000000000000999', 10, 8, '0.00000000')
equal('0.1234567890', 12, 10, '0.1234567890')
equal('0.1234567890', 12, 9, '0.123456789')
equal('0.1234567890', 12, 8, '0.12345679')
equal('0.1234567890', 12, 5, '0.12346')
equal('0.1234567890', 12, 3, '0.123')
equal('0.1234567890', 12, 1, '0.1')
equal('0.1234567890', 12, 0, '0')
equal('0.1234567890', None, 0, '0')
equal('1234567890.1234567890', None, 0, '1234567890')
equal('1234567890.1234567890', None, 2, '1234567890.12')
equal('0.1234', 5, None, '0.1234')
equal('123.12', 5, None, '123.12')
with self.assertRaises(Rounded):
equal('0.1234567890', 5, None, '0.12346')
with self.assertRaises(Rounded):
equal('1234567890.1234', 5, None, '1234600000')