test: add better test for backup restoration

Before restore:
- Wipe DB, files.

After restore:
- Actually check if things got restored.
This commit is contained in:
Ankush Menat 2023-10-11 17:50:46 +05:30
parent 9983bf82e6
commit 404f3fbeec
2 changed files with 42 additions and 4 deletions

View file

@ -38,7 +38,7 @@ from frappe.utils.jinja_globals import bundled_asset
from frappe.utils.scheduler import enable_scheduler, is_scheduler_inactive
_result: Result | None = None
TEST_SITE = "commands-site-O4PN2QKA.test" # added random string tag to avoid collisions
TEST_SITE = "commands-site-O4PN2QK.test" # added random string tag to avoid collisions
CLI_CONTEXT = frappe._dict(sites=[TEST_SITE])
@ -252,14 +252,40 @@ class TestCommands(BaseTestCommands):
if value:
self.execute(f"bench set-config {key} {value} -g")
with self.switch_site(TEST_SITE):
public_file = frappe.new_doc(
"File", file_name=f"test_{frappe.generate_hash()}", content=frappe.generate_hash()
).insert()
private_file = frappe.new_doc(
"File", file_name=f"test_{frappe.generate_hash()}", content=frappe.generate_hash()
).insert()
# test 1: bench restore from full backup
self.execute("bench --site {test_site} backup --ignore-backup-conf", site_data)
self.execute("bench --site {test_site} backup --ignore-backup-conf --with-files", site_data)
self.execute(
"bench --site {test_site} execute frappe.utils.backups.fetch_latest_backups",
site_data,
)
site_data.update({"database": json.loads(self.stdout)["database"]})
self.execute("bench --site {test_site} restore {database}", site_data)
# Destroy some data and files to verify that they are indeed being restored.
with self.switch_site(TEST_SITE):
public_file.delete_file_data_content()
private_file.delete_file_data_content()
frappe.db.sql_ddl("DROP TABLE IF EXISTS `tabToDo`")
self.assertFalse(public_file.exists_on_disk())
self.assertFalse(private_file.exists_on_disk())
backup_data = json.loads(self.stdout)
site_data.update(backup_data)
self.execute(
"bench --site {test_site} restore {database} --with-public-files {public} --with-private-files {private} ",
site_data,
)
self.assertEqual(self.returncode, 0)
with self.switch_site(TEST_SITE):
self.assertTrue(frappe.db.table_exists("ToDo", cached=False))
self.assertTrue(public_file.exists_on_disk())
self.assertTrue(private_file.exists_on_disk())
# test 2: restore from partial backup
self.execute("bench --site {test_site} backup --exclude 'ToDo'", site_data)

View file

@ -142,6 +142,18 @@ class FrappeTestCase(unittest.TestCase):
yield
frappe.set_user(old_user)
@contextmanager
def switch_site(self, site: str):
"""Switch connection to different site.
Note: Drops current site connection completely."""
old_site = frappe.local.site
frappe.init(site, force=True)
frappe.connect()
yield
frappe.init(old_site, force=True)
frappe.connect()
class MockedRequestTestCase(FrappeTestCase):
def setUp(self):