1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

add library tests

This commit is contained in:
Kelly Brazil
2022-02-08 13:32:22 -08:00
parent d04bc3757c
commit 7a1ae4f5fc
3 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,61 @@
import unittest
import jc.streaming
class MyTests(unittest.TestCase):
def test_streaming_input_type_check_wrong(self):
self.assertRaises(TypeError, jc.streaming.streaming_input_type_check, 'abc')
def test_streaming_input_type_check_correct(self):
self.assertEqual(jc.streaming.streaming_input_type_check(['abc']), None)
def test_streaming_line_input_type_check_wrong(self):
self.assertRaises(TypeError, jc.streaming.streaming_line_input_type_check, ['abc'])
def test_streaming_line_input_type_check_correct(self):
self.assertEqual(jc.streaming.streaming_line_input_type_check('abc'), None)
def test_stream_success_ignore_exceptions_true(self):
self.assertEqual(jc.streaming.stream_success({}, True), {'_jc_meta': {'success': True}})
def test_stream_success_ignore_exceptions_false(self):
self.assertEqual(jc.streaming.stream_success({}, False), {})
def test_stream_error(self):
self.assertEqual(jc.streaming.stream_error(
TypeError, 'this is a test'),
{
'_jc_meta':
{
'success': False,
'error': 'type: <class \'TypeError\'>',
'line': 'this is a test'
}
}
)
def test_raise_or_yield_ignore_exceptions(self):
self.assertEqual(jc.streaming.raise_or_yield(
True, TypeError, 'this is a test'),
(TypeError, 'this is a test')
)
def test_raise_or_yield_ignore_exceptions_false(self):
self.assertRaises(
TypeError,
jc.streaming.raise_or_yield,
False, TypeError, 'this is a test'
)
if __name__ == '__main__':
unittest.main()

View File

@ -127,6 +127,7 @@ class MyTests(unittest.TestCase):
'0.0': False, '0.0': False,
'0.1': True, '0.1': True,
'-0.1': True, '-0.1': True,
'*': True,
'true': True, 'true': True,
'True': True, 'True': True,
'false': False, 'false': False,
@ -147,3 +148,46 @@ class MyTests(unittest.TestCase):
for input_string, expected_output in io_map.items(): for input_string, expected_output in io_map.items():
self.assertEqual(jc.utils.convert_to_bool(input_string), expected_output) self.assertEqual(jc.utils.convert_to_bool(input_string), expected_output)
def test_has_data_nodata(self):
self.assertFalse(jc.utils.has_data(' \n '))
def test_has_data_withdata(self):
self.assertTrue(jc.utils.has_data(' \n abcd \n '))
def test_input_type_check_wrong(self):
self.assertRaises(TypeError, jc.utils.input_type_check, ['abc'])
def test_input_type_check_correct(self):
self.assertEqual(jc.utils.input_type_check('abc'), None)
# need to mock shutil.get_terminal_size().columns or add a column parameter to test
# def test_warning_message(self):
# msg = [
# 'this is a long first line that will be wrapped yada yada yada yada yada yada yada.',
# 'this is a second long line that will be wrapped yada yada yada yada yada yada yada yada yada.',
# 'this is a third long line that will be wrapped yada yada yada yada yada yada yada yada yada.'
# ]
# expected = '''jc: Warning - this is a long first line that will be wrapped yada yada yada
# yada yada yada yada.
# this is a second long line that will be wrapped yada yada yada
# yada yada yada yada yada yada.
# this is a third long line that will be wrapped yada yada yada
# yada yada yada yada yada yada.'''
# f = io.StringIO()
# with contextlib.redirect_stderr(f):
# jc.utils.warning_message(msg)
# self.assertEqual(f.getvalue(), expected + '\n')
if __name__ == '__main__':
unittest.main()