Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandermendes committed Aug 5, 2016
1 parent 34235bc commit 5b69c36
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
27 changes: 20 additions & 7 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
os.environ['LIBCROWDS_ANALYST_SETTINGS'] = '../settings_test.py'
flask_app = create_app()

task_id = 100
project_id = 1
project_shortname = u'short_name'


@pytest.fixture(scope='session')
def app(request):
Expand All @@ -35,27 +39,36 @@ def auth_headers():

@pytest.fixture
def analysis_kwargs():
return {'api_key': 'k', 'endpoint': 'e', 'project_short_name':
'sn', 'task_id': 123, 'sleep': 0}
return {'api_key': 'k', 'endpoint': 'e',
'project_short_name': project_shortname, 'task_id': task_id,
'sleep': 0}


@pytest.fixture
def result():
return enki.pbclient.DomainObject({'id': 1, 'task_id': 1,
return enki.pbclient.DomainObject({'id': 1, 'task_id': task_id,
'info': {'n': 1}})


@pytest.fixture
def task():
return enki.pbclient.DomainObject({'id': 1})
return enki.pbclient.DomainObject({"info": {"key": "value"},
"n_answers": 30,
"quorum": 0,
"calibration": 0,
"created": "2014-10-22T13:37:58.259609",
"state": "completed",
"project_id": project_id,
"id": task_id,
"priority_0": 0.0})


@pytest.fixture(scope='session')
def payload():
pl = dict(fired_at=u'2014-11-17 09:49:27',
project_short_name=u'project',
project_id=1,
task_id=1,
project_short_name=project_shortname,
project_id=project_id,
task_id=task_id,
result_id=1,
event=u'task_completed')
return json.dumps(pl)
33 changes: 15 additions & 18 deletions test/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@

class TestAnalysis(object):

def create_tasks(self, n):
return [enki.pbclient.DomainObject({'id': i}) for i in range(n)]

def create_tr_dataframe(self, data):
def create_tr_dataframe(self, data, task_id):
task_runs = []
for i in range(len(data)):
tr = {'id': i, 'info': data[i]}
task_runs.append(enki.pbclient.DomainObject(tr))
return {0: enki.dataframer.create_data_frame(task_runs)}
return {task_id: enki.dataframer.create_data_frame(task_runs)}

def test_get_valid_analyst_func(self):
valid_func = analysis.get_analyst_func(1)
Expand All @@ -31,38 +28,38 @@ class TestCategoryOneAnalysis(TestAnalysis):

def setup(self):
super(TestCategoryOneAnalysis, self)
self.tr_data = {'oclc': '123', 'shelfmark': '456', 'title': 'card.jpg',
'comments': 'hello'}
self.empty_tr_data = {'oclc': '', 'shelfmark': '', 'title': 'card.jpg',
'comments': 'hello'}
self.tr_data = {'oclc': '123', 'shelfmark': '456', 'comments': 'hello',
'key': 'value'}
self.empty_tr_data = {'oclc': '', 'shelfmark': '', 'comments': 'hello',
'key': 'value'}

def analyse(self, data, mock_enki, analysis_kwargs):
def analyse(self, data, mock_enki, analysis_kwargs, task):
mock_info = PropertyMock()
mock_result = MagicMock()
type(mock_result).info = mock_info
mock_enki.pbclient.find_results.return_value = [mock_result]
mock_enki.Enki().tasks = self.create_tasks(1)
mock_enki.Enki().task_runs_df = self.create_tr_dataframe(data)
mock_enki.Enki().tasks = [task]
mock_enki.Enki().task_runs_df = self.create_tr_dataframe(data, task.id)
analysis.category_1(**analysis_kwargs)
return mock_result, mock_info

def test_cat_1_analysis_with_no_answers(self, mocker, analysis_kwargs):
def test_cat_1_analysis_no_answers(self, mocker, analysis_kwargs, task):
e = mocker.patch('libcrowds_analyst.analysis.enki')
data = [self.empty_tr_data for i in range(3)]
(mock_result, mock_info) = self.analyse(data, e, analysis_kwargs)
(mock_result, mock_info) = self.analyse(data, e, analysis_kwargs, task)
e.pbclient.update_result.assert_called_once_with(mock_result)
mock_info.assert_called_once_with(self.empty_tr_data)

def test_cat_1_analysis_with_two_matches(self, mocker, analysis_kwargs):
def test_cat_1_analysis_two_matches(self, mocker, analysis_kwargs, task):
e = mocker.patch('libcrowds_analyst.analysis.enki')
data = [self.empty_tr_data] + [self.tr_data for i in range(2)]
(mock_result, mock_info) = self.analyse(data, e, analysis_kwargs)
(mock_result, mock_info) = self.analyse(data, e, analysis_kwargs, task)
e.pbclient.update_result.assert_called_once_with(mock_result)
mock_info.assert_called_once_with(self.tr_data)

def test_cat_1_with_no_matches(self, mocker, analysis_kwargs):
def test_cat_1_no_matches(self, mocker, analysis_kwargs, task):
e = mocker.patch('libcrowds_analyst.analysis.enki')
data = [self.tr_data] + [self.empty_tr_data for i in range(2)]
(mock_result, mock_info) = self.analyse(data, e, analysis_kwargs)
(mock_result, mock_info) = self.analyse(data, e, analysis_kwargs, task)
e.pbclient.update_result.assert_called_once_with(mock_result)
mock_info.assert_called_once_with('Unanalysed')
18 changes: 9 additions & 9 deletions test/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_basic_auth_not_required_for_index(self, test_client, payload):
assert get_res.status_code == 200

def test_basic_auth_required(self, test_client):
res = test_client.get('/project')
res = test_client.get('/short_name')
assert res.status_code == 401

def test_auth_credentials_accepted(self, test_client, auth_headers):
Expand All @@ -31,7 +31,7 @@ def test_webhook_triggers_analysis(self, test_client, payload, mocker):
headers={'Content-type': 'application/json'})
mock_queue.enqueue.assert_called_once_with(analyser_func, 'yourkey',
'http://localhost:5001',
'project', 1)
'short_name', 100)

