Debian Codegraph @

tests/test_language_detect.py

raw
1 import pytest
2
3 pytest.importorskip("langdetect")
4
5 import unittest
6 from datetime import datetime
7 from unittest.mock import Mock
8
9 from parameterized import param, parameterized
10
11 from dateparser import parse
12 from dateparser.custom_language_detection.langdetect import (
13 detect_languages as lang_detect_detect_languages,
14 )
15 from dateparser.date import DateDataParser
16 from dateparser.search import search_dates
17
18
19 class LangDetectBasicTest(unittest.TestCase):
20 """Tests for basic langdetect functionality"""
21
22 def test_returns_list(self):
23 result = lang_detect_detect_languages("14 June 2020", 0.0)
24 self.assertIsInstance(result, list)
25
26 def test_detects_english(self):
27 result = lang_detect_detect_languages(
28 "The meeting is scheduled for Tuesday, July 22, 2014", 0.5
29 )
30 self.assertIn("en", result)
31
32 def test_detects_spanish(self):
33 result = lang_detect_detect_languages(
34 "La reunión está programada para el martes 22 de julio de 2014", 0.5
35 )
36 self.assertIn("es", result)
37
38 def test_detects_french(self):
39 result = lang_detect_detect_languages(
40 "La réunion est prévue pour le mardi 22 juillet 2014", 0.5
41 )
42 self.assertIn("fr", result)
43
44 def test_detects_german(self):
45 result = lang_detect_detect_languages(
46 "Das Treffen ist für Dienstag, den 22. Juli 2014 geplant", 0.5
47 )
48 self.assertIn("de", result)
49
50 def test_handles_numeric_only_input(self):
51 result = lang_detect_detect_languages("10-10-2021", 0.5)
52 self.assertEqual(result, [])
53
54 def test_handles_empty_string(self):
55 result = lang_detect_detect_languages("", 0.5)
56 self.assertEqual(result, [])
57
58 def test_confidence_threshold_filters_results(self):
59 # With low threshold, should detect language
60 result_low = lang_detect_detect_languages("14 June 2020", 0.0)
61 self.assertGreater(len(result_low), 0)
62
63 # With very high threshold, might not detect anything on short strings
64 result_high = lang_detect_detect_languages("14 June", 0.99)
65 # Short strings may have uncertain detection, result_high might be empty
66 self.assertIsInstance(result_high, list)
67
68
69 class LangDetectIntegrationTest(unittest.TestCase):
70 """Tests for langdetect integration with dateparser"""
71
72 def test_parse_with_langdetect_english(self):
73 result = parse(
74 "Tuesday Jul 22, 2014",
75 detect_languages_function=lang_detect_detect_languages,
76 )
77 self.assertEqual(result, datetime(2014, 7, 22, 0, 0, 0))
78
79 def test_parse_with_langdetect_spanish(self):
80 result = parse(
81 "martes 22 de julio de 2014",
82 detect_languages_function=lang_detect_detect_languages,
83 )
84 self.assertIsNotNone(result)
85 self.assertEqual(result.year, 2014)
86 self.assertEqual(result.month, 7)
87 self.assertEqual(result.day, 22)
88
89 def test_parse_with_langdetect_french(self):
90 result = parse(
91 "mardi 22 juillet 2014",
92 detect_languages_function=lang_detect_detect_languages,
93 )
94 self.assertIsNotNone(result)
95 self.assertEqual(result.year, 2014)
96 self.assertEqual(result.month, 7)
97 self.assertEqual(result.day, 22)
98
99 def test_datedataparser_with_langdetect(self):
100 ddp = DateDataParser(detect_languages_function=lang_detect_detect_languages)
101 result = ddp.get_date_data("Tuesday Jul 22, 2014")
102 self.assertEqual(result["date_obj"], datetime(2014, 7, 22, 0, 0, 0))
103
104 def test_search_dates_with_langdetect(self):
105 result = search_dates(
106 "The event is on January 3, 2017 and ends February 1st",
107 detect_languages_function=lang_detect_detect_languages,
108 )
109 self.assertIsNotNone(result)
110 self.assertEqual(len(result), 2)
111 # Check that dates are found
112 self.assertEqual(result[0][1], datetime(2017, 1, 3, 0, 0))
113 self.assertEqual(result[1][1], datetime(2017, 2, 1, 0, 0))
114
115 def test_parse_without_langdetect_still_works(self):
116 # Ensure dateparser works without custom language detection
117 result = parse("Tuesday Jul 22, 2014")
118 self.assertEqual(result, datetime(2014, 7, 22, 0, 0, 0))
119
120
121 class MockLangDetectTest(unittest.TestCase):
122 """Tests with mocked language detection"""
123
124 # Mock test for parse, search_dates and DateDataParser
125
126 detect_languages = Mock()
127 detect_languages.return_value = ["en"]
128
129 # parse
130
131 def when_date_is_parsed_using_parse(self, dt_string):
132 self.result = parse(dt_string, detect_languages_function=self.detect_languages)
133
134 def then_date_obj_exactly_is(self, expected_date_obj):
135 self.assertEqual(expected_date_obj, self.result)
136
137 @parameterized.expand(
138 [
139 param("Tuesday Jul 22, 2014", datetime(2014, 7, 22, 0, 0, 0)),
140 ]
141 )
142 def test_custom_language_detect_mock_parse(self, dt_string, expected_date_obj):
143 self.when_date_is_parsed_using_parse(dt_string)
144 self.then_date_obj_exactly_is(expected_date_obj)
145
146 # DateDataParser
147
148 def when_date_is_parsed_using_with_datedataparser(self, dt_string):
149 ddp = DateDataParser(detect_languages_function=self.detect_languages)
150 self.result = ddp.get_date_data(dt_string)["date_obj"]
151
152 @parameterized.expand(
153 [
154 param("Tuesday Jul 22, 2014", datetime(2014, 7, 22, 0, 0, 0)),
155 ]
156 )
157 def test_custom_language_detect_mock_datedataparser(
158 self, dt_string, expected_date_obj
159 ):
160 self.when_date_is_parsed_using_with_datedataparser(dt_string)
161 self.then_date_obj_exactly_is(expected_date_obj)
162
163 # search_date
164
165 def when_date_is_parsed_using_with_search_dates(self, dt_string):
166 self.result = search_dates(
167 dt_string, detect_languages_function=self.detect_languages
168 )
169
170 @parameterized.expand(
171 [
172 param(
173 "January 3, 2017 - February 1st",
174 [
175 ("January 3, 2017", datetime(2017, 1, 3, 0, 0)),
176 ("February 1st", datetime(2017, 2, 1, 0, 0)),
177 ],
178 ),
179 ]
180 )
181 def test_custom_language_detect_mock_search_dates(
182 self, dt_string, expected_date_obj
183 ):
184 self.when_date_is_parsed_using_with_search_dates(dt_string)
185 self.then_date_obj_exactly_is(expected_date_obj)