|
| 1 | +# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project |
| 2 | +# SPDX-FileType: SOURCE |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +# Tests for corpus download/remove functions. |
| 6 | +# These tests require network access and the "compact" dependency set. |
| 7 | + |
| 8 | +import os |
| 9 | +import tempfile |
| 10 | +import unittest |
| 11 | +from unittest.mock import patch |
| 12 | + |
| 13 | +from pythainlp.corpus import ( |
| 14 | + download, |
| 15 | + find_synonyms, |
| 16 | + get_corpus_db_detail, |
| 17 | + get_corpus_path, |
| 18 | + remove, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class CorpusDownloadTestCaseC(unittest.TestCase): |
| 23 | + def test_download_and_remove(self): |
| 24 | + """Test the full download/remove lifecycle for a test corpus.""" |
| 25 | + # Invalid-URL / name-not-found cases |
| 26 | + self.assertFalse( |
| 27 | + download(name="test", url="wrongurl00AAfcX2df") |
| 28 | + ) # URL not exist |
| 29 | + self.assertFalse( |
| 30 | + download(name="XxxXXxxx817d37sf") |
| 31 | + ) # corpus name not exist |
| 32 | + |
| 33 | + # Full lifecycle: download, re-download, query, remove |
| 34 | + self.assertTrue(download("test")) # download the first time |
| 35 | + self.assertTrue(download(name="test", force=True)) # force download |
| 36 | + self.assertTrue(download(name="test")) # try download existing |
| 37 | + self.assertIsNotNone(get_corpus_db_detail("test")) # corpus exists |
| 38 | + self.assertIsNotNone(get_corpus_path("test")) # corpus exists |
| 39 | + self.assertTrue(remove("test")) # remove existing |
| 40 | + self.assertFalse(remove("test")) # remove non-existing |
| 41 | + |
| 42 | + # Corpus version not supported in this PyThaiNLP version |
| 43 | + # test 0.0.1 is for PyThaiNLP version <2.0 |
| 44 | + self.assertFalse(download(name="test", version="0.0.1")) |
| 45 | + |
| 46 | + def test_download_ignores_offline_mode(self): |
| 47 | + """download() must work even when PYTHAINLP_OFFLINE=1. |
| 48 | +
|
| 49 | + Explicit calls to download() are deliberate user actions and must |
| 50 | + not be blocked by the PYTHAINLP_OFFLINE environment variable. |
| 51 | + That variable only prevents the *automatic* download triggered by |
| 52 | + get_corpus_path() when a corpus is missing locally. |
| 53 | + """ |
| 54 | + with patch.dict(os.environ, {"PYTHAINLP_OFFLINE": "1"}): |
| 55 | + result = download("test") |
| 56 | + self.assertTrue(result) |
| 57 | + |
| 58 | + def test_zip(self): |
| 59 | + """Test download and extraction of a zip corpus.""" |
| 60 | + self.assertTrue(download("test_zip")) # download first |
| 61 | + p = get_corpus_path("test_zip") |
| 62 | + self.assertTrue(os.path.isdir(p)) |
| 63 | + self.assertTrue(remove("test_zip")) |
| 64 | + |
| 65 | + def test_find_synonyms(self): |
| 66 | + self.assertEqual( |
| 67 | + find_synonyms("หมู"), ["จรุก", "วราหะ", "วราห์", "ศูกร", "สุกร"] |
| 68 | + ) |
| 69 | + self.assertEqual(find_synonyms("1"), []) |
| 70 | + |
| 71 | + |
| 72 | +class ReadOnlyModeExplicitSaveTestCaseC(unittest.TestCase): |
| 73 | + """Test that user-initiated saves are allowed in read-only mode. |
| 74 | +
|
| 75 | + Uses numpy and python-crfsuite (both in the compact dependency set). |
| 76 | + """ |
| 77 | + |
| 78 | + def test_explicit_save_allowed_in_read_only(self): |
| 79 | + """Read-only mode must not block saves to a user-specified path. |
| 80 | +
|
| 81 | + Read-only mode only blocks implicit background writes to PyThaiNLP's |
| 82 | + internal data directory. Operations where the user explicitly |
| 83 | + specifies an output path must proceed normally. |
| 84 | + """ |
| 85 | + import numpy as np |
| 86 | + |
| 87 | + from pythainlp.classify.param_free import GzipModel |
| 88 | + from pythainlp.tag._tag_perceptron import PerceptronTagger |
| 89 | + |
| 90 | + with patch.dict( |
| 91 | + os.environ, |
| 92 | + {"PYTHAINLP_READ_ONLY": "1"}, |
| 93 | + clear=False, |
| 94 | + ): |
| 95 | + os.environ.pop("PYTHAINLP_READ_MODE", None) |
| 96 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 97 | + # GzipModel.save — user explicitly provides the path |
| 98 | + model = object.__new__(GzipModel) |
| 99 | + model.training_data = np.array([("text", "label")]) |
| 100 | + model.cx2_list = [4] |
| 101 | + out = os.path.join(tmpdir, "model.json") |
| 102 | + model.save(out) |
| 103 | + self.assertTrue(os.path.isfile(out)) |
| 104 | + |
| 105 | + # PerceptronTagger.train — user explicitly provides save_loc |
| 106 | + tagger = PerceptronTagger() |
| 107 | + sentences = [[("กิน", "VV"), ("ข้าว", "NN")]] |
| 108 | + tagger_out = os.path.join(tmpdir, "tagger.json") |
| 109 | + tagger.train(sentences, save_loc=tagger_out, nr_iter=1) |
| 110 | + self.assertTrue(os.path.isfile(tagger_out)) |
0 commit comments