| 1 |
from io import StringIO |
| 2 |
from unittest.mock import patch |
| 3 |
|
| 4 |
from django.core.management import execute_from_command_line |
| 5 |
from django.test import TestCase |
| 6 |
|
| 7 |
from dbbackup.management.commands.listbackups import Command as ListbackupsCommand |
| 8 |
from dbbackup.storage import get_storage |
| 9 |
from tests.utils import HANDLED_FILES |
| 10 |
|
| 11 |
|
| 12 |
class ListbackupsCommandTest(TestCase): |
| 13 |
def setUp(self): |
| 14 |
self.command = ListbackupsCommand() |
| 15 |
self.command.storage = get_storage() |
| 16 |
HANDLED_FILES["written_files"] = [ |
| 17 |
(f, None) |
| 18 |
for f in [ |
| 19 |
"2015-02-06-042810.bak", |
| 20 |
"2015-02-07-042810.bak", |
| 21 |
"2015-02-08-042810.bak", |
| 22 |
] |
| 23 |
] |
| 24 |
|
| 25 |
def test_get_backup_attrs(self): |
| 26 |
options = {} |
| 27 |
attrs = self.command.get_backup_attrs(options) |
| 28 |
assert len(HANDLED_FILES["written_files"]) == len(attrs) |
| 29 |
|
| 30 |
|
| 31 |
class ListbackupsCommandArgComputingTest(TestCase): |
| 32 |
def setUp(self): |
| 33 |
HANDLED_FILES["written_files"] = [ |
| 34 |
(f, None) |
| 35 |
for f in [ |
| 36 |
"2015-02-06-042810_foo.db", |
| 37 |
"2015-02-06-042810_foo.db.gz", |
| 38 |
"2015-02-06-042810_foo.db.gpg", |
| 39 |
"2015-02-06-042810_foo.db.gz.gpg", |
| 40 |
"2015-02-06-042810_foo.tar", |
| 41 |
"2015-02-06-042810_foo.tar.gz", |
| 42 |
"2015-02-06-042810_foo.tar.gpg", |
| 43 |
"2015-02-06-042810_foo.tar.gz.gpg", |
| 44 |
"2015-02-06-042810_bar.db", |
| 45 |
"2015-02-06-042810_bar.db.gz", |
| 46 |
"2015-02-06-042810_bar.db.gpg", |
| 47 |
"2015-02-06-042810_bar.db.gz.gpg", |
| 48 |
"2015-02-06-042810_bar.tar", |
| 49 |
"2015-02-06-042810_bar.tar.gz", |
| 50 |
"2015-02-06-042810_bar.tar.gpg", |
| 51 |
"2015-02-06-042810_bar.tar.gz.gpg", |
| 52 |
] |
| 53 |
] |
| 54 |
|
| 55 |
def test_list(self): |
| 56 |
execute_from_command_line(["", "listbackups"]) |
| 57 |
|
| 58 |
def test_filter_encrypted(self): |
| 59 |
stdout = StringIO() |
| 60 |
with patch("sys.stdout", stdout): |
| 61 |
execute_from_command_line(["", "listbackups", "--encrypted", "-q"]) |
| 62 |
stdout.seek(0) |
| 63 |
stdout.readline() |
| 64 |
for line in stdout.readlines(): |
| 65 |
assert ".gpg" in line |
| 66 |
|
| 67 |
def test_filter_not_encrypted(self): |
| 68 |
stdout = StringIO() |
| 69 |
with patch("sys.stdout", stdout): |
| 70 |
execute_from_command_line(["", "listbackups", "--not-encrypted", "-q"]) |
| 71 |
stdout.seek(0) |
| 72 |
stdout.readline() |
| 73 |
for line in stdout.readlines(): |
| 74 |
assert ".gpg" not in line |
| 75 |
|
| 76 |
def test_filter_compressed(self): |
| 77 |
stdout = StringIO() |
| 78 |
with patch("sys.stdout", stdout): |
| 79 |
execute_from_command_line(["", "listbackups", "--compressed", "-q"]) |
| 80 |
stdout.seek(0) |
| 81 |
stdout.readline() |
| 82 |
for line in stdout.readlines(): |
| 83 |
assert ".gz" in line |
| 84 |
|
| 85 |
def test_filter_not_compressed(self): |
| 86 |
stdout = StringIO() |
| 87 |
with patch("sys.stdout", stdout): |
| 88 |
execute_from_command_line(["", "listbackups", "--not-compressed", "-q"]) |
| 89 |
stdout.seek(0) |
| 90 |
stdout.readline() |
| 91 |
for line in stdout.readlines(): |
| 92 |
assert ".gz" not in line |
| 93 |
|
| 94 |
def test_filter_db(self): |
| 95 |
stdout = StringIO() |
| 96 |
with patch("sys.stdout", stdout): |
| 97 |
execute_from_command_line(["", "listbackups", "--content-type", "db", "-q"]) |
| 98 |
stdout.seek(0) |
| 99 |
stdout.readline() |
| 100 |
for line in stdout.readlines(): |
| 101 |
assert ".db" in line |
| 102 |
|
| 103 |
def test_filter_media(self): |
| 104 |
stdout = StringIO() |
| 105 |
with patch("sys.stdout", stdout): |
| 106 |
execute_from_command_line(["", "listbackups", "--content-type", "media", "-q"]) |
| 107 |
stdout.seek(0) |
| 108 |
stdout.readline() |
| 109 |
for line in stdout.readlines(): |
| 110 |
assert ".tar" in line |