File: test_clear_caches.py

package info (click to toggle)
python-django-extensions 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,396 kB
  • sloc: python: 11,858; makefile: 116
file content (52 lines) | stat: -rw-r--r-- 2,022 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
import os
import mock

from django.core.management import call_command
from django.core.management.base import CommandError
from django.test import TestCase


DefaultCacheMock = mock.Mock()
OtherCacheMock = mock.Mock()


class ClearCacheTests(TestCase):
    def setUp(self):
        self.project_root = os.path.join('tests', 'testapp')
        self._settings = os.environ.get('DJANGO_SETTINGS_MODULE')
        os.environ['DJANGO_SETTINGS_MODULE'] = 'django_extensions.settings'
        DefaultCacheMock.reset_mock()
        OtherCacheMock.reset_mock()

    def tearDown(self):
        if self._settings:
            os.environ['DJANGO_SETTINGS_MODULE'] = self._settings

    def test_called_with_no_arguments(self):
        with self.settings(BASE_DIR=self.project_root):
            call_command('clear_cache')
        DefaultCacheMock.return_value.clear.assert_called()
        OtherCacheMock.return_value.clear.assert_not_called()

    def test_called_with_explicit_other(self):
        with self.settings(BASE_DIR=self.project_root):
            call_command('clear_cache', '--cache', 'other')
        DefaultCacheMock.return_value.clear.assert_not_called()
        OtherCacheMock.return_value.clear.assert_called()

    def test_called_with_all_argument(self):
        with self.settings(BASE_DIR=self.project_root):
            call_command('clear_cache', '--all')
        DefaultCacheMock.return_value.clear.assert_called()
        OtherCacheMock.return_value.clear.assert_called()

    def test_called_with_explicit_all(self):
        with self.settings(BASE_DIR=self.project_root):
            call_command('clear_cache', '--cache', 'default', '--cache', 'other')
        DefaultCacheMock.return_value.clear.assert_called()
        OtherCacheMock.return_value.clear.assert_called()

    def test_called_with_invalid_arguments(self):
        with self.settings(BASE_DIR=self.project_root):
            self.assertRaises(CommandError, call_command, 'clear_cache', '--all', '--cache', 'foo')