1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
import logging
import os
import sys
import unittest
from os.path import abspath, dirname, isfile, join, relpath
rootDir = dirname(dirname(dirname(abspath(__file__))))
sys.path.insert(0, rootDir)
from glossary_test import TestGlossaryBase, appTmpDir
from pyglossary.core_test import getMockLogger
from pyglossary.glossary import Glossary
from pyglossary.os_utils import rmtree
__all__ = ["TestGlossaryErrors", "TestGlossaryErrorsBase"]
Glossary.init()
class MyStr(str):
__slots__ = []
class TestGlossaryErrorsBase(TestGlossaryBase):
def __init__(self, *args, **kwargs):
TestGlossaryBase.__init__(self, *args, **kwargs)
self.mockLog = getMockLogger()
def setUp(self):
TestGlossaryBase.setUp(self)
self.mockLog.clear()
def tearDown(self):
TestGlossaryBase.tearDown(self)
method = self._testMethodName
self.assertEqual(0, self.mockLog.printRemainingErrors(method))
warnCount = self.mockLog.printRemainingwWarnings(method)
if warnCount > 0:
print(
f"Got {warnCount} unhandled warnings "
f"from {self.__class__.__name__}: {self._testMethodName}\n",
)
def assertLogCritical(self, errorMsg):
self.assertIsNotNone(
self.mockLog.popLog(
logging.CRITICAL,
errorMsg,
),
msg=f"did not find critical log {errorMsg!r}",
)
def assertLogError(self, errorMsg):
self.assertIsNotNone(
self.mockLog.popLog(
logging.ERROR,
errorMsg,
),
msg=f"did not find error log {errorMsg!r}",
)
def assertLogWarning(self, errorMsg):
self.assertIsNotNone(
self.mockLog.popLog(
logging.WARNING,
errorMsg,
),
msg=f"did not find warning log {errorMsg!r}",
)
def osRoot():
if os.sep == "\\":
return "C:\\"
return "/"
if os.sep == "\\":
osNoSuchFileOrDir = "[WinError 3] The system cannot find the path specified:"
else:
osNoSuchFileOrDir = "[Errno 2] No such file or directory:"
class TestGlossaryErrors(TestGlossaryErrorsBase):
def test_loadPlugins_invalidDir(self):
path = join(osRoot(), "abc", "def", "ghe")
Glossary.loadPlugins(path)
self.assertLogCritical(f"Invalid plugin directory: {path!r}")
def test_detectInputFormat_err1(self):
res = Glossary.detectInputFormat(
filename="",
formatName="",
)
self.assertIsNone(res)
self.assertLogCritical("Unable to detect input format!")
def test_detectInputFormat_err2(self):
res = Glossary.detectInputFormat(
filename="test.abcd",
formatName="",
)
self.assertIsNone(res)
self.assertLogCritical("Unable to detect input format!")
def test_detectInputFormat_err3(self):
res = Glossary.detectInputFormat(
filename="test.sql",
formatName="",
)
self.assertIsNone(res)
self.assertLogCritical("plugin Sql does not support reading")
def test_detectInputFormat_err4(self):
res = Glossary.detectInputFormat(
filename="test",
formatName="FooBar",
)
self.assertIsNone(res)
self.assertLogCritical("Invalid format 'FooBar'")
def test_detectInputFormat_ok1(self):
res = Glossary.detectInputFormat(
filename="test1.txt.gz",
formatName="",
)
self.assertEqual(res, ("test1.txt.gz", "Tabfile", ""))
def test_detectInputFormat_ok2(self):
res = Glossary.detectInputFormat(
filename="test2.txt.zip",
formatName="",
)
self.assertEqual(res, ("test2.txt", "Tabfile", "zip"))
def test_detectOutputFormat_err1(self):
res = Glossary.detectOutputFormat(
filename="",
formatName="",
inputFilename="",
)
self.assertIsNone(res)
self.assertLogCritical("Invalid filename ''")
def test_detectOutputFormat_err2(self):
res = Glossary.detectOutputFormat(
filename="test",
formatName="FooBar",
inputFilename="",
)
self.assertIsNone(res)
self.assertLogCritical("Invalid format FooBar")
def test_detectOutputFormat_err3(self):
res = Glossary.detectOutputFormat(
filename="",
formatName="",
inputFilename="test",
)
self.assertIsNone(res)
self.assertLogCritical("No filename nor format is given for output file")
def test_detectOutputFormat_err4_1(self):
res = Glossary.detectOutputFormat(
filename="",
formatName="BabylonBgl",
inputFilename="test3.txt",
)
self.assertIsNone(res)
self.assertLogCritical("plugin BabylonBgl does not support writing")
def test_detectOutputFormat_err4_2(self):
res = Glossary.detectOutputFormat(
filename="test.bgl",
formatName="",
inputFilename="",
)
self.assertIsNone(res)
self.assertLogCritical("plugin BabylonBgl does not support writing")
def test_detectOutputFormat_err5(self):
res = Glossary.detectOutputFormat(
filename="test",
formatName="",
inputFilename="",
)
self.assertIsNone(res)
self.assertLogCritical("Unable to detect output format!")
def test_detectOutputFormat_err6(self):
res = Glossary.detectOutputFormat(
filename="test",
formatName="Tabfile",
inputFilename="",
addExt=True,
)
self.assertEqual(res, ("test", "Tabfile", ""))
self.assertLogError("inputFilename is empty")
def test_init_infoBadType(self):
try:
Glossary(info=["a"])
except Exception as e:
self.assertEqual(str(type(e)), "<class 'TypeError'>")
self.assertEqual(
str(e),
"Glossary: `info` has invalid type, dict or OrderedDict expected",
)
else:
self.fail("did not raise an exception")
def test_cleanup_removed(self):
glos = Glossary()
tmpFname = "test_cleanup_removed"
entry = glos.newDataEntry(tmpFname, b"test")
tmpFpath = entry._tmpPath
self.assertTrue(bool(tmpFpath), msg="entry tmpPath is empty")
self.assertTrue(isfile(tmpFpath), msg=f"tmp file does not exist: {tmpFpath}")
rmtree(appTmpDir)
glos.cleanup()
self.assertLogError(f"no such file or directory: {appTmpDir}")
def test_lang_err_get_source(self):
glos = Glossary()
glos.setInfo("sourcelang", "test")
self.assertEqual(glos.sourceLangName, "")
self.assertLogError("unknown language 'test'")
def test_lang_err_get_target(self):
glos = Glossary()
glos.setInfo("targetlang", "test")
self.assertEqual(glos.targetLangName, "")
self.assertLogError("unknown language 'test'")
def test_lang_err_set_source(self):
glos = Glossary()
glos.sourceLangName = "foobar"
self.assertLogError("unknown language 'foobar'")
self.assertEqual(glos.sourceLangName, "")
def test_lang_err_set_target(self):
glos = Glossary()
glos.targetLangName = "foobar"
self.assertLogError("unknown language 'foobar'")
self.assertEqual(glos.targetLangName, "")
def test_lang_err_setObj_source(self):
glos = Glossary()
try:
glos.sourceLang = "foobar"
except TypeError as e:
self.assertEqual(str(e), "invalid lang='foobar', must be a Lang object")
else:
self.fail("must raise a TypeError")
def test_lang_err_setObj_target(self):
glos = Glossary()
try:
glos.targetLang = "foobar"
except TypeError as e:
self.assertEqual(str(e), "invalid lang='foobar', must be a Lang object")
else:
self.fail("must raise a TypeError")
def test_config_attr_set_twice(self):
glos = Glossary()
glos.config = {"lower": True}
self.assertEqual(glos.getConfig("lower", False), True)
glos.config = {"lower": False}
self.assertLogError("glos.config is set more than once")
self.assertEqual(glos.getConfig("lower", False), True)
def test_iter_empty(self):
glos = Glossary()
self.assertEqual(list(glos), [])
def test_convert_typeErr_1(self):
glos = Glossary()
try:
glos.convert(
inputFilename=MyStr(""),
)
except TypeError as e:
self.assertEqual(str(e), "inputFilename must be str")
else:
self.fail("must raise TypeError")
def test_convert_typeErr_2(self):
glos = Glossary()
try:
glos.convert(
inputFilename="",
outputFilename=MyStr(""),
)
except TypeError as e:
self.assertEqual(str(e), "outputFilename must be str")
else:
self.fail("must raise TypeError")
def test_convert_typeErr_3(self):
glos = Glossary()
try:
glos.convert(
inputFilename="",
outputFilename="",
inputFormat=MyStr(""),
)
except TypeError as e:
self.assertEqual(str(e), "inputFormat must be str")
else:
self.fail("must raise TypeError")
def test_convert_typeErr_4(self):
glos = Glossary()
try:
glos.convert(
inputFilename="",
outputFilename="",
inputFormat="",
outputFormat=MyStr(""),
)
except TypeError as e:
self.assertEqual(str(e), "outputFormat must be str")
else:
self.fail("must raise TypeError")
def test_read_typeErr_1(self):
glos = Glossary()
try:
glos.read(
filename=MyStr(""),
)
except TypeError as e:
self.assertEqual(str(e), "filename must be str")
else:
self.fail("must raise TypeError")
def test_write_typeErr_1(self):
glos = Glossary()
try:
glos.write(
filename=MyStr(""),
format="",
)
except TypeError as e:
self.assertEqual(str(e), "filename must be str")
else:
self.fail("must raise TypeError")
def test_write_typeErr_2(self):
glos = Glossary()
try:
glos.write(
filename="",
format=MyStr(""),
)
except TypeError as e:
self.assertEqual(str(e), "formatName must be str")
else:
self.fail("must raise TypeError")
def test_convert_sameFilename(self):
glos = Glossary()
res = glos.convert(
inputFilename="test4.txt",
outputFilename="test4.txt",
)
self.assertIsNone(res)
self.assertLogCritical("Input and output files are the same")
def test_convert_dirExists(self):
glos = Glossary()
tempFilePath = self.newTempFilePath("test_convert_dirExists")
with open(tempFilePath, mode="w", encoding="utf-8") as _file:
_file.write("")
res = glos.convert(
inputFilename="test5.txt",
outputFilename=self.tempDir,
outputFormat="Stardict",
)
self.assertIsNone(res)
self.assertLogCritical(
f"Directory already exists and not empty: {relpath(self.tempDir)}",
)
def test_convert_fileNotFound(self):
glos = Glossary()
inputFilename = join(osRoot(), "abc", "def", "test6.txt")
res = glos.convert(
inputFilename=inputFilename,
outputFilename="test2.txt",
)
self.assertIsNone(res)
self.assertLogCritical(
f"[Errno 2] No such file or directory: {inputFilename!r}",
)
self.assertLogCritical(f"Reading file {relpath(inputFilename)!r} failed.")
def test_convert_unableDetectOutputFormat(self):
glos = Glossary()
res = glos.convert(
inputFilename="test7.txt",
outputFilename="test",
outputFormat="",
)
self.assertIsNone(res)
self.assertLogCritical("Unable to detect output format!")
def test_convert_writeFileNotFound_txt(self):
outputFilename = join(
appTmpDir,
"test",
"7de8cf6f17bc4c9abb439e71adbec95d.txt",
)
glos = Glossary()
res = glos.convert(
inputFilename=self.downloadFile("100-en-fa.txt"),
outputFilename=outputFilename,
)
self.assertIsNone(res)
self.assertLogCritical(
f"[Errno 2] No such file or directory: {outputFilename!r}",
)
self.assertLogCritical(f"Writing file {relpath(outputFilename)!r} failed.")
def test_convert_writeFileNotFound_hdir(self):
outputFilename = join(osRoot(), "test", "40e20107f5b04087bfc0ec0d61510017.hdir")
glos = Glossary()
res = glos.convert(
inputFilename=self.downloadFile("100-en-fa.txt"),
outputFilename=outputFilename,
)
self.assertIsNone(res)
self.assertLogCritical(
f"{osNoSuchFileOrDir} {outputFilename!r}",
)
self.assertLogCritical(f"Writing file {relpath(outputFilename)!r} failed.")
def test_convert_invalidSortKeyName(self):
glos = self.glos = Glossary()
outputFilename = self.newTempFilePath("none.txt")
res = glos.convert(
inputFilename=self.downloadFile("100-en-fa.txt"),
outputFilename=outputFilename,
sort=True,
sortKeyName="blah",
)
self.assertIsNone(res)
self.assertLogCritical("invalid sortKeyName = 'blah'")
def test_collectDefiFormat_direct(self):
fname = "100-en-fa.txt"
glos = self.glos = Glossary()
glos.read(self.downloadFile(fname), direct=True)
res = glos.collectDefiFormat(10)
self.assertIsNone(res)
self.assertLogError("collectDefiFormat: not supported in direct mode")
def test_sortWords_invalidSortKeyName(self):
glos = self.glos = Glossary()
glos.sortWords(
sortKeyName="blah",
)
self.assertLogCritical("invalid sortKeyName = 'blah'")
# def test_collectDefiFormat_direct(self):
# from pyglossary.glossary import Glossary as GlossaryLegacy
# fname = "100-en-fa.txt"
# glos = self.glos = GlossaryLegacy()
# glos.read(self.downloadFile(fname), direct=True)
# res = glos.collectDefiFormat(10)
# self.assertIsNone(res)
# self.assertLogError("collectDefiFormat: not supported in direct mode")
if __name__ == "__main__":
unittest.main()
|