Commit 98093aac authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Complete unittests for kamaki.cli.history class

Refs: #4058
parent c4a93182
......@@ -48,23 +48,20 @@ class History(object):
return True
def get(self, match_terms=None, limit=0):
print 'HER?', self.filepath
limit = int(limit) or 0
with open(self.filepath, 'r') as f:
result = ['%s. \t%s' % (
i + 1, line) for i, line in enumerate(f.readlines())
if self._match(line, match_terms)]
offset = len(result) - limit if (
limit and len(result) > limit) else 0
return result[offset:]
return result[- limit:]
def add(self, line):
f = open(self.filepath, 'a+')
f.write(line + '\n')
with open(self.filepath, 'a+') as f:
f.write(line + '\n')
def clean(self):
f = open(self.filepath, 'w')
with open(self.filepath, 'w'):
def retrieve(self, cmd_id):
......@@ -33,6 +33,7 @@
from unittest import makeSuite, TestSuite, TextTestRunner, TestCase
from inspect import getmembers, isclass
from tempfile import NamedTemporaryFile
from kamaki.cli.command_tree.test import Command, CommandTree
from kamaki.cli.argument.test import (
......@@ -41,6 +42,95 @@ from kamaki.cli.argument.test import (
KeyValueArgument, ProgressBarArgument, ArgumentParseManager)
class History(TestCase):
def setUp(self):
from kamaki.cli.history import History as HClass
self.HCLASS = HClass
self.file = NamedTemporaryFile()
def tearDown(self):
def test__match(self):
self.assertRaises(AttributeError, self.HCLASS._match, 'ok', 42)
self.assertRaises(TypeError, self.HCLASS._match, 2.71, 'ok')
for args, expected in (
(('XXX', None), True),
((None, None), True),
(('this line has some terms', 'some terms'), True),
(('this line has some terms', 'some bad terms'), False),
(('small line', 'not so small line terms'), False),
((['line', 'with', 'some', 'terms'], 'some terms'), True),
((['line', 'with', 'some terms'], 'some terms'), False)):
self.assertEqual(self.HCLASS._match(*args), expected)
def test_get(self):
history = self.HCLASS(
self.assertEqual(history.get(), [])
sample_history = (
'kamaki history show\n',
'kamaki file list\n',
'kamaki touch pithos:f1\n',
'kamaki file info pithos:f1\n')
expected = ['%s. \t%s' % (
i + 1, event) for i, event in enumerate(sample_history)]
self.assertEqual(history.get(), expected)
self.assertEqual(history.get('kamaki'), expected)
self.assertEqual(history.get('file kamaki'), expected[1::2])
self.assertEqual(history.get('pithos:f1'), expected[2:])
self.assertEqual(history.get('touch pithos:f1'), expected[2:3])
for limit in range(len(sample_history)):
self.assertEqual(history.get(limit=limit), expected[-limit:])
history.get('kamaki', limit=limit), expected[-limit:])
def test_add(self):
history = self.HCLASS(
some_strings = ('a brick', 'two bricks', 'another brick', 'A wall!')
for i, line in enumerate(some_strings):
self.assertEqual(, '\n'.join(some_strings[:(i + 1)]) + '\n')
def test_clean(self):
content = 'a brick\ntwo bricks\nanother brick\nA wall!\n'
self.assertEqual(, content)
history = self.HCLASS(
self.assertEqual(, '')
def test_retrieve(self):
sample_history = (
'kamaki history show\n',
'kamaki file list\n',
'kamaki touch pithos:f1\n',
'kamaki file info pithos:f1\n',
'current / last command is always excluded')
history = self.HCLASS(
self.assertRaises(ValueError, history.retrieve, 'must be number')
self.assertRaises(TypeError, history.retrieve, [1, 2, 3])
for i in (0, len(sample_history), -len(sample_history)):
self.assertEqual(history.retrieve(i), None)
for i in range(1, len(sample_history)):
self.assertEqual(history.retrieve(i), sample_history[i - 1])
self.assertEqual(history.retrieve(- i), sample_history[- i - 1])
# TestCase auxiliary methods
def runTestCase(cls, test_name, args=[], failure_collector=[]):
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