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
|
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import json
import os
from datetime import datetime
import locale
import numpy as np
from .. import data, misc
from ...tests.helper import remote_data
from ...extern import six
from ...tests.helper import pytest
def test_isiterable():
assert misc.isiterable(2) is False
assert misc.isiterable([2]) is True
assert misc.isiterable([1, 2, 3]) is True
assert misc.isiterable(np.array(2)) is False
assert misc.isiterable(np.array([1, 2, 3])) is True
def test_signal_number_to_name_no_failure():
# Regression test for #5340: ensure signal_number_to_name throws no
# AttributeError (it used ".iteritems()" which was removed in Python3).
misc.signal_number_to_name(0)
@remote_data
def test_api_lookup():
strurl = misc.find_api_page('astropy.utils.misc', 'dev', False, timeout=3)
objurl = misc.find_api_page(misc, 'dev', False, timeout=3)
assert strurl == objurl
assert strurl == 'http://devdocs.astropy.org/utils/index.html#module-astropy.utils.misc'
def test_skip_hidden():
path = data._find_pkg_data_path('data')
for root, dirs, files in os.walk(path):
assert '.hidden_file.txt' in files
assert 'local.dat' in files
# break after the first level since the data dir contains some other
# subdirectories that don't have these files
break
for root, dirs, files in misc.walk_skip_hidden(path):
assert '.hidden_file.txt' not in files
assert 'local.dat' in files
break
def test_JsonCustomEncoder():
assert json.dumps(np.arange(3), cls=misc.JsonCustomEncoder) == '[0, 1, 2]'
assert json.dumps(1+2j, cls=misc.JsonCustomEncoder) == '[1.0, 2.0]'
assert json.dumps(set([1, 2, 1]), cls=misc.JsonCustomEncoder) == '[1, 2]'
assert json.dumps(b'hello world \xc3\x85',
cls=misc.JsonCustomEncoder) == '"hello world \\u00c5"'
assert json.dumps({1: 2},
cls=misc.JsonCustomEncoder) == '{"1": 2}' # default
def test_inherit_docstrings():
@six.add_metaclass(misc.InheritDocstrings)
class Base(object):
def __call__(self, *args):
"FOO"
pass
class Subclass(Base):
def __call__(self, *args):
pass
if Base.__call__.__doc__ is not None:
# TODO: Maybe if __doc__ is None this test should be skipped instead?
assert Subclass.__call__.__doc__ == "FOO"
def test_set_locale():
# First, test if the required locales are available
current = locale.setlocale(locale.LC_ALL)
try:
locale.setlocale(locale.LC_ALL, str('en_US'))
locale.setlocale(locale.LC_ALL, str('de_DE'))
except locale.Error as e:
pytest.skip('Locale error: {}'.format(e))
finally:
locale.setlocale(locale.LC_ALL, current)
date = datetime(2000, 10, 1, 0, 0, 0)
day_mon = date.strftime('%a, %b')
with misc.set_locale('en_US'):
assert date.strftime('%a, %b') == 'Sun, Oct'
with misc.set_locale('de_DE'):
assert date.strftime('%a, %b') == 'So, Okt'
# Back to original
assert date.strftime('%a, %b') == day_mon
with misc.set_locale(current):
assert date.strftime('%a, %b') == day_mon
def test_check_broadcast():
assert misc.check_broadcast((10, 1), (3,)) == (10, 3)
assert misc.check_broadcast((10, 1), (3,), (4, 1, 1, 3)) == (4, 1, 10, 3)
with pytest.raises(ValueError):
misc.check_broadcast((10, 2), (3,))
with pytest.raises(ValueError):
misc.check_broadcast((10, 1), (3,), (4, 1, 2, 3))
|