From fde65270afb3855057a86b40c5996c86f58fe97b Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Mon, 30 Jul 2012 20:58:59 +1200 Subject: [PATCH] Testing examples and docs. --- .sources/examples_context.py | 23 ++++++++++ .sources/examples_setup.py | 28 ++++++++++++ .sources/examples_setupall.py | 37 ++++++++++++++++ .sources/examples_test.py | 17 -------- .sources/make | 4 +- libpathod/templates/docs_test.html | 43 +++++++++++++++++++ libpathod/templates/examples_context.html | 24 +++++++++++ ...examples_test.html => examples_setup.html} | 23 +++++++--- libpathod/templates/examples_setupall.html | 38 ++++++++++++++++ libpathod/templates/index.html | 4 +- libpathod/templates/request_previewform.html | 2 +- libpathod/test.py | 7 +++ test/test_test.py | 9 ++-- 13 files changed, 227 insertions(+), 32 deletions(-) create mode 100644 .sources/examples_context.py create mode 100644 .sources/examples_setup.py create mode 100644 .sources/examples_setupall.py delete mode 100644 .sources/examples_test.py create mode 100644 libpathod/templates/examples_context.html rename libpathod/templates/{examples_test.html => examples_setup.html} (52%) create mode 100644 libpathod/templates/examples_setupall.html diff --git a/.sources/examples_context.py b/.sources/examples_context.py new file mode 100644 index 000000000..80278e760 --- /dev/null +++ b/.sources/examples_context.py @@ -0,0 +1,23 @@ +import requests +from libpathod import test + +class Test: + """ + Testing the requests module with + a pathod context manager. + """ + def test_simple(self): + # Start pathod in a separate thread + with test.Daemon() as d: + # Get a URL for a pathod spec + url = d.p("200:b@100") + # ... and request it + r = requests.put(url) + + # Check the returned data + assert r.status_code == 200 + assert len(r.content) == 100 + + # Check pathod's internal log + log = d.last_log()["request"] + assert log["method"] == "PUT" diff --git a/.sources/examples_setup.py b/.sources/examples_setup.py new file mode 100644 index 000000000..5366c9ef1 --- /dev/null +++ b/.sources/examples_setup.py @@ -0,0 +1,28 @@ +import requests +from libpathod import test + +class Test: + """ + Testing the requests module with + a pathod instance started for + each test. + """ + def setUp(self): + self.d = test.Daemon() + + def tearDown(self): + self.d.shutdown() + + def test_simple(self): + # Get a URL for a pathod spec + url = self.d.p("200:b@100") + # ... and request it + r = requests.put(url) + + # Check the returned data + assert r.status_code == 200 + assert len(r.content) == 100 + + # Check pathod's internal log + log = self.d.last_log()["request"] + assert log["method"] == "PUT" diff --git a/.sources/examples_setupall.py b/.sources/examples_setupall.py new file mode 100644 index 000000000..c8948971c --- /dev/null +++ b/.sources/examples_setupall.py @@ -0,0 +1,37 @@ +import requests +from libpathod import test + +class Test: + """ + Testing the requests module with + a single pathod instance started + for the test suite. + """ + @classmethod + def setUpAll(cls): + cls.d = test.Daemon() + + @classmethod + def tearDownAll(cls): + cls.d.shutdown() + + def setUp(self): + # Clear the pathod logs between tests + self.d.clear_log() + + def test_simple(self): + # Get a URL for a pathod spec + url = self.d.p("200:b@100") + # ... and request it + r = requests.put(url) + + # Check the returned data + assert r.status_code == 200 + assert len(r.content) == 100 + + # Check pathod's internal log + log = self.d.last_log()["request"] + assert log["method"] == "PUT" + + def test_two(self): + assert not self.d.log() diff --git a/.sources/examples_test.py b/.sources/examples_test.py deleted file mode 100644 index 1f2630e03..000000000 --- a/.sources/examples_test.py +++ /dev/null @@ -1,17 +0,0 @@ -import requests -from libpathod import test - -class Test: - def setUp(self): - self.daemon = test.Daemon() - - def tearDown(self): - self.daemon.shutdown() - - def test_simple(self): - path = self.daemon.p("200:b@100") - r = requests.get(path) - assert r.status_code == 200 - assert len(r.content) == 100 - log = self.daemon.last_log() - assert log["request"]["method"] == "GET" diff --git a/.sources/make b/.sources/make index c7208bf47..e0e9d2cc0 100755 --- a/.sources/make +++ b/.sources/make @@ -1,2 +1,4 @@ #!/bin/sh -pygmentize -f html ./examples_test.py > ../libpathod/templates/examples_test.html +pygmentize -f html ./examples_context.py > ../libpathod/templates/examples_context.html +pygmentize -f html ./examples_setup.py > ../libpathod/templates/examples_setup.html +pygmentize -f html ./examples_setupall.py > ../libpathod/templates/examples_setupall.html diff --git a/libpathod/templates/docs_test.html b/libpathod/templates/docs_test.html index 27129f1cc..c9162f877 100644 --- a/libpathod/templates/docs_test.html +++ b/libpathod/templates/docs_test.html @@ -6,4 +6,47 @@ Using pathod and pathoc in your unit tests. + +
+
+ +

