Skip to content

Commit

Permalink
Refactor dashboard activity stream tests
Browse files Browse the repository at this point in the history
- Move tests for contents of dashboard activity stream out of test_activity.py,
this test module was way too long and confusing, leave it for testing the
public activity streams only. Add a docstring to the module saying so.

- Add a docstring to test_follow.py explaining that it tests the follower
functions only (follow, unfollow, etc.) and not the contents of the dashboard
activity stream that is generated from what you're folllowing.

- Add new tests for the contents of the dashboard activity stream in
test_dashboard.py along with other dashboard tests.

Currently some of these tests are failing because activities from followed
groups are not appearing in the dashboard.
  • Loading branch information
Sean Hammond authored and tobes committed Nov 27, 2012
1 parent 9bc76a0 commit eb55a65
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 170 deletions.
164 changes: 11 additions & 153 deletions ckan/tests/functional/api/test_activity.py
@@ -1,3 +1,13 @@
'''Functional tests for the public activity streams API.
This module tests the contents of the various public activity streams:
use activity streams, dataset activity streams, group activity streams, etc.
This module _does not_ test the private user dashboard activity stream (which
is different because the contents depend on what the user is following), that
is tested in test_dashboard.py.
'''
import datetime
import logging
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -163,12 +173,6 @@ def setup_class(self):
'apikey': normal_user.apikey,
'name': normal_user.name,
}
follower = model.User.get('tester')
self.follower = {
'id': follower.id,
'apikey': follower.apikey,
'name': follower.name,
}
warandpeace = model.Package.get('warandpeace')
self.warandpeace = {
'id': warandpeace.id,
Expand All @@ -177,54 +181,14 @@ def setup_class(self):
self.annakarenina = {
'id': annakarenina.id,
}
self.users = [self.sysadmin_user, self.normal_user, self.follower]
self.users = [self.sysadmin_user, self.normal_user]
self.app = paste.fixture.TestApp(pylons.test.pylonsapp)

# Make follower follow everything else.
params = {'id': 'testsysadmin'}
extra_environ = {'Authorization': str(self.follower['apikey'])}
response = self.app.post('/api/action/follow_user',
params=json.dumps(params), extra_environ=extra_environ).json
assert response['success'] is True
params = {'id': 'annafan'}
extra_environ = {'Authorization': str(self.follower['apikey'])}
response = self.app.post('/api/action/follow_user',
params=json.dumps(params), extra_environ=extra_environ).json
assert response['success'] is True
params = {'id': 'warandpeace'}
extra_environ = {'Authorization': str(self.follower['apikey'])}
response = self.app.post('/api/action/follow_dataset',
params=json.dumps(params), extra_environ=extra_environ).json
assert response['success'] is True
params = {'id': 'annakarenina'}
extra_environ = {'Authorization': str(self.follower['apikey'])}
response = self.app.post('/api/action/follow_dataset',
params=json.dumps(params), extra_environ=extra_environ).json
assert response['success'] is True

self.followees = \
[
self.sysadmin_user['id'],
self.normal_user['id'],
self.follower['id'],
self.warandpeace['id'],
self.annakarenina['id']
]

@classmethod
def teardown_class(self):
import ckan.model as model
model.repo.rebuild_db()

def dashboard_activity_stream(self, apikey):

response = self.app.get("/api/action/dashboard_activity_list",
json.dumps({}),
extra_environ={'Authorization': str(apikey)})
response_dict = json.loads(response.body)
assert response_dict['success'] is True
return response_dict['result']

def user_activity_stream(self, user_id, apikey=None):
if apikey:
extra_environ = {'Authorization': str(apikey)}
Expand Down Expand Up @@ -290,28 +254,9 @@ def record_details(self, user_id, package_id=None, group_ids=None,
details['recently changed datasets stream'] = \
self.recently_changed_datasets_stream(apikey)

details['user dashboard activity stream'] = (
self.dashboard_activity_stream(apikey))

details['follower dashboard activity stream'] = (
self.dashboard_activity_stream(self.follower['apikey']))

details['time'] = datetime.datetime.now()
return details

def check_dashboards(self, before, after, activity):
new_activities = [activity_ for activity_ in
after['user dashboard activity stream']
if activity_ not in before['user dashboard activity stream']]
assert [activity['id'] for activity in new_activities] == [
activity['id']]

new_activities = [activity_ for activity_ in
after['follower dashboard activity stream']
if activity_ not in before['follower dashboard activity stream']]
assert [activity['id'] for activity in new_activities] == [
activity['id']]

def _create_package(self, user, name=None):
if user:
user_id = user['id']
Expand Down Expand Up @@ -358,31 +303,6 @@ def _create_package(self, user, name=None):
after['recently changed datasets stream'])
assert new_rcd_activities == [activity]

# The new activity should appear in the user's dashboard activity
# stream.
new_activities = [activity_ for activity_ in
after['user dashboard activity stream']
if activity_ not in before['user dashboard activity stream']]
# There will be other new activities besides the 'follow dataset' one
# because all the dataset's old activities appear in the user's
# dashboard when she starts to follow the dataset.
assert activity['id'] in [
activity['id'] for activity in new_activities]

# The new activity should appear in the user "follower"'s dashboard
# activity stream because she follows all the other users and datasets.
new_activities = [activity_ for activity_ in
after['follower dashboard activity stream']
if activity_ not in before['follower dashboard activity stream']]
# There will be other new activities besides the 'follow dataset' one
# because all the dataset's old activities appear in the user's
# dashboard when she starts to follow the dataset.
assert [activity['id'] for activity in new_activities] == [
activity['id']]

# The same new activity should appear on the dashboard's of the user's
# followers.

# The same new activity should appear in the activity streams of the
# package's groups.
for group_dict in package_created['groups']:
Expand Down Expand Up @@ -497,8 +417,6 @@ def _add_resource(self, package, user):
after['group activity streams'][group_dict['name']])
assert grp_new_activities == [activity]

self.check_dashboards(before, after, activity)

# Check that the new activity has the right attributes.
assert activity['object_id'] == updated_package['id'], \
str(activity['object_id'])
Expand Down Expand Up @@ -589,8 +507,6 @@ def _delete_extra(self, package_dict, user):
after['group activity streams'][group_dict['name']])
assert grp_new_activities == [activity]

self.check_dashboards(before, after, activity)

# Check that the new activity has the right attributes.
assert activity['object_id'] == updated_package['id'], \
str(activity['object_id'])
Expand Down Expand Up @@ -679,8 +595,6 @@ def _update_extra(self, package_dict, user):
after['recently changed datasets stream']) \
== user_new_activities

self.check_dashboards(before, after, activity)

# If the package has any groups, the same new activity should appear
# in the activity stream of each group.
for group_dict in package_dict['groups']:
Expand Down Expand Up @@ -783,8 +697,6 @@ def _add_extra(self, package_dict, user, key=None):
after['group activity streams'][group_dict['name']])
assert grp_new_activities == [activity]

self.check_dashboards(before, after, activity)

# Check that the new activity has the right attributes.
assert activity['object_id'] == updated_package['id'], \
str(activity['object_id'])
Expand Down Expand Up @@ -845,8 +757,6 @@ def _create_activity(self, user, package, params):
after['package activity stream']))
assert pkg_new_activities == user_new_activities

self.check_dashboards(before, after, activity)

# Check that the new activity has the right attributes.
assert activity['object_id'] == params['object_id'], (
str(activity['object_id']))
Expand Down Expand Up @@ -892,8 +802,6 @@ def _delete_group(self, group, user):
new_activities, ("The same activity should also "
"appear in the group's activity stream.")

self.check_dashboards(before, after, activity)

# Check that the new activity has the right attributes.
assert activity['object_id'] == group['id'], str(activity['object_id'])
assert activity['user_id'] == user['id'], str(activity['user_id'])
Expand Down Expand Up @@ -937,8 +845,6 @@ def _update_group(self, group, user):
new_activities, ("The same activity should also "
"appear in the group's activity stream.")

self.check_dashboards(before, after, activity)

# Check that the new activity has the right attributes.
assert activity['object_id'] == group['id'], str(activity['object_id'])
assert activity['user_id'] == user['id'], str(activity['user_id'])
Expand Down Expand Up @@ -986,8 +892,6 @@ def _update_user(self, user):
"the user's activity stream, but found %i" % len(new_activities))
activity = new_activities[0]

self.check_dashboards(before, after, activity)

