|
| 1 | +''' |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2023 Ulster University (https://www.ulster.ac.uk). |
| 5 | +Project: Harmony (https://harmonydata.ac.uk) |
| 6 | +Maintainer: Thomas Wood (https://fastdatascience.com) |
| 7 | +
|
| 8 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +of this software and associated documentation files (the "Software"), to deal |
| 10 | +in the Software without restriction, including without limitation the rights |
| 11 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +copies of the Software, and to permit persons to whom the Software is |
| 13 | +furnished to do so, subject to the following conditions: |
| 14 | +
|
| 15 | +The above copyright notice and this permission notice shall be included in all |
| 16 | +copies or substantial portions of the Software. |
| 17 | +
|
| 18 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +SOFTWARE. |
| 25 | +
|
| 26 | +''' |
| 27 | + |
| 28 | +import os |
| 29 | +import sys |
| 30 | +import types |
| 31 | +import unittest |
| 32 | +from types import SimpleNamespace |
| 33 | +from unittest import mock |
| 34 | + |
| 35 | +sys.path.append("../src") |
| 36 | + |
| 37 | +from harmony.parsing import wrapper_all_parsers |
| 38 | +from harmony.parsing.wrapper_all_parsers import convert_files_to_instruments |
| 39 | +from harmony.schemas.requests.text import RawFile, Instrument |
| 40 | + |
| 41 | +pdf_file = RawFile.model_validate({ |
| 42 | + "file_name": "GAD-7.pdf", |
| 43 | + "file_type": "pdf", |
| 44 | + "content": "data:application/pdf;base64,JVBERi0xLjQK" |
| 45 | +}) |
| 46 | + |
| 47 | +png_file = RawFile.model_validate({ |
| 48 | + "file_name": "GAD-7.png", |
| 49 | + "file_type": "png", |
| 50 | + "content": "data:image/png;base64,iVBORw0KGgo=" |
| 51 | +}) |
| 52 | + |
| 53 | +csv_file = RawFile.model_validate({ |
| 54 | + "file_name": "GAD-7.csv", |
| 55 | + "file_type": "csv", |
| 56 | + "content": "I feel nervous\nI feel afraid" |
| 57 | +}) |
| 58 | + |
| 59 | +pdf_parser_result = [Instrument(instrument_name="from the PDF parser", questions=[])] |
| 60 | + |
| 61 | + |
| 62 | +def fake_openai_module(client): |
| 63 | + module = types.ModuleType("openai") |
| 64 | + module.OpenAI = mock.Mock(return_value=client) |
| 65 | + return module |
| 66 | + |
| 67 | + |
| 68 | +def fake_openai_client(): |
| 69 | + client = mock.Mock() |
| 70 | + client.files.create.return_value = SimpleNamespace(id="file-abc") |
| 71 | + client.responses.parse.return_value = SimpleNamespace(output_parsed=SimpleNamespace(questions=[ |
| 72 | + SimpleNamespace(question_no="1", question_intro="Over the last two weeks", |
| 73 | + question_text="Feeling nervous", response_options=["Not at all", "Several days"]), |
| 74 | + ])) |
| 75 | + return client |
| 76 | + |
| 77 | + |
| 78 | +class TestOpenAIExtraction(unittest.TestCase): |
| 79 | + |
| 80 | + def test_csv_parses_without_key_or_openai_installed(self): |
| 81 | + with mock.patch.dict(os.environ, {}, clear=True), mock.patch.dict(sys.modules, {"openai": None}): |
| 82 | + instruments = convert_files_to_instruments([csv_file]) |
| 83 | + self.assertEqual(2, len(instruments[0].questions)) |
| 84 | + |
| 85 | + @mock.patch.object(wrapper_all_parsers, "convert_pdf_to_instruments", return_value=pdf_parser_result) |
| 86 | + def test_pdf_without_key_uses_pdf_parser(self, convert_pdf): |
| 87 | + with mock.patch.dict(os.environ, {}, clear=True): |
| 88 | + instruments = convert_files_to_instruments([pdf_file]) |
| 89 | + self.assertEqual("from the PDF parser", instruments[0].instrument_name) |
| 90 | + |
| 91 | + @mock.patch.object(wrapper_all_parsers, "convert_pdf_to_instruments", return_value=pdf_parser_result) |
| 92 | + def test_pdf_with_key_uses_openai(self, convert_pdf): |
| 93 | + client = fake_openai_client() |
| 94 | + with mock.patch.dict(os.environ, {"OPENAI_API_KEY": "test"}), \ |
| 95 | + mock.patch.dict(sys.modules, {"openai": fake_openai_module(client)}): |
| 96 | + instruments = convert_files_to_instruments([pdf_file]) |
| 97 | + convert_pdf.assert_not_called() |
| 98 | + client.files.create.assert_called_once() |
| 99 | + self.assertEqual("GAD-7.pdf", instruments[0].instrument_name) |
| 100 | + self.assertEqual("GAD-7.pdf", instruments[0].file_name) |
| 101 | + self.assertEqual("Feeling nervous", instruments[0].questions[0].question_text) |
| 102 | + self.assertEqual("Over the last two weeks", instruments[0].questions[0].question_intro) |
| 103 | + self.assertEqual(["Not at all", "Several days"], instruments[0].questions[0].options) |
| 104 | + self.assertEqual(instruments[0].instrument_id, instruments[0].questions[0].instrument_id) |
| 105 | + |
| 106 | + def test_image_with_key_uses_openai(self): |
| 107 | + client = fake_openai_client() |
| 108 | + with mock.patch.dict(os.environ, {"OPENAI_API_KEY": "test"}), \ |
| 109 | + mock.patch.dict(sys.modules, {"openai": fake_openai_module(client)}): |
| 110 | + instruments = convert_files_to_instruments([png_file]) |
| 111 | + client.files.create.assert_not_called() |
| 112 | + self.assertEqual("GAD-7.png", instruments[0].instrument_name) |
| 113 | + self.assertEqual(1, len(instruments[0].questions)) |
| 114 | + |
| 115 | + @mock.patch.object(wrapper_all_parsers, "convert_pdf_to_instruments", return_value=pdf_parser_result) |
| 116 | + def test_pdf_falls_back_when_openai_fails(self, convert_pdf): |
| 117 | + client = fake_openai_client() |
| 118 | + client.responses.parse.side_effect = RuntimeError("rate limited") |
| 119 | + with mock.patch.dict(os.environ, {"OPENAI_API_KEY": "test"}), \ |
| 120 | + mock.patch.dict(sys.modules, {"openai": fake_openai_module(client)}): |
| 121 | + instruments = convert_files_to_instruments([pdf_file]) |
| 122 | + self.assertEqual("from the PDF parser", instruments[0].instrument_name) |
| 123 | + |
| 124 | + @mock.patch.object(wrapper_all_parsers, "OPENAI_MAX_CONTENT_LENGTH", 100) |
| 125 | + @mock.patch.object(wrapper_all_parsers, "convert_pdf_to_instruments", return_value=pdf_parser_result) |
| 126 | + def test_large_pdf_uses_pdf_parser(self, convert_pdf): |
| 127 | + large_pdf_file = pdf_file.model_copy(update={"content": "data:application/pdf;base64," + "A" * 100}) |
| 128 | + client = fake_openai_client() |
| 129 | + with mock.patch.dict(os.environ, {"OPENAI_API_KEY": "test"}), \ |
| 130 | + mock.patch.dict(sys.modules, {"openai": fake_openai_module(client)}): |
| 131 | + instruments = convert_files_to_instruments([large_pdf_file]) |
| 132 | + client.responses.parse.assert_not_called() |
| 133 | + self.assertEqual("from the PDF parser", instruments[0].instrument_name) |
| 134 | + |
| 135 | + @mock.patch.object(wrapper_all_parsers, "convert_pdf_to_instruments", return_value=pdf_parser_result) |
| 136 | + def test_docx_with_key_uses_pdf_parser(self, convert_pdf): |
| 137 | + docx_file = pdf_file.model_copy(update={"file_name": "GAD-7.docx", "file_type": "docx"}) |
| 138 | + client = fake_openai_client() |
| 139 | + with mock.patch.dict(os.environ, {"OPENAI_API_KEY": "test"}), \ |
| 140 | + mock.patch.dict(sys.modules, {"openai": fake_openai_module(client)}): |
| 141 | + instruments = convert_files_to_instruments([docx_file]) |
| 142 | + client.responses.parse.assert_not_called() |
| 143 | + self.assertEqual("from the PDF parser", instruments[0].instrument_name) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == '__main__': |
| 147 | + unittest.main() |
0 commit comments