aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-12-23 21:09:47 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-12-23 21:09:47 +0100
commit94c578817edd68d5baa956c568770eafe46e6f73 (patch)
treee633186ebf5ad964f49fb10521c3e4b199322ee4
parent83a56d5eb68da0d57645bdc287c92c1bad5ab664 (diff)
Fix printing values of list fields
-rw-r--r--lesana/collection.py15
-rw-r--r--tests/test_commands.py52
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()