Skip to content

Commit 8c15ffb

Browse files
committed
added more unit tests for read_excel and load_example_data to compensate for skipped doctests
because the later kind cannot be skipped when the required optional dependency is missing
1 parent db6c291 commit 8c15ffb

File tree

3 files changed

+88
-11
lines changed

3 files changed

+88
-11
lines changed

larray/tests/data/test.xlsx

860 Bytes
Binary file not shown.

larray/tests/test_array.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,25 +3241,40 @@ def test_read_excel_xlwings():
32413241
arr = read_excel(inputpath('test.xlsx'), '2d')
32423242
assert_array_equal(arr, io_2d)
32433243

3244+
arr = read_excel(inputpath('test.xlsx'), '2d_classic')
3245+
assert_array_equal(arr, ndtest("a=a0..a2; b0..b2"))
3246+
3247+
arr = read_excel(inputpath('test.xlsx'), '2d_classic', nb_axes=2)
3248+
assert_array_equal(arr, ndtest("a=a0..a2; b0..b2"))
3249+
32443250
arr = read_excel(inputpath('test.xlsx'), '3d')
32453251
assert_array_equal(arr, io_3d)
32463252

3253+
# for > 2d, specifying nb_axes is required if there is no name for the horizontal axis
3254+
arr = read_excel(inputpath('test.xlsx'), '3d_classic', nb_axes=3)
3255+
assert_array_equal(arr, ndtest("a=1..3; b=b0,b1; c0..c2"))
3256+
32473257
arr = read_excel(inputpath('test.xlsx'), 'int_labels')
32483258
assert_array_equal(arr, io_int_labels)
32493259

3250-
arr = read_excel(inputpath('test.xlsx'), '2d_classic')
3251-
assert_array_equal(arr, ndtest("a=a0..a2; b0..b2"))
3252-
32533260
# passing a Group as sheet arg
32543261
axis = Axis('dim=1d,2d,3d,5d')
32553262

32563263
arr = read_excel(inputpath('test.xlsx'), axis['1d'])
32573264
assert_array_equal(arr, io_1d)
32583265

3266+
# missing rows, default fill_value
3267+
arr = read_excel(inputpath('test.xlsx'), 'missing_values')
3268+
expected = ndtest("a=1..3; b=b0,b1; c=c0..c2", dtype=float)
3269+
expected[2, 'b0'] = nan
3270+
expected[3, 'b1'] = nan
3271+
assert_array_nan_equal(arr, expected)
3272+
32593273
# missing rows + fill_value argument
32603274
arr = read_excel(inputpath('test.xlsx'), 'missing_values', fill_value=42)
3261-
expected = io_missing_values.copy()
3262-
expected[isnan(expected)] = 42
3275+
expected = ndtest("a=1..3; b=b0,b1; c=c0..c2", dtype=float)
3276+
expected[2, 'b0'] = 42
3277+
expected[3, 'b1'] = 42
32633278
assert_array_equal(arr, expected)
32643279

32653280
# range
@@ -3325,24 +3340,31 @@ def test_read_excel_pandas():
33253340
arr = read_excel(inputpath('test.xlsx'), '1d', engine='xlrd')
33263341
assert_array_equal(arr, io_1d)
33273342

3328-
arr = read_excel(inputpath('test.xlsx'), '2d', nb_axes=2, engine='xlrd')
3343+
arr = read_excel(inputpath('test.xlsx'), '2d', engine='xlrd')
33293344
assert_array_equal(arr, io_2d)
33303345

3331-
arr = read_excel(inputpath('test.xlsx'), '2d', engine='xlrd')
3346+
arr = read_excel(inputpath('test.xlsx'), '2d', nb_axes=2, engine='xlrd')
33323347
assert_array_equal(arr, io_2d)
33333348

3349+
arr = read_excel(inputpath('test.xlsx'), '2d_classic', engine='xlrd')
3350+
assert_array_equal(arr, ndtest("a=a0..a2; b0..b2"))
3351+
3352+
arr = read_excel(inputpath('test.xlsx'), '2d_classic', nb_axes=2, engine='xlrd')
3353+
assert_array_equal(arr, ndtest("a=a0..a2; b0..b2"))
3354+
33343355
arr = read_excel(inputpath('test.xlsx'), '3d', index_col=[0, 1], engine='xlrd')
33353356
assert_array_equal(arr, io_3d)
33363357

