From 0109234eb7fea7a3e5307b0ecbae09e7c8975749 Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Fri, 20 Dec 2013 20:29:48 +0100 Subject: [PATCH] Added meth tests by mocking a Flask app --- tests/__init__.py | 1 + tests/contrib/__init__.hy | 0 tests/contrib/test_meth.hy | 54 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 tests/contrib/__init__.hy create mode 100644 tests/contrib/test_meth.hy diff --git a/tests/__init__.py b/tests/__init__.py index 5e935a2..c58d7b4 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -11,3 +11,4 @@ from .native_tests.unless import * # noqa from .native_tests.when import * # noqa from .native_tests.with_decorator import * # noqa from .native_tests.core import * # noqa +from .contrib.test_meth import * # noqa diff --git a/tests/contrib/__init__.hy b/tests/contrib/__init__.hy new file mode 100644 index 0000000..e69de29 diff --git a/tests/contrib/test_meth.hy b/tests/contrib/test_meth.hy new file mode 100644 index 0000000..2f13cfd --- /dev/null +++ b/tests/contrib/test_meth.hy @@ -0,0 +1,54 @@ +(require hy.contrib.meth) + +(defclass FakeMeth [] + "Mocking decorator class" + [[rules {}] + [route (fn [self rule &kwargs options] + (fn [f] + (assoc self.rules rule (, f options)) + f))]]) + + +(defn test_route [] + (let [[app (FakeMeth)]] + (route get-index "/" [] (str "Hy world!")) + (setv app-rules (getattr app "rules")) + (assert (in "/" app-rules)) + (let [[(, rule-fun rule-opt) (get app-rules "/")]] + (assert (not (empty? rule-opt))) + (assert (in "GET" (get rule-opt "methods"))) + (assert (= (getattr rule-fun "__name__") "get_index")) + (assert (= "Hy world!" (rule-fun)))))) + +(defn test_post_route [] + (let [[app (FakeMeth)]] + (post-route get-index "/" [] (str "Hy world!")) + (setv app-rules (getattr app "rules")) + (assert (in "/" app-rules)) + (let [[(, rule-fun rule-opt) (get app-rules "/")]] + (assert (not (empty? rule-opt))) + (assert (in "POST" (get rule-opt "methods"))) + (assert (= (getattr rule-fun "__name__") "get_index")) + (assert (= "Hy world!" (rule-fun)))))) + +(defn test_put_route [] + (let [[app (FakeMeth)]] + (put-route get-index "/" [] (str "Hy world!")) + (setv app-rules (getattr app "rules")) + (assert (in "/" app-rules)) + (let [[(, rule-fun rule-opt) (get app-rules "/")]] + (assert (not (empty? rule-opt))) + (assert (in "PUT" (get rule-opt "methods"))) + (assert (= (getattr rule-fun "__name__") "get_index")) + (assert (= "Hy world!" (rule-fun)))))) + +(defn test_delete_route [] + (let [[app (FakeMeth)]] + (delete-route get-index "/" [] (str "Hy world!")) + (setv app-rules (getattr app "rules")) + (assert (in "/" app-rules)) + (let [[(, rule-fun rule-opt) (get app-rules "/")]] + (assert (not (empty? rule-opt))) + (assert (in "DELETE" (get rule-opt "methods"))) + (assert (= (getattr rule-fun "__name__") "get_index")) + (assert (= "Hy world!" (rule-fun))))))