Skip to content

Commit

Permalink
#16 consider missing datestamp or identifier elements
Browse files Browse the repository at this point in the history
  • Loading branch information
mloesch authored and Mathias Loesch committed Aug 10, 2017
1 parent 6258c0c commit 81650a0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
9 changes: 5 additions & 4 deletions sickle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ class Header(OAIItem):
def __init__(self, header_element):
super(Header, self).__init__(header_element, strip_ns=True)
self.deleted = self.xml.attrib.get('status') == 'deleted'
self.identifier = self.xml.find(
self._oai_namespace + 'identifier').text
self.datestamp = self.xml.find(
self._oai_namespace + 'datestamp').text
_identifier_element = self.xml.find(self._oai_namespace + 'identifier')
_datestamp_element = self.xml.find(self._oai_namespace + 'datestamp')

self.identifier = getattr(_identifier_element, 'text', None)
self.datestamp = getattr(_datestamp_element, 'text', None)
self.setSpecs = [setSpec.text for setSpec in
self.xml.findall(self._oai_namespace + 'setSpec')]

Expand Down
4 changes: 2 additions & 2 deletions sickle/tests/sample_data/ListRecords4.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<ListRecords>
<record>
<header status="deleted">
<identifier>oai:test.example.com:1585310</identifier>
<datestamp>2011-07-18T16:31:00Z</datestamp>
<!--Missing identifier-->
<!--Missing datestamp-->
</header>
</record>
<record>
Expand Down
31 changes: 16 additions & 15 deletions sickle/tests/test_harvesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,38 +121,39 @@ def test_ListMetadataFormats(self):

def test_ListIdentifiers(self):
records = self.sickle.ListIdentifiers(metadataPrefix='oai_dc')
assert len([r for r in records]) == 4
self.assertEqual(len([r for r in records]), 4)

def test_ListIdentifiers_ignore_deleted(self):
records = self.sickle.ListIdentifiers(
metadataPrefix='oai_dc', ignore_deleted=True)
# There are 2 deleted headers in the test data
num_records = len([r for r in records])
assert num_records == 2
self.assertEqual(num_records, 2)

def test_Identify(self):
identify = self.sickle.Identify()
assert hasattr(identify, 'repositoryName')
assert hasattr(identify, 'baseURL')
assert hasattr(identify, 'adminEmail')
assert hasattr(identify, 'earliestDatestamp')
assert hasattr(identify, 'deletedRecord')
assert hasattr(identify, 'granularity')
assert hasattr(identify, 'description')
assert hasattr(identify, 'oai_identifier')
assert hasattr(identify, 'sampleIdentifier')
self.assertTrue(hasattr(identify, 'repositoryName'))
self.assertTrue(hasattr(identify, 'baseURL'))
self.assertTrue(hasattr(identify, 'adminEmail'))
self.assertTrue(hasattr(identify, 'earliestDatestamp'))
self.assertTrue(hasattr(identify, 'deletedRecord'))
self.assertTrue(hasattr(identify, 'granularity'))
self.assertTrue(hasattr(identify, 'description'))
self.assertTrue(hasattr(identify, 'oai_identifier'))
self.assertTrue(hasattr(identify, 'sampleIdentifier'))
dict(identify)

def test_GetRecord(self):
oai_id = 'oai:test.example.com:1996652'
record = self.sickle.GetRecord(identifier=oai_id)
assert record.header.identifier == oai_id
assert oai_id in record.raw
self.assertEqual(record.header.identifier, oai_id)
self.assertIn(oai_id, record.raw)
self.assertEqual(record.header.datestamp, '2011-09-05T12:51:52Z')
self.assertIsInstance(record.xml, etree._Element)
binary_type(record)
text_type(record)
dict(record.header)
assert dict(record) == record.metadata
self.assertEqual(dict(record), record.metadata)

# Test OAI-specific exceptions

Expand Down Expand Up @@ -194,4 +195,4 @@ def test_undefined_OAI_error_XML(self):
def test_OAIResponseIterator(self):
sickle = Sickle('fake_url', iterator=OAIResponseIterator)
records = [r for r in sickle.ListRecords(metadataPrefix='oai_dc')]
assert len(records) == 4
self.assertEqual(len(records), 4)

0 comments on commit 81650a0

Please sign in to comment.