From a2128d3c6ab3655b3d032dc8e38c143c7d55b249 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Tue, 4 Jan 2022 16:04:57 +0300 Subject: How to test decorators --- api/testing-endpoints.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 api/testing-endpoints.md diff --git a/api/testing-endpoints.md b/api/testing-endpoints.md new file mode 100644 index 0000000..e7b7a71 --- /dev/null +++ b/api/testing-endpoints.md @@ -0,0 +1,44 @@ +# How to test endpoints with decorators + +Say we have an the following endpoint in +"genenetwork2/wqflask/wqflask/metadata_edits.py": that interpolates 2 +strings: + +``` +@metadata_edit.route("//addition/", methods=("POST",)) +@edit_admins_access_required +@login_required +def addition(a, b): + return a + b +``` + + +To test it, see: + +``` +import unittest +import mock +import imp +import wqflask + +class TestMetadataEdits(unittest.TestCase): + def setUp(self): + def kill_patches(): + mock.patch.stopall() + imp.reload(wqflask.metadata_edits) + self.addCleanup(kill_patches) + mock.patch('wqflask.decorators.login_required', + lambda x: x).start() + mock.patch('wqflask.decorators.edit_admins_access_required', + lambda x: x).start() + imp.reload(wqflask.metadata_edits) + imp.reload(wqflask) + self.app = wqflask.app.test_client() + + def test_addition(self): + resp = self.app.post("/datasets/3/addition/2") + self.assertEqual(resp.data, b'32') + +``` + +For an in-depth explanation, read this [post](https://newbedev.com/can-i-patch-a-python-decorator-before-it-wraps-a-function "Can I patch a Python decorator before it wraps a function?") -- cgit v1.2.3