def remove_unwanted_tags(record: Record, material_type: str) -> List[str]: """Removes tags that will be replaced with MarcExpress tags or are simply not desired. Args: record: full Worldcat record as pymarc object Returns: list of tags """ eresource_tags = [ "001", # will be re-generated insted bc sys diffs "019", "020", "024", "037", "084", "099", "091", "263", "856", "938", # is there a way to remove all 9xxs? "949", ] print_tags = [ "001", # will be re-generated instead bc sys diffs "019", "084", "099", "091", "263", "938", "949", ] if material_type == "eresources": record.remove_fields(*eresource_tags) elif material_type == "print": record.remove_fields(*print_tags) else: raise NightShiftError( "Unable to manipulate MARC record for unknown material type." )
class TestBibsUtilities(unittest.TestCase): """ Tests utitlities in the bibs module responsible for reading and writing MARC records in various formats """ def setUp(self): """ create record in MARC21 format and other to simulate operations on them """ # Test MARC record self.marc_bib = Record() self.marc_bib.leader = "00000nam a2200000u 4500" tags = [] tags.append(Field(tag="001", data="0001-test-control_field")) tags.append( Field(tag="245", indicators=["0", "0"], subfields=["a", "Test title"])) tags.append( Field( tag="949", indicators=[" ", "1"], subfields=[ "i", "33333818132462", "l", "fea0f", "p", "9.99", "t", "102", "v", "AMALIVRE", ], )) tags.append( Field( tag="949", indicators=[" ", "1"], subfields=[ "i", "33333818132464", "l", "fea0f", "p", "9.99", "t", "102", "v", "AMALIVRE", ], )) tags.append( Field( tag="960", indicators=[" ", " "], subfields=[ "i", "33333818132466", "l", "fea0f", "p", "9.99", "t", "102", "v", "AMALIVRE", ], )) for tag in tags: self.marc_bib.add_ordered_field(tag) # temp file self.fh_out = "MARCtest.mrc" def tearDown(self): self.marc_bib = None try: os.remove(self.fh_out) except OSError: pass def test_write_marc21(self): bibs.write_marc21(self.fh_out, self.marc_bib) contents = open(self.fh_out).read() self.assertEqual( contents, "00266nam a2200085u 4500001002400000245001500024949004700039949004700086960004700133\x1e0001-test-control_field\x1e00\x1faTest title\x1e 1\x1fi33333818132462\x1flfea0f\x1fp9.99\x1ft102\x1fvAMALIVRE\x1e 1\x1fi33333818132464\x1flfea0f\x1fp9.99\x1ft102\x1fvAMALIVRE\x1e \x1fi33333818132466\x1flfea0f\x1fp9.99\x1ft102\x1fvAMALIVRE\x1e\x1d", ) def test_read_marc21_returns_pymarc_reader(self): # should return an instance of pymarc reader reader = bibs.read_marc21("test.mrc") self.assertIs(type(reader), MARCReader) # def test_count_bibs_when_not_marc_file(self): # with self.assertRaises(OverloadError): # bibs.count_bibs('test.json') def test_read_from_json_retuns_pymarc_reader(self): reader = JSONReader("test.json") self.assertIs(type(reader), JSONReader) def test_create_tag_910_for_nypl_branches(self): self.assertEqual(str(bibs.create_tag_910("NYPL", "branches")), "=910 \\\\$aBL") def test_create_tag_910_for_nypl_research(self): self.assertEqual(str(bibs.create_tag_910("NYPL", "research")), "=910 \\\\$aRL") def test_create_tag_910_for_bpl(self): self.assertIsNone(bibs.create_tag_910("BPL", None)) def test_create_tag_910_invalid_nypl_branch(self): with self.assertRaises(ValueError): bibs.create_tag_910("NYPL", None) def test_create_target_id_field_exceptions(self): with self.assertRaises(ValueError): bibs.create_target_id_field("nypl", "012345") def test_create_target_id_field_returns_instance_of_pymarc_Field(self): self.assertIsInstance(bibs.create_target_id_field("nypl", "01234567"), Field) def test_create_target_id_field_returns_correct_field_values(self): self.assertEqual( bibs.create_target_id_field("bpl", "01234567").tag, "907") self.assertEqual( bibs.create_target_id_field("bpl", "01234567").indicators, [" ", " "]) self.assertEqual( bibs.create_target_id_field("bpl", "01234567").subfields, ["a", ".b01234567a"], ) self.assertEqual( bibs.create_target_id_field("nypl", "01234567").tag, "945") self.assertEqual( bibs.create_target_id_field("nypl", "01234567").indicators, [" ", " "]) self.assertEqual( bibs.create_target_id_field("nypl", "01234567").subfields, ["a", ".b01234567a"], ) def test_check_sierra_id_presence(self): self.assertFalse(bibs.check_sierra_id_presence("nypl", self.marc_bib)) self.assertFalse(bibs.check_sierra_id_presence("bpl", self.marc_bib)) # add 945 self.marc_bib.add_field( Field(tag="945", indicators=[" ", " "], subfields=["a", ".b01234567a"])) self.assertTrue(bibs.check_sierra_id_presence("nypl", self.marc_bib)) self.marc_bib.add_field( Field(tag="907", indicators=[" ", " "], subfields=["a", ".b01234567a"])) self.assertTrue(bibs.check_sierra_id_presence("bpl", self.marc_bib)) def test_check_sierra_command_tag_presence_False(self): self.assertFalse(bibs.sierra_command_tag(self.marc_bib)) self.marc_bib.add_field( Field(tag="949", indicators=[" ", "1"], subfields=["a", "*b2=a;"])) self.marc_bib.add_field( Field(tag="949", indicators=[" ", " "], subfields=["b", "*b2=a;"])) self.assertFalse(bibs.sierra_command_tag(self.marc_bib)) def test_check_sierra_command_tag_presence_True(self): self.marc_bib.add_field( Field(tag="949", indicators=[" ", " "], subfields=["a", "*b2=a;"])) self.assertTrue(bibs.sierra_command_tag(self.marc_bib)) def test_check_sierra_command_tag_presence_exception(self): self.marc_bib.add_field( Field(tag="949", indicators=[" ", " "], subfields=["a", ""])) with self.assertRaises(IndexError): bibs.sierra_command_tag(self.marc_bib) def test_create_field_from_template(self): template = dict( option="skip", tag="949", ind1=None, ind2="1", subfields={ "a": "foo", "b": "bar" }, ) field = bibs.create_field_from_template(template) self.assertIsInstance(field, Field) self.assertEqual(field.tag, "949") self.assertEqual(field.indicators, [" ", "1"]) self.assertEqual(field["a"], "foo") self.assertEqual(field["b"], "bar") def test_set_nypl_sierra_bib_default_location_for_branches_new(self): # test when no command line present bib = bibs.set_nypl_sierra_bib_default_location( "branches", self.marc_bib) for field in bib.get_fields("949"): if field.indicators == [" ", " "]: command = field self.assertEqual(str(command), "=949 \\\\$a*bn=zzzzz;") def test_set_nypl_sierra_bib_default_location_for_branches_present(self): # test adding location skipped when "bn=" command already present self.marc_bib.add_field( Field(tag="949", indicators=[" ", " "], subfields=["a", "*recs=a;bn=xxx;"])) bib = bibs.set_nypl_sierra_bib_default_location( "branches", self.marc_bib) for field in bib.get_fields("949"): if field.indicators == [" ", " "]: command = field self.assertEqual(str(command), "=949 \\\\$a*recs=a;bn=xxx;") def test_set_nypl_sierra_bib_default_location_for_branches_other_command_present( self, ): # simulate command line with other parameters present self.marc_bib.add_field( Field(tag="949", indicators=[" ", " "], subfields=["a", "*recs=a"])) bib = bibs.set_nypl_sierra_bib_default_location( "branches", self.marc_bib) for field in bib.get_fields("949"): if field.indicators == [" ", " "]: command = field self.assertEqual(str(command), "=949 \\\\$a*recs=a;bn=zzzzz;") def test_set_nypl_sierra_bib_default_location_for_research_new(self): # test when no command line present bib = bibs.set_nypl_sierra_bib_default_location( "research", self.marc_bib) for field in bib.get_fields("949"): if field.indicators == [" ", " "]: command = field self.assertEqual(str(command), "=949 \\\\$a*bn=xxx;") def test_bibmeta_object(self): meta = bibs.BibMeta(self.marc_bib, sierraId="12345678") self.assertIsInstance(meta, bibs.BibMeta) self.assertEqual(meta.t001, "0001-test-control_field") self.assertIsNone(meta.t005) self.assertEqual(meta.t020, []) self.assertEqual(meta.t022, []) self.assertEqual(meta.t024, []) self.assertEqual(meta.t028, []) self.assertEqual(meta.sierraId, "12345678") self.assertIsNone(meta.bCallNumber) self.assertEqual(meta.rCallNumber, []) def test_vendor_bibmeta_object(self): self.marc_bib.add_field( Field(tag="945", indicators=[" ", " "], subfields=["a", ".b01234569a"])) meta = bibs.VendorBibMeta(self.marc_bib, vendor="Amalivre", dstLibrary="rl") self.assertIsInstance(meta, bibs.VendorBibMeta) self.assertEqual(meta.t001, "0001-test-control_field") self.assertIsNone(meta.t005) self.assertEqual(meta.t020, []) self.assertEqual(meta.t022, []) self.assertEqual(meta.t024, []) self.assertEqual(meta.t028, []) self.assertIsNone(meta.bCallNumber) self.assertEqual(meta.rCallNumber, []) self.assertEqual(meta.vendor, "Amalivre") self.assertEqual(meta.dstLibrary, "rl") self.assertEqual(meta.sierraId, "01234569") self.assertEqual( meta.barcodes, ["33333818132462", "33333818132464", "33333818132466"]) def test_vendor_bibmeta_object_when_sierra_id_is_provided(self): # nypl scenario self.marc_bib.add_field( Field(tag="945", indicators=[" ", " "], subfields=["a", ".b01234567a"])) meta = bibs.VendorBibMeta(self.marc_bib, vendor="BTODC", dstLibrary="branches") self.assertEqual(meta.sierraId, "01234567") # bpl scencario self.marc_bib.remove_fields("945") self.marc_bib.add_field( Field(tag="907", indicators=[" ", " "], subfields=["a", ".b01234568a"])) meta = bibs.VendorBibMeta(self.marc_bib, vendor="BTCLS", dstLibrary="branches") self.assertEqual(meta.sierraId, "01234568")