The libpathod.test module is a light, flexible testing layer + for HTTP clients. It works by firing up a Pathod instance in a separate + thread, letting you use Pathod's full abilities to generate responses, + and then query Pathod's internal logs to establish what happened. All + the mechanics of startup, shutdown, finding free ports and so forth are + taken care of for you.

+ +

The canonical docs can be accessed using pydoc:

+ +
pydoc libpathod.testing
+ +

The remainder of this page demonstrates some common interaction + patterns using nose. These examples + are also applicable with only minor modification to most commonly used + Python testing engines.

+ +
+
+

Context Decorator

+ + {% include "examples_context.html" %} +
+
+ +
+
+

One instance per test

+ + {% include "examples_setup.html" %} +
+
+

One instance per suite

+ + {% include "examples_setupall.html" %} +
+
+ + {% endblock %} diff --git a/libpathod/templates/examples_context.html b/libpathod/templates/examples_context.html new file mode 100644 index 000000000..3d33e76dc --- /dev/null +++ b/libpathod/templates/examples_context.html @@ -0,0 +1,24 @@ +
import requests
+from libpathod import test
+
+class Test:
+    """
+        Testing the requests module with 
+        a pathod context manager.
+    """
+    def test_simple(self):
+        # Start pathod in a separate thread
+        with test.Daemon() as d:
+            # Get a URL for a pathod spec
+            url = d.p("200:b@100")
+            # ... and request it
+            r = requests.put(url)
+
+            # Check the returned data
+            assert r.status_code == 200
+            assert len(r.content) == 100
+
+            # Check pathod's internal log
+            log = d.last_log()["request"]
+            assert log["method"] == "PUT"
+
diff --git a/libpathod/templates/examples_test.html b/libpathod/templates/examples_setup.html similarity index 52% rename from libpathod/templates/examples_test.html rename to libpathod/templates/examples_setup.html index 57d395344..bde458408 100644 --- a/libpathod/templates/examples_test.html +++ b/libpathod/templates/examples_setup.html @@ -2,17 +2,28 @@ from libpathod import test class Test: + """ + Testing the requests module with + a pathod instance started for + each test. + """ def setUp(self): - self.daemon = test.Daemon() + self.d = test.Daemon() def tearDown(self): - self.daemon.shutdown() + self.d.shutdown() def test_simple(self): - path = self.daemon.p("200:b@100") - r = requests.get(path) + # Get a URL for a pathod spec + url = self.d.p("200:b@100") + # ... and request it + r = requests.put(url) + + # Check the returned data assert r.status_code == 200 assert len(r.content) == 100 - log = self.daemon.last_log() - assert log["request"]["method"] == "GET" + + # Check pathod's internal log + log = self.d.last_log()["request"] + assert log["method"] == "PUT" diff --git a/libpathod/templates/examples_setupall.html b/libpathod/templates/examples_setupall.html new file mode 100644 index 000000000..b7f799753 --- /dev/null +++ b/libpathod/templates/examples_setupall.html @@ -0,0 +1,38 @@ +
import requests
+from libpathod import test
+
+class Test:
+    """
+        Testing the requests module with 
+        a single pathod instance started 
+        for the test suite.
+    """
+    @classmethod
+    def setUpAll(cls):
+        cls.d = test.Daemon()
+
+    @classmethod
+    def tearDownAll(cls):
+        cls.d.shutdown()
+
+    def setUp(self):
+        # Clear the pathod logs between tests
+        self.d.clear_log()
+
+    def test_simple(self):
+        # Get a URL for a pathod spec
+        url = self.d.p("200:b@100")
+        # ... and request it
+        r = requests.put(url)
+
+        # Check the returned data
+        assert r.status_code == 200
+        assert len(r.content) == 100
+
+        # Check pathod's internal log
+        log = self.d.last_log()["request"]
+        assert log["method"] == "PUT"
+
+    def test_two(self):
+        assert not self.d.log()
+
diff --git a/libpathod/templates/index.html b/libpathod/templates/index.html index c31d4414e..b020d60b5 100644 --- a/libpathod/templates/index.html +++ b/libpathod/templates/index.html @@ -70,9 +70,9 @@

libpathod.test

-

Using pathod and pathoc in your unit tests.

+

Using pathod in your unit tests.

- {% include "examples_test.html" %} + {% include "examples_context.html" %}
diff --git a/libpathod/templates/request_previewform.html b/libpathod/templates/request_previewform.html index f38758ea4..263b501ef 100644 --- a/libpathod/templates/request_previewform.html +++ b/libpathod/templates/request_previewform.html @@ -1,6 +1,6 @@