Debian Codegraph @

tests/commands/test_base.py

raw
1 """
2 Tests for base command class.
3 """
4
5 import logging
6 import os
7 from io import BytesIO
8 from unittest.mock import patch
9
10 import pytest
11 from django.core.files import File
12 from django.test import TestCase
13
14 from dbbackup.management.commands._base import BaseDbBackupCommand
15 from dbbackup.storage import get_storage
16 from tests.utils import DEV_NULL, HANDLED_FILES
17
18
19 class BaseDbBackupCommandSetLoggerLevelTest(TestCase):
20 def setUp(self):
21 self.command = BaseDbBackupCommand()
22
23 def test_0_level(self):
24 self.command.verbosity = 0
25 self.command._set_logger_level()
26 assert self.command.logger.level == logging.WARNING
27
28 def test_1_level(self):
29 self.command.verbosity = 1
30 self.command._set_logger_level()
31 assert self.command.logger.level == logging.INFO
32
33 def test_2_level(self):
34 self.command.verbosity = 2
35 self.command._set_logger_level()
36 assert self.command.logger.level == logging.DEBUG
37
38 def test_3_level(self):
39 self.command.verbosity = 3
40 self.command._set_logger_level()
41 assert self.command.logger.level == logging.DEBUG
42
43 def test_quiet(self):
44 self.command.quiet = True
45 self.command._set_logger_level()
46 assert self.command.logger.level > logging.ERROR
47
48
49 class BaseDbBackupCommandMethodsTest(TestCase):
50 def setUp(self):
51 HANDLED_FILES.clean()
52 self.command = BaseDbBackupCommand()
53 self.command.storage = get_storage()
54
55 def test_read_from_storage(self):
56 HANDLED_FILES["written_files"].append(["foo", File(BytesIO(b"bar"))])
57 file_ = self.command.read_from_storage("foo")
58 assert file_.read() == b"bar"
59
60 def test_write_to_storage(self):
61 self.command.write_to_storage(BytesIO(b"foo"), "bar")
62 assert HANDLED_FILES["written_files"][0][0] == "bar"
63
64 def test_read_local_file(self):
65 # setUp: use repository-local tmp directory
66 local_tmp = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "tmp")
67 os.makedirs(local_tmp, exist_ok=True)
68 self.command.path = os.path.join(local_tmp, "foo.bak")
69 open(self.command.path, "w").close()
70 # Test
71 self.command.read_local_file(self.command.path)
72 # tearDown
73 os.remove(self.command.path)
74
75 def test_write_local_file(self):
76 local_tmp = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "tmp")
77 os.makedirs(local_tmp, exist_ok=True)
78 fd, path = File(BytesIO(b"foo")), os.path.join(local_tmp, "foo.bak")
79 self.command.write_local_file(fd, path)
80 assert os.path.exists(path)
81 # tearDown
82 os.remove(path)
83
84 def test_ask_confirmation(self):
85 # Yes
86 with patch("dbbackup.management.commands._base.input", return_value="y"):
87 self.command._ask_confirmation()
88 with patch("dbbackup.management.commands._base.input", return_value="Y"):
89 self.command._ask_confirmation()
90 with patch("dbbackup.management.commands._base.input", return_value=""):
91 self.command._ask_confirmation()
92 with patch("dbbackup.management.commands._base.input", return_value="foo"):
93 self.command._ask_confirmation()
94 # No
95 with patch("dbbackup.management.commands._base.input", return_value="n"), pytest.raises(SystemExit):
96 self.command._ask_confirmation()
97 with patch("dbbackup.management.commands._base.input", return_value="N"), pytest.raises(SystemExit):
98 self.command._ask_confirmation()
99 with patch("dbbackup.management.commands._base.input", return_value="No"), pytest.raises(SystemExit):
100 self.command._ask_confirmation()
101
102
103 class BaseDbBackupCommandCleanupOldBackupsTest(TestCase):
104 def setUp(self):
105 HANDLED_FILES.clean()
106 self.command = BaseDbBackupCommand()
107 self.command.stdout = DEV_NULL
108 self.command.encrypt = False
109 self.command.compress = False
110 self.command.servername = "foo-server"
111 self.command.storage = get_storage()
112 HANDLED_FILES["written_files"] = [
113 (f, None)
114 for f in [
115 "fooserver-2015-02-06-042810.tar",
116 "fooserver-2015-02-07-042810.tar",
117 "fooserver-2015-02-08-042810.tar",
118 "foodb-fooserver-2015-02-06-042810.dump",
119 "foodb-fooserver-2015-02-07-042810.dump",
120 "foodb-fooserver-2015-02-08-042810.dump",
121 "bardb-fooserver-2015-02-06-042810.dump",
122 "bardb-fooserver-2015-02-07-042810.dump",
123 "bardb-fooserver-2015-02-08-042810.dump",
124 "hamdb-hamserver-2015-02-06-042810.dump",
125 "hamdb-hamserver-2015-02-07-042810.dump",
126 "hamdb-hamserver-2015-02-08-042810.dump",
127 ]
128 ]
129
130 @patch("dbbackup.settings.CLEANUP_KEEP", 1)
131 def test_clean_db(self):
132 self.command.content_type = "db"
133 self.command.database = "foodb"
134 self.command._cleanup_old_backups(database="foodb")
135 assert len(HANDLED_FILES["deleted_files"]) == 2
136 assert "foodb-fooserver-2015-02-08-042810.dump" not in HANDLED_FILES["deleted_files"]
137
138 @patch("dbbackup.settings.CLEANUP_KEEP", 1)
139 def test_clean_other_db(self):
140 self.command.content_type = "db"
141 self.command._cleanup_old_backups(database="bardb")
142 assert len(HANDLED_FILES["deleted_files"]) == 2
143 assert "bardb-fooserver-2015-02-08-042810.dump" not in HANDLED_FILES["deleted_files"]
144
145 @patch("dbbackup.settings.CLEANUP_KEEP", 1)
146 def test_clean_other_server_db(self):
147 self.command.content_type = "db"
148 self.command._cleanup_old_backups(database="bardb")
149 assert len(HANDLED_FILES["deleted_files"]) == 2
150 assert "bardb-fooserver-2015-02-08-042810.dump" not in HANDLED_FILES["deleted_files"]
151
152 @patch("dbbackup.settings.CLEANUP_KEEP_MEDIA", 1)
153 def test_clean_media(self):
154 self.command.content_type = "media"
155 self.command._cleanup_old_backups()
156 assert len(HANDLED_FILES["deleted_files"]) == 2
157 assert "foo-server-2015-02-08-042810.tar" not in HANDLED_FILES["deleted_files"]