-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_database.py
More file actions
168 lines (124 loc) · 3.82 KB
/
Copy pathtest_database.py
File metadata and controls
168 lines (124 loc) · 3.82 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import sqlite3
import sys
from pathlib import Path
import pytest
sys.path.append(f"{Path(__file__).parent.parent.parent}/scripts/python")
from database import (
backup_and_reset,
)
@pytest.fixture
def backup_dir(tmp_dir):
return tmp_dir / "backups"
@pytest.fixture
def sqlite_db(tmp_dir):
db_path = tmp_dir / "test.db"
conn = sqlite3.connect(db_path)
conn.execute(
"""
CREATE TABLE stored_instances (
id INTEGER PRIMARY KEY,
value TEXT
)
"""
)
conn.executemany(
"INSERT INTO stored_instances(value) VALUES (?)",
[("a",), ("b",), ("c",)],
)
conn.commit()
conn.close()
return db_path
def row_count(db_path, table):
conn = sqlite3.connect(db_path)
count = conn.execute(f"SELECT COUNT(*) FROM {table}").fetchone()[0]
conn.close()
return count
def test_backup_and_reset_creates_backup(
backup_dir,
sqlite_db,
monkeypatch,
):
"""Backup database creates backup."""
monkeypatch.setenv("DB_PATH", str(sqlite_db))
monkeypatch.setenv("TABLE_NAME", "stored_instances")
monkeypatch.setenv("BACKUP_PATH", str(backup_dir))
monkeypatch.setenv("MAX_BACKUPS", "5")
deleted = backup_and_reset()
assert deleted == 3
backup_file = backup_dir / "test.db.backup.0"
assert backup_file.exists()
# Original DB was cleared
assert row_count(sqlite_db, "stored_instances") == 0
# Backup still contains original rows
assert row_count(backup_file, "stored_instances") == 3
def test_rotation_keeps_five_backups(
backup_dir,
sqlite_db,
monkeypatch,
):
"""Only 5 database backups are kept and older ones are deleted."""
monkeypatch.setenv("DB_PATH", str(sqlite_db))
monkeypatch.setenv("TABLE_NAME", "stored_instances")
monkeypatch.setenv("BACKUP_PATH", str(backup_dir))
monkeypatch.setenv("MAX_BACKUPS", "5")
# Generate 7 backups
for i in range(7):
conn = sqlite3.connect(sqlite_db)
conn.execute(
"INSERT INTO stored_instances(value) VALUES (?)",
(f"run-{i}",),
)
conn.commit()
conn.close()
backup_and_reset()
backups = sorted(backup_dir.glob("test.db.backup.*"))
assert len(backups) == 5
expected = {
"test.db.backup.0",
"test.db.backup.1",
"test.db.backup.2",
"test.db.backup.3",
"test.db.backup.4",
}
assert {p.name for p in backups} == expected
def test_newest_backup_is_backup_zero(
backup_dir,
sqlite_db,
monkeypatch,
):
"""Newest backup is always backup.0."""
monkeypatch.setenv("DB_PATH", str(sqlite_db))
monkeypatch.setenv("TABLE_NAME", "stored_instances")
monkeypatch.setenv("BACKUP_PATH", str(backup_dir))
monkeypatch.setenv("MAX_BACKUPS", "5")
# First backup contains 3 rows
backup_and_reset()
# Add one row and create another backup
conn = sqlite3.connect(sqlite_db)
conn.execute("INSERT INTO stored_instances(value) VALUES ('new')")
conn.commit()
conn.close()
backup_and_reset()
newest = backup_dir / "test.db.backup.0"
previous = backup_dir / "test.db.backup.1"
assert row_count(newest, "stored_instances") == 1
assert row_count(previous, "stored_instances") == 3
def test_invalid_table_name_is_ignored(
backup_dir,
sqlite_db,
monkeypatch,
):
"""Invalid table name is ignored."""
monkeypatch.setenv("DB_PATH", str(sqlite_db))
monkeypatch.setenv("TABLE_NAME", "users")
monkeypatch.setenv("BACKUP_PATH", str(backup_dir))
result = backup_and_reset()
assert result == 0
assert not backup_dir.exists()
def test_missing_db_path_is_ignored(
monkeypatch,
):
"""Missing DB_PATH is ignored."""
monkeypatch.delenv("DB_PATH", raising=False)
result = backup_and_reset()
assert result == 0