From 6ea47833ec607be0739f6052d757573cb5cef234 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Fri, 25 Sep 2020 14:35:38 +0530 Subject: [PATCH] test: Added tests for backup command --- frappe/tests/test_commands.py | 62 ++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/frappe/tests/test_commands.py b/frappe/tests/test_commands.py index 82c0cdce5c..fdab89e4d4 100644 --- a/frappe/tests/test_commands.py +++ b/frappe/tests/test_commands.py @@ -1,12 +1,14 @@ # Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors # imports - standard imports +import os import shlex import subprocess import unittest # imports - module imports import frappe +from frappe.utils.backups import fetch_latest_backups def clean(value): @@ -25,7 +27,7 @@ class BaseTestCommands: self.returncode = clean(self._proc.returncode) -class TestCommands(BaseTestCommands, unittest.TestCase): +class TestCommands(BaseTestCommands): def test_execute(self): # test 1: execute a command expecting a numeric output self.execute("bench --site {site} execute frappe.db.get_database_size") @@ -44,3 +46,61 @@ class TestCommands(BaseTestCommands, unittest.TestCase): self.execute("""bench --site {site} execute frappe.bold --kwargs '{{"text": "DocType"}}'""") self.assertEquals(self.returncode, 0) self.assertEquals(self.stdout[1:-1], frappe.bold(text='DocType')) + + def test_backup(self): + home = os.expanduser("~") + + # test 1: take a backup + before_backup = fetch_latest_backups() + self.execute("bench --site {site} backup") + after_backup = fetch_latest_backups() + + self.assertEquals(self.returncode, 0) + self.assertIn("successfully completed", self.stdout) + self.assertNotEqual(before_backup["database"], after_backup["database"]) + + # test 2: take a backup with --with-files + before_backup = after_backup.copy() + self.execute("bench --site {site} backup --with-files") + after_backup = fetch_latest_backups() + + self.assertEquals(self.returncode, 0) + self.assertIn("successfully completed", self.stdout) + self.assertIn("with files", self.stdout) + self.assertNotEqual(before_backup, after_backup) + self.assertIsNotNone(after_backup["public"]) + self.assertIsNotNone(after_backup["private"]) + + # test 3: take a backup with --backup-path + backup_path = os.path.join(home, "backups") + self.execute("bench --site {site} backup --backup-path {backup_path}", {"backup_path": backup_path}) + + self.assertEquals(self.returncode, 0) + self.assertTrue(os.path.exists(backup_path)) + self.assertGreaterEqual(len(os.listdir(backup_path)), 2) + + # test 4: take a backup with --backup-path-db, --backup-path-files, --backup-path-private-files, --backup-path-conf + kwargs = { key: os.path.join(home, key) for key in ["db_path", "files_path", "private_path", "conf_path"] } + + self.execute("""bench + --site {site} backup --with-files + --backup-path-db {db_path} + --backup-path-files {files_path} + --backup-path-private-files {private_path} + --backup-path-conf {conf_path}""", kwargs) + + self.assertEquals(self.returncode, 0) + for path in kwargs.values(): + self.assertTrue(len(os.listdir(path)), 1) + + # test 5: take a backup with --compress + self.execute("bench --site {site} backup --with-files --compress") + backup_files = fetch_latest_backups() + + self.assertEquals(self.returncode, 0) + self.assertTrue(backup_files["private"].endswith("tgz")) + self.assertTrue(backup_files["public"].endswith("tgz")) + + # test 6: take a backup with --verbose + self.execute("bench --site {site} backup --verbose") + self.assertEquals(self.returncode, 0)