Milimetric has uploaded a new change for review.
https://gerrit.wikimedia.org/r/80516
Change subject: even more tests
......................................................................
even more tests
Change-Id: Ia9a36aaab74b9ab76ceefc16b05bf3eae2a6a39d
---
A scripts/test
M tests/fixtures.py
M tests/test_controllers/test_cohorts.py
M wikimetrics/controllers/cohorts.py
4 files changed, 148 insertions(+), 16 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/analytics/wikimetrics
refs/changes/16/80516/1
diff --git a/scripts/test b/scripts/test
new file mode 100755
index 0000000..6603e99
--- /dev/null
+++ b/scripts/test
@@ -0,0 +1,3 @@
+# for example:
+# scripts/test "models
tests/test_controllers/test_cohorts.py:TestCohortsController"
+rm .coverage *.db ; find -name *.pyc | xargs rm ; nosetests --cover-erase -e $1
diff --git a/tests/fixtures.py b/tests/fixtures.py
index 305cf2f..c5fd4a1 100644
--- a/tests/fixtures.py
+++ b/tests/fixtures.py
@@ -288,6 +288,7 @@
self.test_user_id = dan_user.id
self.test_web_user_id = web_test_user.id
self.test_cohort_id = test_cohort.id
+ self.test_cohort_name = test_cohort.name
self.test_cohort_user_id = dan_owns_test.id
self.test_wiki_user_id = dan.id
self.test_cohort_wiki_user_id = dan_in_test.id
diff --git a/tests/test_controllers/test_cohorts.py
b/tests/test_controllers/test_cohorts.py
index 04b2499..0c5e860 100644
--- a/tests/test_controllers/test_cohorts.py
+++ b/tests/test_controllers/test_cohorts.py
@@ -1,8 +1,14 @@
# -*- coding:utf-8 -*-
import json
-from nose.tools import assert_equal, assert_not_equal
+from nose.tools import assert_equal, assert_not_equal, raises, assert_true
from tests.fixtures import WebTest
-from wikimetrics.controllers.cohorts import parse_username, normalize_user
+from wikimetrics.controllers.cohorts import (
+ parse_username,
+ normalize_user,
+ normalize_newlines,
+ to_safe_json,
+)
+from wikimetrics.models import Cohort
class TestCohortsController(WebTest):
@@ -27,6 +33,12 @@
assert_equal(response.status_code, 200)
assert_equal(len(parsed['wikiusers']), 3)
+ def test_detail_by_name(self):
+ response =
self.app.get('/cohorts/detail/{0}'.format(self.test_cohort_name))
+ parsed = json.loads(response.data)
+ assert_equal(response.status_code, 200)
+ assert_equal(len(parsed['wikiusers']), 3)
+
def test_full_detail(self):
response = self.app.get('/cohorts/detail/{0}?full_detail=true'.format(
self.test_cohort_id
@@ -34,11 +46,6 @@
parsed = json.loads(response.data)
assert_equal(response.status_code, 200)
assert_equal(len(parsed['wikiusers']), 4)
-
- def test_not_found(self):
- response = self.app.get('/cohorts/detail/1-OI--LASJLI---LIJSL$EIOJ')
-
- assert_equal(response.status_code, 404)
def test_validate_username(self):
# this username has a few problems that the normalize call should
handle
@@ -50,3 +57,121 @@
parsed_user = parse_username(problem_username, decode=False)
valid_user = normalize_user(parsed_user, 'enwiki')
assert_not_equal(valid_user, None)
+
+ def test_get_cohort_by_name_not_found(self):
+ response = self.app.get('/cohorts/detail/1-OI--LASJLI---LIJSL$EIOJ')
+ assert_equal(response.status_code, 404)
+
+ def test_get_cohort_by_id_not_found(self):
+ response = self.app.get('/cohorts/detail/133715435033')
+ assert_equal(response.status_code, 404)
+
+ def test_cohort_upload_finish_existing_name(self):
+ response = self.app.post('/cohorts/create', data=dict(
+ name=self.test_cohort_name,
+ ))
+ assert_true(response.data.find('isError') >= 0)
+ assert_true(response.data.find('There was a problem') >= 0)
+
+ def test_cohort_upload_finish_no_users_error(self):
+ response = self.app.post('/cohorts/create', data=dict(
+ name='blijalsijelij',
+ project='enwiki',
+ description='test',
+ users='', # invalid JSON should throw an error
+ ))
+ assert_true(response.data.find('isError') >= 0)
+ assert_true(response.data.find('There was a problem') >= 0)
+
+ def test_cohort_upload_finish_no_project_error(self):
+ users = '''[
+ {"username":"Dan","user_id":1,"project":"en"},
+ {"username":"Stefan","user_id":2,"project":"ar"}
+ ]'''
+ response = self.app.post('/cohorts/create', data=dict(
+ name='blijalsijelij',
+ description='test',
+ users=users,
+ ))
+ assert_true(response.data.find('isError') >= 0)
+ assert_true(response.data.find('If all the users do not belong') >= 0)
+
+ def test_cohort_upload_finish(self):
+ new_cohort_name = 'New Test Cohort'
+ new_cohort_description = 'test enwiki cohort'
+ users = '''[
+ {{"username":"Dan","project":"enwiki","user_id":{0}}},
+ {{"username":"Evan","project":"dewiki","user_id":{1}}}
+ ]'''.format(
+ self.test_mediawiki_user_id,
+ self.test_mediawiki_user_id_evan,
+ )
+ response = self.app.post('/cohorts/create', data=dict(
+ name=new_cohort_name,
+ description=new_cohort_description,
+ project='enwiki',
+ users=users,
+ ))
+ assert_true(response.data.find('isRedirect') >= 0)
+ assert_true(response.data.find('/cohorts/') >= 0)
+
+ # look for the newly created cohort
+ cohort = self.session.query(Cohort).filter(Cohort.name ==
new_cohort_name).one()
+ assert_equal(cohort.description, new_cohort_description)
+
+ def test_cohort_upload_finish_sets_project_from_users(self):
+ new_cohort_name = 'New Test Cohort'
+ new_cohort_description = 'test enwiki cohort'
+ users = '''[
+ {{"username":"Dan","project":"enwiki","user_id":{0}}},
+ {{"username":"Evan","project":"enwiki","user_id":{1}}}
+ ]'''.format(
+ self.test_mediawiki_user_id,
+ self.test_mediawiki_user_id_evan,
+ )
+ self.app.post('/cohorts/create', data=dict(
+ name=new_cohort_name,
+ description=new_cohort_description,
+ users=users,
+ ))
+
+ # look for the newly created cohort
+ cohort = self.session.query(Cohort).filter(Cohort.name ==
new_cohort_name).one()
+ assert_equal(cohort.default_project, 'enwiki')
+
+ def test_validate_cohort_name_allowed(self):
+ response = self.app.get('/cohorts/validate/name?name={0}'.format(
+ self.test_cohort_name)
+ )
+
+ assert_equal(response.status_code, 200)
+ assert_equal(json.loads(response.data), False)
+
+ def test_validate_cohort_project_allowed(self):
+ response = self.app.get('/cohorts/validate/project?project=enwiki')
+
+ assert_equal(response.status_code, 200)
+ assert_equal(json.loads(response.data), True)
+
+ def test_normalize_newlines(self):
+ stream = [
+ 'blahblah\r',
+ 'blahblahblahnor',
+ 'blahblah1\rblahblah2',
+ ]
+ lines = list(normalize_newlines(stream))
+ assert_equal(len(lines), 5)
+ assert_equal(lines[0], 'blahblah')
+ assert_equal(lines[1], '')
+ assert_equal(lines[2], 'blahblahblahnor')
+ assert_equal(lines[3], 'blahblah1')
+ assert_equal(lines[4], 'blahblah2')
+
+ def test_to_safe_json(self):
+ unsafe_json = '{"quotes":"He''s said: \"Real Artists Ship.\""}'
+ safe_json = to_safe_json(unsafe_json)
+
+ assert_equal(
+ safe_json,
+ '\\"{\\\\"quotes\\\\":\\\\"Hes said: \\\\"Real Artists
Ship.\\\\"\\\\"}\\"'
+ )
diff --git a/wikimetrics/controllers/cohorts.py
b/wikimetrics/controllers/cohorts.py
index f4df24f..1943561 100644
--- a/wikimetrics/controllers/cohorts.py
+++ b/wikimetrics/controllers/cohorts.py
@@ -86,8 +86,7 @@
cohort = query.filter(Cohort.id == id).one()
db_session.close()
return cohort
- # MultipleResultsFound NoResultFound
- except:
+ except (MultipleResultsFound, NoResultFound):
return None
@@ -97,8 +96,7 @@
cohort = query.filter(Cohort.name == name).one()
db_session.close()
return cohort
- # MultipleResultsFound NoResultFound
- except:
+ except (MultipleResultsFound, NoResultFound):
return None
@@ -169,13 +167,14 @@
def cohort_upload_finish():
try:
name = request.form.get('name')
+ # re-validate the name
+ if get_cohort_by_name(name):
+ raise Exception('Cohort name {0} is already used'.format(name))
+
project = request.form.get('project')
description = request.form.get('description')
users_json = request.form.get('users')
users = json.loads(users_json)
- # re-validate
- if get_cohort_by_name(name):
- raise Exception('Cohort name {0} is already used'.format(name))
# NOTE: If we don't re-validate here, the user can change the cohort
client-side
# This will produce weird results but we sort of don't care.
@@ -189,10 +188,14 @@
if not project:
if all([user['project'] == users[0]['project'] for user in users]):
project = users[0]['project']
- app.logger.info('adding cohort: {0}, with project: {1}'.format(name,
project))
+
+ if not project:
+ return json_error('If all the users do not belong to the same
project, '
+ 'your cohort needs a default project.')
+
create_cohort(name, description, project, valid)
return json_redirect(url_for('cohorts_index'))
-
+
except Exception, e:
app.logger.exception(str(e))
return json_error(
--
To view, visit https://gerrit.wikimedia.org/r/80516
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia9a36aaab74b9ab76ceefc16b05bf3eae2a6a39d
Gerrit-PatchSet: 1
Gerrit-Project: analytics/wikimetrics
Gerrit-Branch: master
Gerrit-Owner: Milimetric <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits