# -*- coding: utf-8 -*-

import mock
import six

from preggy import expect
from django.core.management import call_command

from django_unused_media.management.commands.cleanup_unused_media import Command
from .base import BaseTestCase


class TestManagementCommand(BaseTestCase):
    def test_command_call(self):
        expect(call_command('cleanup_unused_media', interactive=False)).Not.to_be_an_error()

    def test_command_nothing_to_delete(self):
        cmd = Command()
        cmd.handle(interactive=False)
        expect(cmd.stdout.getvalue().split('\n'))\
            .to_include(u'Nothing to delete. Exit')

    def test_command_not_interactive(self):
        self._media_create('file.txt')

        cmd = Command()
        cmd.handle(interactive=False)
        expect(cmd.stdout.getvalue().split('\n'))\
            .to_include(u'Remove {}'.format(self._media_abs_path(u'file.txt')))\
            .to_include(u'Done. 1 unused files have been removed')

        expect(self._media_exists('file.txt')).to_be_false()

    @mock.patch('six.moves.input', return_value='n')
    def test_command_interactive_n(self, mock_input):
        self._media_create(u'file.txt')

        cmd = Command()
        cmd.handle(interactive=True)
        expect(cmd.stdout.getvalue().split('\n'))\
            .to_include(u'Interrupted by user. Exit.')

        expect(self._media_exists(u'file.txt')).to_be_true()

    @mock.patch('six.moves.input', return_value='Y')
    def test_command_interactive_y(self, mock_input):
        self._media_create(u'file.txt')

        cmd = Command()
        cmd.handle(interactive=True)
        expect(cmd.stdout.getvalue().split('\n')) \
            .to_include(u'Remove {}'.format(self._media_abs_path(u'file.txt'))) \
            .to_include(u'Done. 1 unused files have been removed')

        expect(self._media_exists(u'file.txt')).to_be_false()

    @mock.patch('six.moves.input', return_value='Y')
    def test_command_interactive_y_with_ascii(self, mock_input):
        self._media_create(u'Тест.txt')

        expected_string = u'Remove {}'.format(self._media_abs_path(u'Тест.txt'))
        if six.PY2:
            expected_string = expected_string.encode('utf-8')

        cmd = Command()
        cmd.handle(interactive=True)
        expect(cmd.stdout.getvalue().split('\n')) \
            .to_include(expected_string) \
            .to_include(u'Done. 1 unused files have been removed')

        expect(self._media_exists(u'Тест.txt')).to_be_false()

    @mock.patch('django_unused_media.management.commands.cleanup_unused_media.remove_empty_dirs')
    def test_command_do_not_remove_dirs(self, mock_remove_empty_dirs):
        self._media_create(u'sub1/sub2/sub3/notused.txt')

        cmd = Command()
        cmd.handle(interactive=False)

        mock_remove_empty_dirs.assert_not_called()

    @mock.patch('django_unused_media.management.commands.cleanup_unused_media.remove_empty_dirs')
    def test_command_remove_dirs(self, mock_remove_empty_dirs):
        self._media_create(u'sub1/sub2/sub3/notused.txt')

        cmd = Command()
        cmd.handle(interactive=False, remove_empty_dirs=True)

        mock_remove_empty_dirs.assert_called_once()

@@ -121,89 +121,8 @@
 nd%0A%0A
-from django_unused_media.management.commands.cleanup_unused_media import Command%0A
 from
@@ -378,43 +378,77 @@
     
-cmd = Command()%0A        cmd.handle(
+stdout = six.StringIO()%0A        call_command('cleanup_unused_media', 
 inte
@@ -452,32 +452,47 @@
 nteractive=False
+, stdout=stdout
 )%0A        expect
@@ -484,36 +484,32 @@
 %0A        expect(
-cmd.
 stdout.getvalue(
@@ -673,43 +673,77 @@
     
-cmd = Command()%0A        cmd.handle(
+stdout = six.StringIO()%0A        call_command('cleanup_unused_media', 
 inte
@@ -755,16 +755,31 @@
 ve=False
+, stdout=stdout
 )%0A      
@@ -779,36 +779,32 @@
 %0A        expect(
-cmd.
 stdout.getvalue(
@@ -1190,43 +1190,77 @@
     
-cmd = Command()%0A        cmd.handle(
+stdout = six.StringIO()%0A        call_command('cleanup_unused_media', 
 inte
@@ -1263,32 +1263,47 @@
 interactive=True
+, stdout=stdout
 )%0A        expect
@@ -1295,36 +1295,32 @@
 %0A        expect(
-cmd.
 stdout.getvalue(
@@ -1613,43 +1613,77 @@
     
-cmd = Command()%0A        cmd.handle(
+stdout = six.StringIO()%0A        call_command('cleanup_unused_media', 
 inte
@@ -1686,32 +1686,47 @@
 interactive=True
+, stdout=stdout
 )%0A        expect
@@ -1718,36 +1718,32 @@
 %0A        expect(
-cmd.
 stdout.getvalue(
@@ -2307,43 +2307,77 @@
     
-cmd = Command()%0A        cmd.handle(
+stdout = six.StringIO()%0A        call_command('cleanup_unused_media', 
 inte
@@ -2388,16 +2388,31 @@
 ive=True
+, stdout=stdout
 )%0A      
@@ -2420,20 +2420,16 @@
  expect(
-cmd.
 stdout.g
@@ -2868,42 +2868,44 @@
    c
-md = C
+all_c
 ommand(
-)%0A        cmd.handle(
+'cleanup_unused_media', 
 inte
@@ -3206,42 +3206,44 @@
    c
-md = C
+all_c
 ommand(
-)%0A        cmd.handle(
+'cleanup_unused_media', 
 inte

Update commands tests to prevent errors from new options