def test_sm_dict_keys_flattened_default(self): test_data = { 'lol': 'yay', 'urmom': { 'sez': { 'what': 'what' } }, 'cats': { 'hah': 'i win again', 'abc': '123' } } undefined = object() smunch = safe_munchify(test_data, factory=DefaultSafeMunch, default=undefined) flattened_keys = smunch.sm_dict_keys_flattened(separator='-') sorted_flattened_keys = sorted(flattened_keys) expected_keys = ['cats-abc', 'cats-hah', 'lol', 'urmom-sez-what'] self.assertEqual(sorted_flattened_keys, expected_keys) test_data['keys_test'] = {'keys': 'possible to set but not to get'} smunch = safe_munchify(test_data, factory=DefaultSafeMunch, default=undefined) flattened_keys = smunch.sm_dict_keys_flattened(data=dict(smunch), separator='.') sorted_flattened_keys = sorted(flattened_keys) expected_keys = [ 'cats.abc', 'cats.hah', 'keys_test.keys', 'lol', 'urmom.sez.what' ] self.assertEqual(sorted_flattened_keys, expected_keys)
def test_munchify(self): smunch = safe_munchify({'urmom': {'sez': {'what': 'what'}}}) self.assertEqual(smunch.urmom.sez.what, 'what') smunch = safe_munchify({ 'lol': ('cats', { 'hah': 'i win again' }), 'hello': [{ 'french': 'salut', 'german': 'hallo' }] }) self.assertEqual(smunch.hello[0].french, 'salut') self.assertEqual(smunch.lol[1].hah, 'i win again')
def test_munchify_default(self): undefined = object() smunch = safe_munchify({'urmom': { 'sez': { 'whatk': 'whatv' } }}, factory=DefaultSafeMunch, default=undefined) self.assertEqual(smunch.urmom.sez.whatk, 'whatv') self.assertIs(smunch.urdad, undefined) self.assertIs(smunch.urmom.sez.ni, undefined)
def test_update_column_name_from_document_positive_nested_alias(self): logger = LstailLogger(LOG_DOCUMENT_CONFIG, output=sys.stdout, verbose=False) # alias match document_values = safe_munchify({'nested.alias': 'localhost'}) test_column_name = 'nested.test.column' test_column = LOG_DOCUMENT_COLUMNS['nested.test.column'] # test column_name = logger._update_column_name_from_document( test_column_name, test_column, document_values) # check self.assertEqual(column_name, 'nested.alias')
def test_update_column_name_from_document_positive_direct(self): logger = LstailLogger(LOG_DOCUMENT_CONFIG, output=sys.stdout, verbose=False) # direct match via column name document_values = safe_munchify({'host': 'localhost'}) test_column_name = 'host' test_column = LOG_DOCUMENT_COLUMNS['host'] # test column_name = logger._update_column_name_from_document( test_column_name, test_column, document_values) # check self.assertEqual(column_name, test_column_name)
def test_update_column_name_from_document_negative_nested(self): logger = LstailLogger(LOG_DOCUMENT_CONFIG, output=sys.stdout, verbose=False) # alias match document_values = safe_munchify({'nested.foo': 'blah'}) test_column_name = 'nested.bar' test_column = LOG_DOCUMENT_COLUMNS['host'] # test column_name = logger._update_column_name_from_document( test_column_name, test_column, document_values) # check - we get the passed column name # but the initially missing column name was added to document_values self.assertEqual(column_name, test_column_name) self.assertIn(test_column_name, document_values)
def _print_document(self, document, document_values=None, force_color=None): document_values_initial = document_values or self._get_document_values( document) # sanity check for duplicate documents self._assert_document_already_processed(document_values_initial) # support attribute-style access to dict; necessary if the columns contains dots # (e.g. for nested documents) which are interpreted as attributes by format() below document_values = safe_munchify(document_values_initial, factory=DefaultSafeMunch, default=LSTAIL_FALLBACK_FIELD_VALUE) # output if self._config.csv_output and not self._is_internal_document( document_values_initial): self._print_document_as_csv(document, document_values) else: self._print_document_as_text(document, document_values, force_color)