def get_voting_results_by_spec_url(self): return { AbstractVoteSpecification.uri_generic(vote_spec.id): 'local:Discussion/%d/widgets/%d/vote_specifications/%d/vote_results' % (self.discussion_id, self.id, vote_spec.id) for vote_spec in self.vote_specifications }
def get_voting_results_by_spec_url(self): return { AbstractVoteSpecification.uri_generic(vote_spec.id): 'local:Discussion/%d/widgets/%d/vote_specifications/%d/vote_results' % ( self.discussion_id, self.id, vote_spec.id) for vote_spec in self.vote_specifications }
def get_voting_urls(self, target_idea_id): # TODO: Does not work yet. return { AbstractVoteSpecification.uri_generic(vote_spec.id): 'local:Discussion/%d/widgets/%d/vote_specifications/%d/vote_targets/%d/votes' % (self.discussion_id, self.id, vote_spec.id, Idea.get_database_id(target_idea_id)) for vote_spec in self.vote_specifications }
def get_voting_urls(self, target_idea_id): # TODO: Does not work yet. return { AbstractVoteSpecification.uri_generic(vote_spec.id): 'local:Discussion/%d/widgets/%d/vote_specifications/%d/vote_targets/%d/votes' % ( self.discussion_id, self.id, vote_spec.id, Idea.get_database_id(target_idea_id)) for vote_spec in self.vote_specifications }
def test_voting_widget(discussion, test_app, subidea_1_1, criterion_1, criterion_2, criterion_3, admin_user, participant1_user, test_session, request): # Post the initial configuration db = discussion.db criteria = (criterion_1, criterion_2, criterion_3) new_widget_loc = test_app.post( '/data/Discussion/%d/widgets' % (discussion.id, ), { 'type': 'MultiCriterionVotingWidget', 'settings': json.dumps({"votable_root_id": subidea_1_1.uri()}) }) assert new_widget_loc.status_code == 201 # Get the widget from the db db.flush() widget_uri = new_widget_loc.location new_widget = Widget.get_instance(widget_uri) assert new_widget assert new_widget.base_idea == subidea_1_1 db.expire(new_widget, ('criteria', 'votable_ideas', 'vote_specifications')) # Get the widget from the api widget_rep = test_app.get(local_to_absolute(widget_uri), headers={"Accept": "application/json"}) assert widget_rep.status_code == 200 widget_rep = widget_rep.json votespecs_url = widget_rep.get('votespecs_url', None) assert votespecs_url votespecs_url = local_to_absolute(votespecs_url) # Add a first criterion vote_spec_1 = { '@type': 'LickertVoteSpecification', 'minimum': 0, 'maximum': 1, 'criterion_idea': criterion_1.uri() } new_vote_spec_loc = test_app.post(votespecs_url, json.dumps(vote_spec_1), headers=JSON_HEADER) assert new_vote_spec_loc.status_code == 201 new_vote_spec_uri = new_vote_spec_loc.location new_vote_spec = AbstractVoteSpecification.get_instance(new_vote_spec_uri) assert new_vote_spec # and another one vote_spec_2 = { '@type': 'BinaryVoteSpecification', 'criterion_idea': criterion_2.uri() } new_vote_spec_loc = test_app.post(votespecs_url, json.dumps(vote_spec_2), headers=JSON_HEADER) assert new_vote_spec_loc.status_code == 201 new_vote_spec_uri = new_vote_spec_loc.location new_vote_spec = AbstractVoteSpecification.get_instance(new_vote_spec_uri) assert new_vote_spec # and another one vote_spec_3 = { '@type': 'MultipleChoiceVoteSpecification', 'num_choices': 5, 'criterion_idea': criterion_3.uri() } new_vote_spec_loc = test_app.post(votespecs_url, json.dumps(vote_spec_3), headers=JSON_HEADER) assert new_vote_spec_loc.status_code == 201 new_vote_spec_uri = new_vote_spec_loc.location new_vote_spec = AbstractVoteSpecification.get_instance(new_vote_spec_uri) assert new_vote_spec # Get an updated widget_rep with target widget_rep = test_app.get(local_to_absolute(widget_uri), {'target': subidea_1_1.uri()}, headers={"Accept": "application/json"}) assert widget_rep.status_code == 200 widget_rep = widget_rep.json voting_urls = widget_rep['voting_urls'] vote_spec_reps = widget_rep['vote_specifications'] assert voting_urls # User votes should be empty user_votes_url = local_to_absolute(widget_rep['user_votes_url']) test = test_app.get(user_votes_url) assert test.status_code == 200 assert len(test.json) == 0 # Get the voting endpoint for each vote_spec, and post a vote. # Here we're using the voting_urls of the widget based on a single target; # The alternative is to look at the voting_urls of a vote_spec # and to get an url per target. The end result should be the same. for i, (vote_spec_id, voting_url) in enumerate(voting_urls.iteritems()): voting_url = local_to_absolute(voting_url) for spec in vote_spec_reps: if spec['@id'] == vote_spec_id: break else: assert False, "vote spec %s in voting_urls "\ "but not in vote_specifications" % (vote_spec_id) vote_type = spec['vote_class'] if vote_type == 'LickertIdeaVote': vote_range = spec['maximum'] - spec['minimum'] value = spec['minimum'] + (i % vote_range) elif vote_type == 'BinaryIdeaVote': value = True elif vote_type == 'MultipleChoiceIdeaVote': value = (i % spec['num_choices']) test = test_app.post(voting_url, json.dumps({ "@type": vote_type, "value": value, }), headers=JSON_HEADER) assert test.status_code == 201 # Get them back test = test_app.get(user_votes_url) assert test.status_code == 200 assert len(test.json) == len(vote_spec_reps) # Add votes for another user # TODO # Get vote results. vote_results_urls = widget_rep['voting_results_by_spec_url'] for spec_rep in vote_spec_reps: assert spec_rep['@id'] in vote_results_urls vote_results_url = vote_results_urls.get(spec_rep['@id'], None) assert vote_results_url vote_results = test_app.get(local_to_absolute(vote_results_url)) assert vote_results.status_code == 200 vote_results = vote_results.json assert vote_results[subidea_1_1.uri()]['n'] == 1 if spec_rep['@type'] == "LickertVoteSpecification": assert vote_results[subidea_1_1.uri()]['avg'] == 0 return # So far so good, rest to be done. # Change my mind criterion_key = criteria[0].uri() voting_url = local_to_absolute(voting_urls[criterion_key]) test_app.post(voting_url, {"type": "LickertIdeaVote", "value": 10}) votes = db.query(AbstractIdeaVote).filter_by( voter_id=admin_user.id, idea_id=subidea_1_1.id, criterion_id=criteria[0].id).all() assert len(votes) == 2 assert len([v for v in votes if v.is_tombstone]) == 1 for v in votes: assert v.widget_id == new_widget.id # Get vote results again. vote_results_urls = widget_rep['voting_results_by_spec_url'] for spec_rep in vote_spec_reps: assert spec_rep['@id'] in vote_results_urls vote_results_url = vote_results_urls.get(spec_rep['@id'], None) assert vote_results_url vote_results = test_app.get(local_to_absolute(vote_results_url)) assert vote_results.status_code == 200 vote_results = vote_results.json assert vote_results[subidea_1_1.uri()]['n'] == 1 if spec_rep['@type'] == "LickertVoteSpecification": assert vote_results[subidea_1_1.uri()]['avg'] == 10 # ideas_data = test_app.get('/api/v1/discussion/%d/ideas' % discussion.id) # assert ideas_data.status_code == 200 # print ideas_data def fin(): print "finalizer test_voting_widget" new_widget.delete() # this should cascade to specs and votes test_session.flush() request.addfinalizer(fin)
def test_voting_widget( discussion, test_app, subidea_1_1, criterion_1, criterion_2, criterion_3, admin_user, participant1_user, test_session, request): # Post the initial configuration db = discussion.db criteria = (criterion_1, criterion_2, criterion_3) new_widget_loc = test_app.post( '/data/Discussion/%d/widgets' % (discussion.id,), { 'type': 'MultiCriterionVotingWidget', 'settings': json.dumps({ "votable_root_id": subidea_1_1.uri() }) }) assert new_widget_loc.status_code == 201 # Get the widget from the db db.flush() widget_uri = new_widget_loc.location new_widget = Widget.get_instance(widget_uri) assert new_widget assert new_widget.base_idea == subidea_1_1 db.expire(new_widget, ('criteria', 'votable_ideas', 'vote_specifications')) # Get the widget from the api widget_rep = test_app.get( local_to_absolute(widget_uri), headers={"Accept": "application/json"} ) assert widget_rep.status_code == 200 widget_rep = widget_rep.json votespecs_url = widget_rep.get('votespecs_url', None) assert votespecs_url votespecs_url = local_to_absolute(votespecs_url) # Add a first criterion vote_spec_1 = { '@type': 'LickertVoteSpecification', 'minimum': 0, 'maximum': 1, 'criterion_idea': criterion_1.uri() } new_vote_spec_loc = test_app.post( votespecs_url, json.dumps(vote_spec_1), headers=JSON_HEADER) assert new_vote_spec_loc.status_code == 201 new_vote_spec_uri = new_vote_spec_loc.location new_vote_spec = AbstractVoteSpecification.get_instance(new_vote_spec_uri) assert new_vote_spec # and another one vote_spec_2 = { '@type': 'BinaryVoteSpecification', 'criterion_idea': criterion_2.uri() } new_vote_spec_loc = test_app.post( votespecs_url, json.dumps(vote_spec_2), headers=JSON_HEADER) assert new_vote_spec_loc.status_code == 201 new_vote_spec_uri = new_vote_spec_loc.location new_vote_spec = AbstractVoteSpecification.get_instance(new_vote_spec_uri) assert new_vote_spec # and another one vote_spec_3 = { '@type': 'MultipleChoiceVoteSpecification', 'num_choices': 5, 'criterion_idea': criterion_3.uri() } new_vote_spec_loc = test_app.post( votespecs_url, json.dumps(vote_spec_3), headers=JSON_HEADER) assert new_vote_spec_loc.status_code == 201 new_vote_spec_uri = new_vote_spec_loc.location new_vote_spec = AbstractVoteSpecification.get_instance(new_vote_spec_uri) assert new_vote_spec # Get an updated widget_rep with target widget_rep = test_app.get( local_to_absolute(widget_uri), {'target': subidea_1_1.uri()}, headers={"Accept": "application/json"} ) assert widget_rep.status_code == 200 widget_rep = widget_rep.json voting_urls = widget_rep['voting_urls'] vote_spec_reps = widget_rep['vote_specifications'] assert voting_urls # User votes should be empty user_votes_url = local_to_absolute(widget_rep['user_votes_url']) test = test_app.get(user_votes_url) assert test.status_code == 200 assert len(test.json) == 0 # Get the voting endpoint for each vote_spec, and post a vote. # Here we're using the voting_urls of the widget based on a single target; # The alternative is to look at the voting_urls of a vote_spec # and to get an url per target. The end result should be the same. for i, (vote_spec_id, voting_url) in enumerate(voting_urls.iteritems()): voting_url = local_to_absolute(voting_url) for spec in vote_spec_reps: if spec['@id'] == vote_spec_id: break else: assert False, "vote spec %s in voting_urls "\ "but not in vote_specifications" % (vote_spec_id) vote_type = spec['vote_class'] if vote_type == 'LickertIdeaVote': vote_range = spec['maximum'] - spec['minimum'] value = spec['minimum'] + (i % vote_range) elif vote_type == 'BinaryIdeaVote': value = True elif vote_type == 'MultipleChoiceIdeaVote': value = (i % spec['num_choices']) test = test_app.post(voting_url, json.dumps({ "@type": vote_type, "value": value, }), headers=JSON_HEADER) assert test.status_code == 201 # Get them back test = test_app.get(user_votes_url) assert test.status_code == 200 assert len(test.json) == len(vote_spec_reps) # Add votes for another user # TODO # Get vote results. vote_results_urls = widget_rep['voting_results_by_spec_url'] for spec_rep in vote_spec_reps: assert spec_rep['@id'] in vote_results_urls vote_results_url = vote_results_urls.get(spec_rep['@id'], None) assert vote_results_url vote_results = test_app.get(local_to_absolute(vote_results_url)) assert vote_results.status_code == 200 vote_results = vote_results.json assert vote_results[subidea_1_1.uri()]['n'] == 1 if spec_rep['@type'] == "LickertVoteSpecification": assert vote_results[subidea_1_1.uri()]['avg'] == 0 return # So far so good, rest to be done. # Change my mind criterion_key = criteria[0].uri() voting_url = local_to_absolute(voting_urls[criterion_key]) test_app.post(voting_url, { "type": "LickertIdeaVote", "value": 10}) votes = db.query(AbstractIdeaVote).filter_by( voter_id=admin_user.id, idea_id=subidea_1_1.id, criterion_id=criteria[0].id).all() assert len(votes) == 2 assert len([v for v in votes if v.is_tombstone]) == 1 for v in votes: assert v.widget_id == new_widget.id # Get vote results again. vote_results_urls = widget_rep['voting_results_by_spec_url'] for spec_rep in vote_spec_reps: assert spec_rep['@id'] in vote_results_urls vote_results_url = vote_results_urls.get(spec_rep['@id'], None) assert vote_results_url vote_results = test_app.get(local_to_absolute(vote_results_url)) assert vote_results.status_code == 200 vote_results = vote_results.json assert vote_results[subidea_1_1.uri()]['n'] == 1 if spec_rep['@type'] == "LickertVoteSpecification": assert vote_results[subidea_1_1.uri()]['avg'] == 10 # ideas_data = test_app.get('/api/v1/discussion/%d/ideas' % discussion.id) # assert ideas_data.status_code == 200 # print ideas_data def fin(): print "finalizer test_voting_widget" new_widget.delete() # this should cascade to specs and votes test_session.flush() request.addfinalizer(fin)