]> jfr.im git - z_archive/twitter.git/commitdiff
Unit test for previous silly regression
authorMike Verdone <redacted>
Tue, 21 Oct 2014 10:07:13 +0000 (12:07 +0200)
committerMike Verdone <redacted>
Tue, 21 Oct 2014 10:07:13 +0000 (12:07 +0200)
tests/test_internals.py [new file with mode: 0644]
twitter/api.py

diff --git a/tests/test_internals.py b/tests/test_internals.py
new file mode 100644 (file)
index 0000000..edea203
--- /dev/null
@@ -0,0 +1,22 @@
+# encoding: utf-8
+from __future__ import unicode_literals
+
+from twitter.api import method_for_uri, build_uri
+
+def test_method_for_uri__lookup():
+    assert "POST" == method_for_uri("/1.1/users/lookup")
+    assert "POST" == method_for_uri("/1.1/statuses/lookup")
+    assert "POST" == method_for_uri("/1.1/users/lookup/12345")
+    assert "GET" == method_for_uri("/1.1/friendships/lookup")
+
+def test_build_uri():
+    uri = build_uri(["1.1", "foo", "bar"], {})
+    assert uri == "1.1/foo/bar"
+
+    # Interpolation works
+    uri = build_uri(["1.1", "_foo", "bar"], {"_foo": "asdf"})
+    assert uri == "1.1/asdf/bar"
+
+    # But only for strings beginning with _.
+    uri = build_uri(["1.1", "foo", "bar"], {"foo": "asdf"})
+    assert uri == "1.1/foo/bar"
index 05d934c0c8a31dbece79d8102bb433930598b938..db20e0e94cc334fdb7c19942ae20f5cbe60244c7 100644 (file)
@@ -138,6 +138,31 @@ def method_for_uri(uri):
         return "POST"
     return "GET"
 
+
+def build_uri(orig_uriparts, kwargs):
+    """
+    Build the URI from the original uriparts and kwargs. Modifies kwargs.
+    """
+    uriparts = []
+    for uripart in orig_uriparts:
+        # If this part matches a keyword argument (starting with _), use
+        # the supplied value. Otherwise, just use the part.
+        if uripart.startswith("_"):
+            part = (str(kwargs.pop(uripart, uripart)))
+        else:
+            part = uripart
+        uriparts.append(part)
+    uri = '/'.join(uriparts)
+
+    # If an id kwarg is present and there is no id to fill in in
+    # the list of uriparts, assume the id goes at the end.
+    id = kwargs.pop('id', None)
+    if id:
+        uri += "/%s" % (id)
+
+    return uri
+
+
 class TwitterCall(object):
 
     TWITTER_UNAVAILABLE_WAIT = 30  # delay after HTTP codes 502, 503 or 504
@@ -172,26 +197,10 @@ class TwitterCall(object):
                 return extend_call(k)
 
     def __call__(self, **kwargs):
-        # Build the uri.
-        uriparts = []
-        for uripart in self.uriparts:
-            # If this part matches a keyword argument (starting with _), use
-            # the supplied value. Otherwise, just use the part.
-            if uripart.startswith("_"):
-                part = (str(kwargs.pop(uripart, uripart)))
-            else:
-                part = uripart
-            uriparts.append(part)
-        uri = '/'.join(uriparts)
-
+        kwargs = dict(kwargs)
+        uri = build_uri(self.uriparts, kwargs)
         method = kwargs.pop('_method', None) or method_for_uri(uri)
 
-        # If an id kwarg is present and there is no id to fill in in
-        # the list of uriparts, assume the id goes at the end.
-        id = kwargs.pop('id', None)
-        if id:
-            uri += "/%s" % (id)
-
         # If an _id kwarg is present, this is treated as id as a CGI
         # param.
         _id = kwargs.pop('_id', None)