Implement unittests for HTTPResponse kamaki class
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Fri, 15 Feb 2013 15:44:10 +0000 (17:44 +0200)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Fri, 15 Feb 2013 15:44:10 +0000 (17:44 +0200)
Class Location: kamaki.clients.connection.HTTPResponse
Test Location: kamaki.clients.connection.test.HTTPResponse

kamaki/cli/commands/test_cli.py
kamaki/clients/connection/__init__.py
kamaki/clients/connection/test.py [new file with mode: 0644]
kamaki/clients/tests/__init__.py
kamaki/clients/tests/image.py
setup.py

index 03ea13e..69e2a02 100644 (file)
@@ -46,9 +46,9 @@ class _test_init(_command_init):
 
     def _run(self, client, method=None):
         if method:
-            tests._main([client, method], config=self.config)
+            tests.main([client, method], config=self.config)
         else:
-            tests._main([client], config=self.config)
+            tests.main([client], config=self.config)
 
     def main(self, client, method=None):
         return self._run(client, method)
index 7c8cf69..28bb787 100644 (file)
@@ -37,12 +37,12 @@ class HTTPResponse(object):
     Subclass implementation required
     """
 
-    def __init__(self, request=None, prefetched=False):
+    def __init__(self, request, prefetched=False):
         self.request = request
         self.prefetched = prefetched
 
     def _get_response(self):
-        """Wait for http response as late as possible: the first time needed"""
+        """Wait for http response as late as possible"""
         if self.prefetched:
             return
         self = self.request.response
@@ -64,7 +64,7 @@ class HTTPResponse(object):
 
     @property
     def content(self):
-        """(binary) request response content (data)"""
+        """:returns: (binary) request response content (data)"""
         self._get_response()
         return self._content
 
diff --git a/kamaki/clients/connection/test.py b/kamaki/clients/connection/test.py
new file mode 100644 (file)
index 0000000..e0d35c1
--- /dev/null
@@ -0,0 +1,90 @@
+# Copyright 2013 GRNET S.A. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or
+# without modification, are permitted provided that the following
+# conditions are met:
+#
+#   1. Redistributions of source code must retain the above
+#      copyright notice, self.list of conditions and the following
+#      disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above
+#      copyright notice, self.list of conditions and the following
+#      disclaimer in the documentation and/or other materials
+#      provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and
+# documentation are those of the authors and should not be
+# interpreted as representing official policies, either expressed
+# or implied, of GRNET S.A.
+
+from unittest import TestCase, TestSuite, TextTestRunner, TestLoader
+from mock import Mock
+
+
+class HTTPResponse(TestCase):
+
+    def setUp(self):
+        from kamaki.clients.connection import HTTPResponse as HTTPR
+        self.resp = HTTPR('Abstract class, so test with fake request (str)')
+
+    def _mock_get_response(foo):
+        def mocker(self):
+            self.resp._get_response = Mock()
+            foo(self)
+        return mocker
+
+    def test_release(self):
+        self.assertRaises(NotImplementedError, self.resp.release)
+
+    def test_prefetched(self):
+        self.assertFalse(self.resp.prefetched)
+        self.resp.prefetched = True
+        self.assertTrue(self.resp.prefetched)
+
+    @_mock_get_response
+    def test_content(self):
+        rsp = self.resp
+        for cont in ('Sample Content', u'\u03c7\u03cd\u03bd\u03c9\x00'):
+            rsp.content = cont
+            self.assertEquals(rsp.content, cont)
+
+    (
+        test_text,
+        test_json,
+        test_headers,
+        test_status,
+        test_status_code) = 5 * (test_content,)
+
+    def test_request(self):
+        from httplib import HTTPSConnection
+        r = self.resp.request
+        self.assertTrue(isinstance(r, HTTPSConnection))
+
+
+def main(argv):
+    if argv:
+        suite = TestSuite()
+        test_method = 'test_%s' % '_'.join(argv)
+        suite.addTest(HTTPResponse(test_method))
+    else:
+        suite = TestLoader().loadTestsFromTestCase(HTTPResponse)
+    TextTestRunner(verbosity=2).run(suite)
+
+
+if __name__ == '__main__':
+    from sys import argv
+    main(argv[1:])
index cf471c4..75f6359 100644 (file)
@@ -164,11 +164,7 @@ def init_parser():
     return parser
 
 
-def main(argv):
-    _main(argv, config=None)
-
-
-def _main(argv, config=None):
+def main(argv, config=None):
     suiteFew = TestSuite()
     if len(argv) == 0 or argv[0] == 'pithos':
         from kamaki.clients.tests.pithos import Pithos
index ba5a951..9a4bc75 100644 (file)
@@ -57,11 +57,9 @@ class Image(tests.Generic):
     def _prepare_img(self):
         f = open(self['image', 'local_path'], 'rb')
         (token, uuid) = (self['token'], self['store', 'account'])
-        print('UUID HERE: %s (%s)' % (uuid, token))
         if not uuid:
             from kamaki.clients.astakos import AstakosClient
             uuid = AstakosClient(self['astakos', 'url'], token).term('uuid')
-        print('UUID HERE: %s' % uuid)
         from kamaki.clients.pithos import PithosClient
         self.pithcli = PithosClient(self['store', 'url'], token, uuid)
         cont = 'cont_%s' % self.now
@@ -70,7 +68,7 @@ class Image(tests.Generic):
         print('\t- Create container %s on Pithos server' % cont)
         self.pithcli.container_put()
         self.location = 'pithos://%s/%s/%s' % (uuid, cont, self.obj)
-        print('\t- Upload an image at %s...' % self.location)
+        print('\t- Upload an image at %s...\n' % self.location)
         self.pithcli.upload_object(self.obj, f)
         print('\t- ok')
         f.close()
index e7a99a3..7daf3c9 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -42,7 +42,7 @@ import kamaki
 
 optional = ['ansicolors',
             'progress>=1.0.2']
-requires = ['objpool']
+requires = ['objpool', 'mock']
 
 if version_info < (2, 7):
     requires.append('argparse')