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_close_true(self):
"""If close_fh is true, then the file handle is also closed."""
file_handle = StringIO()
self.assertFalse(file_handle.closed, "The file handle should be open")
writer = pymarc.JSONWriter(file_handle)
self.assertFalse(file_handle.closed, "The file handle should still be open")
writer.close()
self.assertTrue(
file_handle.closed, "The file handle should close when the writer closes"
)
"ind1": "0",
"ind2": "0",
"subfields": [
{ "a": "Foo /" },
{ "c": "by me." }
]
}
}
]
}
]
"""
)
file_handle = StringIO()
try:
writer = pymarc.JSONWriter(file_handle)
record = pymarc.Record()
record.add_field(pymarc.Field("100", ["0", "0"], ["a", "me"]))
record.add_field(
pymarc.Field("245", ["0", "0"], ["a", "Foo /", "c", "by me."])
)
writer.write(record)
writer.close(close_fh=False)
actual = json.loads(file_handle.getvalue())
self.assertEquals(actual, expected)
finally:
file_handle.close()
def test_writing_empty_record(self):
expected = json.loads(
r"""
[
{
"leader" : " 22 4500",
"fields" : []
}
]
"""
)
file_handle = StringIO()
try:
writer = pymarc.JSONWriter(file_handle)
record = pymarc.Record()
writer.write(record)
writer.close(close_fh=False)
actual = json.loads(file_handle.getvalue())
self.assertEquals(actual, expected)
finally:
file_handle.close()
def test_writing_0_records(self):
expected = json.loads(
r"""
[]
"""
)
file_handle = StringIO()
try:
writer = pymarc.JSONWriter(file_handle)
writer.close(close_fh=False)
actual = json.loads(file_handle.getvalue())
self.assertEquals(actual, expected)
finally:
file_handle.close()
def test_close_false(self):
"""If close_fh is false, then the file handle is NOT closed."""
file_handle = StringIO()
self.assertFalse(file_handle.closed, "The file handle should be open")
writer = pymarc.JSONWriter(file_handle)
self.assertFalse(file_handle.closed, "The file handle should still be open")
writer.close(close_fh=False)
self.assertFalse(
file_handle.closed,
"The file handle should NOT close when the writer closes",
)
"ind1": "0",
"ind2": "0",
"subfields": [
{ "a": "Foo /" },
{ "c": "by me." }
]
}
}
]
}
]
"""
)
file_handle = StringIO()
try:
writer = pymarc.JSONWriter(file_handle)
record = pymarc.Record()
record.add_field(
pymarc.Field("008", data="090227s2009 mau chi d")
)
record.add_field(pymarc.Field("100", ["0", "0"], ["a", "me"]))
record.add_field(
pymarc.Field("245", ["0", "0"], ["a", "Foo /", "c", "by me."])
)
writer.write(record)
record = pymarc.Record()
record.add_field(pymarc.Field("100", ["0", "0"], ["a", "me"]))
record.add_field(
pymarc.Field("245", ["0", "0"], ["a", "Foo /", "c", "by me."])
)
writer.write(record)
record = pymarc.Record()