2022-10-11 12:19:11 -07:00
|
|
|
from copy import deepcopy
|
2022-01-19 09:15:24 -08:00
|
|
|
import unittest
|
|
|
|
from typing import Generator
|
|
|
|
import jc.lib
|
|
|
|
|
|
|
|
|
|
|
|
class MyTests(unittest.TestCase):
|
2022-01-19 10:31:36 -08:00
|
|
|
def test_lib_parse_csv(self):
|
2022-01-19 09:15:24 -08:00
|
|
|
data = {
|
|
|
|
'': [],
|
|
|
|
'a,b,c\n1,2,3': [{'a':'1', 'b':'2', 'c':'3'}]
|
|
|
|
}
|
|
|
|
|
|
|
|
for test_data, expected_output in data.items():
|
|
|
|
self.assertEqual(jc.lib.parse('csv', test_data), expected_output)
|
|
|
|
|
2022-01-19 10:31:36 -08:00
|
|
|
def test_lib_parse_csv_s_is_generator(self):
|
2022-01-19 09:15:24 -08:00
|
|
|
self.assertIsInstance(jc.lib.parse('csv_s', 'a,b,c\n1,2,3'), Generator)
|
|
|
|
|
2022-01-19 10:31:36 -08:00
|
|
|
def test_lib_parse_kv(self):
|
2022-01-19 09:15:24 -08:00
|
|
|
data = {
|
|
|
|
'': {},
|
|
|
|
'a=1\nb=2\nc=3': {'a':'1', 'b':'2', 'c':'3'}
|
|
|
|
}
|
|
|
|
|
|
|
|
for test_data, expected_output in data.items():
|
|
|
|
self.assertEqual(jc.lib.parse('kv', test_data), expected_output)
|
|
|
|
|
2022-01-19 10:31:36 -08:00
|
|
|
def test_lib_parser_mod_list_is_list(self):
|
2022-01-19 09:15:24 -08:00
|
|
|
self.assertIsInstance(jc.lib.parser_mod_list(), list)
|
|
|
|
|
2022-01-19 10:31:36 -08:00
|
|
|
def test_lib_parser_mod_list_contains_csv(self):
|
2022-01-19 09:15:24 -08:00
|
|
|
self.assertTrue('csv' in jc.lib.parser_mod_list())
|
|
|
|
|
2022-01-19 10:31:36 -08:00
|
|
|
def test_lib_parser_mod_list_length(self):
|
2022-01-19 09:15:24 -08:00
|
|
|
self.assertGreaterEqual(len(jc.lib.parser_mod_list()), 80)
|
|
|
|
|
2022-01-27 12:54:44 -08:00
|
|
|
def test_lib_parser_info_is_dict(self):
|
|
|
|
self.assertIsInstance(jc.lib.parser_info('csv'), dict)
|
|
|
|
|
|
|
|
def test_lib_parser_info_csv(self):
|
|
|
|
self.assertTrue(jc.lib.parser_info('csv')['name'] == 'csv')
|
|
|
|
|
2022-01-27 13:03:02 -08:00
|
|
|
def test_lib_all_parser_info_is_list_of_dicts(self):
|
2022-01-27 12:54:44 -08:00
|
|
|
self.assertIsInstance(jc.lib.all_parser_info(), list)
|
2022-01-27 13:03:02 -08:00
|
|
|
self.assertIsInstance(jc.lib.all_parser_info()[0], dict)
|
2022-01-27 12:54:44 -08:00
|
|
|
|
|
|
|
def test_lib_all_parser_info_contains_csv(self):
|
|
|
|
p_list = []
|
|
|
|
for p in jc.lib.all_parser_info():
|
|
|
|
p_list.append(p['name'])
|
|
|
|
self.assertTrue('csv' in p_list)
|
|
|
|
|
|
|
|
def test_lib_all_parser_info_length(self):
|
|
|
|
self.assertGreaterEqual(len(jc.lib.all_parser_info()), 80)
|
|
|
|
|
2022-09-06 09:21:40 -07:00
|
|
|
def test_lib_all_parser_hidden_length(self):
|
|
|
|
reg_length = len(jc.lib.all_parser_info())
|
|
|
|
hidden_length = len(jc.lib.all_parser_info(show_hidden=True))
|
|
|
|
self.assertGreater(hidden_length, reg_length)
|
|
|
|
|
2022-01-19 10:31:36 -08:00
|
|
|
def test_lib_plugin_parser_mod_list_is_list(self):
|
2022-01-19 09:15:24 -08:00
|
|
|
self.assertIsInstance(jc.lib.plugin_parser_mod_list(), list)
|
|
|
|
|
2022-01-19 10:31:36 -08:00
|
|
|
def test_lib_plugin_parser_mod_list_length_is_zero(self):
|
2022-01-21 12:15:16 -08:00
|
|
|
"""Ensure there are no plugin parsers present during test/build."""
|
2022-01-19 09:29:09 -08:00
|
|
|
self.assertEqual(len(jc.lib.plugin_parser_mod_list()), 0)
|
|
|
|
|
2022-01-19 10:31:36 -08:00
|
|
|
def test_lib_cliname_to_modname(self):
|
|
|
|
self.assertEqual(jc.lib._cliname_to_modname('module-name'), 'module_name')
|
|
|
|
|
2022-01-27 12:54:44 -08:00
|
|
|
def test_lib_argumentname_to_modname(self):
|
|
|
|
self.assertEqual(jc.lib._cliname_to_modname('--module-name'), 'module_name')
|
|
|
|
|
2022-01-19 10:31:36 -08:00
|
|
|
def test_lib_modname_to_cliname(self):
|
|
|
|
self.assertEqual(jc.lib._modname_to_cliname('module_name'), 'module-name')
|
|
|
|
|
2022-10-11 12:19:11 -07:00
|
|
|
def test_lib_all_parser_info_show_deprecated(self):
|
|
|
|
# save old state
|
|
|
|
old_parsers = deepcopy(jc.lib.parsers)
|
|
|
|
old_get_parser = deepcopy(jc.lib._get_parser)
|
|
|
|
|
|
|
|
# mock data
|
|
|
|
class mock_parser_info:
|
|
|
|
version = "1.1"
|
|
|
|
description = "`deprecated` command parser"
|
|
|
|
author = "nobody"
|
|
|
|
author_email = "nobody@gmail.com"
|
|
|
|
compatible = ["linux", "darwin"]
|
|
|
|
magic_commands = ["deprecated"]
|
|
|
|
deprecated = True
|
|
|
|
|
|
|
|
class mock_parser:
|
|
|
|
info = mock_parser_info
|
|
|
|
|
|
|
|
jc.lib.parsers = ['deprecated']
|
2022-10-18 09:36:17 -07:00
|
|
|
jc.lib._get_parser = lambda x: mock_parser # type: ignore
|
2022-10-11 12:19:11 -07:00
|
|
|
result = jc.lib.all_parser_info(show_deprecated=True)
|
|
|
|
|
|
|
|
# reset
|
|
|
|
jc.lib.parsers = old_parsers
|
|
|
|
jc.lib._get_parser = old_get_parser
|
|
|
|
|
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
|
|
|
|
def test_lib_all_parser_info_show_hidden(self):
|
|
|
|
# save old state
|
|
|
|
old_parsers = deepcopy(jc.lib.parsers)
|
|
|
|
old_get_parser = deepcopy(jc.lib._get_parser)
|
|
|
|
|
|
|
|
# mock data
|
|
|
|
class mock_parser_info:
|
|
|
|
version = "1.1"
|
|
|
|
description = "`deprecated` command parser"
|
|
|
|
author = "nobody"
|
|
|
|
author_email = "nobody@gmail.com"
|
|
|
|
compatible = ["linux", "darwin"]
|
|
|
|
magic_commands = ["deprecated"]
|
|
|
|
hidden = True
|
|
|
|
|
|
|
|
class mock_parser:
|
|
|
|
info = mock_parser_info
|
|
|
|
|
|
|
|
jc.lib.parsers = ['deprecated']
|
2022-10-18 09:36:17 -07:00
|
|
|
jc.lib._get_parser = lambda x: mock_parser # type: ignore
|
2022-10-11 12:19:11 -07:00
|
|
|
result = jc.lib.all_parser_info(show_hidden=True)
|
|
|
|
|
|
|
|
# reset
|
|
|
|
jc.lib.parsers = old_parsers
|
|
|
|
jc.lib._get_parser = old_get_parser
|
|
|
|
|
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
|
|
|
|
|
2022-01-19 09:15:24 -08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|