class InboxTest(SeecrTestCase): def setUp(self): SeecrTestCase.setUp(self) self.reactor = Reactor() self.observer = CallTrace('Observer') self.inboxDirectory = join(self.tempdir, 'inbox') self.doneDirectory = join(self.tempdir, 'done') makedirs(self.inboxDirectory) makedirs(self.doneDirectory) self.inbox = Inbox(self.reactor, inboxDirectory=self.inboxDirectory, doneDirectory=self.doneDirectory) self.inbox.addObserver(self.observer) def testCheckDirectoriesOnCreate(self): try: Inbox(CallTrace('Reactor'), inboxDirectory="/no_such_inbox", doneDirectory="/tmp") self.fail() except InboxException, e: self.assertEquals("directory /no_such_inbox does not exist", str(e)) try: Inbox(CallTrace('Reactor'), inboxDirectory="/tmp", doneDirectory="/no_such_done") self.fail() except InboxException, e: self.assertEquals("directory /no_such_done does not exist", str(e))
class InboxTest(SeecrTestCase): def setUp(self): SeecrTestCase.setUp(self) self.reactor = Reactor() self.observer = CallTrace('Observer') self.inboxDirectory = join(self.tempdir, 'inbox') self.doneDirectory = join(self.tempdir, 'done') makedirs(self.inboxDirectory) makedirs(self.doneDirectory) self.inbox = Inbox(self.reactor, inboxDirectory=self.inboxDirectory, doneDirectory=self.doneDirectory) self.inbox.addObserver(self.observer) def testCheckDirectoriesOnCreate(self): try: Inbox(CallTrace('Reactor'), inboxDirectory="/no_such_inbox", doneDirectory="/tmp") self.fail() except InboxException as e: self.assertEqual("directory /no_such_inbox does not exist", str(e)) try: Inbox(CallTrace('Reactor'), inboxDirectory="/tmp", doneDirectory="/no_such_done") self.fail() except InboxException as e: self.assertEqual("directory /no_such_done does not exist", str(e)) def testMovedInFileTriggersThings(self): events = [] self.inbox.processFile = lambda event: events.append(event) self.moveInRecord('repository:some:identifier:1') self.reactor.step() self.assertEqual(1, len(events)) self.assertEqual("repository:some:identifier:1.record", events[0]) def testProcessedGetsMovedToDone(self): self.moveInRecord(identifier='repository:some:identifier:1') self.assertEqual(0, len(self.observer.calledMethods)) self.assertTrue( isfile( join(self.inboxDirectory, 'repository:some:identifier:1.record'))) self.assertFalse( isfile( join(self.doneDirectory, 'repository:some:identifier:1.record'))) self.reactor.step() self.assertEqual(1, len(self.observer.calledMethods)) self.assertFalse( isfile( join(self.inboxDirectory, 'repository:some:identifier:1.record'))) self.assertTrue( isfile( join(self.doneDirectory, 'repository:some:identifier:1.record'))) def testProcessRecordsOnCommand(self): self.inboxDirectory = join(self.tempdir, 'inbox2') self.doneDirectory = join(self.tempdir, 'done2') makedirs(self.inboxDirectory) makedirs(self.doneDirectory) self.moveInRecord(identifier='repo:ident:1') self.moveInRecord(identifier='repo:ident:2') self.moveInRecord(identifier='repo:ident:3') inbox = Inbox(self.reactor, inboxDirectory=self.inboxDirectory, doneDirectory=self.doneDirectory) dna = be((Transparent(), (inbox, ))) dna.once.observer_init() self.assertEqual( set([ 'repo:ident:1.record', 'repo:ident:2.record', 'repo:ident:3.record' ]), set(listdir(self.inboxDirectory))) inbox.processInboxDirectory() self.assertEqual( set([ 'repo:ident:1.record', 'repo:ident:2.record', 'repo:ident:3.record' ]), set(listdir(self.doneDirectory))) def testErrorHandling(self): self.moveInRecord(identifier='repo:identifier:1') def mockedAddCall(identifier=None, name=None, lxmlNode=None): 1 / 0 self.observer.add = mockedAddCall self.reactor.step() self.assertFalse( isfile(join(self.inboxDirectory, 'repo:identifier:1.record'))) self.assertTrue( isfile(join(self.doneDirectory, 'repo:identifier:1.record'))) errorFile = join(self.doneDirectory, 'repo:identifier:1.record.error') self.assertTrue(isfile(errorFile)) with open(errorFile) as fp: errorMessage = fp.read() self.assertTrue( errorMessage.startswith("Traceback (most recent call last):")) self.assertTrue( errorMessage.endswith("ZeroDivisionError: division by zero\n"), errorMessage) def testNoXmlFile(self): identifier = 'repository:record' self.moveInRecord(identifier=identifier, data='this is no xml') self.reactor.step() errorFile = join(self.doneDirectory, identifier + '.record.error') self.assertTrue(isfile(errorFile)) self.assertFalse( isfile(join(self.inboxDirectory, identifier + '.record'))) with open(errorFile) as fp: self.assertTrue('Start tag expected' in fp.read()) def testFileDeletedBeforeHandling(self): identifier = 'repository:record' self.moveInRecord(identifier=identifier) self.removeRecord(identifier=identifier) self.reactor.step() errorFile = join(self.doneDirectory, '%s.record.error' % identifier) self.assertTrue(isfile(errorFile)) with open(errorFile) as fp: errorMessage = fp.read() self.assertTrue( errorMessage.startswith("Traceback (most recent call last):")) self.assertTrue( "FileNotFoundError: [Errno 2] No such file or directory" in errorMessage, errorMessage) def testFileDeleteWhileProcessing(self): identifier = 'repository:record' def mockedAddCall(identifier=None, name=None, lxmlNode=None): identifierWithoutExtension = identifier[:-len('.record')] self.removeRecord(identifierWithoutExtension) self.observer.add = mockedAddCall self.moveInRecord(identifier=identifier) errorFile = join(self.doneDirectory, '%s.record.error' % identifier) self.assertFalse(isfile(errorFile)) self.reactor.step() self.assertTrue(isfile(errorFile)) with open(errorFile) as fp: errorMessage = fp.read() self.assertTrue( errorMessage.startswith("Traceback (most recent call last):")) self.assertTrue( "rename(join(self._inboxDirectory, filename), join(self._doneDirectory, filename))" in errorMessage, errorMessage) self.assertTrue( "FileNotFoundError: [Errno 2] No such file or directory" in errorMessage, errorMessage) def moveInRecord(self, identifier, data="<record/>"): filename = join(self.tempdir, identifier + ".record") with open(filename, "w") as fp: fp.write(data) rename(filename, join(self.inboxDirectory, identifier + ".record")) def removeRecord(self, identifier): remove(join(self.inboxDirectory, identifier + ".record"))