Example #1
0
class OaiRecordTest(SeecrTestCase):
    def setUp(self):
        SeecrTestCase.setUp(self)
        self.setUpOaiRecord()

    def setUpOaiRecord(self, **kwargs):
        self.oaiRecord = OaiRecord(**kwargs)
        self.observer = CallTrace('Observer')
        self.oaiRecord.addObserver(self.observer)
        self.observer.returnValues['provenance'] = (f for f in [])
        self.observer.returnValues['getData'] = '<data/>'

    def testRecord(self):
        result = ''.join(compose(self.oaiRecord.oaiRecord(record=MockRecord('id'), metadataPrefix='oai_dc', fetchedRecords=None)))
        self.assertEqualsWS("""<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <data/>
</metadata>
</record>""", result)
        self.assertEquals(["getData(identifier='id', name='oai_dc')", "provenance('id')"], [str(m) for m in self.observer.calledMethods])

    def testRecordWithRetrieveData(self):
        def getData(*_, **__):
            raise DeclineMessage()
        def retrieveData(*_, **__):
            raise StopIteration('<retrieved/>')
            yield
        self.observer.methods['retrieveData'] = retrieveData
        self.observer.methods['getData'] = getData
        self.observer.returnValues.pop('getData')
        result = ''.join(compose(self.oaiRecord.oaiRecord(record=MockRecord('id'), metadataPrefix='oai_dc', fetchedRecords=None)))
        self.assertEqualsWS("""<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <retrieved/>
</metadata>
</record>""", result)
        self.assertEquals([
            "getData(identifier='id', name='oai_dc')",
            "retrieveData(identifier='id', name='oai_dc')",
            "provenance('id')"
        ], [str(m) for m in self.observer.calledMethods])

    def testRecordWithFetchedRecords(self):
        record = MockRecord('id')
        result = ''.join(compose(self.oaiRecord.oaiRecord(record=record, metadataPrefix='oai_dc', fetchedRecords={record.identifier: "<the>data</the>", 'abc': '<some>other data</some>'})))
        self.assertEqualsWS("""<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <the>data</the>
</metadata>
</record>""", result)
        self.assertEquals(["provenance('id')"], [str(m) for m in self.observer.calledMethods])

    def testPreciseDatestamp(self):
        self.setUpOaiRecord(preciseDatestamp=True)
        record = MockRecord('id')
        result = ''.join(compose(self.oaiRecord.oaiRecord(record=record, metadataPrefix='oai_dc', fetchedRecords={record.identifier: "<the>data</the>", 'abc': '<some>other data</some>'})))
        self.assertEqualsWS("""<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00.123Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <the>data</the>
</metadata>
</record>""", result)

    def testRecordIsDeleted(self):
        result = ''.join(compose(self.oaiRecord.oaiRecord(record=MockRecord('id', deleted=True), metadataPrefix='oai_dc')))
        self.assertEqualsWS("""<record>
<header status="deleted">
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
</record>""", result)
        self.assertEquals([], [str(m) for m in self.observer.calledMethods])

    def testRecordWithDeleteInSetsSupport(self):
        self.setUpOaiRecord(deleteInSets=True)
        result = ''.join(compose(self.oaiRecord.oaiRecord(record=MockRecord('id', sets={'set0', 'set1'}, deletedSets={'set1'}), metadataPrefix='oai_dc')))
        self.assertEqualsWS("""<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec status="deleted">set1</setSpec>
</header>
<metadata><data/></metadata>
</record>""", result)


    def testRecordsWithoutSets(self):
        result = ''.join(compose(self.oaiRecord.oaiRecord(record=MockRecord('id', sets=[]), metadataPrefix='oai_dc')))
        self.assertEqualsWS("""<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
</header>
<metadata>
    <data/>
</metadata>
</record>""", result)
        self.assertEquals(["getData(identifier='id', name='oai_dc')", "provenance('id')"], [str(m) for m in self.observer.calledMethods])

    def testRecordWithProvenance(self):
        self.observer.returnValues['provenance'] = (f for f in ['PROV','ENANCE'])
        result = ''.join(compose(self.oaiRecord.oaiRecord(record=MockRecord('id'), metadataPrefix='oai_dc')))
        self.assertEqualsWS("""<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <data/>
</metadata>
<about>PROVENANCE</about>
</record>""", result)
        self.assertEquals(["getData(identifier='id', name='oai_dc')", "provenance('id')"], [str(m) for m in self.observer.calledMethods])

    def testDeletedRecordWithProvenance(self):
        self.observer.returnValues['provenance'] = (f for f in ['PROV','ENANCE'])
        result = ''.join(compose(self.oaiRecord.oaiRecord(record=MockRecord('id&0', deleted=True), metadataPrefix='oai_dc')))
        self.assertEqualsWS("""<record>
<header status="deleted">
    <identifier>id&amp;0</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
</record>""", result)
        self.assertEquals([], [str(m) for m in self.observer.calledMethods])

    def testRecordForListIdentifiers(self):
        result = ''.join(compose(self.oaiRecord.oaiRecordHeader(record=MockRecord('id'), metadataPrefix='oai_dc', kwarg0="ignored")))
        self.assertEqualsWS("""<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>""", result)
        self.assertEquals([], [str(m) for m in self.observer.calledMethods])

    def testRecordWithRepositoryIdentifier(self):
        self.setUpOaiRecord(repository=OaiRepository(identifier='example.org'))
        result = ''.join(compose(self.oaiRecord.oaiRecord(record=MockRecord('id'), metadataPrefix='oai_dc', fetchedRecords=None)))
        self.assertEqualsWS("""<record>
<header>
    <identifier>oai:example.org:id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <data/>
</metadata>
</record>""", result)
        self.assertEquals(["getData(identifier='id', name='oai_dc')", "provenance('id')"], [str(m) for m in self.observer.calledMethods])

    def testRecordWithFetchedRecordsWithRepositoryIdentifier(self):
        self.setUpOaiRecord(repository=OaiRepository(identifier='example.org'))
        record = MockRecord('id')
        result = ''.join(compose(self.oaiRecord.oaiRecord(record=record, metadataPrefix='oai_dc', fetchedRecords={record.identifier: "<the>data</the>", 'abc': '<some>other data</some>'})))
        self.assertEqualsWS("""<record>
<header>
    <identifier>oai:example.org:id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <the>data</the>
</metadata>
</record>""", result)
        self.assertEquals(["provenance('id')"], [str(m) for m in self.observer.calledMethods])
