|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Copyright 2025 Telefónica Innovación Digital, S.L. |
| 4 | +This file is part of Toolium. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +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 | + |
| 19 | +import mock |
| 20 | +import pytest |
| 21 | + |
| 22 | +from toolium.driver_wrappers_pool import DriverWrappersPool |
| 23 | +from toolium.utils.ai_utils import (get_text_similarity_with_spacy, get_text_similarity_with_sentence_transformers, |
| 24 | + get_text_similarity_with_openai_llm, check_text_similarity) |
| 25 | + |
| 26 | + |
| 27 | +get_similarity_examples = ( |
| 28 | + ('Today it will be sunny', 'Today it will be sunny', 0.9, 1), |
| 29 | + ('Today is sunny', 'Today it will be sunny', 0.6, 0.9), |
| 30 | + ('It is sunny', 'Today it will be sunny', 0.5, 0.7), |
| 31 | + ('Nothing related', 'Today it will be sunny', 0, 0.6), |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize('input_text, expected_text, expected_low, expected_high', get_similarity_examples) |
| 36 | +def test_get_text_similarity_with_spacy(input_text, expected_text, expected_low, expected_high): |
| 37 | + similarity = get_text_similarity_with_spacy(input_text, expected_text) |
| 38 | + assert expected_low <= similarity <= expected_high |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize('input_text, expected_text, expected_low, expected_high', get_similarity_examples) |
| 42 | +def test_get_text_similarity_with_sentence_transformers(input_text, expected_text, expected_low, expected_high): |
| 43 | + similarity = get_text_similarity_with_sentence_transformers(input_text, expected_text) |
| 44 | + assert expected_low <= similarity <= expected_high |
| 45 | + |
| 46 | + |
| 47 | +get_openai_similarity_examples = ( |
| 48 | + ('Today it will be sunny', 'Today it will be sunny', 0.9, 1), |
| 49 | + ('Today is sunny', 'Today it will be sunny', 0.7, 0.9), |
| 50 | + ('It is sunny', 'Today it will be sunny', 0.7, 0.9), |
| 51 | + ('Nothing related', 'Today it will be sunny', 0, 0.1), |
| 52 | + ('Today it will be cloudy', 'Today it will be sunny', 0, 0.1), |
| 53 | + ('A splendid and rainless day is expected', 'Today it will be sunny', 0.8, 1), |
| 54 | +) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize('input_text, expected_text, expected_low, expected_high', get_openai_similarity_examples) |
| 58 | +def test_get_text_similarity_with_openai_llm(input_text, expected_text, expected_low, expected_high): |
| 59 | + similarity = get_text_similarity_with_openai_llm(input_text, expected_text) |
| 60 | + assert expected_low <= similarity <= expected_high |
| 61 | + |
| 62 | + |
| 63 | +check_similarity_passed_examples = ( |
| 64 | + ('Today it will be sunny', 'Today it will be sunny', 0.9), |
| 65 | + ('Today it will be sunny', ['Nothing related', 'Today it will be sunny'], 0.9), |
| 66 | + ('Today is sunny', ['It will be nice', 'Today it will be sunny'], 0.6), |
| 67 | +) |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize('input_text, expected_text, threshold', check_similarity_passed_examples) |
| 71 | +def test_check_text_similarity_with_spacy_passed(input_text, expected_text, threshold): |
| 72 | + check_text_similarity(input_text, expected_text, threshold=threshold, similarity_method='spacy') |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize('input_text, expected_text, threshold', check_similarity_passed_examples) |
| 76 | +def test_check_text_similarity_with_sentence_transformers_passed(input_text, expected_text, threshold): |
| 77 | + check_text_similarity(input_text, expected_text, threshold=threshold, similarity_method='sentence_transformers') |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.parametrize('input_text, expected_text, threshold', check_similarity_passed_examples) |
| 81 | +def test_check_text_similarity_with_openai_llm_passed(input_text, expected_text, threshold): |
| 82 | + check_text_similarity(input_text, expected_text, threshold=threshold, similarity_method='openai_llm') |
| 83 | + |
| 84 | + |
| 85 | +check_similarity_failed_examples = ( |
| 86 | + ('Today it will be sunny', 'Nothing related', 0.9), |
| 87 | + ('Today it will be sunny', ['Nothing related', 'It is sunny'], 0.9), |
| 88 | +) |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize('input_text, expected_text, threshold', check_similarity_failed_examples) |
| 92 | +def test_check_text_similarity_with_spacy_failed(input_text, expected_text, threshold): |
| 93 | + with pytest.raises(Exception) as excinfo: |
| 94 | + check_text_similarity(input_text, expected_text, threshold=threshold, similarity_method='spacy') |
| 95 | + assert str(excinfo.value).startswith('Similarity between received and expected texts is below threshold') |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize('input_text, expected_text, threshold', check_similarity_failed_examples) |
| 99 | +def test_check_text_similarity_with_sentence_transformers_failed(input_text, expected_text, threshold): |
| 100 | + with pytest.raises(Exception) as excinfo: |
| 101 | + check_text_similarity(input_text, expected_text, threshold=threshold, similarity_method='sentence_transformers') |
| 102 | + assert str(excinfo.value).startswith('Similarity between received and expected texts is below threshold') |
| 103 | + |
| 104 | + |
| 105 | +check_openai_similarity_failed_examples = ( |
| 106 | + ('Today it will be sunny', 'Nothing related', 0.9), |
| 107 | + ('Today it will be sunny', ['Nothing related', 'Today it is cold'], 0.9), |
| 108 | +) |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.parametrize('input_text, expected_text, threshold', check_openai_similarity_failed_examples) |
| 112 | +def test_check_text_similarity_with_openai_llm_failed(input_text, expected_text, threshold): |
| 113 | + with pytest.raises(Exception) as excinfo: |
| 114 | + check_text_similarity(input_text, expected_text, threshold=threshold, similarity_method='openai_llm') |
| 115 | + assert str(excinfo.value).startswith('Similarity between received and expected texts is below threshold') |
| 116 | + |
| 117 | + |
| 118 | +@mock.patch('toolium.utils.ai_utils.get_text_similarity_with_spacy') |
| 119 | +def test_check_text_similarity_with_default_method(similarity_mock): |
| 120 | + similarity_mock.return_value = 0.9 |
| 121 | + input_text = 'Today it will be sunny' |
| 122 | + expected_text = 'Today is sunny' |
| 123 | + check_text_similarity(input_text, expected_text, threshold=0.8) |
| 124 | + similarity_mock.assert_called_once_with(input_text, expected_text) |
| 125 | + |
| 126 | + |
| 127 | +@mock.patch('toolium.utils.ai_utils.get_text_similarity_with_sentence_transformers') |
| 128 | +def test_check_text_similarity_with_configured_method(similarity_mock): |
| 129 | + config = DriverWrappersPool.get_default_wrapper().config |
| 130 | + try: |
| 131 | + config.add_section('AI') |
| 132 | + except Exception: |
| 133 | + pass |
| 134 | + config.set('AI', 'text_similarity_method', 'sentence_transformers') |
| 135 | + similarity_mock.return_value = 0.9 |
| 136 | + |
| 137 | + input_text = 'Today it will be sunny' |
| 138 | + expected_text = 'Today is sunny' |
| 139 | + check_text_similarity(input_text, expected_text, threshold=0.8) |
| 140 | + similarity_mock.assert_called_once_with(input_text, expected_text) |
| 141 | + |
| 142 | + |
| 143 | +@mock.patch('toolium.utils.ai_utils.get_text_similarity_with_spacy') |
| 144 | +def test_check_text_similarity_with_configured_and_explicit_method(similarity_mock): |
| 145 | + config = DriverWrappersPool.get_default_wrapper().config |
| 146 | + try: |
| 147 | + config.add_section('AI') |
| 148 | + except Exception: |
| 149 | + pass |
| 150 | + config.set('AI', 'text_similarity_method', 'sentence_transformers') |
| 151 | + similarity_mock.return_value = 0.9 |
| 152 | + |
| 153 | + input_text = 'Today it will be sunny' |
| 154 | + expected_text = 'Today is sunny' |
| 155 | + check_text_similarity(input_text, expected_text, threshold=0.8, similarity_method='spacy') |
| 156 | + similarity_mock.assert_called_once_with(input_text, expected_text) |
0 commit comments