Exemplo n.º 1
0
class BaseDbBackupCommandMethodsTest(TestCase):
    def setUp(self):
        HANDLED_FILES.clean()
        self.command = BaseDbBackupCommand()
        self.command.storage = get_storage()

    def test_read_from_storage(self):
        HANDLED_FILES['written_files'].append(
            ['foo', File(six.BytesIO(b'bar'))])
        file_ = self.command.read_from_storage('foo')
        self.assertEqual(file_.read(), b'bar')

    def test_write_to_storage(self):
        self.command.write_to_storage(six.BytesIO(b'foo'), 'bar')
        self.assertEqual(HANDLED_FILES['written_files'][0][0], 'bar')

    def test_read_local_file(self):
        # setUp
        self.command.path = '/tmp/foo.bak'
        open(self.command.path, 'w').close()
        # Test
        output_file = self.command.read_local_file(self.command.path)
        # tearDown
        os.remove(self.command.path)

    def test_write_local_file(self):
        fd, path = File(six.BytesIO(b"foo")), '/tmp/foo.bak'
        self.command.write_local_file(fd, path)
        self.assertTrue(os.path.exists(path))
        # tearDown
        os.remove(path)

    def test_ask_confirmation(self):
        # Yes
        with patch('dbbackup.management.commands._base.input',
                   return_value='y'):
            self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input',
                   return_value='Y'):
            self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input',
                   return_value=''):
            self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input',
                   return_value='foo'):
            self.command._ask_confirmation()
        # No
        with patch('dbbackup.management.commands._base.input',
                   return_value='n'):
            with self.assertRaises(SystemExit):
                self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input',
                   return_value='N'):
            with self.assertRaises(SystemExit):
                self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input',
                   return_value='No'):
            with self.assertRaises(SystemExit):
                self.command._ask_confirmation()
Exemplo n.º 2
0
class BaseDbBackupCommandMethodsTest(TestCase):
    def setUp(self):
        HANDLED_FILES.clean()
        self.command = BaseDbBackupCommand()
        self.command.storage = get_storage()

    def test_read_from_storage(self):
        HANDLED_FILES['written_files'].append(['foo', File(six.BytesIO(b'bar'))])
        file_ = self.command.read_from_storage('foo')
        self.assertEqual(file_.read(), b'bar')

    def test_write_to_storage(self):
        self.command.write_to_storage(six.BytesIO(b'foo'), 'bar')
        self.assertEqual(HANDLED_FILES['written_files'][0][0], 'bar')

    def test_read_local_file(self):
        # setUp
        self.command.path = '/tmp/foo.bak'
        open(self.command.path, 'w').close()
        # Test
        output_file = self.command.read_local_file(self.command.path)
        # tearDown
        os.remove(self.command.path)

    def test_write_local_file(self):
        fd, path = File(six.BytesIO(b"foo")), '/tmp/foo.bak'
        self.command.write_local_file(fd, path)
        self.assertTrue(os.path.exists(path))
        # tearDown
        os.remove(path)

    def test_ask_confirmation(self):
        # Yes
        with patch('dbbackup.management.commands._base.input', return_value='y'):
            self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input', return_value='Y'):
            self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input', return_value=''):
            self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input', return_value='foo'):
            self.command._ask_confirmation()
        # No
        with patch('dbbackup.management.commands._base.input', return_value='n'):
            with self.assertRaises(SystemExit):
                self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input', return_value='N'):
            with self.assertRaises(SystemExit):
                self.command._ask_confirmation()
        with patch('dbbackup.management.commands._base.input', return_value='No'):
            with self.assertRaises(SystemExit):
                self.command._ask_confirmation()