def test_list_of_dicts(): "Input: a list of dictionaries." lod = [{"foo": 1, "bar": 2}, {"foo": 3, "bar": 4}] expected1 = "\n".join(["- -", "1 2", "3 4", "- -"]) expected2 = "\n".join(["- -", "2 1", "4 3", "- -"]) result = tabulate(lod) assert_in(result, [expected1, expected2])
def test_list_of_userdicts(): "Input: a list of UserDicts." lod = [UserDict(foo=1, bar=2), UserDict(foo=3, bar=4)] expected1 = "\n".join(["- -", "1 2", "3 4", "- -"]) expected2 = "\n".join(["- -", "2 1", "4 3", "- -"]) result = tabulate(lod) assert_in(result, [expected1, expected2])
def test_list_of_dicts(): "Input: a list of dictionaries." lod = [{'foo': 1, 'bar': 2}, {'foo': 3, 'bar': 4}] expected1 = "\n".join(['- -', '1 2', '3 4', '- -']) expected2 = "\n".join(['- -', '2 1', '4 3', '- -']) result = tabulate(lod) assert_in(result, [expected1, expected2])
def test_list_of_dicts_keys(): "Input: a list of dictionaries, with keys as headers." lod = [{"foo": 1, "bar": 2}, {"foo": 3, "bar": 4}] expected1 = "\n".join( [" foo bar", "----- -----", " 1 2", " 3 4"]) expected2 = "\n".join( [" bar foo", "----- -----", " 2 1", " 4 3"]) result = tabulate(lod, headers="keys") assert_in(result, [expected1, expected2])
def test_list_of_dicts_keys(): "Input: a list of dictionaries, with keys as headers." lod = [{'foo': 1, 'bar': 2}, {'foo': 3, 'bar': 4}] expected1 = "\n".join( [' foo bar', '----- -----', ' 1 2', ' 3 4']) expected2 = "\n".join( [' bar foo', '----- -----', ' 2 1', ' 4 3']) result = tabulate(lod, headers="keys") assert_in(result, [expected1, expected2])
def test_list_of_userdicts_keys(): "Input: a list of UserDicts." lod = [UserDict(foo=1, bar=2), UserDict(foo=3, bar=4)] expected1 = "\n".join( [" foo bar", "----- -----", " 1 2", " 3 4"]) expected2 = "\n".join( [" bar foo", "----- -----", " 2 1", " 4 3"]) result = tabulate(lod, headers="keys") assert_in(result, [expected1, expected2])
def test_list_of_dicts_with_dict_of_headers(): "Input: a dict of user headers for a list of dicts (issue #23)" table = [{"letters": "ABCDE", "digits": 12345}] headers = {"digits": "DIGITS", "letters": "LETTERS"} expected1 = "\n".join( [" DIGITS LETTERS", "-------- ---------", " 12345 ABCDE"]) expected2 = "\n".join( ["LETTERS DIGITS", "--------- --------", "ABCDE 12345"]) result = tabulate(table, headers=headers) assert_in(result, [expected1, expected2])
def test_list_of_dicts_firstrow(): "Input: a list of dictionaries, with the first dict as headers." lod = [{"foo": "FOO", "bar": "BAR"}, {"foo": 3, "bar": 4, "baz": 5}] # if some key is missing in the first dict, use the key name instead expected1 = "\n".join( [" FOO BAR baz", "----- ----- -----", " 3 4 5"]) expected2 = "\n".join( [" BAR FOO baz", "----- ----- -----", " 4 3 5"]) result = tabulate(lod, headers="firstrow") assert_in(result, [expected1, expected2])
def test_list_of_dicts_firstrow(): "Input: a list of dictionaries, with the first dict as headers." lod = [{'foo': "FOO", 'bar': "BAR"}, {'foo': 3, 'bar': 4, 'baz': 5}] # if some key is missing in the first dict, use the key name instead expected1 = "\n".join( [' FOO BAR baz', '----- ----- -----', ' 3 4 5']) expected2 = "\n".join( [' BAR FOO baz', '----- ----- -----', ' 4 3 5']) result = tabulate(lod, headers="firstrow") assert_in(result, [expected1, expected2])
def test_isconvertible_on_set_values(): "Regression: don't fail with TypeError on set values (issue #35)" expected_py2 = "\n".join([ 'a b', '--- -------', 'Foo set([])',]) expected_py3 = "\n".join([ 'a b', '--- -----', 'Foo set()',]) result = tabulate([["Foo",set()]], headers=["a","b"]) assert_in(result, [expected_py2, expected_py3])
def test_dict_like(): "Input: a dict of iterables with keys as headers." # columns should be padded with None, keys should be used as headers dd = {"a": range(3), "b": range(101, 105)} # keys' order (hence columns' order) is not deterministic in Python 3 # => we have to consider both possible results as valid expected1 = "\n".join([ " a b", "--- ---", " 0 101", " 1 102", " 2 103", " 104" ]) expected2 = "\n".join( [" b a", "--- ---", "101 0", "102 1", "103 2", "104"]) result = tabulate(dd, "keys") print("Keys' order: %s" % dd.keys()) assert_in(result, [expected1, expected2])
def test_list_of_dicts_firstrow(): "Input: a list of dictionaries, with the first dict as headers." lod = [{'foo' : "FOO", 'bar' : "BAR"}, {'foo' : 3, 'bar': 4, 'baz': 5}] # if some key is missing in the first dict, use the key name instead expected1 = "\n".join([ ' FOO BAR baz', '----- ----- -----', ' 3 4 5']) expected2 = "\n".join([ ' BAR FOO baz', '----- ----- -----', ' 4 3 5']) result = tabulate(lod, headers="firstrow") assert_in(result, [expected1, expected2])
def test_list_of_dicts_with_dict_of_headers(): "Input: a dict of user headers for a list of dicts (issue #23)" table = [{"letters": "ABCDE", "digits": 12345}] headers = {"digits": "DIGITS", "letters": "LETTERS"} expected1 = "\n".join([ ' DIGITS LETTERS', '-------- ---------', ' 12345 ABCDE']) expected2 = "\n".join([ 'LETTERS DIGITS', '--------- --------', 'ABCDE 12345']) result = tabulate(table, headers=headers) assert_in(result, [expected1, expected2])
def test_list_of_dicts(): "Input: a list of dictionaries." lod = [{'foo' : 1, 'bar' : 2}, {'foo' : 3, 'bar' : 4}] expected1 = "\n".join([ '- -', '1 2', '3 4', '- -']) expected2 = "\n".join([ '- -', '2 1', '4 3', '- -']) result = tabulate(lod) assert_in(result, [expected1, expected2])
def test_list_of_dicts_keys(): "Input: a list of dictionaries, with keys as headers." lod = [{'foo' : 1, 'bar' : 2}, {'foo' : 3, 'bar' : 4}] expected1 = "\n".join([ ' foo bar', '----- -----', ' 1 2', ' 3 4']) expected2 = "\n".join([ ' bar foo', '----- -----', ' 2 1', ' 4 3']) result = tabulate(lod, headers="keys") assert_in(result, [expected1, expected2])
def test_list_proc_ret_structure(self): """List process must return all required fields.""" result = Agent.list_process() for proc in result['list']: for field in LIST_PROCESS_STRUCTURE: self.assertTrue(assert_in(field, proc), "{field} not in {result}".format(field=field, result=proc))
def test_get_proc_pid_ret_structure(self): """Get process must return all required fields.""" current_pid = os.getpid() parent_pid = os.getppid() result = Agent.get_process(pids=[current_pid, parent_pid]) for proc in result['list']: for field in GET_PROCESS_STRUCTURE: self.assertTrue(assert_in(field, proc), "{field} not in {result}".format(field=field, result=proc))
def test_dict_like(): "Input: a dict of iterables with keys as headers." # columns should be padded with None, keys should be used as headers dd = {"a": range(3), "b": range(101,105)} # keys' order (hence columns' order) is not deterministic in Python 3 # => we have to consider both possible results as valid expected1 = "\n".join([ ' a b', '--- ---', ' 0 101', ' 1 102', ' 2 103', ' 104']) expected2 = "\n".join([ ' b a', '--- ---', '101 0', '102 1', '103 2', '104']) result = tabulate(dd, "keys") print("Keys' order: %s" % dd.keys()) assert_in(result, [expected1, expected2])
def test_get_proc_by_name(self): """Get process with one pid and name returns same result.""" current_pid = os.getppid() result_pid = Agent.get_process(pids=[current_pid]) curr_name = result_pid["list"][0]["cmd"] result_name = Agent.get_process(names=[curr_name]) del result_pid['list'][0]['cpu'] del result_pid['list'][0]['ram'] del result_pid['list'][0]['uuid'] del result_name['list'][0]['cpu'] del result_name['list'][0]['ram'] del result_name['list'][0]['uuid'] self.assertTrue(assert_in(result_pid['list'][0], result_name['list']), 'Process by name should be returned')