Skip to content

Commit 712faf8

Browse files
Copilotbact
andcommitted
Mock large corpus file downloads in tests for speed and stability
Co-authored-by: bact <128572+bact@users.noreply.github.com>
1 parent 2f4143f commit 712faf8

1 file changed

Lines changed: 102 additions & 8 deletions

File tree

tests/core/test_corpus.py

Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
# SPDX-FileType: SOURCE
33
# SPDX-License-Identifier: Apache-2.0
44

5+
import io
56
import os
67
import unittest
8+
from unittest.mock import mock_open, patch
79

810
from pythainlp.corpus import (
911
countries,
@@ -111,18 +113,110 @@ def test_corpus(self):
111113
self.assertFalse(download(name="test", version="0.0.1"))
112114

113115
def test_oscar(self):
114-
self.assertIsNotNone(oscar.word_freqs())
115-
self.assertIsNotNone(oscar.unigram_word_freqs())
116+
# Mock the oscar corpus file to avoid slow download and parsing
117+
# Format: word,count
118+
# Note: A line starting with space becomes empty string after strip()
119+
mock_oscar_data = """word,count
120+
คน,1000
121+
ไทย,500
122+
ภาษา,300
123+
,100
124+
"test",50
125+
"""
126+
mock_path = "/mock/path/oscar_icu"
127+
128+
with patch('pythainlp.corpus.oscar.get_corpus_path', return_value=mock_path):
129+
with patch('builtins.open', mock_open(read_data=mock_oscar_data)):
130+
result = oscar.word_freqs()
131+
self.assertIsNotNone(result)
132+
self.assertIsInstance(result, list)
133+
self.assertGreater(len(result), 0)
134+
# Verify parsing logic
135+
self.assertEqual(result[0], ("คน", 1000))
136+
# Space line becomes empty string (not <s/>) due to strip()
137+
self.assertIn(("", 100), result)
138+
# Verify quoted values are filtered out
139+
for word, _ in result:
140+
self.assertNotIn('"', word)
141+
142+
# Reset mock for unigram test
143+
with patch('builtins.open', mock_open(read_data=mock_oscar_data)):
144+
result_unigram = oscar.unigram_word_freqs()
145+
self.assertIsNotNone(result_unigram)
146+
self.assertIsInstance(result_unigram, dict)
147+
self.assertGreater(len(result_unigram), 0)
148+
self.assertEqual(result_unigram["คน"], 1000)
116149

117150
def test_tnc(self):
118-
self.assertIsNotNone(tnc.word_freqs())
119-
self.assertIsNotNone(tnc.unigram_word_freqs())
120-
self.assertIsNotNone(tnc.bigram_word_freqs())
121-
self.assertIsNotNone(tnc.trigram_word_freqs())
151+
# Mock TNC unigram corpus
152+
mock_unigram_data = """คน 1000
153+
ไทย 500
154+
ภาษา 300"""
155+
156+
# Mock TNC bigram corpus
157+
mock_bigram_data = """คน ไทย 100
158+
ไทย ภาษา 50
159+
ภาษา ไทย 30"""
160+
161+
# Mock TNC trigram corpus
162+
mock_trigram_data = """คน ไทย ภาษา 10
163+
ไทย ภาษา ไทย 5
164+
ภาษา ไทย คน 3"""
165+
166+
# Test unigram functions
167+
with patch('pythainlp.corpus.tnc.get_corpus', return_value=frozenset(mock_unigram_data.split('\n'))):
168+
result = tnc.word_freqs()
169+
self.assertIsNotNone(result)
170+
self.assertIsInstance(result, list)
171+
self.assertGreater(len(result), 0)
172+
# Check that at least one expected entry exists (order not guaranteed)
173+
self.assertIn(("คน", 1000), result)
174+
175+
result_unigram = tnc.unigram_word_freqs()
176+
self.assertIsNotNone(result_unigram)
177+
self.assertIsInstance(result_unigram, dict)
178+
self.assertGreater(len(result_unigram), 0)
179+
self.assertEqual(result_unigram["คน"], 1000)
180+
181+
# Test bigram function
182+
mock_bigram_path = "/mock/path/bigram"
183+
with patch('pythainlp.corpus.tnc.get_corpus_path', return_value=mock_bigram_path):
184+
with patch('builtins.open', mock_open(read_data=mock_bigram_data)):
185+
result_bigram = tnc.bigram_word_freqs()
186+
self.assertIsNotNone(result_bigram)
187+
self.assertIsInstance(result_bigram, dict)
188+
self.assertGreater(len(result_bigram), 0)
189+
self.assertEqual(result_bigram[("คน", "ไทย")], 100)
190+
191+
# Test trigram function
192+
mock_trigram_path = "/mock/path/trigram"
193+
with patch('pythainlp.corpus.tnc.get_corpus_path', return_value=mock_trigram_path):
194+
with patch('builtins.open', mock_open(read_data=mock_trigram_data)):
195+
result_trigram = tnc.trigram_word_freqs()
196+
self.assertIsNotNone(result_trigram)
197+
self.assertIsInstance(result_trigram, dict)
198+
self.assertGreater(len(result_trigram), 0)
199+
self.assertEqual(result_trigram[("คน", "ไทย", "ภาษา")], 10)
122200

123201
def test_ttc(self):
124-
self.assertIsNotNone(ttc.word_freqs())
125-
self.assertIsNotNone(ttc.unigram_word_freqs())
202+
# Mock TTC corpus
203+
mock_ttc_data = """คน 1000
204+
ไทย 500
205+
ภาษา 300"""
206+
207+
with patch('pythainlp.corpus.ttc.get_corpus', return_value=frozenset(mock_ttc_data.split('\n'))):
208+
result = ttc.word_freqs()
209+
self.assertIsNotNone(result)
210+
self.assertIsInstance(result, list)
211+
self.assertGreater(len(result), 0)
212+
# Check that at least one expected entry exists (order not guaranteed)
213+
self.assertIn(("คน", 1000), result)
214+
215+
result_unigram = ttc.unigram_word_freqs()
216+
self.assertIsNotNone(result_unigram)
217+
self.assertIsInstance(result_unigram, dict)
218+
self.assertGreater(len(result_unigram), 0)
219+
self.assertEqual(result_unigram["คน"], 1000)
126220

127221
def test_revise_wordset(self):
128222
training_data = [

0 commit comments

Comments
 (0)