Ejemplo n.º 1
0
class RepositoryTest(unittest.TestCase):
    def setUp(self):
        self.repo = Repository('domainId','rep')
        self.repo._proxy = self
        self.logAndStateDir = os.path.join(tempfile.gettempdir(),'repositorytest')
        os.path.isdir(self.logAndStateDir) or os.mkdir(self.logAndStateDir)

    def tearDown(self):
        shutil.rmtree(self.logAndStateDir)

    def testNoTimeslots(self):
        slots = self.repo.shopclosed
        self.assertEquals(None, slots)
        self.assertFalse(self.repo.shopClosed())

    def testInitHarvestExclusionInterval(self):
        self.repo.fill(self, GETREPOSITORY)
        slots = self.repo.shopclosed
        self.assertEquals(2, len(slots))
        self.assertEquals('*:*:10:30-*:*:11:45', slots[0])
        self.assertEquals('*:5:5:59-*:5:23:00', slots[1])

    def testShopClosed(self):
        self.repo.fill(self, GETREPOSITORY)
        self.repo.closedSlots()
        self.assertEquals(False, self.repo.shopClosed(dateTuple = (2006,1,1,11,50)))

    def testTimeslotInitialization(self):
        self.repo.fill(self, GETREPOSITORY)
        timeslots = self.repo.closedSlots()
        self.assertEquals(2, len(timeslots))
        self.assertFalse(self.repo.shopClosed(dateTuple = (2006,1,1,11,50)))
        timeslots[1]._end = (Wildcard(), Wildcard(), Wildcard(), Wildcard())

        self.assertTrue(self.repo.shopClosed(dateTuple = (2006,1,1,11,50)))

    def testShopNotClosedAndThenClosed(self):
        self.repo.fill(self, GETREPOSITORY)
        timeslots = self.repo.closedSlots()
        self.assertFalse(self.repo.shopClosed(dateTuple = (2006,1,1,11,50)))

        timeslots[0]._end = (Wildcard(), Wildcard(), Wildcard(), Wildcard())
        self.assertTrue(self.repo.shopClosed(dateTuple = (2006,1,1,11,50)))

    def testDoNothing(self):
        self.repo.use = False
        self.repo.action = None
        action = MockAction()
        self.repo._createAction=lambda stateDir,logDir,generalHarvestLog: action
        result = self.repo.do(stateDir=self.logAndStateDir, logDir=self.logAndStateDir)
        self.assertEquals(('', False), result)
        self.assert_(action.called)
        self.assertEquals(False, self.repo.use)
        self.assertEquals(None, self.repo.action)

    def testHarvestWithBadResumptionToken(self):
        self.repo.use = True
        self.repo.action = None
        self.repo.complete = True
        action = CallTrace('Action')
        oaiError = OAIError('url', 'resumptionToken expired', 'badResumptionToken', 'lxmlResponse')
        action.exceptions['do'] = oaiError
        self.repo._createAction = lambda **kwargs: action
        message, again = self.repo.do(stateDir=self.logAndStateDir, logDir=self.logAndStateDir)
        self.assertTrue('resumptionToken expired' in message)
        self.assertEquals(['info', 'do', 'resetState'], [m.name for m in action.calledMethods])
        self.assertTrue(again)

    def testDoHarvest(self):
        self.repo.use = True
        self.repo.action = None
        action = MockAction(DONE)
        self.repo._createAction=lambda stateDir,logDir,generalHarvestLog: action
        result = self.repo.do(stateDir=self.logAndStateDir, logDir=self.logAndStateDir)
        self.assertEquals((DONE, False), result)
        self.assert_(action.called)
        self.assertEquals(True, self.repo.use)
        self.assertEquals(None, self.repo.action)

    def testDoHarvestWithCompleteHarvestingEnabled(self):
        self.repo.use = True
        self.repo.action = None
        self.repo.complete = True
        action = MockAction(DONE, hasResumptionToken=True)
        self.repo._createAction=lambda stateDir,logDir,generalHarvestLog: action
        result = self.repo.do(stateDir=self.logAndStateDir, logDir=self.logAndStateDir)
        self.assertEquals((DONE, True), result)

    def testDoHarvestWithCompleteHarvestingDisabled(self):
        self.repo.use = True
        self.repo.action = None
        self.repo.complete = False
        action = MockAction(DONE, hasResumptionToken=True)
        self.repo._createAction=lambda stateDir,logDir,generalHarvestLog: action
        result = self.repo.do(stateDir=self.logAndStateDir, logDir=self.logAndStateDir)
        self.assertEquals((DONE, False), result)

    def testDoSomeAction(self):
        self.repo._saharaget = self
        self.repo.action = 'someaction'
        action = MockAction(DONE)
        self.repo._createAction=lambda stateDir,logDir,generalHarvestLog: action
        result = self.repo.do(stateDir=self.logAndStateDir, logDir=self.logAndStateDir)
        self.assertEquals((DONE, False), result)
        self.assert_(action.called)
        self.assertEquals(None, self.repo.action)
        self.assertEquals('domainId', self.mock_repositoryActionDone_domainId)
        self.assertEquals('rep', self.mock_repositoryActionDone_repositoryId)

    def testDoSomeActionThatMustBeRepeated(self):
        self.repo.use = True
        self.repo.action = 'someaction'
        action = MockAction('Not yet done!', False)
        self.repo._createAction=lambda stateDir,logDir,generalHarvestLog: action
        result, hasResumptionToken = self.repo.do(stateDir=self.logAndStateDir, logDir=self.logAndStateDir)
        self.assertEquals('Not yet done!', result)
        self.assert_(action.called)
        self.assertEquals(True, self.repo.use)
        self.assertEquals('someaction', self.repo.action)

    def _testAction(self, use, action, expectedTypeName):
        self.repo.use = use
        self.repo.action = action
        createdAction = self.repo._createAction(stateDir=self.logAndStateDir, logDir=self.logAndStateDir, generalHarvestLog=NilEventLogger())
        self.assertEquals(expectedTypeName, createdAction.__class__.__name__)

    def testCreateAction(self):
        self._testAction(False, None, 'NoneAction')
        self._testAction(None, None, 'NoneAction')
        self._testAction(True, None, 'HarvestAction')
        self._testAction(False, 'clear', 'DeleteIdsAction')
        self._testAction(True, 'clear', 'DeleteIdsAction')
        self._testAction(False, 'refresh', 'SmoothAction')
        self._testAction(True, 'refresh', 'SmoothAction')
        try:
            self._testAction(True, 'nonexisting', 'ignored')
            self.fail()
        except ActionException, afe:
            self.assertEquals("Action 'nonexisting' not supported.", str(afe))