Skip to content

Commit

Permalink
Fix bug on optional values
Browse files Browse the repository at this point in the history
  • Loading branch information
allevo committed Jun 21, 2013
1 parent f07b49a commit 3f7d80f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
17 changes: 12 additions & 5 deletions resuds/factory.py
Expand Up @@ -14,7 +14,8 @@ def __init__(self, name, attribute_keys, children_keys):
self.attribute_keys = attribute_keys
self.children_keys = children_keys
self.attrs = dict((Factory.clean(attr), '') for attr in attribute_keys)
self.children = dict((Factory.clean(child), None) for child in children_keys)
self.children = dict((Factory.clean(child), None)
for child in children_keys)

def __setattr__(self, k, v):
if k in ('cls_name', 'attribute_keys', 'children_keys', 'attrs', 'children'):
Expand Down Expand Up @@ -58,7 +59,8 @@ def rebuild(cls, obj):
return str(obj)

attrs = [attr for attr in obj.__keylist__ if attr.startswith('_')]
children = [child for child in obj.__keylist__ if not child.startswith('_')]
children = [
child for child in obj.__keylist__ if not child.startswith('_')]
if cls.is_list(obj):
if set(children) == set(['EntityId']):
return SoapObject('EntityIdList', [], ['entityid'])
Expand Down Expand Up @@ -101,9 +103,13 @@ def create_object(cls, factory, obj):
return EntityIdList(obj.children['entityid'])
soap = factory.create(obj.cls_name)
for key in obj.attribute_keys:
setattr(soap, key, getattr(obj, cls.clean(key)))
val = getattr(obj, cls.clean(key))
if val:
setattr(soap, key, val)
for key in obj.children_keys:
setattr(soap, key, cls.build(factory, getattr(obj, cls.clean(key))))
val = getattr(obj, cls.clean(key))
if val:
setattr(soap, key, cls.build(factory, val))
return soap

@classmethod
Expand All @@ -114,5 +120,6 @@ def create_list(cls, factory, l):
else:
element_class_name = l[0].cls_name
obj_list = factory.create(element_class_name + 'List')
setattr(obj_list, element_class_name, [cls.build(factory, el) for el in l])
setattr(obj_list, element_class_name, [
cls.build(factory, el) for el in l])
return obj_list
64 changes: 50 additions & 14 deletions resuds/tests/test_factory.py
Expand Up @@ -4,16 +4,19 @@
from suds.client import SoapClient
from resuds.tests import LOCALDIR
from resuds.api import ResudsClient
from resuds.factory import Factory
from resuds.factory import Factory, EntityIdList


class FactoryTestCase(TestCase):
wsdl_file = 'file://' + os.path.join(LOCALDIR, 'inventory_service.wsdl')
report_file = 'file://' + os.path.join(LOCALDIR, 'PublisherRevenueWS.wsdl')

def testIsList(self):
import collections
self.assertTrue(self.factory.is_list(collections.namedtuple('MyList', [])()))
self.assertFalse(self.factory.is_list(collections.namedtuple('Object', [])()))
self.assertTrue(self.factory.is_list(
collections.namedtuple('MyList', [])()))
self.assertFalse(self.factory.is_list(
collections.namedtuple('Object', [])()))

def testRebuildNoneObject(self):
self.assertEquals('', self.factory.rebuild(None))
Expand All @@ -23,13 +26,16 @@ def testRebuildPrimitiveType(self):

def testRebuildDateTime(self):
import datetime
self.assertEquals('2013-02-22 00:00:00', self.factory.rebuild(datetime.datetime(2013, 02, 22)))
self.assertEquals('2013-02-22 00:00:00', self.factory.rebuild(
datetime.datetime(2013, 02, 22)))

def testBuildEmptyList(self):
self.assertEquals([], self.factory.build(self.client.client.factory, []))
self.assertEquals([], self.factory.build(
self.client.client.factory, []))

def testBuildSimpleList(self):
self.assertEquals([1, 4, 6, ], self.factory.build(self.client.client.factory, [1, 4, 6, ]))
self.assertEquals([1, 4, 6, ], self.factory.build(
self.client.client.factory, [1, 4, 6, ]))

