diff options
author | Alejandro Gallo <aamsgallo@gmail.com> | 2018-09-04 23:00:04 +0200 |
---|---|---|
committer | Alejandro Gallo <aamsgallo@gmail.com> | 2018-09-04 23:00:04 +0200 |
commit | 898ae510c931c7a64e953c2382273f50f6896887 (patch) | |
tree | d226f42894a2318d16bb41ec31223c1e108d1bb0 | |
parent | 0009b8a5b74302f790a6a4259823a3f2505929db (diff) |
Update tests add remove --no-bibtex from explore
-rw-r--r-- | papis/commands/export.py | 34 | ||||
-rw-r--r-- | tests/__init__.py | 61 | ||||
-rw-r--r-- | tests/commands/test_add.py | 2 | ||||
-rw-r--r-- | tests/commands/test_export.py | 116 | ||||
-rw-r--r-- | tests/database/__init__.py | 2 | ||||
-rw-r--r-- | tests/test_utils.py | 39 |
6 files changed, 186 insertions, 68 deletions
diff --git a/papis/commands/export.py b/papis/commands/export.py index 2156f40d..cb5cca8c 100644 --- a/papis/commands/export.py +++ b/papis/commands/export.py @@ -138,12 +138,6 @@ def run( is_flag=True ) @click.option( - "--no-bibtex", - help="When exporting to a folder, do not include the bibtex", - default=False, - is_flag=True -) -@click.option( "-o", "--out", help="Outfile or outdir", @@ -174,7 +168,6 @@ def cli( bibtex, json, folder, - no_bibtex, out, text, all, @@ -194,14 +187,6 @@ def cli( return 0 documents = [document] - if out is not None and not folder and not file: - logger.info("Dumping to {0}".format(out)) - out = open(out, 'a+') - - if out is None and not folder and not file: - logger.info("Dumping to stdout") - out = sys.stdout - ret_string = run( documents, yaml=yaml, @@ -211,7 +196,13 @@ def cli( ) if ret_string is not None: - out.write(ret_string) + if out is not None: + logger.info("Dumping to {0}".format(out)) + with open(out, 'w+') as fd: + fd.write(ret_string) + else: + logger.info("Dumping to stdout") + print(ret_string) return 0 for document in documents: @@ -223,14 +214,15 @@ def cli( outdir, document.get_main_folder_name() ) shutil.copytree(folder, outdir) - if not no_bibtex: - open( - os.path.join(outdir, "info.bib"), - "a+" - ).write(papis.document.to_bibtex(document)) elif file: logger.info("Exporting file") files = document.get_files() + assert(isinstance(files, list)) + if not files: + logger.error('No files found for doc in {0}'.format( + document.get_main_folder() + )) + continue files_to_open = [papis.api.pick( files, pick_config=dict( diff --git a/tests/__init__.py b/tests/__init__.py index 77f96a46..2cd9a6d9 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -4,13 +4,56 @@ import papis.api import papis.utils import papis.document import os +import shutil + + +def create_random_pdf(suffix='', prefix=''): + tempf = tempfile.mktemp(suffix=suffix, prefix=prefix) + with open(tempf, 'wb+') as fd: + fd.write('%PDF-1.5%\n'.encode()) + return tempf + + +def create_random_epub(suffix='', prefix=''): + tempf = tempfile.mktemp(suffix=suffix, prefix=prefix) + buf = [0x50, 0x4B, 0x3, 0x4] + buf += [0x00 for i in range(26)] + buf += [0x6D, 0x69, 0x6D, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70, + 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2F, + 0x65, 0x70, 0x75, 0x62, 0x2B, 0x7A, 0x69, 0x70] + buf += [0x00 for i in range(1)] + with open(tempf, 'wb+') as fd: + fd.write(bytearray(buf)) + return tempf + + +def create_random_file(suffix='', prefix=''): + tempf = tempfile.mktemp(suffix=suffix, prefix=prefix) + with open(tempf, 'wb+') as fd: + fd.write('hello'.encode()) + return tempf test_data = [ - {"author": 'J. Krishnamurti', "title": 'Freedom from the known', - "year": '2009'}, - {"author": 'K. Popper', 'doi':'10.1021/ct5004252', - "title": 'The open society', "volume": 'I'}, + { + "author": 'doc without files', + "title": 'Title of doc without files', + "year": '1093', + "_test_files": 0, + }, + { + "author": 'J. Krishnamurti', + "title": 'Freedom from the known', + "year": '2009', + "_test_files": 1, + }, + { + "author": 'K. Popper', + 'doi':'10.1021/ct5004252', + "title": 'The open society', + "volume": 'I', + "_test_files": 0, + }, ] @@ -34,11 +77,21 @@ def setup_test_library(): papis.api.set_lib(lib) papis.database.clear_cached() + for i, data in enumerate(test_data): + data['files'] = [ + create_random_pdf() for i in range(data.get('_test_files')) + ] doc = papis.document.from_data(data) folder = os.path.join( papis.config.get('dir'), str(i) ) os.makedirs(folder) doc.set_folder(folder) + doc['files'] = [os.path.basename(f) for f in data['files']] doc.save() + for f in data['files']: + shutil.move( + f, + doc.get_main_folder() + ) diff --git a/tests/commands/test_add.py b/tests/commands/test_add.py index 3089c3e7..b89c6c36 100644 --- a/tests/commands/test_add.py +++ b/tests/commands/test_add.py @@ -10,7 +10,7 @@ from papis.commands.add import ( get_file_name, get_hash_folder ) -from tests.test_utils import ( +from tests import ( create_random_pdf, create_random_file, create_random_epub ) from papis.utils import get_document_extension diff --git a/tests/commands/test_export.py b/tests/commands/test_export.py index 11ae69c4..a35b2350 100644 --- a/tests/commands/test_export.py +++ b/tests/commands/test_export.py @@ -4,11 +4,15 @@ import yaml import tempfile import unittest import tests +import tests.cli import papis.config -from papis.commands.export import run +import papis.document +from papis.commands.export import run, cli +import re +import os -class Test(unittest.TestCase): +class TestRun(unittest.TestCase): @classmethod def setUpClass(self): @@ -40,13 +44,109 @@ class Test(unittest.TestCase): self.assertTrue(len(data) > 0) def test_yaml(self): - # FIXME: The string gets created, but the loading does not work docs = self.get_docs() string = run(docs, yaml=True) self.assertTrue(len(string) > 0) - yamlfile = open(tempfile.mktemp(), 'w+') - yamlfile.write(string) - data = yaml.load_all(yamlfile) + yamlfile = tempfile.mktemp() + with open(yamlfile, 'w+') as fd: + fd.write(string) + with open(yamlfile) as fd: + data = list(yaml.load_all(fd)) self.assertTrue(data is not None) - # FIXME: THIS DOES NOT WORK, WHY? - # self.assertTrue(len(list(data)) > 0) + self.assertTrue(len(list(data)) > 0) + + +class TestCli(tests.cli.TestCli): + + cli = cli + + def test_main(self): + self.do_test_cli_function_exists() + self.do_test_help() + + def test_json(self): + + # output stdout + result = self.invoke([ + 'krishnamurti', '--json' + ]) + self.assertTrue(result.exit_code == 0) + data = json.loads(result.output_bytes.decode()) + assert(isinstance(data, list)) + assert(len(data) == 1) + assert(re.match(r'.*Krishnamurti.*', data[0]['author']) is not None) + + # output stdout + outfile = tempfile.mktemp() + self.assertTrue(not os.path.exists(outfile)) + result = self.invoke([ + 'Krishnamurti', '--json', '--out', outfile + ]) + self.assertTrue(result.exit_code == 0) + self.assertTrue(os.path.exists(outfile)) + + with open(outfile) as fd: + data = json.load(fd) + assert(isinstance(data, list)) + assert(len(data) == 1) + assert(re.match(r'.*Krishnamurti.*', data[0]['author']) is not None) + + def test_yaml(self): + + # output stdout + result = self.invoke([ + 'krishnamurti', '--yaml' + ]) + self.assertTrue(result.exit_code == 0) + data = yaml.load(result.output_bytes) + assert(re.match(r'.*Krishnamurti.*', data['author']) is not None) + + # output stdout + outfile = tempfile.mktemp() + self.assertTrue(not os.path.exists(outfile)) + result = self.invoke([ + 'Krishnamurti', '--yaml', '--out', outfile + ]) + self.assertTrue(result.exit_code == 0) + self.assertTrue(os.path.exists(outfile)) + + with open(outfile) as fd: + data = yaml.load(fd.read()) + assert(data is not None) + assert(re.match(r'.*Krishnamurti.*', data['author']) is not None) + + def test_folder(self): + + outdir = tempfile.mktemp() + self.assertTrue(not os.path.exists(outdir)) + # output stdout + result = self.invoke([ + 'krishnamurti', '--folder', '--out', outdir + ]) + self.assertTrue(os.path.exists(outdir)) + self.assertTrue(os.path.isdir(outdir)) + self.assertTrue(result.exit_code == 0) + self.assertTrue(result.output_bytes == b'') + doc = papis.document.from_folder(outdir) + self.assertTrue(doc is not None) + assert(re.match(r'.*Krishnamurti.*', doc['author']) is not None) + + def test_file(self): + outfile = tempfile.mktemp() + self.assertTrue(not os.path.exists(outfile)) + # output stdout + result = self.invoke([ + 'krishnamurti', '--file', '--out', outfile + ]) + self.assertTrue(result.exit_code == 0) + self.assertTrue(result.output_bytes == b'') + self.assertTrue(os.path.exists(outfile)) + + outfile = tempfile.mktemp() + self.assertTrue(not os.path.exists(outfile)) + result = self.invoke([ + '"doc without files"', '--file', '--out', outfile + ]) + self.assertTrue(result.exit_code == 0) + self.assertTrue(result.output_bytes == b'') + self.assertTrue(not os.path.exists(outfile)) diff --git a/tests/database/__init__.py b/tests/database/__init__.py index e7c30d8b..61d925f6 100644 --- a/tests/database/__init__.py +++ b/tests/database/__init__.py @@ -103,7 +103,7 @@ class DatabaseTest(unittest.TestCase): print(doc.get_main_folder()) database.add(doc) docs = database.get_all_documents() - self.assertEqual(len(docs), N+2) + self.assertEqual(len(docs), N*2) def test_clear(self): database = papis.database.get() diff --git a/tests/test_utils.py b/tests/test_utils.py index bd891e69..cc099b93 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -100,41 +100,14 @@ def test_format_doc(): 'FulanoSomething' -def create_random_pdf(suffix='', prefix=''): - tempf = tempfile.mktemp(suffix=suffix, prefix=prefix) - with open(tempf, 'wb+') as fd: - fd.write('%PDF-1.5%\n'.encode()) - return tempf - - -def create_random_epub(suffix='', prefix=''): - tempf = tempfile.mktemp(suffix=suffix, prefix=prefix) - buf = [0x50, 0x4B, 0x3, 0x4] - buf += [0x00 for i in range(26)] - buf += [0x6D, 0x69, 0x6D, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70, - 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2F, - 0x65, 0x70, 0x75, 0x62, 0x2B, 0x7A, 0x69, 0x70] - buf += [0x00 for i in range(1)] - with open(tempf, 'wb+') as fd: - fd.write(bytearray(buf)) - return tempf - - -def create_random_file(suffix='', prefix=''): - tempf = tempfile.mktemp(suffix=suffix, prefix=prefix) - with open(tempf, 'wb+') as fd: - fd.write('hello'.encode()) - return tempf - - def test_extension(): docs = [ - [create_random_pdf(), "pdf"], - [create_random_pdf(), "pdf"], - [create_random_file(), "data"], - [create_random_epub(), "epub"], - [create_random_file(suffix='.yaml'), "yaml"], - [create_random_file(suffix='.text'), "text"], + [tests.create_random_pdf(), "pdf"], + [tests.create_random_pdf(), "pdf"], + [tests.create_random_file(), "data"], + [tests.create_random_epub(), "epub"], + [tests.create_random_file(suffix='.yaml'), "yaml"], + [tests.create_random_file(suffix='.text'), "text"], ] for d in docs: assert(get_document_extension(d[0]) == d[1]) |