test_creation.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import copy
  2. import unittest
  3. from contextlib import contextmanager
  4. from django.db import DEFAULT_DB_ALIAS, connection, connections
  5. from django.db.backends.base.creation import (
  6. TEST_DATABASE_PREFIX, BaseDatabaseCreation,
  7. )
  8. from django.db.backends.postgresql.creation import DatabaseCreation
  9. from django.test import SimpleTestCase
  10. class TestDbSignatureTests(SimpleTestCase):
  11. def get_connection_copy(self):
  12. # Get a copy of the default connection. (Can't use django.db.connection
  13. # because it'll modify the default connection itself.)
  14. test_connection = copy.copy(connections[DEFAULT_DB_ALIAS])
  15. test_connection.settings_dict = copy.copy(connections[DEFAULT_DB_ALIAS].settings_dict)
  16. return test_connection
  17. def test_default_name(self):
  18. # A test db name isn't set.
  19. prod_name = 'hodor'
  20. test_connection = self.get_connection_copy()
  21. test_connection.settings_dict['NAME'] = prod_name
  22. test_connection.settings_dict['TEST'] = {'NAME': None}
  23. signature = BaseDatabaseCreation(test_connection).test_db_signature()
  24. self.assertEqual(signature[3], TEST_DATABASE_PREFIX + prod_name)
  25. def test_custom_test_name(self):
  26. # A regular test db name is set.
  27. test_name = 'hodor'
  28. test_connection = self.get_connection_copy()
  29. test_connection.settings_dict['TEST'] = {'NAME': test_name}
  30. signature = BaseDatabaseCreation(test_connection).test_db_signature()
  31. self.assertEqual(signature[3], test_name)
  32. def test_custom_test_name_with_test_prefix(self):
  33. # A test db name prefixed with TEST_DATABASE_PREFIX is set.
  34. test_name = TEST_DATABASE_PREFIX + 'hodor'
  35. test_connection = self.get_connection_copy()
  36. test_connection.settings_dict['TEST'] = {'NAME': test_name}
  37. signature = BaseDatabaseCreation(test_connection).test_db_signature()
  38. self.assertEqual(signature[3], test_name)
  39. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL-specific tests")
  40. class PostgreSQLDatabaseCreationTests(SimpleTestCase):
  41. @contextmanager
  42. def changed_test_settings(self, **kwargs):
  43. settings = connection.settings_dict['TEST']
  44. saved_values = {}
  45. for name in kwargs:
  46. if name in settings:
  47. saved_values[name] = settings[name]
  48. for name, value in kwargs.items():
  49. settings[name] = value
  50. try:
  51. yield
  52. finally:
  53. for name, value in kwargs.items():
  54. if name in saved_values:
  55. settings[name] = saved_values[name]
  56. else:
  57. del settings[name]
  58. def check_sql_table_creation_suffix(self, settings, expected):
  59. with self.changed_test_settings(**settings):
  60. creation = DatabaseCreation(connection)
  61. suffix = creation.sql_table_creation_suffix()
  62. self.assertEqual(suffix, expected)
  63. def test_sql_table_creation_suffix_with_none_settings(self):
  64. settings = dict(CHARSET=None, TEMPLATE=None)
  65. self.check_sql_table_creation_suffix(settings, "")
  66. def test_sql_table_creation_suffix_with_encoding(self):
  67. settings = dict(CHARSET='UTF8')
  68. self.check_sql_table_creation_suffix(settings, "WITH ENCODING 'UTF8'")
  69. def test_sql_table_creation_suffix_with_template(self):
  70. settings = dict(TEMPLATE='template0')
  71. self.check_sql_table_creation_suffix(settings, 'WITH TEMPLATE "template0"')
  72. def test_sql_table_creation_suffix_with_encoding_and_template(self):
  73. settings = dict(CHARSET='UTF8', TEMPLATE='template0')
  74. self.check_sql_table_creation_suffix(settings, '''WITH ENCODING 'UTF8' TEMPLATE "template0"''')