Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_write(self):
"""Write a record off to a file."""
file_handle = open("test/writer-test.dat", "wb")
writer = pymarc.MARCWriter(file_handle)
record = pymarc.Record()
field = pymarc.Field("245", ["0", "0"], ["a", "foo"])
record.add_field(field)
writer.write(record)
writer.close()
self.assertTrue(
file_handle.closed, "The file handle should close when the writer closes"
)
# read it back in
reader = pymarc.MARCReader(open("test/writer-test.dat", "rb"))
next(reader)
reader.close()
# remove it
os.remove("test/writer-test.dat")
def test_classify_work_class(self):
book_rec = pymarc.Record()
book_rec.leader = ' a '
book_ingester = MARC21toCreativeWork(redis_datastore=TEST_REDIS,
record=book_rec)
book_ingester.__classify_work_class__()
self.assertEquals(book_ingester.work_class,
Book)
Internetressource
2018-04-25T00:00:00.01Z
imslpvifamusikvalsskramstadhansSkramstad,
Hans<title>Vals for
pianoforte</title>RomanticPianohttp://imslp.org/wiki/Vals_(Skramstad,_Hans)Internetressource]]>
"""
result = imslp_xml_to_marc(example)
assert result is not None
assert isinstance(result, pymarc.Record)
assert result["001"].value() == "finc-15-dmFsc3NrcmFtc3RhZGhhbnM"
assert result["100"]["a"] == "Skramstad, Hans"
assert result["245"]["a"] == "Vals for pianoforte"
assert result["856"]["u"] == "http://imslp.org/wiki/Vals_(Skramstad,_Hans)"
def test_date2(self):
# Authority Record
record = pymarc.Record()
record.add_field(pymarc.Field(tag='130',
indicators=[' ', '0'],
subfields=['f', '1902']))
title_parser = MARC21toTitleEntity(redis_datastore=TEST_REDIS,
record=record)
title_parser.ingest()
self.assertEquals(title_parser.title_entity.date,
['1902'])
def test_duplicate_cc_marc21(self):
# Default in test for Colorado College specific ingestor
record = pymarc.Record()
record.add_field(pymarc.Field(tag='907',
indicators=[' ',' '],
subfields=['a', '.b12345x']))
TEST_REDIS.hset('ils-bib-numbers', 'b12345', 'bf:Work:1')
ingester = MARC21toBIBFRAME(redis_datastore=TEST_REDIS,
record=record)
self.assert_(ingester.__duplicate_check__('ils-bib-numbers') is True)
def test_parse_to_array(self):
records = pymarc.parse_xml_to_array("test/batch.xml")
self.assertEqual(len(records), 2)
# should've got two records
self.assertEqual(type(records[0]), pymarc.Record)
self.assertEqual(type(records[1]), pymarc.Record)
# first record should have 18 fields
record = records[0]
self.assertEqual(len(record.get_fields()), 18)
# check the content of a control field
self.assertEqual(
record["008"].data, u"910926s1957 nyuuun eng "
)
# check a data field with subfields
field = record["245"]
self.assertEqual(field.indicator1, "0")
self.assertEqual(field.indicator2, "4")
self.assertEqual(field["a"], u"The Great Ray Charles")
self.assertEqual(field["h"], u"[sound recording].")
def startElementNS(self, name, qname, attrs):
element = name[1]
self._text = []
if element == 'record':
self._record = Record()
elif element == 'controlfield':
tag = attrs.getValue((None, u'tag'))
self._field = Field(tag)
elif element == 'datafield':
tag = attrs.getValue((None, u'tag'))
ind1 = attrs.getValue((None, u'ind1'))
ind2 = attrs.getValue((None, u'ind2'))
self._field = Field(tag, [ind1, ind2])
elif element == 'subfield':
self._subfield_code = attrs[(None, 'code')]
def transform(self, **kwargs):
"""Method transforms a MARC record (either instance source
or passed in MARC21 record) into BIBFRAME 2.0
Args:
record(pymarc.Record): MARC21 Record
"""
record=kwargs.pop("record")
if "calculate_default" in kwargs:
calculate_default = kwargs.pop("calculate_default")
else:
calculate_default = None
if record is not None:
if isinstance(record, pymarc.Record):
self.source = record
self.graph = new_graph()
bf_instance, bf_item = super(MARCIngester, self).transform(**kwargs)
# Run de-duplication methods
self.deduplicate_instances()
self.deduplicate_agents(
NS_MGR.schema.alternativeName,
NS_MGR.bf.Person)
self.deduplicate_agents(
NS_MGR.schema.oclc,
NS_MGR.bf.Organization,
calculate_default)
def bib(self, bibid, add_holdings=True):
try:
zoomrec = self.zoom_record(bibid)
marc = zoomrec.data.bibliographicRecord.encoding[1]
bib = Bib(marc=pymarc.Record(data=marc))
if add_holdings:
bib.holdings = self.holdings(zoom_record=zoomrec)
return bib
except:
raise
def element(self, element_dict, name=None):
"""Converts a JSON `element_dict` to pymarc fields."""
if not name:
self._record = Record()
self.element(element_dict, "leader")
elif name == "leader":
self._record.leader = element_dict[name]
self.element(element_dict, "fields")
elif name == "fields":
fields = iter(element_dict[name])
for field in fields:
tag, remaining = field.popitem()
self._field = Field(tag)
if self._field.is_control_field():
self._field.data = remaining
else:
self.element(remaining, "subfields")
self._field.indicators.extend(
[remaining["ind1"], remaining["ind2"]]
)