Skip to content

Commit cd96bce

Browse files
Alec Glassfordvinnysenthil
andauthored
Make predict test more lenient (GoogleCloudPlatform#4916)
Co-authored-by: Vinny Senthil <vinnysenthil@gmail.com>
1 parent f292b71 commit cd96bce

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

ml_engine/online_prediction/predict_test.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
MODEL = 'census'
2323
JSON_VERSION = 'v2json'
2424
PROJECT = 'python-docs-samples-tests'
25+
CONF_KEY = u'confidence'
26+
PRED_KEY = u'predictions'
2527
EXPECTED_OUTPUT = {
26-
u'confidence': 0.7760370969772339,
27-
u'predictions': u' <=50K'
28+
CONF_KEY: 0.7760370969772339,
29+
PRED_KEY: u' <=50K'
2830
}
31+
CONFIDENCE_EPSILON = 1e-4
2932

3033
# Raise the socket timeout. The requests involved in the sample can take
3134
# a long time to complete.
@@ -40,7 +43,14 @@
4043
def test_predict_json():
4144
result = predict.predict_json(
4245
PROJECT, MODEL, [JSON, JSON], version=JSON_VERSION)
43-
assert [EXPECTED_OUTPUT, EXPECTED_OUTPUT] == result
46+
# Result contains two identical predictions
47+
assert len(result) == 2 and result[0] == result[1]
48+
# Each prediction has `confidence` and `predictions`
49+
assert result[0].keys() == EXPECTED_OUTPUT.keys()
50+
# Prediction matches
51+
assert result[0][PRED_KEY] == EXPECTED_OUTPUT[PRED_KEY]
52+
# Confidence within epsilon
53+
assert abs(result[0][CONF_KEY] - EXPECTED_OUTPUT[CONF_KEY]) < CONFIDENCE_EPSILON
4454

4555

4656
@pytest.mark.flaky

0 commit comments

Comments
 (0)