def test_webhook_404_when_no_analyser(self, test_client, payload, mocker):
mock_enki = mocker.patch('libcrowds_analyst.view.enki')
Expand All @@ -46,7 +46,7 @@ def test_unanalysed_result_get(self, test_client, auth_headers, mocker,
mock_pbclient.find_results.return_value = [result]
mock_render = mocker.patch('libcrowds_analyst.view.render_template')
mock_render.return_value = "OK"
test_client.get('/project', headers=auth_headers)
test_client.get('/short_name', headers=auth_headers)
assert mock_render.call_args_list[0][1]['result'] == result

def test_unanalysed_result_post(self, test_client, auth_headers, mocker,
Expand All @@ -57,7 +57,7 @@ def test_unanalysed_result_post(self, test_client, auth_headers, mocker,
mock_pbclient.update_result.return_value = True
mock_render = mocker.patch('libcrowds_analyst.view.render_template')
mock_render.return_value = "OK"
res = test_client.post('/project', headers=auth_headers)
res = test_client.post('/short_name', headers=auth_headers)
mock_pbclient.update_result.assert_called_once_with(result)

def test_edit_result_get(self, test_client, auth_headers, mocker, result):
Expand All @@ -66,7 +66,7 @@ def test_edit_result_get(self, test_client, auth_headers, mocker, result):
mock_pbclient.find_results.return_value = [result]
mock_render = mocker.patch('libcrowds_analyst.view.render_template')
mock_render.return_value = "OK"
test_client.get('/project/1/edit', headers=auth_headers)
test_client.get('/short_name/1/edit', headers=auth_headers)
form = mock_render.call_args_list[0][1]['form']
assert form.info.data == json.dumps({"n": 1})

Expand All @@ -78,7 +78,7 @@ def test_edit_result_post(self, test_client, auth_headers, mocker, result):
mock_render = mocker.patch('libcrowds_analyst.view.render_template')
mock_render.return_value = "OK"
form_data = {"info": json.dumps(result.info)}
test_client.post('/project/1/edit', data=form_data,
test_client.post('/short_name/1/edit', data=form_data,
headers=auth_headers)
mock_pbclient.update_result.assert_called_once_with(result)

Expand All @@ -88,7 +88,7 @@ def test_reanalyse_get(self, test_client, auth_headers, mocker, result):
mock_enki.Enki.return_value.project = mock_project
mock_render = mocker.patch('libcrowds_analyst.view.render_template')
mock_render.return_value = "OK"
test_client.get('/project/reanalyse', headers=auth_headers)
test_client.get('/short_name/reanalyse', headers=auth_headers)
project = mock_render.call_args_list[0][1]['project']
assert project == mock_project

Expand All @@ -99,8 +99,8 @@ def test_reanalyse_post(self, test_client, auth_headers, mocker, task):
analysis = mocker.patch('libcrowds_analyst.view.analysis')
analyser_func = (lambda x: x + 1)
analysis.get_analyst_func.return_value = analyser_func
test_client.post('/project/reanalyse', headers=auth_headers,
test_client.post('/short_name/reanalyse', headers=auth_headers,
data={'sleep': 1})
mock_queue.enqueue.assert_called_once_with(analyser_func, 'yourkey',
'http://localhost:5001',
'project', 1, sleep=1)
'short_name', 100, sleep=1)

0 comments on commit 5b69c36

Please sign in to comment.