33373358
arr = read_excel(inputpath('test.xlsx'), '3d', engine='xlrd')
33383359
assert_array_equal(arr, io_3d)
33393360

3361+
# for > 2d, specifying nb_axes is required if there is no name for the horizontal axis
3362+
arr = read_excel(inputpath('test.xlsx'), '3d_classic', nb_axes=3, engine='xlrd')
3363+
assert_array_equal(arr, ndtest("a=1..3; b=b0,b1; c0..c2"))
3364+
33403365
arr = read_excel(inputpath('test.xlsx'), 'int_labels', engine='xlrd')
33413366
assert_array_equal(arr, io_int_labels)
33423367

3343-
arr = read_excel(inputpath('test.xlsx'), '2d_classic', engine='xlrd')
3344-
assert_array_equal(arr, ndtest("a=a0..a2; b0..b2"))
3345-
33463368
# passing a Group as sheet arg
33473369
axis = Axis('dim=1d,2d,3d,5d')
33483370

@@ -3370,7 +3392,7 @@ def test_read_excel_pandas():
33703392
# missing rows + fill_value argument
33713393
arr = read_excel(inputpath('test_narrow.xlsx'), 'missing_values',
33723394
fill_value=42, wide=False, engine='xlrd')
3373-
expected = io_narrow_missing_values
3395+
expected = io_narrow_missing_values.copy()
33743396
expected[isnan(expected)] = 42
33753397
assert_array_equal(arr, expected)
33763398

larray/tests/test_example.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from __future__ import absolute_import, division, print_function
2+
3+
import pytest
4+
5+
from larray.tests.common import needs_pytables
6+
from larray import load_example_data, Array, Session, Axis, Group
7+
8+
9+
def obj_summary(obj):
10+
if isinstance(obj, Session):
11+
return {obj_name: obj_summary(child_obj) for obj_name, child_obj in obj.items()}
12+
elif isinstance(obj, Array):
13+
return [obj_summary(axis) for axis in obj.axes]
14+
elif isinstance(obj, Axis):
15+
return obj.name, len(obj)
16+
elif isinstance(obj, Group):
17+
return obj.name, obj.axis.name, len(obj)
18+
else:
19+
raise TypeError('unsupported object')
20+
21+
22+
@needs_pytables
23+
def test_load_example_data():
24+
demo = load_example_data('demography')
25+
expected_summary = {
26+
'hh': [('time', 26), ('geo', 3), ('hh_type', 7)],
27+
'pop': [('time', 26), ('geo', 3), ('age', 121), ('sex', 2), ('nat', 2)],
28+
'qx': [('time', 26), ('geo', 3), ('age', 121), ('sex', 2), ('nat', 2)]
29+
}
30+
assert obj_summary(demo) == expected_summary
31+
32+
demo = load_example_data('demography_eurostat')
33+
expected_summary = {
34+
'births': [('country', 3), ('gender', 2), ('time', 5)],
35+
'citizenship': ('citizenship', 3),
36+
'country': ('country', 3),
37+
'country_benelux': ('country', 3),
38+
'deaths': [('country', 3), ('gender', 2), ('time', 5)],
39+
'even_years': ('even_years', 'time', 2),
40+
'gender': ('gender', 2),
41+
'immigration': [('country', 3),
42+
('citizenship', 3),
43+
('gender', 2),
44+
('time', 5)],
45+
'odd_years': ('odd_years', 'time', 3),
46+
'population': [('country', 3), ('gender', 2), ('time', 5)],
47+
'population_5_countries': [('country', 5), ('gender', 2), ('time', 5)],
48+
'population_benelux': [('country', 3), ('gender', 2), ('time', 5)],
49+
'time': ('time', 5)
50+
}
51+
assert obj_summary(demo) == expected_summary
52+
53+
54+
if __name__ == "__main__":
55+
pytest.main()

0 commit comments

Comments
 (0)