File: test_unreferenced_files.py

package info (click to toggle)
python-django-extensions 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,820 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (60 lines) | stat: -rw-r--r-- 1,895 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
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
import os
import shutil
from io import StringIO
from tempfile import mkdtemp

from django.core.management import CommandError, call_command
from django.test import TestCase
from django.test.utils import override_settings

from ...testapp.models import Photo

from unittest.mock import patch


class UnreferencedFilesExceptionsTests(TestCase):
    @override_settings(MEDIA_ROOT=None)
    def test_should_raise_ComandError_if_MEDIA_ROOT_is_not_set(self):
        with self.assertRaisesRegex(
            CommandError, "MEDIA_ROOT is not set, nothing to do"
        ):
            call_command("unreferenced_files")


class UnreferencedFilesTests(TestCase):
    def setUp(self):
        self.media_root_dir = mkdtemp()

    def tearDown(self):
        super().tearDown()
        shutil.rmtree(self.media_root_dir)

    @patch("sys.stdout", new_callable=StringIO)
    def test_should_not_print_any_output(self, m_stdout):
        with override_settings(MEDIA_ROOT=self.media_root_dir):
            call_command("unreferenced_files")

        self.assertIs("", m_stdout.getvalue())

    @patch("sys.stdout", new_callable=StringIO)
    def test_should_print_unreferenced_hello_txt_file(self, m_stdout):
        fn = os.path.join(self.media_root_dir, "hello.txt")
        open(fn, "a").close()

        with override_settings(MEDIA_ROOT=self.media_root_dir):
            call_command("unreferenced_files")

        self.assertIn(fn, m_stdout.getvalue())

    @patch("sys.stdout", new_callable=StringIO)
    def test_should_not_print_referenced_image_jpg_file(self, m_stdout):
        fn = os.path.join(self.media_root_dir, "image.jpg")
        open(fn, "a").close()

        Photo.objects.create(photo="image.jpg")

        with override_settings(MEDIA_ROOT=self.media_root_dir):
            call_command("unreferenced_files")

        self.assertNotIn(fn, m_stdout.getvalue())