Skip to content

Commit

Permalink
Patch to increase test coverage (#8902)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
icyblade authored and fchollet committed Jan 10, 2018
1 parent 7591740 commit 7d7060f
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
'pytest-pep8',
'pytest-xdist',
'pytest-cov',
'pandas'],
'pandas',
'requests'],
},
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down
13 changes: 13 additions & 0 deletions tests/keras/preprocessing/sequence_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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__])
27 changes: 27 additions & 0 deletions tests/keras/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__])
43 changes: 43 additions & 0 deletions tests/keras/utils/io_utils_test.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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__])
1 change: 0 additions & 1 deletion tests/keras/utils/vis_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down

0 comments on commit 7d7060f

Please sign in to comment.