# Check that the new activity has the right attributes.
assert activity['object_id'] == user_dict['id'], (
str(activity['object_id']))
Expand Down Expand Up @@ -1054,8 +958,6 @@ def _delete_resources(self, package):
after['group activity streams'][group_dict['name']])
assert grp_new_activities == [activity]

self.check_dashboards(before, after, activity)

# Check that the new activity has the right attributes.
assert activity['object_id'] == package['id'], (
str(activity['object_id']))
Expand Down Expand Up @@ -1132,8 +1034,6 @@ def _update_package(self, package, user):
after['recently changed datasets stream']) \
== user_new_activities

self.check_dashboards(before, after, activity)

# If the package has any groups, the same new activity should appear
# in the activity stream of each group.
for group_dict in package['groups']:
Expand Down Expand Up @@ -1211,8 +1111,6 @@ def _update_resource(self, package, resource, user):
after['recently changed datasets stream']) \
== user_new_activities

self.check_dashboards(before, after, activity)

# If the package has any groups, the same new activity should appear
# in the activity stream of each group.
for group_dict in package['groups']:
Expand Down Expand Up @@ -1289,8 +1187,6 @@ def _delete_package(self, package):
after['recently changed datasets stream']) \
== user_new_activities

self.check_dashboards(before, after, activity)

# If the package has any groups, the same new activity should appear
# in the activity stream of each group.
for group_dict in package['groups']:
Expand Down Expand Up @@ -1406,8 +1302,6 @@ def test_01_remove_tag(self):
after['recently changed datasets stream']) \
== user_new_activities

self.check_dashboards(before, after, activity)

# If the package has any groups, the same new activity should appear
# in the activity stream of each group.
for group_dict in pkg_dict['groups']:
Expand Down Expand Up @@ -1618,8 +1512,6 @@ def test_create_group(self):
new_activities, ("The same activity should also appear in "
"the group's activity stream.")

self.check_dashboards(before, after, activity)

# Check that the new activity has the right attributes.
assert activity['object_id'] == group_created['id'], \
str(activity['object_id'])
Expand Down Expand Up @@ -1692,8 +1584,6 @@ def test_add_tag(self):
after['recently changed datasets stream']) \
== user_new_activities

self.check_dashboards(before, after, activity)

# If the package has any groups, the same new activity should appear
# in the activity stream of each group.
for group_dict in pkg_dict['groups']:
Expand Down Expand Up @@ -2119,28 +2009,6 @@ def test_follow_dataset(self):
for activity in user_new_activities:
assert activity in pkg_new_activities

# The new activity should appear in the user's dashboard activity
# stream.
new_activities = [activity_ for activity_ in
after['user dashboard activity stream']
if activity_ not in before['user dashboard activity stream']]
# There will be other new activities besides the 'follow dataset' one
# because all the dataset's old activities appear in the user's
# dashboard when she starts to follow the dataset.
assert activity['id'] in [
activity['id'] for activity in new_activities]

# The new activity should appear in the user "follower"'s dashboard
# activity stream because she follows all the other users and datasets.
new_activities = [activity_ for activity_ in
after['follower dashboard activity stream']
if activity_ not in before['follower dashboard activity stream']]
# There will be other new activities besides the 'follow dataset' one
# because all the dataset's old activities appear in the user's
# dashboard when she starts to follow the dataset.
assert [activity['id'] for activity in new_activities] == [
activity['id']]

# Check that the new activity has the right attributes.
assert activity['object_id'] == self.warandpeace['id'], \
str(activity['object_id'])
Expand Down Expand Up @@ -2182,16 +2050,6 @@ def test_follow_user(self):
len(user_new_activities))
activity = user_new_activities[0]

# Check that the new activity appears in the user's private activity
# stream.
user_new_activities = (find_new_activities(
before['follower dashboard activity stream'],
after['follower dashboard activity stream']))
assert len(user_new_activities) == 1, ("There should be 1 new "
" activity in the user's activity stream, but found %i" %
len(user_new_activities))
assert user_new_activities[0]['id'] == activity['id']

# Check that the new activity has the right attributes.
assert activity['object_id'] == self.sysadmin_user['id'], \
str(activity['object_id'])
Expand Down

0 comments on commit eb55a65

Please sign in to comment.