Added tests, fixed broken filter when missing tags argument

This commit is contained in:
Johan Stenberg 2016-04-26 14:19:07 -07:00
Родитель 0cd0f7243e
Коммит 7d80fb8f1f
3 изменённых файлов: 84 добавлений и 10 удалений

Просмотреть файл

@ -62,6 +62,10 @@
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_params.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_validators.py" />
<Compile Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\tests\test_custom_vm_commands.py" />
<Compile Include="azure\cli\utils\command_test_script.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="command_modules\azure-cli-resource\azure\cli\command_modules\resource\tests\test_resource_list_odata_filter.py" />
<Compile Include="command_modules\azure-cli-resource\azure\cli\command_modules\resource\tests\test_api_check.py">
<SubType>Code</SubType>
</Compile>

Просмотреть файл

@ -71,6 +71,11 @@ def _list_resources_odata_filter_builder(args):
'''
filters = []
name = args.get('name')
if name:
filters.append("name eq '%s'" % name)
location = args.get('location')
if location:
filters.append("location eq '%s'" % location)
@ -79,22 +84,19 @@ def _list_resources_odata_filter_builder(args):
if resource_type:
filters.append("resourceType eq '%s'" % resource_type)
name = args.get('name')
if name:
filters.append("name eq '%s'" % name)
tag = args.get('tag') or ''
if tag and (name or location):
raise IncorrectUsageError('you cannot use the tagname or tagvalue filters with other filters')
tag_name_value = tag.split('=')
tag_name = tag_name_value[0]
if tag_name[-1] == '*':
filters.append("startswith(tagname, '%s')" % tag_name[0:-1])
else:
filters.append("tagname eq '%s'" % tag_name_value[0])
if len(tag_name_value) == 2:
filters.append("tagvalue eq '%s'" % tag_name_value[1])
if tag_name:
if tag_name[-1] == '*':
filters.append("startswith(tagname, '%s')" % tag_name[0:-1])
else:
filters.append("tagname eq '%s'" % tag_name_value[0])
if len(tag_name_value) == 2:
filters.append("tagvalue eq '%s'" % tag_name_value[1])
return ' and '.join(filters)
@command_table.command('resource list', description=L('List resources'))

Просмотреть файл

@ -0,0 +1,68 @@
import unittest
from azure.cli.command_modules.resource import _list_resources_odata_filter_builder
from azure.cli.parser import IncorrectUsageError
class TestListResources(unittest.TestCase):
@classmethod
def setUpClass(cls):
pass
@classmethod
def tearDownClass(cls):
pass
def setUp(self):
pass
def tearDown(self):
pass
def test_tag_name(self):
args = {
'tag': 'foo'
}
filter = _list_resources_odata_filter_builder(args)
self.assertEquals(filter, "tagname eq 'foo'")
def test_tag_name_starts_with(self):
args = {
'tag': 'f*'
}
filter = _list_resources_odata_filter_builder(args)
self.assertEquals(filter, "startswith(tagname, 'f')")
def test_tag_name_value_equals(self):
args = {
'tag': 'foo=bar'
}
filter = _list_resources_odata_filter_builder(args)
self.assertEquals(filter, "tagname eq 'foo' and tagvalue eq 'bar'")
def test_name_location_equals(self):
args = {
'name': 'wonky',
'location': 'dory',
'resourcetype': 'resource/type'
}
filter = _list_resources_odata_filter_builder(args)
self.assertEquals(filter, "name eq 'wonky' and location eq 'dory' and resourceType eq 'resource/type'")
def test_name_location_equals(self):
args = {
'name': 'wonky',
'location': 'dory'
}
filter = _list_resources_odata_filter_builder(args)
self.assertEquals(filter, "name eq 'wonky' and location eq 'dory'")
def test_tag_and_name_fails(self):
args = {
'tag': 'foo=bar',
'name': 'will not work'
}
with self.assertRaises(IncorrectUsageError):
filter = _list_resources_odata_filter_builder(args)
if __name__ == '__main__':
unittest.main()