Skip to content

Commit

Permalink
Merge pull request #2876 from jeff1evesque/feature-2751
Browse files Browse the repository at this point in the history
#2751: Ensure 'programmatic_interface' tests more specific
  • Loading branch information
jeff1evesque committed Jan 20, 2017
2 parents ff80c6b + 167c370 commit 6397a94
Show file tree
Hide file tree
Showing 16 changed files with 444 additions and 135 deletions.
13 changes: 7 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ install:
##
## Note: unit testing is performed within the docker container, which is
## defined from the Dockerfile.
##
script:
# lint codebase
- flake8 .
Expand Down Expand Up @@ -114,12 +115,12 @@ script:
- cat pytest.log
- (! grep -qE '= FAILURES =|= ERRORS =|= no tests ran in 0.00 seconds =' pytest.log)

## acquire coverage results
##
## Note: more information regarding the '.coverage.docker' naming convention:
##
## https://github.com/pytest-dev/pytest-cov/issues/146#issuecomment-272971136
##
# acquire coverage results
#
# Note: more information regarding the '.coverage.docker' naming convention:
#
# https://github.com/pytest-dev/pytest-cov/issues/146#issuecomment-272971136
#
- docker cp webserver-pytest:/var/machine-learning/.coverage .coverage.docker

# check exit code: 'docker exec' will fail if the container has an exit code
Expand Down
53 changes: 44 additions & 9 deletions brain/load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
This file allocates input to respective 'data_xxx.py', 'model_xx.py', and
generates a return object, when required.
json.dumps(
'''

import json
from brain.session.data_append import Data_Append
from brain.session.data_new import Data_New
from brain.session.model_generate import Model_Generate
Expand Down Expand Up @@ -78,10 +79,19 @@ def load_data_new(self):
session.save_premodel_dataset()
session.check()

return 'Dataset(s) properly uploaded into database'
response = {
'status': 0,
'msg': 'Dataset(s) properly uploaded into database'
}

else:
print session.get_errors()
return None
response = {
'status': 1,
'msg': 'Dataset(s) not uploaded into database'
}

return json.dumps(response)

def load_data_append(self):
'''@load_data_append
Expand Down Expand Up @@ -117,10 +127,19 @@ def load_data_append(self):
session.save_premodel_dataset()
session.check()

return 'Dataset(s) properly appended into database'
response = {
'status': 0,
'msg': 'Dataset(s) properly appended into database'
}

else:
print session.get_errors()
return None
response = {
'status': 1,
'msg': 'Dataset(s) not uploaded into database'
}

return json.dumps(response)

def load_model_generate(self):
'''@load_model_generate
Expand All @@ -142,9 +161,17 @@ def load_model_generate(self):

# return
if session.return_error():
return False
response = {
'status': 1,
'msg': 'Model not generated'
}
else:
return 'Model properly generated'
response = {
'status': 0,
'msg': 'Model properly generated'
}

return json.dumps(response)

def load_model_predict(self):
'''@load_model_predict
Expand All @@ -164,9 +191,17 @@ def load_model_predict(self):

my_prediction = session.predict()
if my_prediction['error']:
return {'result': None, 'error': my_prediction['error']}
response = {
'status': 1,
'result': my_prediction['error'],
}
else:
return {'result': my_prediction, 'error': None}
response = {
'status': 0,
'result': my_prediction,
}

return json.dumps(response)

def get_session_type(self):
'''@load_model_predict
Expand Down
4 changes: 2 additions & 2 deletions interface/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def load_data():
response = loader.get_errors()

# return response
return json.dumps(response)
return response

# load web-interface
else:
Expand Down Expand Up @@ -147,7 +147,7 @@ def load_data():
response = loader.get_errors()

# return response
return json.dumps(response)
return response


@blueprint.route('/login', methods=['POST'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ def test_registration(client, live_server):
result = Save_Account().save_account(username, email, hashed)

# notification: attempt to store account
assert (
result['status'] and
result['id'] and not
result['error']
)
assert result['status']
assert result['id']
assert not result['error']

# notification: email already exists
else:
Expand Down
3 changes: 2 additions & 1 deletion test/live_server/authentication/pytest_user_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def test_login(client, live_server):
payload = {'user[login]': username, 'user[password]': password}
login = client.post(url, data=payload)

assert login.status_code == 200 and session.get('uid')
assert login.status_code == 200
assert session.get('uid') == 1
else:
assert False

Expand Down
8 changes: 3 additions & 5 deletions test/live_server/authentication/pytest_user_logout.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ def test_logout(client, live_server):
assert False

# check logout succeeded
assert (
login.status_code == 200 and
logout.status_code == 200 and
not session.get('uid')
)
assert login.status_code == 200
assert logout.status_code == 200
assert not session.get('uid')
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
'''

import json
import pytest
import os.path
from flask import url_for
from flask import current_app
Expand All @@ -36,25 +35,21 @@ def get_sample_json(jsonfile, model_type):
# open file
json_dataset = None

try:
with open(
os.path.join(
root,
'interface',
'static',
'data',
'json',
'programmatic_interface',
model_type,
'dataset_url',
jsonfile
),
'r'
) as json_file:
json_dataset = json.load(json_file)

except Exception as error:
pytest.fail(error)
with open(
os.path.join(
root,
'interface',
'static',
'data',
'json',
'programmatic_interface',
model_type,
'dataset_url',
jsonfile
),
'r'
) as json_file:
json_dataset = json.load(json_file)

return json.dumps(json_dataset)

Expand All @@ -78,7 +73,9 @@ def get_endpoint():
data=get_sample_json('svm-data-new.json', 'svm')
)

