aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBonfaceKilz2022-01-04 16:04:57 +0300
committerBonfaceKilz2022-01-04 16:04:57 +0300
commita2128d3c6ab3655b3d032dc8e38c143c7d55b249 (patch)
tree6cffe996b64c0295d5b4c6858f4a66c213374622
parentabb7c8cf70964bd42fd1b8c97c34428ad07049cb (diff)
downloadgn-docs-a2128d3c6ab3655b3d032dc8e38c143c7d55b249.tar.gz
How to test decorators
-rw-r--r--api/testing-endpoints.md44
1 files changed, 44 insertions, 0 deletions
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("/<a>/addition/<b>", 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?")