def test_default_document_catches_transform_exceptions(self): # Sample file conforming to NamedFile interface filename = 'test.txt' mimetype = 'application/pdf' data = 'foo' sample_blob = self.mocker.mock() sample_file = self.mocker.mock() self.expect(sample_file._blob).result(sample_blob) self.expect(sample_file.data).result(data) self.expect(sample_file.filename).result(filename) self.expect(sample_file.contentType).result(mimetype) # Sample document containing our file doc1 = self.mocker.mock() self.expect(doc1.get_file()).result(sample_file).count(1, None) def raise_transform_exception(*args, **kwargs): raise Exception("This transform failed!") # Mock portal object for getSite() site = self.providing_stub(interfaces=[ISiteRoot]) self.expect(site.aq_chain).result([site]) setSite(site) # Mock the portal_transforms tool to raise an exception mock_portal_transforms = self.mocker.mock() self.expect( mock_portal_transforms.convertTo( 'text/plain', data, mimetype=mimetype, filename=filename, object=sample_blob)).call(raise_transform_exception) self.mock_tool(mock_portal_transforms, "portal_transforms") self.replay() default_doc_indexer = DefaultDocumentIndexer(doc1) try: fulltext = default_doc_indexer.extract_text() except: # noqa - this bare except is the whole point of the test self.fail("extract_text() didn't catch exception raised " "by transform!") self.assertEquals('', fulltext)
def test_default_document_catches_transform_exceptions(self): # Sample file conforming to NamedFile interface filename = 'test.txt' mimetype = 'application/pdf' data = 'foo' sample_blob = self.mocker.mock() sample_file = self.mocker.mock() self.expect(sample_file._blob).result(sample_blob) self.expect(sample_file.data).result(data) self.expect(sample_file.filename).result(filename) self.expect(sample_file.contentType).result(mimetype) # Sample document containing our file doc1 = self.mocker.mock() self.expect(doc1.get_file()).result(sample_file).count(1, None) def raise_transform_exception(*args, **kwargs): raise Exception("This transform failed!") # Mock portal object for getSite() site = self.providing_stub(interfaces=[ISiteRoot]) self.expect(site.aq_chain).result([site]) setSite(site) # Mock the portal_transforms tool to raise an exception mock_portal_transforms = self.mocker.mock() self.expect(mock_portal_transforms.convertTo( 'text/plain', data, mimetype=mimetype, filename=filename, object=sample_blob)).call(raise_transform_exception) self.mock_tool(mock_portal_transforms, "portal_transforms") self.replay() default_doc_indexer = DefaultDocumentIndexer(doc1) try: fulltext = default_doc_indexer.extract_text() except: # noqa - this bare except is the whole point of the test self.fail("extract_text() didn't catch exception raised " "by transform!") self.assertEquals('', fulltext)
def test_default_document_indexer_calls_portal_transforms_correctly(self): # Sample file conforming to NamedFile interface filename = 'test.txt' mimetype = 'application/pdf' data = 'foo' sample_blob = self.mocker.mock() sample_file = self.mocker.mock() self.expect(sample_file._blob).result(sample_blob) self.expect(sample_file.data).result(data) self.expect(sample_file.filename).result(filename) self.expect(sample_file.contentType).result(mimetype) # Sample document containing our file doc1 = self.mocker.mock() self.expect(doc1.get_file()).result(sample_file).count(1, None) # datastream returned by transform expected_fulltext = 'FULLTEXT' stream = self.mocker.mock() self.expect(stream.getData()).result(expected_fulltext) # Mock portal object for getSite() site = self.providing_stub(interfaces=[ISiteRoot]) self.expect(site.aq_chain).result([site]) setSite(site) # Mock the portal_transforms tool mock_portal_transforms = self.mocker.mock() self.expect( mock_portal_transforms.convertTo( 'text/plain', data, mimetype=mimetype, filename=filename, object=sample_blob)).result(stream) self.mock_tool(mock_portal_transforms, "portal_transforms") self.replay() default_doc_indexer = DefaultDocumentIndexer(doc1) fulltext = default_doc_indexer.extract_text() self.assertEquals(expected_fulltext, fulltext)
def test_default_document_indexer_calls_portal_transforms_correctly(self): # Sample file conforming to NamedFile interface filename = 'test.txt' mimetype = 'application/pdf' data = 'foo' sample_blob = self.mocker.mock() sample_file = self.mocker.mock() self.expect(sample_file._blob).result(sample_blob) self.expect(sample_file.data).result(data) self.expect(sample_file.filename).result(filename) self.expect(sample_file.contentType).result(mimetype) # Sample document containing our file doc1 = self.mocker.mock() self.expect(doc1.get_file()).result(sample_file).count(1, None) # datastream returned by transform expected_fulltext = 'FULLTEXT' stream = self.mocker.mock() self.expect(stream.getData()).result(expected_fulltext) # Mock portal object for getSite() site = self.providing_stub(interfaces=[ISiteRoot]) self.expect(site.aq_chain).result([site]) setSite(site) # Mock the portal_transforms tool mock_portal_transforms = self.mocker.mock() self.expect(mock_portal_transforms.convertTo( 'text/plain', data, mimetype=mimetype, filename=filename, object=sample_blob)).result(stream) self.mock_tool(mock_portal_transforms, "portal_transforms") self.replay() default_doc_indexer = DefaultDocumentIndexer(doc1) fulltext = default_doc_indexer.extract_text() self.assertEquals(expected_fulltext, fulltext)