# assertion checks
assert res.status_code == 200
assert res.json['status'] == 0


def test_data_append(client, live_server):
Expand All @@ -100,7 +97,9 @@ def get_endpoint():
data=get_sample_json('svm-data-append.json', 'svm')
)

# assertion checks
assert res.status_code == 200
assert res.json['status'] == 0


def test_model_generate(client, live_server):
Expand All @@ -122,14 +121,21 @@ def get_endpoint():
data=get_sample_json('svm-model-generate.json', 'svm')
)

# assertion checks
assert res.status_code == 200
assert res.json['status'] == 0


def test_model_predict(client, live_server):
'''@test_model_predict
This method tests the 'model_predict' session.
Note: for debugging, the following syntax will output the corresponding
json values, nested within 'json.loads()', to the travis ci:
raise ValueError(res.json['result']['key1'])
'''

@live_server.app.route('/load-data')
Expand All @@ -144,4 +150,44 @@ def get_endpoint():
data=get_sample_json('svm-model-predict.json', 'svm')
)

# check each probability is within acceptable margin
fixed_prob = [
0.14075033321086294,
0.14500955005546354,
0.14156072707544004,
0.19249135186767916,
0.38018803779055466
]
cp = res.json['result']['confidence']['probability']
margin_prob = 0.005
check_prob = [
i for i in fixed_prob if any(abs(i-j) > margin_prob for j in cp)
]

# assertion checks
assert res.status_code == 200
assert res.json['status'] == 0
assert res.json['result']
assert res.json['result']['confidence']
assert res.json['result']['confidence']['classes'] == [
'dep-variable-1',
'dep-variable-2',
'dep-variable-3',
'dep-variable-4',
'dep-variable-5'
]
assert res.json['result']['confidence']['decision_function'] == [
0.1221379769127864,
0.0,
-0.2201467913263242,
-0.22014661657537662,
-0.12213797691278638,
-0.33333297925570843,
-0.33333281615328886,
-0.2201467913263242,
-0.22014661657537662,
1.8353514974478458e-07
]
assert check_prob
assert res.json['result']['model'] == 'svm'
assert res.json['result']['result'] == 'dep-variable-4'
Loading

0 comments on commit 6397a94

Please sign in to comment.