|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright ownership. |
| 6 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +# (the "License"); you may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# python -m unittest tika.tests.test_from_file_service |
| 19 | + |
| 20 | +import unittest |
| 21 | +import tika.parser |
| 22 | + |
| 23 | + |
| 24 | +class CreateTest(unittest.TestCase): |
| 25 | + 'test different services in from_file parsing: Content, Metadata or both in recursive mode' |
| 26 | + |
| 27 | + def test_default_service(self): |
| 28 | + 'parse file using default service' |
| 29 | + result = tika.parser.from_file( |
| 30 | + 'https://boe.es/boe/dias/2019/12/02/pdfs/BOE-A-2019-17288.pdf') |
| 31 | + self.assertEqual(result['metadata']['Content-Type'],'application/pdf') |
| 32 | + self.assertIn('AUTORIDADES Y PERSONAL',result['content']) |
| 33 | + def test_default_service_explicit(self): |
| 34 | + 'parse file using default service explicitly' |
| 35 | + result = tika.parser.from_file( |
| 36 | + 'https://boe.es/boe/dias/2019/12/02/pdfs/BOE-A-2019-17288.pdf', service='all') |
| 37 | + self.assertEqual(result['metadata']['Content-Type'],'application/pdf') |
| 38 | + self.assertIn('AUTORIDADES Y PERSONAL',result['content']) |
| 39 | + def test_text_service(self): |
| 40 | + 'parse file using the content only service' |
| 41 | + result = tika.parser.from_file( |
| 42 | + 'https://boe.es/boe/dias/2019/12/02/pdfs/BOE-A-2019-17288.pdf', service='text') |
| 43 | + self.assertIsNone(result['metadata']) |
| 44 | + self.assertIn('AUTORIDADES Y PERSONAL',result['content']) |
| 45 | + def test_meta_service(self): |
| 46 | + 'parse file using the content only service' |
| 47 | + result = tika.parser.from_file( |
| 48 | + 'https://boe.es/boe/dias/2019/12/02/pdfs/BOE-A-2019-17288.pdf', service='meta') |
| 49 | + self.assertIsNone(result['content']) |
| 50 | + self.assertEqual(result['metadata']['Content-Type'],'application/pdf') |
| 51 | + def test_invalid_service(self): |
| 52 | + 'parse file using an invalid service should perform the default parsing' |
| 53 | + result = tika.parser.from_file( |
| 54 | + 'https://boe.es/boe/dias/2019/12/02/pdfs/BOE-A-2019-17288.pdf', service='bad') |
| 55 | + self.assertEqual(result['metadata']['Content-Type'],'application/pdf') |
| 56 | + self.assertIn('AUTORIDADES Y PERSONAL',result['content']) |
| 57 | + |
| 58 | +if __name__ == '__main__': |
| 59 | + unittest.main() |
0 commit comments