From 94c578817edd68d5baa956c568770eafe46e6f73 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Thu, 23 Dec 2021 21:09:47 +0100 Subject: Fix printing values of list fields --- lesana/collection.py | 15 +++++++++++---- tests/test_commands.py | 52 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/lesana/collection.py b/lesana/collection.py index 20f8a0c..d51187f 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -429,10 +429,17 @@ class Collection(object): logger.info( "This is going to be pretty inefficient." ) - values = ( - e.data[field.field['name']] - for e in self.get_all_documents() - ) + if field.field['type'] == 'list': + values = [] + for e in self.get_all_documents(): + values.extend(e.data[field.field['name']]) + else: + values = ( + e.data[field.field['name']] + for e in self.get_all_documents() + ) + logger.info("Values are %s", str(values)) + counter = collections.Counter(values) for v in counter.most_common(): yield { diff --git a/tests/test_commands.py b/tests/test_commands.py index 99cc649..bd7098c 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -22,18 +22,7 @@ class Args: raise AttributeError(e) -class testCommands(unittest.TestCase): - def setUp(self): - self.tmpdir = tempfile.TemporaryDirectory() - utils.copytree( - 'tests/data/simple', - self.tmpdir.name, - dirs_exist_ok=True, - ) - - def tearDown(self): - pass - +class CommandsMixin: def _edit_file(self, filepath): return True @@ -49,6 +38,19 @@ class testCommands(unittest.TestCase): cmd.main() return stream + +class testCommandsSimple(unittest.TestCase, CommandsMixin): + def setUp(self): + self.tmpdir = tempfile.TemporaryDirectory() + utils.copytree( + 'tests/data/simple', + self.tmpdir.name, + dirs_exist_ok=True, + ) + + def tearDown(self): + pass + def test_init(self): args = { 'collection': self.tmpdir.name, @@ -180,5 +182,31 @@ class testCommands(unittest.TestCase): self.assertEqual(streams['stderr'].getvalue(), '') +class testCommandsComplex(unittest.TestCase, CommandsMixin): + def setUp(self): + self.tmpdir = tempfile.TemporaryDirectory() + utils.copytree( + 'tests/data/complex', + self.tmpdir.name, + dirs_exist_ok=True, + ) + + def tearDown(self): + pass + + def test_get_values_from_list(self): + args = { + 'collection': self.tmpdir.name, + 'git': True, + 'template': False, + 'query': '*', + 'field': 'tags', + } + streams = self._run_command(command.GetValues(), args) + print(streams['stdout'].getvalue()) + self.assertIn('this: 1', streams['stdout'].getvalue()) + self.assertEqual(streams['stderr'].getvalue(), '') + + if __name__ == '__main__': unittest.main() -- cgit v1.2.3