tests.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 The Distro Tracker Developers
  3. # See the COPYRIGHT file at the top-level directory of this distribution and
  4. # at http://deb.li/DTAuthors
  5. #
  6. # This file is part of Distro Tracker. It is subject to the license terms
  7. # in the LICENSE file found in the top-level directory of this
  8. # distribution and at http://deb.li/DTLicense. No part of Distro Tracker,
  9. # including this file, may be copied, modified, propagated, or distributed
  10. # except according to the terms contained in the LICENSE file.
  11. """
  12. Tests for test functionalities of Distro Tracker.
  13. """
  14. from __future__ import unicode_literals
  15. from distro_tracker.test import SimpleTestCase, TestCase, TransactionTestCase
  16. from distro_tracker.test import TempDirsMixin
  17. from django.conf import settings
  18. import copy
  19. import os.path
  20. settings_copy = copy.deepcopy(settings)
  21. class TempDirsTests(object):
  22. def setUp(self):
  23. self._settings_during_setup = {}
  24. for name in self.get_settings_names():
  25. self._settings_during_setup[name] = getattr(settings, name)
  26. def get_settings_names(self):
  27. """
  28. Return names of all settings that should point to temporary
  29. directories during tests.
  30. """
  31. return TempDirsMixin.DISTRO_TRACKER_PATH_SETTINGS.keys()
  32. def test_setup_has_same_settings(self):
  33. """ Test that .setUp() already has the overriden settings. """
  34. for name in self.get_settings_names():
  35. self.assertEqual(self._settings_during_setup[name],
  36. getattr(settings, name))
  37. def test_temp_dirs_outside_of_base_path(self):
  38. """ Test that the settings no longer point inside the base path. """
  39. for name in self.get_settings_names():
  40. self.assertNotIn(getattr(settings, 'BASE_DIR'),
  41. getattr(settings, name))
  42. def test_temp_dirs_in_data_path(self):
  43. """ Test that the settings point within DISTRO_TRACKER_DATA_PATH. """
  44. for name in self.get_settings_names():
  45. self.assertIn(getattr(settings, 'DISTRO_TRACKER_DATA_PATH'),
  46. getattr(settings, name))
  47. def test_path_settings_changed(self):
  48. """
  49. Tests that the settings have changed (hopefully to point to temporary
  50. directories).
  51. """
  52. for name in self.get_settings_names():
  53. self.assertNotEqual(getattr(settings, name),
  54. getattr(settings_copy, name))
  55. class TestCaseHelpersTests(object):
  56. def test_get_test_data_path(self):
  57. self.assertEqual(self.get_test_data_path('myfile'),
  58. os.path.join(os.path.dirname(__file__),
  59. 'tests-data', 'myfile'))
  60. def test_add_test_template_dir(self):
  61. template_dir = self.get_test_data_path('tests-templates')
  62. self.assertNotIn(template_dir, settings.TEMPLATES[0]['DIRS'])
  63. self.add_test_template_dir()
  64. self.assertIn(template_dir, settings.TEMPLATES[0]['DIRS'])
  65. self.doCleanups() # Ensure a cleanup function is added
  66. self.assertNotIn(template_dir, settings.TEMPLATES[0]['DIRS'])
  67. class TempDirsOnSimpleTestCase(TempDirsTests, TestCaseHelpersTests,
  68. SimpleTestCase):
  69. pass
  70. class TempDirsOnTestCase(TempDirsTests, TestCaseHelpersTests,
  71. TestCase):
  72. pass
  73. class TempDirsOnTransactionTestCase(TempDirsTests, TestCaseHelpersTests,
  74. TransactionTestCase):
  75. pass