This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Testing: add vote unit test and coverage script
- Loading branch information
1 parent
fc2841a
commit af49fa3
Showing
6 changed files
with
126 additions
and
8 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
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
BASEDIR=$(readlink -f $(dirname $0)) | ||
cd $BASEDIR | ||
|
||
VERSION=$(git rev-parse HEAD) | ||
COVERDIR="$BASEDIR/build/cover-$VERSION" | ||
|
||
function usage() { | ||
echo "Run unit tests and coverage reports on reddit codebase with optional" | ||
echo "http server to the report" | ||
echo | ||
echo "Usage: `basename $0` [options]"; | ||
echo | ||
echo " -h show this message" | ||
echo " -p \$PORT run an simple http server on \$PORT to view results" | ||
echo " -v verbose mode (set -x)" | ||
echo | ||
} | ||
|
||
while getopts ":vhp:" opt; do | ||
case $opt in | ||
p) PORT="$OPTARG" ;; | ||
v) set -x ;; | ||
h) | ||
usage | ||
exit 0 | ||
;; | ||
\?) | ||
echo "Invalid option: -$OPTARG" >&2 | ||
usage | ||
exit 1 | ||
;; | ||
:) | ||
echo "Option -$OPTARG requires an argument." >&2 | ||
exit 1 | ||
;; | ||
esac | ||
done | ||
|
||
nosetests \ | ||
--with-coverage \ | ||
--cover-html \ | ||
--cover-html-dir=$COVERDIR \ | ||
--cover-erase \ | ||
--cover-package=r2 | ||
|
||
if [ "$PORT" != "" ]; then | ||
echo "Starting http server on :$PORT (^C to exit)" | ||
pushd $COVERDIR | ||
python -m SimpleHTTPServer $PORT || echo "Done." | ||
popd | ||
fi | ||
rm -r $COVERDIR |
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
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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from mock import patch, MagicMock | ||
from datetime import datetime | ||
|
||
import pytz | ||
|
||
from r2.models.vote import Vote | ||
from r2.tests import RedditTestCase | ||
|
||
|
||
class TestVoteValidator(RedditTestCase): | ||
|
||
def setUp(self): | ||
self.user = MagicMock(name="user") | ||
self.thing = MagicMock(name="thing") | ||
self.vote_data = {} | ||
super(RedditTestCase, self).setUp() | ||
|
||
def cast_vote(self, **kw): | ||
kw.setdefault("date", datetime.now(pytz.UTC)) | ||
kw.setdefault("direction", Vote.DIRECTIONS.up) | ||
kw.setdefault("get_previous_vote", False) | ||
kw.setdefault("data", self.vote_data) | ||
return Vote( | ||
user=self.user, | ||
thing=self.thing, | ||
**kw | ||
) | ||
|
||
def assert_vote_effects( | ||
self, vote, affects_score, affects_karma, | ||
affected_thing_attr, *notes | ||
): | ||
self.assertEqual(vote.effects.affects_score, affects_score) | ||
self.assertEqual(vote.effects.affects_karma, affects_karma) | ||
self.assertEqual(vote.affected_thing_attr, affected_thing_attr) | ||
self.assertEqual(set(vote.effects.notes), set(notes)) | ||
return vote | ||
|
||
def test_upvote_effects(self): | ||
vote = self.cast_vote() | ||
self.assertTrue(vote.is_upvote) | ||
self.assertFalse(vote.is_downvote) | ||
self.assertFalse(vote.is_self_vote) | ||
self.assert_vote_effects(vote, True, True, "_ups") | ||
|
||
def test_downvote_effects(self): | ||
vote = self.cast_vote(direction=Vote.DIRECTIONS.down) | ||
self.assertFalse(vote.is_upvote) | ||
self.assertTrue(vote.is_downvote) | ||
self.assertFalse(vote.is_self_vote) | ||
self.assert_vote_effects(vote, True, True, "_downs") |
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