From 9b5d3da213ce261a98109c90160929c70f0a4d04 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Sat, 22 Apr 2017 19:18:41 +0200 Subject: Pass settings to Collection.init --- tests/test_collection.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'tests/test_collection.py') diff --git a/tests/test_collection.py b/tests/test_collection.py index e17e044..bcd45e7 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -298,6 +298,25 @@ class testCollectionCreation(unittest.TestCase): self.assertTrue(self._find_file_in_git_index(fname, repo.index)) shutil.rmtree(tmpdir) + def test_init_custom_settings(self): + tmpdir = tempfile.mkdtemp() + collection = lesana.Collection.init( + tmpdir, + edit_file=self.do_nothing, + settings={ + 'name': 'A different name', + 'fields': [ + {'name': 'title', 'type': 'string'}, + {'name': 'author', 'type': 'string'}, + ], + }, + ) + self.assertIsInstance(collection, lesana.Collection) + self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'settings.yaml'))) + self.assertEqual(collection.settings['name'], 'A different name') + self.assertEqual(len(collection.settings['fields']), 2) + shutil.rmtree(tmpdir) + if __name__ == '__main__': unittest.main() -- cgit v1.2.3 From 9d6c7b5ef7f651a64e27db3d676a3a0153a85e7b Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Sun, 21 May 2017 13:01:23 +0200 Subject: Fix creation of an entry with uid but no filename --- lesana/collection.py | 3 ++- tests/test_collection.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) (limited to 'tests/test_collection.py') diff --git a/lesana/collection.py b/lesana/collection.py index 5b80d3b..d3715d1 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -26,7 +26,8 @@ class Entry(object): self.uid, ext = os.path.splitext(os.path.basename(self.fname)) else: self.uid = uuid.uuid4().hex - self.fname = self.uid + '.yaml' + if not self.fname: + self.fname = self.uid + '.yaml' def __str__(self): label = self.collection.settings.get('entry_label', None) diff --git a/tests/test_collection.py b/tests/test_collection.py index bcd45e7..891a439 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -164,6 +164,30 @@ class testEntries(unittest.TestCase): '{uid}: {name}'.format(uid=uid, name='Another item') ) + def test_entry_creation_uid_but_no_filename(self): + fname = '11189ee47ddf4796b718a483b379f976.yaml' + with open(os.path.join(self.basepath, fname)) as fp: + data = ruamel.yaml.safe_load(fp) + data['uid'] = '11189ee47ddf4796b718a483b379f976' + entry = lesana.Entry(self.collection, data=data) + self.assertEqual(entry.fname, fname) + + def test_entry_creation_no_uid_no_filename(self): + fname = '11189ee47ddf4796b718a483b379f976.yaml' + with open(os.path.join(self.basepath, fname)) as fp: + data = ruamel.yaml.safe_load(fp) + entry = lesana.Entry(self.collection, data=data) + self.assertIsNotNone(entry.uid) + self.assertIsNotNone(entry.fname) + + def test_entry_creation_filename_but_no_uid(self): + fname = '11189ee47ddf4796b718a483b379f976.yaml' + uid = '11189ee47ddf4796b718a483b379f976' + with open(os.path.join(self.basepath, fname)) as fp: + data = ruamel.yaml.safe_load(fp) + entry = lesana.Entry(self.collection, data=data, fname=fname) + self.assertEqual(entry.uid, uid) + class testComplexCollection(unittest.TestCase): @classmethod -- cgit v1.2.3 From 96465262f72b1b4230513e978d8412d0affc75a1 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Sun, 4 Jun 2017 22:49:54 +0200 Subject: Search for all entries --- lesana/collection.py | 7 +++++++ lesana/command.py | 15 +++++++++------ tests/test_collection.py | 8 ++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) (limited to 'tests/test_collection.py') diff --git a/lesana/collection.py b/lesana/collection.py index d3715d1..da001ae 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -263,6 +263,13 @@ class Collection(object): yield self._match_to_entry(match) offset += pagesize + def get_all_documents(self): + cache = self._get_cache() + postlist = cache.postlist("") + for post in postlist: + doc = cache.get_document(post.docid) + yield self._doc_to_entry(doc) + def _match_to_entry(self, match): return self._doc_to_entry(match.document) diff --git a/lesana/command.py b/lesana/command.py index 41448b3..2915d30 100644 --- a/lesana/command.py +++ b/lesana/command.py @@ -198,13 +198,16 @@ class Search(guacamole.Command): offset = ctx.args.offset or 0 pagesize = ctx.args.pagesize or 12 collection = Collection(ctx.args.collection) - collection.start_search(' '.join(ctx.args.query)) - if ctx.args.all: - results = collection.get_all_search_results() + if ctx.args.query == ['*']: + results = collection.get_all_documents() else: - results = collection.get_search_results( - offset, - pagesize) + collection.start_search(' '.join(ctx.args.query)) + if ctx.args.all: + results = collection.get_all_search_results() + else: + results = collection.get_search_results( + offset, + pagesize) if ctx.args.template: env = jinja2.Environment( loader=jinja2.FileSystemLoader( diff --git a/tests/test_collection.py b/tests/test_collection.py index 891a439..5c326e3 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -87,6 +87,14 @@ class testCollection(unittest.TestCase): matches = list(self.collection.get_all_search_results()) self.assertEqual(matches, []) + def test_all_entries(self): + self.collection = lesana.Collection('tests/data/simple') + res = self.collection.get_all_documents() + matches = list(res) + self.assertEqual(len(matches), 3) + for m in matches: + self.assertIsInstance(m, lesana.Entry) + def test_entry_from_uid(self): self.collection = lesana.Collection('tests/data/simple') entry = self.collection.entry_from_uid( -- cgit v1.2.3 From c2885894c06e15a5be0b6a00edd8e0de7746b4a9 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Sat, 5 Aug 2017 16:58:45 +0200 Subject: Fix printing Entry id when data includes the id --- lesana/collection.py | 8 ++++---- tests/test_collection.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) (limited to 'tests/test_collection.py') diff --git a/lesana/collection.py b/lesana/collection.py index da001ae..2ac99aa 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -33,10 +33,10 @@ class Entry(object): label = self.collection.settings.get('entry_label', None) if label: t = jinja2.Template(label) - return t.render( - uid=self.uid, - fname=self.fname, - **self.data) + data = self.data + data['uid'] = self.uid + data['fname'] = self.fname + return t.render(**data) else: return self.uid diff --git a/tests/test_collection.py b/tests/test_collection.py index 5c326e3..869828a 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -196,6 +196,26 @@ class testEntries(unittest.TestCase): entry = lesana.Entry(self.collection, data=data, fname=fname) self.assertEqual(entry.uid, uid) + def test_entry_str_filename_and_uid(self): + fname = '11189ee47ddf4796b718a483b379f976.yaml' + with open(os.path.join(self.basepath, fname)) as fp: + data = ruamel.yaml.safe_load(fp) + data['uid'] = '11189ee47ddf4796b718a483b379f976' + entry = lesana.Entry(self.collection, data=data) + self.assertEqual(str(entry), data['uid']) + self.collection.settings['entry_label'] = '{{ uid }}: {{ name }}' + self.assertEqual(str(entry), data['uid'] + ': Another item') + + def test_entry_str_filename_no_uid(self): + fname = '11189ee47ddf4796b718a483b379f976.yaml' + with open(os.path.join(self.basepath, fname)) as fp: + data = ruamel.yaml.safe_load(fp) + entry = lesana.Entry(self.collection, data=data) + uid = entry.uid + self.assertEqual(str(entry), uid) + self.collection.settings['entry_label'] = '{{ uid }}: {{ name }}' + self.assertEqual(str(entry), uid + ': Another item') + class testComplexCollection(unittest.TestCase): @classmethod -- cgit v1.2.3