Skip to content

Commit

Permalink
Implemented method to create a property group
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoruiz committed Mar 12, 2014
1 parent 9aed0fe commit c22e2af
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
47 changes: 47 additions & 0 deletions hubspot/contacts/property_groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
##############################################################################
#
# Copyright (c) 2014, 2degrees Limited.
# All Rights Reserved.
#
# This file is part of hubspot-contacts
# <https://github.com/2degrees/hubspot-contacts>, which is subject to the
# provisions of the BSD at
# <http://dev.2degreesnetwork.com/p/2degrees-license.html>. A copy of the
# license should accompany this distribution. THIS SOFTWARE IS PROVIDED "AS IS"
# AND ANY AND ALL EXPRESS OR IMPLIED WARRANTIES ARE DISCLAIMED, INCLUDING, BUT
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST
# INFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
#
##############################################################################

from pyrecord import Record
from voluptuous import Schema


PropertyGroup = Record.create_type(
'PropertyGroup',
'name',
'display_name',
display_name=None,
)


_PROPERTY_GROUP_CREATION_SCHEMA = Schema(
{'name': unicode, 'displayName': unicode},
required=True,
extra=True,
)


def create_property_group(property_group, connection):
request_body_deserialization = {'name': property_group.name}
if property_group.display_name:
request_body_deserialization['displayName'] = \
property_group.display_name

response_data = connection.send_put_request(
'/groups/' + property_group.name,
request_body_deserialization,
)
response_data = _PROPERTY_GROUP_CREATION_SCHEMA(response_data)
return response_data
106 changes: 106 additions & 0 deletions tests/test_property_groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
##############################################################################
#
# Copyright (c) 2014, 2degrees Limited.
# All Rights Reserved.
#
# This file is part of hubspot-contacts
# <https://github.com/2degrees/hubspot-contacts>, which is subject to the
# provisions of the BSD at
# <http://dev.2degreesnetwork.com/p/2degrees-license.html>. A copy of the
# license should accompany this distribution. THIS SOFTWARE IS PROVIDED "AS IS"
# AND ANY AND ALL EXPRESS OR IMPLIED WARRANTIES ARE DISCLAIMED, INCLUDING, BUT
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST
# INFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
#
##############################################################################

from nose.tools import assert_in
from nose.tools import assert_not_in
from nose.tools import assert_raises
from nose.tools import eq_

from hubspot.contacts.exc import HubspotClientError
from hubspot.contacts.property_groups import PropertyGroup
from hubspot.contacts.property_groups import create_property_group

from tests.utils import BaseMethodTestCase
from tests.utils import RemoteMethod
from tests.utils.connection import MockPortalConnection
from tests.utils.generic import get_uuid4_str


class TestPropertyGroupCreation(BaseMethodTestCase):

_PROPERTY_GROUP_NAME = 'test-property-group'

_REMOTE_METHOD = RemoteMethod('/groups/' + _PROPERTY_GROUP_NAME, 'PUT')

def test_display_name_specified(self):
property_group = \
PropertyGroup(self._PROPERTY_GROUP_NAME, 'Test Property Group')

request_data, response_data = \
self._create_property_group(property_group)

eq_(property_group.name, request_data.body_deserialization['name'])
eq_(
property_group.display_name,
request_data.body_deserialization['displayName'],
)

eq_(property_group.name, response_data['name'])
eq_(property_group.display_name, response_data['displayName'])

def test_display_name_not_specified(self):
property_group = PropertyGroup(self._PROPERTY_GROUP_NAME)

request_data, response_data = \
self._create_property_group(property_group)

assert_not_in('displayName', request_data.body_deserialization)

eq_('', response_data['displayName'])

def test_group_already_exists(self):
connection = MockPortalConnection(
_replicate_create_property_group_error_response,
)

property_group = PropertyGroup(self._PROPERTY_GROUP_NAME)

with assert_raises(HubspotClientError) as context_manager:
create_property_group(property_group, connection)

exception = context_manager.exception
assert_in(property_group.name, str(exception))

def _create_property_group(self, property_group):
connection = \
MockPortalConnection(_replicate_create_property_group_response_data)

response_data = create_property_group(property_group, connection)

self._assert_expected_remote_method_used(connection)

eq_(1, len(connection.requests_data))
request_data = connection.requests_data[0]

return request_data, response_data


def _replicate_create_property_group_response_data(request_data):
response_data = {
'name': request_data.body_deserialization['name'],
'displayName': request_data.body_deserialization.get('displayName', ''),
'displayOrder': 1,
'portalId': 1,
}
return response_data


def _replicate_create_property_group_error_response(request_data):
property_group_name = request_data.body_deserialization['name']
raise HubspotClientError(
"The Group named '{}' already exists.".format(property_group_name),
get_uuid4_str(),
)
8 changes: 8 additions & 0 deletions tests/utils/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def send_post_request(self, path_info, body_deserialization):
)
return self._send_request(request_data)

def send_put_request(self, path_info, body_deserialization):
request_data = MockRequestData(
'PUT',
path_info,
body_deserialization=body_deserialization,
)
return self._send_request(request_data)

def _send_request(self, request_data):
self.requests_data.append(request_data)

Expand Down

0 comments on commit c22e2af

Please sign in to comment.