def testBuildComplicatedList(self):
adspace = self.createAdspace()
Expand All @@ -40,7 +46,8 @@ def testBuildComplicatedList(self):
)

def testBuildSimpleObject(self):
self.assertEquals('1', self.factory.build(self.client.client.factory, 1))
self.assertEquals('1', self.factory.build(
self.client.client.factory, 1))

def testBuildComplicatedObject(self):
adspace = self.createAdspace()
Expand All @@ -66,7 +73,8 @@ def testGetUnexistentProperty(self):

def testSetUnexistentProperty(self):
adspace = self.createAdspace()
self.assertRaises(AttributeError, setattr, adspace, 'dummy', 'dummyvalue')
self.assertRaises(
AttributeError, setattr, adspace, 'dummy', 'dummyvalue')

def testChangeObjectProperty(self):
adspace = self.createAdspace()
Expand All @@ -79,7 +87,8 @@ def testAddListToObject(self):
5, adspace.formatresourcetypelist[0].formatid
)
self.assertEquals(
[5, 6, ], adspace.formatresourcetypelist[0].resourcetypeidlist.entityid
[5, 6, ], adspace.formatresourcetypelist[
0].resourcetypeidlist.entityid
)

def testEntityIdList(self):
Expand All @@ -93,26 +102,53 @@ def testEntityIdList(self):
self.assertTrue('<EntityId>4</EntityId>' in envelope)
self.assertTrue('</CategoryIdList>' in envelope)

@httprettified
def testCreateObjectWithoutOptionalParameters(self):
self.setupDefaultSchemas()
client = ResudsClient(FactoryTestCase.report_file, faults=False)

criteria = client.create('PublisherRevenueCriteria')
criteria.startdate = '2013-05-03+00:00'
criteria.enddate = '2013-06-03+00:00'
criteria.publisherid = 50214056
envelope = self.getEnvelope(
criteria, method='GetPublisherRevenueCSV', client=client)
self.assertTrue('<criteria>' in envelope and '</criteria>' in envelope)
self.assertTrue('<publisherId>50214056</publisherId>' in envelope)
self.assertTrue('<startDate>2013-05-03+00:00</startDate>' in envelope)
self.assertTrue('<endDate>2013-06-03+00:00</endDate>' in envelope)
self.assertTrue('<endDate>2013-06-03+00:00</endDate>' in envelope)
self.assertTrue('<adspaceId>' not in envelope)

def testEntityIsListToString(self):
entityIdList = EntityIdList(['value1', 'value2'])
self.assertEquals(
'<EntityId>value1</EntityId><EntityId>value2</EntityId>', str(entityIdList))

@httprettified
def setUp(self):
self.setupDefaultSchemas()
self.client = ResudsClient(FactoryTestCase.wsdl_file, faults=False)
self.factory = Factory

def getEnvelope(self, obj, method='CreateAdspace'):
soapObject = Factory.build(self.client.client.factory, obj)
method = getattr(self.client.client.service, method)
def getEnvelope(self, obj, method='CreateAdspace', client=None):
if not client:
client = self.client
soapObject = Factory.build(client.client.factory, obj)
method = getattr(client.client.service, method)
c = SoapClient(method.client, method.method)
return str(c.method.binding.input.get_message(c.method, ('name', 'pwd', 'opId', soapObject, ), {}))

def setupDefaultSchemas(self):
with open(os.path.join(LOCALDIR, 'XMLSchema.xsd')) as fp:
body = fp.read()
HTTPretty.register_uri(HTTPretty.GET, 'http://www.w3.org/2001/XMLSchema.xsd', body=body)
HTTPretty.register_uri(
HTTPretty.GET, 'http://www.w3.org/2001/XMLSchema.xsd', body=body)

with open(os.path.join(LOCALDIR, 'xml.xsd')) as fp:
body = fp.read()
HTTPretty.register_uri(HTTPretty.GET, 'http://www.w3.org/2001/xml.xsd', body=body)
HTTPretty.register_uri(
HTTPretty.GET, 'http://www.w3.org/2001/xml.xsd', body=body)

def createAdspace(self):
adspace = self.client.create('Adspace')
Expand Down

0 comments on commit 3f7d80f

Please sign in to comment.