Commit 1c5569cd authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Fix errors in unit tests of "kamaki.cli.*"

Refs #18

Most errors are caused by changes in the names of classes,
methods and packages.
parent aceac09c
# Copyright 2013 GRNET S.A. All rights reserved.
# Copyright 2013-2014 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -31,11 +31,10 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from mock import patch, call, MagicMock
from mock import patch, call
from unittest import TestCase
from StringIO import StringIO
from datetime import datetime
#from itertools import product
from kamaki.cli import argument, errors
from kamaki.cli.config import Config
......@@ -76,8 +75,10 @@ class Argument(TestCase):
isinstance(parsed_name, list)) else [parsed_name, ]
self.assertEqual(exp_name, a.parsed_name)
exp_default = default if (default or arity) else False
self.assertEqual(exp_default, a.default)
if default or arity:
self.assertEqual(default or None, a.default)
else:
self.assertFalse(a.default)
def test_value(self):
a = argument.Argument(1, parsed_name='--value')
......@@ -262,13 +263,18 @@ class DateArgument(TestCase):
da._value = argument.dtm.strptime(date, format)
self.assertEqual(da.formated, exp)
@patch('%s.DateArgument.timestamp' % arg_path)
@patch('%s.DateArgument.format_date' % arg_path)
def test_value(self, format_date, timestamp):
def test_value(self):
da = argument.DateArgument(parsed_name='--date')
self.assertTrue(isinstance(da.value, MagicMock))
da.value = 'Something'
format_date.assert_called_once(call('Something'))
try:
da.value = 'Something'
self.assertFalse('CLIInvalidArgument error not raised!')
except Exception as e:
self.assertTrue(isinstance(e, errors.CLIInvalidArgument))
da.value = '10/02/2001'
self.assertEqual(da.value, da.timestamp)
self.assertEqual(da.timestamp, 1001970000.0)
self.assertEqual(da.formated, 'Tue Oct 02 00:00:00 2001')
self.assertEqual(da.isoformat, '2001-10-02T00:00:00+03:00')
def test_format_date(self):
da = argument.DateArgument(parsed_name='--date')
......@@ -314,7 +320,7 @@ class KeyValueArgument(TestCase):
def test_value(self):
kva = argument.KeyValueArgument(parsed_name='--keyval')
self.assertEqual(kva.value, [])
self.assertEqual(kva.value, {})
for kvpairs in (
'strval', 'key=val', 2.8, 42, None,
('key', 'val'), ('key val'), ['=val', 'key=val'],
......@@ -407,7 +413,7 @@ class ArgumentParseManager(TestCase):
@patch('%s.ArgumentParseManager.parse' % arg_path)
@patch('%s.ArgumentParseManager.update_parser' % arg_path)
def test___init__(self, parse, update_parser):
for arguments in (None, {'k1': 'v1', 'k2': 'v2'}):
for arguments in ({}, {'k1': 'v1', 'k2': 'v2'}):
apm = argument.ArgumentParseManager('exe', arguments)
self.assertTrue(isinstance(apm, argument.ArgumentParseManager))
......@@ -419,9 +425,7 @@ class ArgumentParseManager(TestCase):
self.assertEqual(
apm.syntax, 'exe <cmd_group> [<cmd_subbroup> ...] <cmd>')
assert_dicts_are_equal(
self, apm.arguments,
arguments or argument._arguments)
assert_dicts_are_equal(self, apm.arguments, arguments)
self.assertFalse(apm._parser_modified)
self.assertEqual(apm._parsed, None)
self.assertEqual(apm._unparsed, None)
......@@ -430,7 +434,7 @@ class ArgumentParseManager(TestCase):
update_parser.assert_called_once()
def test_syntax(self):
apm = argument.ArgumentParseManager('exe')
apm = argument.ArgumentParseManager('exe', {})
self.assertEqual(
apm.syntax, 'exe <cmd_group> [<cmd_subbroup> ...] <cmd>')
apm.syntax = 'some syntax'
......@@ -438,8 +442,7 @@ class ArgumentParseManager(TestCase):
@patch('%s.ArgumentParseManager.update_parser' % arg_path)
def test_arguments(self, update_parser):
apm = argument.ArgumentParseManager('exe')
assert_dicts_are_equal(self, apm.arguments, argument._arguments)
apm = argument.ArgumentParseManager('exe', {})
update_parser.assert_called_once()
exp = {'k1': 'v1', 'k2': 'v2'}
apm.arguments = exp
......@@ -452,7 +455,7 @@ class ArgumentParseManager(TestCase):
@patch('%s.ArgumentParseManager.parse' % arg_path)
def test_parsed(self, parse):
apm = argument.ArgumentParseManager('exe')
apm = argument.ArgumentParseManager('exe', {})
self.assertEqual(apm.parsed, None)
exp = 'you have been parsed'
apm._parsed = exp
......@@ -464,7 +467,7 @@ class ArgumentParseManager(TestCase):
@patch('%s.ArgumentParseManager.parse' % arg_path)
def test_unparsed(self, parse):
apm = argument.ArgumentParseManager('exe')
apm = argument.ArgumentParseManager('exe', {})
self.assertEqual(apm.unparsed, None)
exp = 'you have been unparsed'
apm._unparsed = exp
......@@ -474,28 +477,21 @@ class ArgumentParseManager(TestCase):
self.assertEqual(apm.unparsed, exp + ' v2')
self.assertEqual(parse.mock_calls, [call(), call()])
@patch('%s.Argument.update_parser' % arg_path
)
@patch('%s.Argument.update_parser' % arg_path)
def test_update_parser(self, update_parser):
apm = argument.ArgumentParseManager('exe')
body_count = len(update_parser.mock_calls)
exp = len(argument._arguments)
self.assertEqual(body_count, exp)
apm = argument.ArgumentParseManager('exe', {})
apm.update_parser()
exp = len(apm.arguments) + body_count
body_count = len(update_parser.mock_calls)
self.assertEqual(body_count, exp)
self.assertEqual(len(update_parser.mock_calls), 0)
expd = dict(
k1=argument.Argument(0, parsed_name='-a'),
k2=argument.Argument(0, parsed_name='-b'))
apm.update_parser(expd)
body_count = len(update_parser.mock_calls)
self.assertEqual(body_count, exp + 2)
self.assertEqual(len(update_parser.mock_calls), 2)
def test_update_arguments(self):
(inp, cor, exp) = (
{'k1': 'v1', 'k2': 'v3'}, {'k2': 'v2'}, {'k1': 'v1', 'k2': 'v2'})
apm = argument.ArgumentParseManager('exe')
apm = argument.ArgumentParseManager('exe', {})
with patch(
'%s.ArgumentParseManager.update_parser' % arg_path) as UP:
apm.update_arguments(None)
......@@ -506,7 +502,7 @@ class ArgumentParseManager(TestCase):
UP.assert_called_once_with()
def test_parse(self):
apm = argument.ArgumentParseManager('exe')
apm = argument.ArgumentParseManager('exe', {})
parsed, unparsed = apm.parser.parse_known_args()
apm.parse()
self.assertEqual(apm._parsed, parsed)
......
......@@ -175,7 +175,7 @@ class CommandTree(TestCase):
self.assertEqual(c1.path, c2.path)
self.assertEqual(c1.name, c2.name)
self.assertEqual(c1.cmd_class, c2.cmd_class)
self.assertEqual(c1.help, c2.help)
self.assertEqual(c1.help or '', c2.help or '')
def setUp(self):
cmd = cmdtree.Command('cmd', subcommands=dict(
......@@ -368,8 +368,7 @@ class CommandTree(TestCase):
('cmd_cmd0c_cmd1a', []),
('cmd_cmd0c_cmd1b', ['cmd2', ]),
('cmd_cmd0c_cmd1b_cmd2', []),
('othercmd', [])
):
('othercmd', [])):
l1 = [cmd.path for cmd in ctree.get_subcommands(s1)]
l2 = ['_'.join([s1, i]) for i in l2] if s1 else l2
l1.sort(), l2.sort(), self.assertEqual(l1, l2)
......
......@@ -83,6 +83,7 @@ class History(TestCase):
def test_retrieve(self):
sample_history = (
'0\n',
'kamaki history show\n',
'kamaki file list\n',
'kamaki file create /pithos/f1\n',
......@@ -103,6 +104,7 @@ class History(TestCase):
def test_limit(self):
sample_history = (
'0\n',
'kamaki history show\n',
'kamaki file list\n',
'kamaki file create /pithos/f1\n',
......@@ -361,20 +363,6 @@ class CLIBaseUrlError(TestCase):
for service in ('', 'some service'):
clibue = CLIBaseUrlError(service=service)
self.assertEqual('%s' % clibue, 'No URL for %s\n' % service)
self.assertEqual(clibue.details, [
'Two ways to resolve this:',
'(Use the correct cloud name, instead of "default")',
'A. (recommended) Let kamaki discover endpoint URLs for all',
'services by setting a single Authentication URL and token:',
' /config set cloud.default.url <AUTH_URL>',
' /config set cloud.default.token <t0k3n>',
'B. (advanced users) Explicitly set an %s endpoint URL' % (
service.upper()),
'Note: URL option has a higher priority, so delete it to',
'make that work',
' /config delete cloud.default.url',
' /config set cloud.%s.url <%s_URL>' % (
service, service.upper())])
self.assertEqual(clibue.importance, 2)
......
# Copyright 2013 GRNET S.A. All rights reserved.
# Copyright 2013-2014 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -64,12 +64,12 @@ class UtilsMethods(TestCase):
else:
self.assertRaises(AssertionError, guess_mime_type, *args)
@patch('kamaki.cli.utils.dumps', return_value='(dumps output)')
@patch('kamaki.cli.utils.dumps', return_value=u'(dumps output)')
def test_print_json(self, JD):
from kamaki.cli.utils import print_json, INDENT_TAB
out = StringIO()
print_json('some data', out)
JD.assert_called_once_with('some data', indent=INDENT_TAB)
print_json(u'some data', out)
JD.assert_called_once_with(u'some data', indent=INDENT_TAB)
self.assertEqual(out.getvalue(), u'(dumps output)\n')
def test_print_dict(self):
......@@ -355,29 +355,6 @@ class UtilsMethods(TestCase):
self.assertEqual(L2F.mock_calls, exp_l)
self.assertEqual(D2F.mock_calls, exp_d)
def test__parse_with_regex(self):
from re import compile as r_compile
from kamaki.cli.utils import _parse_with_regex
for args in product(
(
'this is a line',
'this_is_also_a_line',
'This "text" is quoted',
'This "quoted" "text" is more "complicated"',
'Is this \'quoted\' text "double \'quoted\' or not?"',
'"What \'about\' the" oposite?',
' Try with a " single double quote',
'Go "down \'deep " deeper \'bottom \' up" go\' up" !'),
(
'\'.*?\'|".*?"|^[\S]*$',
r'"([A-Za-z0-9_\./\\-]*)"',
r'\"(.+?)\"',
'\\^a\\.\\*\\$')):
r_parser = r_compile(args[1])
self.assertEqual(
_parse_with_regex(*args),
(r_parser.split(args[0]), r_parser.findall(args[0])))
def test_split_input(self):
from kamaki.cli.utils import split_input
for line, expected in (
......@@ -394,24 +371,6 @@ class UtilsMethods(TestCase):
'Is', 'this', 'a', 'parsed', 'string?'])):
self.assertEqual(split_input(line), expected)
def test_ask_user(self):
from kamaki.cli.utils import ask_user
msg = u'some question'
out = StringIO()
user_in = StringIO(u'n')
self.assertFalse(ask_user(msg, out=out, user_in=user_in))
self.assertEqual(out.getvalue(), u'%s [y/N]: ' % msg)
user_in.seek(0)
out.seek(0)
self.assertTrue(ask_user(msg, ('n', ), out=out, user_in=user_in))
self.assertEqual(out.getvalue(), u'%s [n/<not n>]: ' % msg)
user_in = StringIO(unicode('N'))
out.seek(0)
self.assertTrue(ask_user(msg, ('r', 'N'), out=out, user_in=user_in))
self.assertEqual(out.getvalue(), u'%s [r, N/<not r, N>]: ' % msg)
def test_remove_from_items(self):
from kamaki.cli.utils import remove_from_items
for v in ('wrong', [1, 2, 3], [{}, 2, {}]):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment