From 7d7060fdc1cba0e7ead68df7385e0995abfd1ad5 Mon Sep 17 00:00:00 2001 From: Yuchao Dai <3407450+icyblade@users.noreply.github.com> Date: Thu, 11 Jan 2018 03:54:26 +0800 Subject: [PATCH] Patch to increase test coverage (#8902) * no need to disable vis test in python 3.x this unit test works in my environment * add test for RemoteMonitor * install pydot and graphvis for python 3.x * monkey patch requests.post remove unnecessary code * travis fix * pep8 fix * add test for _remove_long_seq * add test for HDF5Matrix and ask_to_proceed_with_overwrite * typo --- .travis.yml | 4 +- setup.py | 3 +- tests/keras/preprocessing/sequence_test.py | 13 +++++++ tests/keras/test_callbacks.py | 27 ++++++++++++++ tests/keras/utils/io_utils_test.py | 43 ++++++++++++++++++++++ tests/keras/utils/vis_utils_test.py | 1 - 6 files changed, 86 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index a79851b8c1e..3666cc1b983 100644 --- a/.travis.yml +++ b/.travis.yml @@ -66,9 +66,7 @@ install: fi # install pydot for visualization tests - - if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then - conda install pydot graphviz; - fi + - conda install pydot graphviz # exclude different backends to measure a coverage for the designated backend only - if [[ "$KERAS_BACKEND" != "tensorflow" ]]; then diff --git a/setup.py b/setup.py index fdf49dc90fb..d661be081b8 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,8 @@ 'pytest-pep8', 'pytest-xdist', 'pytest-cov', - 'pandas'], + 'pandas', + 'requests'], }, classifiers=[ 'Development Status :: 5 - Production/Stable', diff --git a/tests/keras/preprocessing/sequence_test.py b/tests/keras/preprocessing/sequence_test.py index 2ca2fbad9b7..52d32c97a93 100644 --- a/tests/keras/preprocessing/sequence_test.py +++ b/tests/keras/preprocessing/sequence_test.py @@ -6,6 +6,7 @@ from keras.preprocessing.sequence import pad_sequences from keras.preprocessing.sequence import make_sampling_table from keras.preprocessing.sequence import skipgrams +from keras.preprocessing.sequence import _remove_long_seq def test_pad_sequences(): @@ -82,5 +83,17 @@ def test_skipgrams(): assert len(l) == 2 +def test_remove_long_seq(): + maxlen = 5 + seq = [ + [1, 2, 3], + [1, 2, 3, 4, 5, 6], + ] + label = ['a', 'b'] + new_seq, new_label = _remove_long_seq(maxlen, seq, label) + assert new_seq == [[1, 2, 3]] + assert new_label == ['a'] + + if __name__ == '__main__': pytest.main([__file__]) diff --git a/tests/keras/test_callbacks.py b/tests/keras/test_callbacks.py index 94237a1dede..9f4a105b206 100644 --- a/tests/keras/test_callbacks.py +++ b/tests/keras/test_callbacks.py @@ -17,6 +17,11 @@ from keras.utils.test_utils import keras_test from keras import backend as K from keras.utils import np_utils +try: + from unittest.mock import patch +except: + from mock import patch + input_dim = 2 num_hidden = 4 @@ -787,5 +792,27 @@ def test_TensorBoard_with_ReduceLROnPlateau(tmpdir): assert not tmpdir.listdir() +@keras_test +def tests_RemoteMonitor(): + (X_train, y_train), (X_test, y_test) = get_test_data(num_train=train_samples, + num_test=test_samples, + input_shape=(input_dim,), + classification=True, + num_classes=num_classes) + y_test = np_utils.to_categorical(y_test) + y_train = np_utils.to_categorical(y_train) + model = Sequential() + model.add(Dense(num_hidden, input_dim=input_dim, activation='relu')) + model.add(Dense(num_classes, activation='softmax')) + model.compile(loss='categorical_crossentropy', + optimizer='rmsprop', + metrics=['accuracy']) + cbks = [callbacks.RemoteMonitor()] + + with patch('requests.post'): + model.fit(X_train, y_train, batch_size=batch_size, + validation_data=(X_test, y_test), callbacks=cbks, epochs=1) + + if __name__ == '__main__': pytest.main([__file__]) diff --git a/tests/keras/utils/io_utils_test.py b/tests/keras/utils/io_utils_test.py index 37dd63edbc5..2ff9396c6b2 100644 --- a/tests/keras/utils/io_utils_test.py +++ b/tests/keras/utils/io_utils_test.py @@ -1,13 +1,19 @@ '''Tests for functions in io_utils.py. ''' import os +import sys import pytest from keras.models import Sequential from keras.layers import Dense from keras.utils.io_utils import HDF5Matrix +from keras.utils.io_utils import ask_to_proceed_with_overwrite import numpy as np import warnings import h5py +try: + from unittest.mock import patch +except: + from mock import patch @pytest.fixture @@ -76,8 +82,45 @@ def test_io_utils(in_tmpdir): # test slicing for shortened array assert len(X_train[0:]) == len(X_train), 'Incorrect shape for sliced data' + # test __getitem__ + with pytest.raises(IndexError): + X_train[1000] + with pytest.raises(IndexError): + X_train[1000:1001] + with pytest.raises(IndexError): + X_train[[1000, 1001]] + with pytest.raises(IndexError): + X_train[np.array([1000])] + with pytest.raises(IndexError): + X_train[None] + assert (X_train[0] == X_train[:1][0]).all() + assert (X_train[[0, 1]] == X_train[:2]).all() + assert (X_train[np.array([0, 1])] == X_train[:2]).all() + + # test normalizer + normalizer = lambda x: x + 1 + normalized_X_train = HDF5Matrix(h5_path, 'my_data', start=0, end=150, normalizer=normalizer) + assert np.isclose(normalized_X_train[0][0], X_train[0][0] + 1) + os.remove(h5_path) +def test_ask_to_proceed_with_overwrite(): + if sys.version_info[:2] <= (2, 7): + with patch('__builtin__.raw_input') as mock: + mock.return_value = 'y' + assert ask_to_proceed_with_overwrite('/tmp/not_exists') + + mock.return_value = 'n' + assert not ask_to_proceed_with_overwrite('/tmp/not_exists') + else: + with patch('builtins.input') as mock: + mock.return_value = 'y' + assert ask_to_proceed_with_overwrite('/tmp/not_exists') + + mock.return_value = 'n' + assert not ask_to_proceed_with_overwrite('/tmp/not_exists') + + if __name__ == '__main__': pytest.main([__file__]) diff --git a/tests/keras/utils/vis_utils_test.py b/tests/keras/utils/vis_utils_test.py index 65c507bb32f..0c74344d415 100644 --- a/tests/keras/utils/vis_utils_test.py +++ b/tests/keras/utils/vis_utils_test.py @@ -11,7 +11,6 @@ from keras.utils import vis_utils -@pytest.mark.skipif(sys.version_info > (3, 0), reason='pydot-ng currently supports python 3.4') def test_plot_model(): model = Sequential() model.add(Conv2D(filters=2, kernel_size=(2, 3), input_shape=(3, 5, 5), name='conv'))