forked from brosner/django-voting
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: https://django-voting.googlecode.com/svn/trunk@48 662f01ad-f42a-0410-a340-718c64ddaef4
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,63 @@ | ||
# -*- coding: utf-8 -*- | ||
r""" | ||
>>> from django.contrib.auth.models import User | ||
>>> from voting.models import Vote | ||
>>> from voting.tests.models import Item | ||
########## | ||
# Voting # | ||
########## | ||
# Basic voting ############################################################### | ||
>>> i1 = Item.objects.create(name='test1') | ||
>>> users = [] | ||
>>> for username in ['u1', 'u2', 'u3', 'u4']: | ||
... users.append(User.objects.create_user(username, '%s@test.com' % username, 'test')) | ||
>>> Vote.objects.get_score(i1) | ||
{'score': 0, 'num_votes': 0} | ||
>>> Vote.objects.record_vote(i1, users[0], +1) | ||
>>> Vote.objects.get_score(i1) | ||
{'score': 1, 'num_votes': 1} | ||
>>> Vote.objects.record_vote(i1, users[0], -1) | ||
>>> Vote.objects.get_score(i1) | ||
{'score': -1, 'num_votes': 1} | ||
>>> Vote.objects.record_vote(i1, users[0], 0) | ||
>>> Vote.objects.get_score(i1) | ||
{'score': 0, 'num_votes': 0} | ||
>>> for user in users: | ||
... Vote.objects.record_vote(i1, user, +1) | ||
>>> Vote.objects.get_score(i1) | ||
{'score': 4, 'num_votes': 4} | ||
>>> for user in users[:2]: | ||
... Vote.objects.record_vote(i1, user, 0) | ||
>>> Vote.objects.get_score(i1) | ||
{'score': 2, 'num_votes': 2} | ||
>>> for user in users[:2]: | ||
... Vote.objects.record_vote(i1, user, -1) | ||
>>> Vote.objects.get_score(i1) | ||
{'score': 0, 'num_votes': 4} | ||
# Retrieval of votes ######################################################### | ||
>>> i2 = Item.objects.create(name='test2') | ||
>>> i3 = Item.objects.create(name='test3') | ||
>>> i4 = Item.objects.create(name='test4') | ||
>>> Vote.objects.record_vote(i2, users[0], +1) | ||
>>> Vote.objects.record_vote(i3, users[0], -1) | ||
>>> Vote.objects.record_vote(i4, users[0], 0) | ||
>>> vote = Vote.objects.get_for_user(i2, users[0]) | ||
>>> (vote.vote, vote.is_upvote(), vote.is_downvote()) | ||
(1, True, False) | ||
>>> vote = Vote.objects.get_for_user(i3, users[0]) | ||
>>> (vote.vote, vote.is_upvote(), vote.is_downvote()) | ||
(-1, False, True) | ||
>>> Vote.objects.get_for_user(i4, users[0]) is None | ||
True | ||
# In bulk | ||
>>> votes = Vote.objects.get_for_user_in_bulk([i1, i2, i3, i4], users[0]) | ||
>>> [(id, vote.vote) for id, vote in votes.items()] | ||
[(1, -1), (2, 1), (3, -1)] | ||
""" |