Skip to content
Permalink
Browse files

Add error tests for custom cltk_data dir (#919)

* change cls to self in tests

* add pragma to v2 error

* add assertion tests for cltk_data errors

* rm custom os enviorn

* re-add contibs
  • Loading branch information...
kylepjohnson committed Jun 10, 2019
1 parent a18f289 commit 0da0df6033161f445691fcc3611c3cc8e8be9b51
@@ -8,7 +8,7 @@
import builtins
from pkg_resources import get_distribution

if sys.version_info[0] != 3:
if sys.version_info[0] != 3: # pragma: no cover
raise ImportError('Python Version 3 or above is required for cltk.')

__copyright__ = 'Copyright (c) 2016 Kyle P. Johnson. Distributed and Licensed under the MIT License.' # pylint: disable=line-too-long
@@ -21,7 +21,7 @@

__version__ = get_distribution('cltk').version # pylint: disable=no-member

if 'CLTK_DATA' in os.environ: # pragma: no cover
if 'CLTK_DATA' in os.environ:
__cltk_data_dir__ = os.path.expanduser(
os.path.normpath(os.environ['CLTK_DATA']))
if not os.path.isdir(__cltk_data_dir__):
@@ -69,7 +69,7 @@ class TestSequenceFunctions(unittest.TestCase): # pylint: disable=R0904
"""Class for unittest"""

@classmethod
def setUpClass(cls):
def setUpClass(self):
try:
corpus_importer = CorpusImporter('latin')
corpus_importer.import_corpus('latin_text_latin_library')
@@ -707,19 +707,19 @@ def test_normalize_middle_english(self):
class TestFilteredCorpus(unittest.TestCase):
"""Test the Latin Library corpus reader filter"""
@classmethod
def setUpClass(cls):
def setUpClass(self):
try:
corpus_importer = CorpusImporter('latin')
corpus_importer.import_corpus('latin_models_cltk')
corpus_importer.import_corpus('latin_text_latin_library')
except:
raise Exception('Failure to download test corpus')
cls.reader = get_corpus_reader(language='latin', corpus_name='latin_text_latin_library')
cls.reader._fileids = ['pervig.txt']
self.reader = get_corpus_reader(language='latin', corpus_name='latin_text_latin_library')
self.reader._fileids = ['pervig.txt']
# Need a additional instance because tests below change internals #TO-DO Fix
cls.reader_2 = get_corpus_reader(language='latin', corpus_name='latin_text_latin_library')
cls.reader_3 = get_corpus_reader(language='latin', corpus_name='latin_text_latin_library')
cls.reader_4 = get_corpus_reader(language='latin', corpus_name='latin_text_latin_library')
self.reader_2 = get_corpus_reader(language='latin', corpus_name='latin_text_latin_library')
self.reader_3 = get_corpus_reader(language='latin', corpus_name='latin_text_latin_library')
self.reader_4 = get_corpus_reader(language='latin', corpus_name='latin_text_latin_library')

def test_import_latin_library_corpus_filter_by_file(self):
"""Test the Latin Library corpus reader filter by files."""
@@ -25,9 +25,9 @@ class TestSequenceFunctions(unittest.TestCase):

"""Test the Latin Library corpus reader filter"""
@classmethod
def setUpClass(cls):
cls.greek_transcriber = grc.Transcriber("Attic", "Probert")
cls.latin_transcriber = lat.Transcriber("Classical", "Allen")
def setUpClass(self):
self.greek_transcriber = grc.Transcriber("Attic", "Probert")
self.latin_transcriber = lat.Transcriber("Classical", "Allen")

"""greek.transcription"""
def test_greek_refresh(self):
@@ -37,7 +37,7 @@ class TestSequenceFunctions(unittest.TestCase): # pylint: disable=R0904
# self.assertTrue(file_exists)

@classmethod
def setUpClass(cls):
def setUpClass(self):
try:
corpus_importer = CorpusImporter('sanskrit')
corpus_importer.import_corpus('sanskrit_models_cltk')
@@ -2,6 +2,7 @@

from collections import Counter
from collections import defaultdict
from importlib import reload
import os
from pickle import UnpicklingError
import unittest
@@ -159,6 +160,20 @@ def test_path(self):
self.assertEqual(make_cltk_path('greek', 'perseus_corpus'),
os.path.expanduser(os.path.join(get_cltk_data_dir(), 'greek', 'perseus_corpus')))

def test_data_envvar(self):
os.environ['CLTK_DATA'] = '/var/non_existent_dir'
with self.assertRaises(FileNotFoundError):
import cltk
reload(cltk)
os.environ['CLTK_DATA'] = '/root'
with self.assertRaises(PermissionError):
import cltk
reload(cltk)
# now reset so later tests run OK
del os.environ['CLTK_DATA']
import cltk
reload(cltk)


if __name__ == '__main__':
unittest.main()

0 comments on commit 0da0df6

Please sign in to comment.
You can’t perform that action at this time.