Example #2
0
class OaiRecordTest(SeecrTestCase):
    def setUp(self):
        SeecrTestCase.setUp(self)
        self.setUpOaiRecord()

    def setUpOaiRecord(self, **kwargs):
        self.oaiRecord = OaiRecord(**kwargs)
        self.observer = CallTrace('Observer')
        self.oaiRecord.addObserver(self.observer)
        self.observer.returnValues['provenance'] = (f for f in [])
        self.observer.returnValues['getData'] = '<data/>'

    def testRecord(self):
        result = ''.join(
            compose(
                self.oaiRecord.oaiRecord(record=MockRecord('id'),
                                         metadataPrefix='oai_dc',
                                         fetchedRecords=None)))
        self.assertEqualsWS(
            """<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <data/>
</metadata>
</record>""", result)
        self.assertEqual(
            ["getData(identifier='id', name='oai_dc')", "provenance('id')"],
            [str(m) for m in self.observer.calledMethods])

    def testRecordWithRetrieveData(self):
        def getData(*_, **__):
            raise DeclineMessage()

        def retrieveData(*_, **__):
            return '<retrieved/>'
            yield

        self.observer.methods['retrieveData'] = retrieveData
        self.observer.methods['getData'] = getData
        self.observer.returnValues.pop('getData')
        result = ''.join(
            compose(
                self.oaiRecord.oaiRecord(record=MockRecord('id'),
                                         metadataPrefix='oai_dc',
                                         fetchedRecords=None)))
        self.assertEqualsWS(
            """<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <retrieved/>
</metadata>
</record>""", result)
        self.assertEqual([
            "getData(identifier='id', name='oai_dc')",
            "retrieveData(identifier='id', name='oai_dc')", "provenance('id')"
        ], [str(m) for m in self.observer.calledMethods])

    def testRecordWithFetchedRecords(self):
        record = MockRecord('id')
        result = ''.join(
            compose(
                self.oaiRecord.oaiRecord(record=record,
                                         metadataPrefix='oai_dc',
                                         fetchedRecords={
                                             record.identifier:
                                             "<the>data</the>",
                                             'abc': '<some>other data</some>'
                                         })))
        self.assertEqualsWS(
            """<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <the>data</the>
</metadata>
</record>""", result)
        self.assertEqual(["provenance('id')"],
                         [str(m) for m in self.observer.calledMethods])

    def testPreciseDatestamp(self):
        self.setUpOaiRecord(preciseDatestamp=True)
        record = MockRecord('id')
        result = ''.join(
            compose(
                self.oaiRecord.oaiRecord(record=record,
                                         metadataPrefix='oai_dc',
                                         fetchedRecords={
                                             record.identifier:
                                             "<the>data</the>",
                                             'abc': '<some>other data</some>'
                                         })))
        self.assertEqualsWS(
            """<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00.123Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <the>data</the>
</metadata>
</record>""", result)

    def testRecordIsDeleted(self):
        result = ''.join(
            compose(
                self.oaiRecord.oaiRecord(record=MockRecord('id', deleted=True),
                                         metadataPrefix='oai_dc')))
        self.assertEqualsWS(
            """<record>
<header status="deleted">
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
</record>""", result)
        self.assertEqual([], [str(m) for m in self.observer.calledMethods])

    def testRecordWithDeleteInSetsSupport(self):
        self.setUpOaiRecord(deleteInSets=True)
        result = ''.join(
            compose(
                self.oaiRecord.oaiRecord(record=MockRecord(
                    'id', sets={'set0', 'set1'}, deletedSets={'set1'}),
                                         metadataPrefix='oai_dc')))
        self.assertEqualsWS(
            """<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec status="deleted">set1</setSpec>
</header>
<metadata><data/></metadata>
</record>""", result)

    def testRecordsWithoutSets(self):
        result = ''.join(
            compose(
                self.oaiRecord.oaiRecord(record=MockRecord('id', sets=[]),
                                         metadataPrefix='oai_dc')))
        self.assertEqualsWS(
            """<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
</header>
<metadata>
    <data/>
</metadata>
</record>""", result)
        self.assertEqual(
            ["getData(identifier='id', name='oai_dc')", "provenance('id')"],
            [str(m) for m in self.observer.calledMethods])

    def testRecordWithProvenance(self):
        self.observer.returnValues['provenance'] = (
            f for f in ['PROV', 'ENANCE'])
        result = ''.join(
            compose(
                self.oaiRecord.oaiRecord(record=MockRecord('id'),
                                         metadataPrefix='oai_dc')))
        self.assertEqualsWS(
            """<record>
<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <data/>
</metadata>
<about>PROVENANCE</about>
</record>""", result)
        self.assertEqual(
            ["getData(identifier='id', name='oai_dc')", "provenance('id')"],
            [str(m) for m in self.observer.calledMethods])

    def testDeletedRecordWithProvenance(self):
        self.observer.returnValues['provenance'] = (
            f for f in ['PROV', 'ENANCE'])
        result = ''.join(
            compose(
                self.oaiRecord.oaiRecord(record=MockRecord('id&0',
                                                           deleted=True),
                                         metadataPrefix='oai_dc')))
        self.assertEqualsWS(
            """<record>
<header status="deleted">
    <identifier>id&amp;0</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
</record>""", result)
        self.assertEqual([], [str(m) for m in self.observer.calledMethods])

    def testRecordForListIdentifiers(self):
        result = ''.join(
            compose(
                self.oaiRecord.oaiRecordHeader(record=MockRecord('id'),
                                               metadataPrefix='oai_dc',
                                               kwarg0="ignored")))
        self.assertEqualsWS(
            """<header>
    <identifier>id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>""", result)
        self.assertEqual([], [str(m) for m in self.observer.calledMethods])

    def testRecordWithRepositoryIdentifier(self):
        self.setUpOaiRecord(repository=OaiRepository(identifier='example.org'))
        result = ''.join(
            compose(
                self.oaiRecord.oaiRecord(record=MockRecord('id'),
                                         metadataPrefix='oai_dc',
                                         fetchedRecords=None)))
        self.assertEqualsWS(
            """<record>
<header>
    <identifier>oai:example.org:id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <data/>
</metadata>
</record>""", result)
        self.assertEqual(
            ["getData(identifier='id', name='oai_dc')", "provenance('id')"],
            [str(m) for m in self.observer.calledMethods])

    def testRecordWithFetchedRecordsWithRepositoryIdentifier(self):
        self.setUpOaiRecord(repository=OaiRepository(identifier='example.org'))
        record = MockRecord('id')
        result = ''.join(
            compose(
                self.oaiRecord.oaiRecord(record=record,
                                         metadataPrefix='oai_dc',
                                         fetchedRecords={
                                             record.identifier:
                                             "<the>data</the>",
                                             'abc': '<some>other data</some>'
                                         })))
        self.assertEqualsWS(
            """<record>
<header>
    <identifier>oai:example.org:id</identifier>
    <datestamp>2011-03-25T10:45:00Z</datestamp>
    <setSpec>set0</setSpec>
    <setSpec>set1</setSpec>
</header>
<metadata>
    <the>data</the>
</metadata>
</record>""", result)
        self.assertEqual(["provenance('id')"],
                         [str(m) for m in self.observer.calledMethods])