Skip to content

Commit

Permalink
#11: support for updating/creating tags, unit tests for tags operations
Browse files Browse the repository at this point in the history
  • Loading branch information
shroffk committed Dec 11, 2015
1 parent 8a20e63 commit 1696b62
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 49 deletions.
39 changes: 27 additions & 12 deletions channelfinder/ChannelFinderClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def __hadleSingleAddParameter(self, **kwds):
r.raise_for_status()
elif 'tags' in kwds:
data = JSONEncoder().encode(kwds['tags'])
r = self.__session.post(self.__baseURL + self.__tagsResource, \
r = self.__session.put(self.__baseURL + self.__tagsResource, \
data=data, \
headers=copy(self.__jsonheader), \
verify=False, \
Expand All @@ -167,18 +167,22 @@ def __hadleSingleAddParameter(self, **kwds):
def __handleMultipleAddParameters(self, **kwds):
# set a tag to a channel
if 'tag' in kwds and 'channelName' in kwds:
channels = [{u'name':kwds['channelName'].strip(), u'owner':self.__userName, u'tags':[kwds['tag']]}]
channels = [{u'name':kwds['channelName'].strip(), u'owner':self.__userName}]
kwds['tag']['channels'] = channels
data = kwds['tag']
self.__session.put(self.__baseURL + self.__tagsResource + '/' + kwds['tag'][u'name'], \
data=JSONEncoder().encode(kwds['tag'], withChannels=channels), \
data=JSONEncoder().encode(data), \
headers=copy(self.__jsonheader), \
verify=False, \
auth=self.__auth).raise_for_status()
elif 'tag' in kwds and 'channelNames' in kwds:
channels = []
for eachChannel in kwds['channelNames']:
channels.append({u'name':eachChannel, u'owner':self.__userName, u'tags':[kwds['tag']]})
channels.append({u'name':eachChannel, u'owner':self.__userName})
kwds['tag']['channels'] = channels
data = kwds['tag']
self.__session.put(self.__baseURL + self.__tagsResource + '/' + kwds['tag'][u'name'], \
data=JSONEncoder().encode(self.__encodeTag(kwds['tag'], withChannels=channels)), \
data=JSONEncoder().encode(data), \
headers=copy(self.__jsonheader), \
verify=False, \
auth=self.__auth).raise_for_status()
Expand Down Expand Up @@ -435,6 +439,7 @@ def __handleMultipleDeleteParameters(self, **kwds):
# find channels with the tag
channelsWithTag = self.find(tagName=kwds['tag'][u'name'])
# delete channels from which tag is to be removed

channelNames = [channel[u'name'] for channel in channelsWithTag if channel[u'name'] not in kwds['channelNames']]
self.set(tag=kwds['tag'], channelNames=channelNames)
elif 'property' in kwds and 'channelName' in kwds:
Expand Down Expand Up @@ -518,25 +523,35 @@ def update(self, **kwds):
def __handleSingleUpdateParameter(self, **kwds):
if 'channel' in kwds:
ch = kwds['channel']
self.__session.post(self.__baseURL + self.__channelsResource + '/' + ch[u'name'], \
r = self.__session.post(self.__baseURL + self.__channelsResource + '/' + ch[u'name'], \
data=JSONEncoder().encode(self.__encodeChannel(ch)), \
headers=copy(self.__jsonheader), \
verify=False, \
auth=self.__auth).raise_for_status()
auth=self.__auth)
r.raise_for_status()
elif 'property' in kwds:
property = kwds['property']
self.__session.post(self.__baseURL + self.__propertiesResource + '/' + property[u'name'], \
r = self.__session.post(self.__baseURL + self.__propertiesResource + '/' + property[u'name'], \
data=JSONEncoder().encode(self.__encodeProperty(property)), \
headers=copy(self.__jsonheader), \
verify=False, \
auth=self.__auth).raise_for_status()
auth=self.__auth)
r.raise_for_status()
elif 'tag' in kwds:
tag = kwds['tag']
self.__session.post(self.__baseURL + self.__tagsResource + '/' + tag[u'name'], \
data=JSONEncoder().encode(self.__encodeTag(tag)), \
r = self.__session.post(self.__baseURL + self.__tagsResource + '/' + tag[u'name'], \
data=JSONEncoder().encode(tag), \
headers=copy(self.__jsonheader), \
verify=False, \
auth=self.__auth).raise_for_status()
auth=self.__auth)
r.raise_for_status()
elif 'tags' in kwds:
r = self.__session.post(self.__baseURL + self.__tagsResource, \
data=JSONEncoder().encode(kwds['tags']), \
headers=copy(self.__jsonheader), \
verify=False, \
auth=self.__auth)
r.raise_for_status()
else:
raise Exception, ' unkown key '

Expand Down
105 changes: 68 additions & 37 deletions test/ChannelFinderClientTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,27 +144,6 @@ def testCreateAndDeleteTags(self):
self.assertEqual(self.client.findTag(tagName='pyTag1'), None, \
'Error: tag ' + tag[u'name'] + ' was not removed')

def testUpdateTag(self):
'''Test the updating of a tag'''
initialTag = {'name':'initialTestTag', 'owner':self.tagOwner}
updatedTag = {'name':'updatedTestTag', 'owner':self.tagOwner}
try:
'''Create initial tag'''
self.clientTag.set(tag=initialTag)
self.assertIsNotNone(self.client.findTag(initialTag['name']), 'failed to create a test tag')
'''Update tag'''
self.clientTag.update(tag = updatedTag, originalTagName = initialTag['name'])
self.assertEqual(self.client.findTag(updatedTag['name']), updatedTag, 'Failed to updated initial tag to new tag')
self.assertIsNone(self.client.findTag(initialTag['name']), 'Initially created tag still present')
finally:
'''cleanup'''
if self.client.findTag(updatedTag['name']):
self.client.delete(tagName=updatedTag['name'])
self.assertIsNone(self.client.findTag(updatedTag['name']), 'failed to delete the test tag:'+updatedTag['name'])
if self.client.findTag(initialTag['name']):
self.client.delete(tagName=initialTag['name'])
self.assertIsNone(self.client.findTag(initialTag['name']), 'failed to delete the test tag:'+initialTag['name'])

def testSetRemoveTag2Channel(self):
'''
Set Tag to channel removing it from all other channels
Expand All @@ -175,24 +154,27 @@ def testSetRemoveTag2Channel(self):
self.client.set(tag=testTag)
self.client.set(tag=testTag, channelName=self.testChannels[0][u'name'])

self.assertTrue(testTag in self.client.find(name='pyTestChannel1')[0][u'tags'], \
self.assertTrue(checkTagOnChannel(self.client, 'pyTestChannel1', testTag) , \
'Error: Tag-pySetTag not added to the channel-pyTestChannel1')

self.client.set(tag=testTag, channelName=self.testChannels[1][u'name'])
# check if the tag has been added to the new channel and removed from the old channel
self.assertTrue(self.__checkTagExists(self.testChannels[1][u'name'], testTag) and
not self.__checkTagExists(self.testChannels[0][u'name'], testTag), \
self.assertTrue(checkTagOnChannel(self.client, self.testChannels[1][u'name'], testTag) and
not checkTagOnChannel(self.client, self.testChannels[0][u'name'], testTag), \
'Error: Tag-pySetTag not added to the channel-pyTestChannel2')

self.client.delete(tag=testTag, channelName=self.testChannels[1][u'name'])
self.assertTrue(not self.__checkTagExists(self.testChannels[1][u'name'], testTag), \
self.assertTrue(not checkTagOnChannel(self.client, self.testChannels[1][u'name'], testTag), \
'Error: Failed to delete the tag-pySetTag from channel-pyTestChannel1')
finally:
self.client.delete(tagName=testTag[u'name'])

# TODO set a check for removing the tag from a subset of channels which have that tag

def testSetRemoveTag2Channels(self):
'''
Set tags to a set of channels and remove it from all other channels
'''
testTag = {u'name':u'pySetTag', u'owner':self.tagOwner}
# the list comprehension is used to construct a list of all the channel names
channelNames = [channel[u'name'] for channel in self.testChannels]
Expand All @@ -210,6 +192,57 @@ def testSetRemoveTag2Channels(self):
finally:
self.client.delete(tagName=testTag[u'name'])

def testUpdateTag(self):
'''
Add a tag to a group of channels without removing it from existing channels
'''
tag = {'name':'initialTestTag', 'owner':self.tagOwner}
tag['channels'] = [self.testChannels[0]]
try:
'''Create initial tag'''
self.clientTag.set(tag=tag)
self.assertIsNotNone(self.client.findTag(tag['name']), 'failed to create a test tag')
'''Update tag with new channels'''
tag['channels'] = [self.testChannels[1], self.testChannels[2]]
self.clientTag.update(tag = tag)

for channel in self.testChannels:
self.assertTrue(checkTagOnChannel(self.client, channel['name'], tag), 'Failed to updated tag')
finally:
'''cleanup'''
self.client.delete(tagName=tag['name'])
self.assertIsNone(self.client.findTag(tag['name']), 'failed to delete the test tag:'+tag['name'])

def testUpdateTags(self):
'''
Add tags to a group of channels without removing it from existing channels
'''
tag1 = {'name':'pyTestTag1', 'owner':self.tagOwner}
tag1['channels'] = [self.testChannels[0]]
tag2 = {'name':'pyTestTag2', 'owner':self.tagOwner}
tag2['channels'] = [self.testChannels[0]]

try:
'''Create initial tags which are set on the pyTestChannel1'''
self.clientTag.set(tags=[tag1,tag2])
self.assertIsNotNone(self.client.findTag(tag1['name']), 'failed to create a test tag: pyTestTag1')
self.assertIsNotNone(self.client.findTag(tag1['name']), 'failed to create a test tag: pyTestTag2')
'''Update tags with new channels'''
tag1['channels'] = [self.testChannels[1], self.testChannels[2]]
tag2['channels'] = [self.testChannels[1], self.testChannels[2]]
self.clientTag.update(tags = [tag1, tag2])
'''Check that the all channels have been updated with the tags'''
for channel in self.testChannels:
self.assertTrue(checkTagOnChannel(self.client, channel['name'], tag1) and \
checkTagOnChannel(self.client, channel['name'], tag2), \
'Failed to updated tags')
finally:
'''cleanup'''
self.client.delete(tagName=tag1['name'])
self.client.delete(tagName=tag2['name'])
self.assertIsNone(self.client.findTag(tag1['name']), 'failed to delete the test tag:'+tag1['name'])
self.assertIsNone(self.client.findTag(tag2['name']), 'failed to delete the test tag:'+tag2['name'])

def testGetAllTags(self):
'''Test setting multiple tags and listing all tags'''
testTags = []
Expand Down Expand Up @@ -476,19 +509,7 @@ def tearDown(self):
for ch in self.testChannels:
self.client.delete(channelName=ch[u'name'])
pass
'''

'''
def __checkTagExists(self, channelName, tag):
'''
utility method which return true is channelName contains tag
'''
ch = self.client.find(name=channelName)[0]
if ch[u'tags'] != None and tag in ch[u'tags']:
return True
else:
return False

'''
def testSetRemoveProperty2Channel(self):
''''''
Expand Down Expand Up @@ -1091,6 +1112,16 @@ def checkPropInList(allProps, props):
[ found.append(prop) for p in allProps if p['name'] == prop['name'] and p['owner'] == prop['owner'] ]
return props == found

def checkTagOnChannel(client, channelName, tag):
'''
utility method which return true is channelName contains tag
'''
ch = client.find(name=channelName)[0]
if ch[u'tags'] != None and checkTagInList(ch[u'tags'], [tag]):
return True
else:
return False

def getDefaultTestConfig(arg):
if _testConf.has_option('DEFAULT', arg):
return _testConf.get('DEFAULT', arg)
Expand Down

0 comments on commit 1696b62

Please sign in to comment.