def test_should_update_journals_with_document_bundle_ids(self, read_json_file_mock): read_json_file_mock.return_value = { "0001-3714": [ { "id": "0034-8910-1978-v12-n2", "number": "2", "order": "2", "supplement": "", "volume": "12", "year": "1978", } ] } pipeline.import_documents_bundles_link_with_journal( "~/json/output.json", self.session ) _journal = self.session.journals.fetch("0001-3714") _changes = self.session.changes.filter() self.assertIn( { "id": "0034-8910-1978-v12-n2", "number": "2", "order": "2", "supplement": "", "volume": "12", "year": "1978", }, _journal.issues, ) self.assertEqual(1, len(_changes))
def migrate_isis_parser(sargs): parser = argparse.ArgumentParser(description="ISIS database migration tool") subparsers = parser.add_subparsers(title="Commands", metavar="", dest="command") extract_parser = subparsers.add_parser("extract", help="Extract mst files to json") extract_parser.add_argument( "mst_file_path", metavar="file", help="Path to MST file that will be extracted" ) extract_parser.add_argument("--output", required=True, help="The output file path") import_parser = subparsers.add_parser( "import", parents=[mongodb_parser(sargs)], help="Process JSON files then import into Kernel database", ) import_parser.add_argument( "import_file", metavar="file", help="JSON file path that contains mst extraction result, e.g: collection-title.json", ) import_parser.add_argument( "--type", help="Type of JSON file that will load into Kernel database", choices=["journal", "issue", "documents-bundles-link"], required=True, ) link_parser = subparsers.add_parser( "link", help="Generate JSON file of journals' ids and their issues linked by ISSN", ) link_parser.add_argument( "issues", help="JSON file path that contains mst extraction result, e.g: ~/json/collection-issues.json", ) link_parser.add_argument("--output", required=True, help="The output file path") args = parser.parse_args(sargs) if args.command == "extract": extract_isis.create_output_dir(args.output) extract_isis.run(args.mst_file_path, args.output) elif args.command == "import": mongo = ds_adapters.MongoDB(uri=args.uri, dbname=args.db) Session = ds_adapters.Session.partial(mongo) if args.type == "journal": pipeline.import_journals(args.import_file, session=Session()) elif args.type == "issue": pipeline.import_issues(args.import_file, session=Session()) elif args.type == "documents-bundles-link": pipeline.import_documents_bundles_link_with_journal( args.import_file, session=Session() ) elif args.command == "link": pipeline.link_documents_bundles_with_journals(args.issues, args.output) else: parser.print_help()
def test_should_not_update_journals_with_document_bundle(self, read_json_file_mock): read_json_file_mock.return_value = {"0001-3714": []} pipeline.import_documents_bundles_link_with_journal( "~/json/output.json", self.session ) _journal = self.session.journals.fetch("0001-3714") self.assertEqual([], _journal.issues)
def test_should_not_update_journals_with_duplicated_bundles( self, read_json_file_mock ): read_json_file_mock.return_value = { "0001-3714": [ { "id": "issue-1", "order": "0001", "number": "01", "volume": "01", "year": "2019", "supplement": "supplement", }, { "id": "issue-2", "order": "0002", "number": "02", "volume": "02", "year": "2019", "supplement": "supplement", }, { "id": "issue-2", "order": "0002", "number": "02", "volume": "02", "year": "2019", "supplement": "supplement", }, ] } pipeline.import_documents_bundles_link_with_journal( "~/json/output.json", self.session ) _journal = self.session.journals.fetch("0001-3714") self.assertEqual( [ { "id": "issue-1", "order": "0001", "number": "01", "volume": "01", "year": "2019", "supplement": "supplement", }, { "id": "issue-2", "order": "0002", "number": "02", "volume": "02", "year": "2019", "supplement": "supplement", }, ], _journal.issues, )
def test_should_update_journals_with_document_bundle_ids(self, read_json_file_mock): read_json_file_mock.return_value = {"0001-3714": ["0001-3714-1998-v29-n3"]} pipeline.import_documents_bundles_link_with_journal( "~/json/output.json", self.session ) _journal = self.session.journals.fetch("0001-3714") _changes = self.session.changes.filter() self.assertIn("0001-3714-1998-v29-n3", _journal.issues) self.assertEqual(1, len(_changes))
def test_should_log_dabase_exceptions(self, read_json_file_mock): read_json_file_mock.side_effect = [ { "0001-3714": [ { "id": "issue-1", "order": "0001", "number": "01", "volume": "01", "year": "2019", "supplement": "supplement", }, { "id": "issue-1", "order": "0001", "number": "01", "volume": "01", "year": "2019", "supplement": "supplement", }, { "id": "issue-2", "order": "0002", "number": "02", "volume": "02", "year": "2019", "supplement": "supplement", }, ] }, {"missing-journal": []}, ] with self.assertLogs(level="DEBUG") as log: pipeline.import_documents_bundles_link_with_journal( "~/json/output.json", self.session ) self.assertIn( "Bundle issue-1 already exists in journal 0001-3714", log[-1][-1] ) with self.assertLogs(level="DEBUG") as log: pipeline.import_documents_bundles_link_with_journal( "~/json/output.json", self.session ) self.assertIn( "Journal missing-journal does not exists, cannot link bundles.", log[-1][-1], )
def test_read_journal_bundle_file(self, read_json_file_mock): read_json_file_mock.return_value = {} pipeline.import_documents_bundles_link_with_journal( "~/json/output.json", self.session ) read_json_file_mock.assert_called_once_with("~/json/output.json")