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
|
import os
import unittest
import tempfile
from contextlib import contextmanager
try:
from numpy.testing import assert_array_compare
except ImportError:
# numpy < 1.14
from numpy.testing.utils import assert_array_compare
import numpy as np
import Orange
if Orange.data.Table.LOCKING is None:
Orange.data.Table.LOCKING = True
@contextmanager
def named_file(content, encoding=None, suffix=''):
file = tempfile.NamedTemporaryFile("wt", delete=False,
encoding=encoding, suffix=suffix)
file.write(content)
name = file.name
file.close()
try:
yield name
finally:
os.remove(name)
@np.vectorize
def naneq(a, b):
try:
return (np.isnan(a) and np.isnan(b)) or a == b
except TypeError:
return a == b
def assert_array_nanequal(a, b, *args, **kwargs):
"""
Similar as np.testing.assert_array_equal but with better handling of
object arrays.
Note
----
Is not fast!
Parameters
----------
a : array-like
b : array-like
"""
return assert_array_compare(naneq, a, b, *args, **kwargs)
def test_dirname():
"""
Return the absolute path to the Orange.tests package.
Returns
-------
path : str
"""
return os.path.dirname(__file__)
def test_filename(path):
"""
Return an absolute path to a resource within Orange.tests package.
Parameters
----------
path : str
Path relative to `test_dirname()`
Returns
-------
abspath : str
Absolute path
"""
return os.path.join(test_dirname(), path)
def suite(loader=None, pattern='test*.py'):
test_dir = os.path.dirname(__file__)
if loader is None:
loader = unittest.TestLoader()
if pattern is None:
pattern = 'test*.py'
orange_dir = os.path.dirname(Orange.__file__)
top_level_dir = os.path.dirname(orange_dir)
all_tests = [loader.discover(test_dir, pattern, top_level_dir)]
if not suite.in_tests: # prevent recursion
suite.in_tests = True
all_tests += (loader.discover(dir, pattern, dir)
for dir in (os.path.join(orange_dir, fn, "tests")
for fn in os.listdir(orange_dir)
if fn != "widgets")
if os.path.exists(dir))
return unittest.TestSuite(all_tests)
suite.in_tests = False
def load_tests(loader, tests, pattern):
return suite(loader, pattern)
if __name__ == '__main__':
unittest.main(defaultTest='suite')
|