Permalink
Browse files

Add first round of unit tests

  • Loading branch information...
1 parent 5ac0068 commit aed074ebdbba194ac49d7618a50da458720e6f9a @nickstenning nickstenning committed Dec 5, 2012
View
@@ -1,2 +1,3 @@
*.pyc
/*.egg-info
+/.tox
View
No changes.
View
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Error xmlns="http://www.vmware.com/vcloud/v1.5" minorErrorCode="RESOURCE_NOT_FOUND" message="Resource not found" majorErrorCode="404" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://test-api-root/v1.5/schema/master.xsd"></Error>
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<OrgList xmlns="http://www.vmware.com/vcloud/v1.5" type="application/vnd.vmware.vcloud.orgList+xml" href="https://test-api-root/org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 https://test-api-root/v1.5/schema/master.xsd">
+ <Org type="application/vnd.vmware.vcloud.org+xml" name="Org-Foo" href="https://test-api-root/org/734772e4-166f-4dcc-9391-d35ddafce90e"/>
+ <Org type="application/vnd.vmware.vcloud.org+xml" name="Org-Bar" href="https://test-api-root/org/d5a44e58-135b-4a27-903e-a4c41112e165"/>
+ <Org type="application/vnd.vmware.vcloud.org+xml" name="Org-Baz" href="https://test-api-root/org/eb0537cb-a160-47da-97dc-fd83cb275f4a"/>
+</OrgList>
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Session xmlns="http://www.vmware.com/vcloud/v1.5" user="test_user" org="Test-Org" type="application/vnd.vmware.vcloud.session+xml" href="http://test-api-root/session/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://test-api-root/v1.5/schema/master.xsd">
+ <Link rel="down" type="application/vnd.vmware.vcloud.orgList+xml" href="http://test-api-root/myCustomOrgListPath/"/>
+ <Link rel="down" type="application/vnd.vmware.admin.vcloud+xml" href="http://test-api-root/admin/"/>
+ <Link rel="down" type="application/vnd.vmware.vcloud.org+xml" name="Test-Org" href="http://test-api-root/org/eb0537cb-a160-47da-97dc-fd83cb275f4a"/>
+ <Link rel="down" type="application/vnd.vmware.vcloud.query.queryList+xml" href="http://test-api-root/query"/>
+ <Link rel="entityResolver" type="application/vnd.vmware.vcloud.entity+xml" href="http://test-api-root/entity/"/>
+</Session>
View
@@ -0,0 +1,11 @@
+import os
+
+from nose.tools import *
+from mock import MagicMock, patch
+
+from .requestmocker import RequestMockTestCase
+
+HERE = os.path.dirname(__file__)
+
+def fixture(name):
+ return open(os.path.join(HERE, 'fixtures', name)).read()
View
@@ -0,0 +1,42 @@
+from mock import MagicMock, patch
+
+from requests import Response
+from requests.structures import CaseInsensitiveDict
+
+class RequestMockTestCase(object):
+
+ def setup(self):
+ if not hasattr(self, 'session_to_patch'):
+ raise RuntimeError("You should set self.session_to_patch before calling super(..., self).setup() in subclasses of RequestMockTestCase")
+
+ self._urls = {}
+
+ self._sess_patcher = patch(self.session_to_patch)
+ sess_mock = self._sess_patcher.start()
+ self._resp_mock = sess_mock.return_value.request
+
+ def _match(method, url, *args, **kwargs):
+ default = ((), {})
+ mr_args, mr_kwargs = self._urls.get((method, url), default)
+ return self._mock_response(*mr_args, **mr_kwargs)
+
+ self._resp_mock.side_effect = _match
+
+ def _mock_response(self, status_code=404, headers={}, content='PAGE_NOT_FOUND'):
+ r = MagicMock(Response())
+ r.status_code = status_code
+ r.headers = CaseInsensitiveDict(headers if headers is not None else {})
+ r.content = content
+ r.ok = status_code < 400
+ return r
+
+ def teardown(self):
+ self._sess_patcher.stop()
+
+ def register_response(self, method, url, *args, **kwargs):
+ self._urls[(method, url)] = (args, kwargs)
+
+ def unregister_response(self, method, url):
+ self._urls.pop((method, url))
+
+
No changes.
View
@@ -0,0 +1,68 @@
+import logging
+
+from ..helpers import *
+
+from vcloudtools.api import VCloudAPIClient, envkey, envget
+
+# This environment variable is expected to be set when testing. Tox will set
+# it (see tox.ini)
+API_ROOT = os.environ.get('VCLOUD_API_ROOT')
+
+class TestVCloudAPIClient(RequestMockTestCase):
+
+ def setup(self):
+ self.session_to_patch = 'vcloudtools.api.requests.Session'
+ super(TestVCloudAPIClient, self).setup()
+
+ self.login()
+
+ def login(self):
+ session_xml = fixture('session')
+ self.register_response('post', API_ROOT + '/sessions', 200, {'x-vcloud-authorization': 'test_token'}, session_xml)
+ self.register_response('get', API_ROOT + '/session', 200, {}, session_xml)
+
+ def logout(self):
+ self.register_response('get', API_ROOT + '/session', 401)
+
+ def test_login_should_set_token(self):
+ c = VCloudAPIClient()
+ assert_equal(c.token, None)
+ c.login('foo', 'bar')
+ assert_equal(c.token, 'test_token')
+
+ def test_browse_gets_api_root_with_no_args(self):
+ c = VCloudAPIClient()
+ self.register_response('get', API_ROOT + '/', 200, content='FOO BAR')
+
+ res = c.browse()
+ assert_equal(res.content, 'FOO BAR')
+
+ def test_browse_gets_specified_path(self):
+ c = VCloudAPIClient()
+ self.register_response('get', API_ROOT + '/wibble', 200, content='BAZ BAT')
+
+ res = c.browse('/wibble')
+ assert_equal(res.content, 'BAZ BAT')
+
+ def test_org_list_should_return_list_of_orgs(self):
+ c = VCloudAPIClient()
+ org_list_xml = fixture('orglist')
+
+ # org_list should work out where to look from the hypermedia returned
+ # at the /session path. "/myCustomOrgListPath/" is where the orgList
+ # is defined to exist in the "session" fixture
+ self.register_response('get', API_ROOT + '/myCustomOrgListPath/', 200, {}, org_list_xml)
+
+ org_list = c.org_list()
+ assert_equal(len(org_list.orgs), 3)
+
+
+def test_envkey():
+ assert_equal(envkey('foo'), 'VCLOUD_FOO')
+ assert_equal(envkey('foo_bar'), 'VCLOUD_FOO_BAR')
+
+
+@patch('vcloudtools.api.env')
+def test_envget(env_mock):
+ env_mock.get.return_value = 'fooval'
+ assert_equal(envget('foo'), 'fooval')
@@ -0,0 +1,54 @@
+import copy
+from nose.tools import *
+
+from vcloudtools.vcloud import Link, Org, OrgList
+
+MOCK_LINK = {
+ 'type': 'application/foo+xml',
+ 'href': 'https://test/foo',
+ 'rel': 'test_rel',
+ 'name': 'foo',
+}
+
+MOCK_ORG = {
+ 'type': 'application/vnd.vmware.vcloud.org+xml',
+ 'href': 'http://test-api-client/org/734772e4-166f-4dcc-9391-d35ddafce90e',
+ 'name': 'Test-Org',
+ 'id': 'urn:vcloud:org:734772e4-166f-4dcc-9391-d35ddafce90e',
+ 'full_name': 'Test Organisation',
+ 'description': None,
+ 'links': { },
+}
+
+class TestLink(object):
+
+ def setup(self):
+ self.l = copy.deepcopy(MOCK_LINK)
+
+ def test_name_optional(self):
+ self.l.pop('name')
+ link = Link(**self.l)
+ assert_equal(link.name, None)
+
+class TestOrg(object):
+
+ def test_optional(self):
+ def _test(key):
+ o = copy.deepcopy(MOCK_ORG)
+ o.pop(key)
+ org = Org(**o)
+ assert_equal(getattr(org, key), None)
+
+ for k in ['id', 'full_name', 'description', 'links']:
+ yield _test, k
+
+class TestOrgList(object):
+
+ def test_org_by_name(self):
+ foo = Org(type='testtype', href='testhref', name='foo')
+ bar = Org(type='testtype', href='testhref', name='bar')
+ baz = Org(type='testtype', href='testhref', name='baz')
+
+ olist = OrgList(orgs=[foo, bar, baz])
+
+ assert_equal(olist.org_by_name('bar'), bar)
View
@@ -0,0 +1,15 @@
+# Tox (http://tox.testrun.org/) is a tool for running tests
+# in multiple virtualenvs. This configuration file will run the
+# test suite on all supported python versions. To use it, "pip install tox"
+# and then run "tox" from this directory.
+
+[tox]
+envlist = py27, pypy
+
+[testenv]
+deps =
+ nose
+ mock
+setenv =
+ VCLOUD_API_ROOT=http://test-api-root
+commands = nosetests

0 comments on commit aed074e

Please sign in to comment.