<s> import unittest <EOL> import random <EOL> from django . db import models <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . conf import settings <EOL> from exceptions import * <EOL> from models import Vote , SimilarUser , IgnoredObject <EOL> from fields import AnonymousRatingField , RatingField <EOL> settings . RATINGS_VOTES_PER_IP = <NUM_LIT:1> <EOL> class RatingTestModel ( models . Model ) : <EOL> rating = AnonymousRatingField ( range = <NUM_LIT:2> , can_change_vote = True ) <EOL> rating2 = RatingField ( range = <NUM_LIT:2> , can_change_vote = False ) <EOL> def __unicode__ ( self ) : <EOL> return unicode ( self . pk ) <EOL> class RatingTestCase ( unittest . TestCase ) : <EOL> def testRatings ( self ) : <EOL> instance = RatingTestModel . objects . create ( ) <EOL> instance . rating . add ( score = <NUM_LIT:1> , user = None , ip_address = '<STR_LIT:127.0.0.1>' ) <EOL> self . assertEquals ( instance . rating . score , <NUM_LIT:1> ) <EOL> self . assertEquals ( instance . rating . votes , <NUM_LIT:1> ) <EOL> instance . rating . add ( score = <NUM_LIT:2> , user = None , ip_address = '<STR_LIT>' ) <EOL> self . assertEquals ( instance . rating . score , <NUM_LIT:3> ) <EOL> self . assertEquals ( instance . rating . votes , <NUM_LIT:2> ) <EOL> instance . rating . add ( score = <NUM_LIT:2> , user = None , ip_address = '<STR_LIT:127.0.0.1>' ) <EOL> self . assertEquals ( instance . rating . score , <NUM_LIT:4> ) <EOL> self . assertEquals ( instance . rating . votes , <NUM_LIT:2> ) <EOL> user = User . objects . create ( username = str ( random . randint ( <NUM_LIT:0> , <NUM_LIT> ) ) ) <EOL> user2 = User . objects . create ( username = str ( random . randint ( <NUM_LIT:0> , <NUM_LIT> ) ) ) <EOL> instance . rating . add ( score = <NUM_LIT:2> , user = user , ip_address = '<STR_LIT>' ) <EOL> self . assertEquals ( instance . rating . score , <NUM_LIT:6> ) <EOL> self . assertEquals ( instance . rating . votes , <NUM_LIT:3> ) <EOL> instance . rating2 . add ( score = <NUM_LIT:2> , user = user , ip_address = '<STR_LIT>' ) <EOL> self . assertEquals ( instance . rating2 . score , <NUM_LIT:2> ) <EOL> self . assertEquals ( instance . rating2 . votes , <NUM_LIT:1> ) <EOL> self . assertRaises ( IPLimitReached , instance . rating2 . add , score = <NUM_LIT:2> , user = user2 , ip_address = '<STR_LIT>' ) <EOL> Vote . objects . filter ( ip_address = '<STR_LIT>' ) . delete ( ) <EOL> instance = RatingTestModel . objects . get ( pk = instance . pk ) <EOL> self . assertEquals ( instance . rating . score , <NUM_LIT:4> ) <EOL> self . assertEquals ( instance . rating . votes , <NUM_LIT:2> ) <EOL> self . assertEquals ( instance . rating2 . score , <NUM_LIT:0> ) <EOL> self . assertEquals ( instance . rating2 . votes , <NUM_LIT:0> ) <EOL> class RecommendationsTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . instance = RatingTestModel . objects . create ( ) <EOL> self . instance2 = RatingTestModel . objects . create ( ) <EOL> self . instance3 = RatingTestModel . objects . create ( ) <EOL> self . instance4 = RatingTestModel . objects . create ( ) <EOL> self . instance5 = RatingTestModel . objects . create ( ) <EOL> self . user = User . objects . create ( username = str ( random . randint ( <NUM_LIT:0> , <NUM_LIT> ) ) ) <EOL> self . user2 = User . objects . create ( username = str ( random . randint ( <NUM_LIT:0> , <NUM_LIT> ) ) ) <EOL> def testExclusions ( self ) : <EOL> Vote . objects . all ( ) . delete ( ) <EOL> self . instance . rating . add ( score = <NUM_LIT:1> , user = self . user , ip_address = '<STR_LIT:127.0.0.1>' ) <EOL> self . instance2 . rating . add ( score = <NUM_LIT:1> , user = self . user , ip_address = '<STR_LIT:127.0.0.1>' ) <EOL> self . instance3 . rating . add ( score = <NUM_LIT:1> , user = self . user , ip_address = '<STR_LIT:127.0.0.1>' ) <EOL> self . instance4 . rating . add ( score = <NUM_LIT:1> , user = self . user , ip_address = '<STR_LIT:127.0.0.1>' ) <EOL> self . instance5 . rating . add ( score = <NUM_LIT:1> , user = self . user , ip_address = '<STR_LIT:127.0.0.1>' ) <EOL> self . instance . rating . add ( score = <NUM_LIT:1> , user = self . user2 , ip_address = '<STR_LIT>' ) <EOL> SimilarUser . objects . update_recommendations ( ) <EOL> self . assertEquals ( SimilarUser . objects . count ( ) , <NUM_LIT:2> ) <EOL> recs = list ( SimilarUser . objects . get_recommendations ( self . user2 , RatingTestModel ) ) <EOL> self . assertEquals ( len ( recs ) , <NUM_LIT:4> ) <EOL> ct = ContentType . objects . get_for_model ( RatingTestModel ) <EOL> IgnoredObject . objects . create ( user = self . user2 , content_type = ct , object_id = self . instance2 . pk ) <EOL> recs = list ( SimilarUser . objects . get_recommendations ( self . user2 , RatingTestModel ) ) <EOL> self . assertEquals ( len ( recs ) , <NUM_LIT:3> ) <EOL> IgnoredObject . objects . create ( user = self . user2 , content_type = ct , object_id = self . instance3 . pk ) <EOL> IgnoredObject . objects . create ( user = self . user2 , content_type = ct , object_id = self . instance4 . pk ) <EOL> recs = list ( SimilarUser . objects . get_recommendations ( self . user2 , RatingTestModel ) ) <EOL> self . assertEquals ( len ( recs ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( recs , [ self . instance5 ] ) <EOL> self . instance5 . rating . add ( score = <NUM_LIT:1> , user = self . user2 , ip_address = '<STR_LIT>' ) <EOL> recs = list ( SimilarUser . objects . get_recommendations ( self . user2 , RatingTestModel ) ) <EOL> self . assertEquals ( len ( recs ) , <NUM_LIT:0> ) <EOL> def testSimilarUsers ( self ) : <EOL> Vote . objects . all ( ) . delete ( ) <EOL> self . instance . rating . add ( score = <NUM_LIT:1> , user = self . user , ip_address = '<STR_LIT:127.0.0.1>' ) <EOL> self . instance2 . rating . add ( score = <NUM_LIT:1> , user = self . user , ip_address = '<STR_LIT:127.0.0.1>' ) <EOL> self . instance3 . rating . add ( score = <NUM_LIT:1> , user = self . user , ip_address = '<STR_LIT:127.0.0.1>' ) <EOL> self . instance4 . rating . add ( score = <NUM_LIT:1> , user = self . user , ip_address = '<STR_LIT:127.0.0.1>' ) <EOL> self . instance5 . rating . add ( score = <NUM_LIT:1> , user = self . user , ip_address = '<STR_LIT:127.0.0.1>' ) <EOL> self . instance . rating . add ( score = <NUM_LIT:1> , user = self . user2 , ip_address = '<STR_LIT>' ) <EOL> self . instance2 . rating . add ( score = <NUM_LIT:1> , user = self . user2 , ip_address = '<STR_LIT>' ) <EOL> self . instance3 . rating . add ( score = <NUM_LIT:1> , user = self . user2 , ip_address = '<STR_LIT>' ) <EOL> SimilarUser . objects . update_recommendations ( ) <EOL> self . assertEquals ( SimilarUser . objects . count ( ) , <NUM_LIT:2> ) <EOL> recs = list ( SimilarUser . objects . get_recommendations ( self . user2 , RatingTestModel ) ) <EOL> self . assertEquals ( len ( recs ) , <NUM_LIT:2> ) <EOL> self . instance4 . rating . add ( score = <NUM_LIT:1> , user = self . user2 , ip_address = '<STR_LIT>' ) <EOL> SimilarUser . objects . update_recommendations ( ) <EOL> self . assertEquals ( SimilarUser . objects . count ( ) , <NUM_LIT:2> ) <EOL> recs = list ( SimilarUser . objects . get_recommendations ( self . user2 , RatingTestModel ) ) <EOL> self . assertEquals ( len ( recs ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( recs , [ self . instance5 ] ) <EOL> self . instance5 . rating . add ( score = <NUM_LIT:1> , user = self . user2 , ip_address = '<STR_LIT>' ) <EOL> SimilarUser . objects . update_recommendations ( ) <EOL> self . assertEquals ( SimilarUser . objects . count ( ) , <NUM_LIT:2> ) <EOL> recs = list ( SimilarUser . objects . get_recommendations ( self . user2 , RatingTestModel ) ) <EOL> self . assertEquals ( len ( recs ) , <NUM_LIT:0> ) </s>
