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()
Exemple #2
0
    def test_should_raise_already_exists_if_insert_journal_with_same_id(
        self, journals_to_kernel_mock, read_json_mock
    ):
        journals_to_kernel_mock.return_value = [SAMPLE_KERNEL_JOURNAL]

        with self.assertLogs(level="INFO") as log:
            pipeline.import_journals("~/json/title.json", self.session)
            pipeline.import_journals("~/json/title.json", self.session)
            self.assertIn("pipeline", log[1][0])
Exemple #3
0
    def test_should_add_journal_in_changes(
        self, journals_to_kernel_mock, read_json_mock
    ):
        journals_to_kernel_mock.return_value = [SAMPLE_KERNEL_JOURNAL]
        pipeline.import_journals("~/json/title.json", self.session)

        _id_expected = SAMPLE_KERNEL_JOURNAL["id"]
        _changes = self.session.changes.filter()

        self.assertEqual(1, len(_changes))
        self.assertEqual(_id_expected, _changes[0]["id"])
Exemple #4
0
    def test_should_import_journal(self, journals_to_kernel_mock, read_json_mock):
        journals_to_kernel_mock.return_value = [SAMPLE_KERNEL_JOURNAL]
        pipeline.import_journals("~/json/title.json", self.session)

        expected = SAMPLE_KERNEL_JOURNAL["_id"]
        self.assertEqual(expected, self.session.journals.fetch(expected).id())
Exemple #5
0
    def test_pipeline_should_log_exceptions(self, read_json_file_mock):
        read_json_file_mock.side_effect = FileNotFoundError

        with self.assertLogs(level="DEBUG") as log:
            pipeline.import_journals("~/json/title.json", self.session)
            self.assertIn("DEBUG", log.output[0])
Exemple #6
0
 def test_pipeline_should_read_correct_json_file(
     self, extract_isis_mock, read_json_file_mock
 ):
     pipeline.import_journals("~/json/title.json", self.session)
     read_json_file_mock.assert_called_once_with("~/json/title.json")