From 20a1c074b589b3b0722d1a23e39b0a0efc62a49f Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Thu, 27 Jan 2022 09:51:03 +0530 Subject: [PATCH] test: all known patches.txt format parsing --- frappe/tests/test_patches.py | 89 +++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/frappe/tests/test_patches.py b/frappe/tests/test_patches.py index 8e6d97bd0d..64e8684f55 100644 --- a/frappe/tests/test_patches.py +++ b/frappe/tests/test_patches.py @@ -1,7 +1,43 @@ -import unittest, frappe +import unittest +import frappe from frappe.modules import patch_handler +from unittest.mock import mock_open, patch + + +EMTPY_FILE = "" +EMTPY_SECTION = """ +[pre_model_sync] + +[post_model_sync] +""" +FILLED_SECTIONS = """ +[pre_model_sync] +app.module.patch1 +app.module.patch2 + +[post_model_sync] +app.module.patch3 + +""" +OLD_STYLE_PATCH_TXT = """ +app.module.patch1 +app.module.patch2 +app.module.patch3 +""" + +EDGE_CASES = """ +[pre_model_sync] +App.module.patch1 +app.module.patch2 # rerun +execute:frappe.db.updatedb("Item") +execute:frappe.function(arg="1") + +[post_model_sync] +app.module.patch3 +""" + class TestPatches(unittest.TestCase): def test_patch_module_names(self): frappe.flags.final_patches = [] @@ -30,3 +66,54 @@ class TestPatches(unittest.TestCase): finished_patches = frappe.db.count("Patch Log") self.assertGreaterEqual(finished_patches, len(all_patches)) + + + +class TestPatchReader(unittest.TestCase): + @patch("builtins.open", new_callable=mock_open, read_data=EMTPY_FILE) + def test_empty_file(self, _file): + all_patches = patch_handler.get_patches_from_app("frappe") + pre = patch_handler.get_patches_from_app("frappe", patch_handler.PatchType.pre_model_sync) + post = patch_handler.get_patches_from_app("frappe", patch_handler.PatchType.post_model_sync) + self.assertEqual(all_patches, []) + self.assertEqual(pre, []) + self.assertEqual(post, []) + + + @patch("builtins.open", new_callable=mock_open, read_data=EMTPY_SECTION) + def test_empty_sections(self, _file): + all_patches = patch_handler.get_patches_from_app("frappe") + pre = patch_handler.get_patches_from_app("frappe", patch_handler.PatchType.pre_model_sync) + post = patch_handler.get_patches_from_app("frappe", patch_handler.PatchType.post_model_sync) + self.assertEqual(all_patches, []) + self.assertEqual(pre, []) + self.assertEqual(post, []) + + @patch("builtins.open", new_callable=mock_open, read_data=FILLED_SECTIONS) + def test_new_style(self, _file): + all_patches = patch_handler.get_patches_from_app("frappe") + pre = patch_handler.get_patches_from_app("frappe", patch_handler.PatchType.pre_model_sync) + post = patch_handler.get_patches_from_app("frappe", patch_handler.PatchType.post_model_sync) + self.assertEqual(all_patches, ["app.module.patch1", "app.module.patch2", "app.module.patch3"]) + self.assertEqual(pre, ["app.module.patch1", "app.module.patch2"]) + self.assertEqual(post, ["app.module.patch3",]) + + @patch("builtins.open", new_callable=mock_open, read_data=OLD_STYLE_PATCH_TXT) + def test_old_style(self, _file): + all_patches = patch_handler.get_patches_from_app("frappe") + pre = patch_handler.get_patches_from_app("frappe", patch_handler.PatchType.pre_model_sync) + post = patch_handler.get_patches_from_app("frappe", patch_handler.PatchType.post_model_sync) + self.assertEqual(all_patches, ["app.module.patch1", "app.module.patch2", "app.module.patch3"]) + self.assertEqual(pre, ["app.module.patch1", "app.module.patch2", "app.module.patch3"]) + self.assertEqual(post, []) + + + @patch("builtins.open", new_callable=mock_open, read_data=EDGE_CASES) + def test_new_style_edge_cases(self, _file): + pre = patch_handler.get_patches_from_app("frappe", patch_handler.PatchType.pre_model_sync) + self.assertEqual(pre, [ + "App.module.patch1", + "app.module.patch2 # rerun", + 'execute:frappe.db.updatedb("Item")', + 'execute:frappe.function(arg="1")', + ])