|
| 1 | +# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project |
| 2 | +# SPDX-FileType: SOURCE |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +""" |
| 5 | +Test corpus catalog download and query functionality. |
| 6 | +
|
| 7 | +These tests verify that the corpus catalog can be downloaded from |
| 8 | +the remote server and queried correctly. |
| 9 | +""" |
| 10 | + |
| 11 | +import unittest |
| 12 | + |
| 13 | +from pythainlp.corpus import ( |
| 14 | + corpus_db_path, |
| 15 | + corpus_db_url, |
| 16 | + get_corpus_db, |
| 17 | + get_corpus_db_detail, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +class CorpusCatalogTestCase(unittest.TestCase): |
| 22 | + """Test corpus catalog functionality.""" |
| 23 | + |
| 24 | + def test_catalog_url(self): |
| 25 | + """Test that corpus catalog URL is valid and accessible.""" |
| 26 | + url = corpus_db_url() |
| 27 | + self.assertIsNotNone(url) |
| 28 | + self.assertIsInstance(url, str) |
| 29 | + self.assertTrue(url.startswith("https://")) |
| 30 | + self.assertIn("pythainlp", url.lower()) |
| 31 | + |
| 32 | + def test_catalog_path(self): |
| 33 | + """Test that corpus catalog path is valid.""" |
| 34 | + path = corpus_db_path() |
| 35 | + self.assertIsNotNone(path) |
| 36 | + self.assertIsInstance(path, str) |
| 37 | + self.assertTrue(path.endswith("db.json")) |
| 38 | + |
| 39 | + def test_catalog_download(self): |
| 40 | + """Test that corpus catalog can be downloaded from remote server.""" |
| 41 | + url = corpus_db_url() |
| 42 | + catalog = get_corpus_db(url) |
| 43 | + |
| 44 | + self.assertIsNotNone(catalog, "Catalog download should succeed") |
| 45 | + self.assertTrue(hasattr(catalog, "json"), "Catalog should have json method") |
| 46 | + self.assertTrue( |
| 47 | + hasattr(catalog, "status_code"), "Catalog should have status_code" |
| 48 | + ) |
| 49 | + |
| 50 | + def test_catalog_structure(self): |
| 51 | + """Test that downloaded catalog has valid JSON structure.""" |
| 52 | + url = corpus_db_url() |
| 53 | + catalog = get_corpus_db(url) |
| 54 | + |
| 55 | + self.assertIsNotNone(catalog) |
| 56 | + catalog_data = catalog.json() |
| 57 | + |
| 58 | + self.assertIsInstance(catalog_data, dict, "Catalog should be a dictionary") |
| 59 | + self.assertGreater( |
| 60 | + len(catalog_data), 0, "Catalog should contain at least one corpus" |
| 61 | + ) |
| 62 | + |
| 63 | + # Check that catalog entries have expected structure |
| 64 | + for corpus_name, corpus_info in list(catalog_data.items())[:5]: |
| 65 | + self.assertIsInstance(corpus_name, str) |
| 66 | + self.assertIsInstance(corpus_info, dict) |
| 67 | + self.assertIn("latest_version", corpus_info) |
| 68 | + self.assertIn("versions", corpus_info) |
| 69 | + self.assertIsInstance(corpus_info["versions"], dict) |
| 70 | + |
| 71 | + def test_catalog_known_entries(self): |
| 72 | + """Test that catalog contains known corpus entries.""" |
| 73 | + url = corpus_db_url() |
| 74 | + catalog = get_corpus_db(url) |
| 75 | + |
| 76 | + self.assertIsNotNone(catalog) |
| 77 | + catalog_data = catalog.json() |
| 78 | + |
| 79 | + # Check for some known corpus entries |
| 80 | + # "test" is a standard test corpus that should always exist |
| 81 | + self.assertIn("test", catalog_data, "Catalog should contain 'test' corpus") |
| 82 | + |
| 83 | + # Verify the test corpus has required fields |
| 84 | + test_corpus = catalog_data["test"] |
| 85 | + self.assertIn("latest_version", test_corpus) |
| 86 | + self.assertIn("versions", test_corpus) |
| 87 | + |
| 88 | + def test_corpus_detail_query(self): |
| 89 | + """Test querying details for specific corpus from local database.""" |
| 90 | + # This tests the local query function |
| 91 | + # First, ensure we have a local database by attempting a download/query |
| 92 | + url = corpus_db_url() |
| 93 | + catalog = get_corpus_db(url) |
| 94 | + self.assertIsNotNone(catalog) |
| 95 | + |
| 96 | + # Test querying a corpus that may exist locally |
| 97 | + # get_corpus_db_detail reads from local db.json file |
| 98 | + detail = get_corpus_db_detail("test") |
| 99 | + # Detail could be empty if not downloaded, but should return a dict |
| 100 | + self.assertIsInstance(detail, dict) |
| 101 | + |
| 102 | + # Test querying non-existent corpus |
| 103 | + detail_nonexist = get_corpus_db_detail("NONEXISTENT_CORPUS_12345") |
| 104 | + self.assertIsInstance(detail_nonexist, dict) |
| 105 | + self.assertEqual(len(detail_nonexist), 0, "Non-existent corpus should return empty dict") |
| 106 | + |
| 107 | + def test_catalog_version_info(self): |
| 108 | + """Test that catalog entries contain valid version information.""" |
| 109 | + url = corpus_db_url() |
| 110 | + catalog = get_corpus_db(url) |
| 111 | + |
| 112 | + self.assertIsNotNone(catalog) |
| 113 | + catalog_data = catalog.json() |
| 114 | + |
| 115 | + # Check version information for test corpus |
| 116 | + if "test" in catalog_data: |
| 117 | + test_corpus = catalog_data["test"] |
| 118 | + versions = test_corpus.get("versions", {}) |
| 119 | + |
| 120 | + self.assertIsInstance(versions, dict) |
| 121 | + if versions: |
| 122 | + # Check structure of version entries |
| 123 | + for version_key, version_info in list(versions.items())[:1]: |
| 124 | + self.assertIsInstance(version_key, str) |
| 125 | + self.assertIsInstance(version_info, dict) |
| 126 | + # Version info should have these fields |
| 127 | + self.assertIn("filename", version_info) |
| 128 | + self.assertIn("download_url", version_info) |
0 commit comments