Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
add tests for achievements. achievements mostly functional
  • Loading branch information
dtraviglia committed Oct 13, 2015
1 parent 9d26f54 commit 3e371a031bd2a7c9dcb6fd6cba96c247a84961b4
Showing 3 changed files with 148 additions and 40 deletions.
@@ -1,45 +1,108 @@
from op_tasks.models import UserProfile, \
Product, Dataset, OpTask, TaskListItem, Experiment, \
UserAchievement, Achievement

def checkAchievements(request):
if tasksComplete(request):
user = request.user
userprofile = user.userprofile
userachievement = UserAchievement()
userachievement.userprofile = userprofile
userachievement.achievement = Achievement.objects.get(name='tasksComplete')
userachievement.save()


def tasksComplete(request):
# if request.method == 'POST':
# user = request.user
# userprofile = user.userprofile
return True


def genTaskAccuracy(request):
return 10


def devTaskAccuracy(request):
return 10
from django.core.exceptions import ObjectDoesNotExist
from op_tasks.models import UserAchievement, Achievement


def referralsOne(request):
return True


def referralsTwo(request):
return True

def checkAchievements(request):
user = request.user

def referralsThree(request):
return True
tasksComplete(user)
genTaskAccuracy(user)
devTaskAccuracy(user)
genTaskAccuracy(user)
referralsOne(user)
referralsTwo(user)
referralsThree(user)
referralsFour(user)


def referralsFour(request):
def tasksComplete(user):
return True


def genTaskAccuracy(user):
award = False
userprofile = user.userprofile
genAccuracyAchivement = Achievement.objects.get(name='genTaskAccuracy')

try:
userAchievement = UserAchievement.objects.get(userprofile=userprofile, achievement=genAccuracyAchivement)
except ObjectDoesNotExist:
if userprofile.bestDevAccuracy > 89:
userAchievement = UserAchievement(userprofile=userprofile, achievement=genAccuracyAchivement)
userAchievement.save()
award=True
return award


def devTaskAccuracy(user):
award = False
userprofile = user.userprofile
devAccuracyAchivement = Achievement.objects.get(name='devTaskAccuracy')

try:
userAchievement = UserAchievement.objects.get(userprofile=userprofile, achievement=devAccuracyAchivement)
except ObjectDoesNotExist:
if userprofile.bestDevAccuracy > 89:
userAchievement = UserAchievement(userprofile=userprofile, achievement=devAccuracyAchivement)
userAchievement.save()
award=True
return award


def referralsOne(user):
"""
checks referrals in userprofile to see if a certain level is met
:param user: the user object to check
:return: boolean, true if condition met, false otherwise
"""
return referralsCheck(user, 3, 'referralsOne')


def referralsTwo(user):
"""
checks referrals in userprofile to see if level two is met
:param user: the user object to check
:return: award - boolean, true if condition met, false otherwise
"""
return referralsCheck(user, 6, 'referralsTwo')


def referralsThree(user):
"""
:param user:
:return:
"""
return referralsCheck(user, 9, 'referralsThree')


def referralsFour(user):
"""
:param user:
:return:
"""
return referralsCheck(user, 12, 'referralsFour')


def referralsCheck(user, count, achievementName):
"""
general method to check different levels of referrals
:param user: the user object to check
:param count: the level to check
:param achievementName: the name of the achievement
:return: boolean - true if level met, false otherwise
"""
award = False
userprofile = user.userprofile
referralsAchievement = Achievement.objects.get(name=achievementName)

# first check if user already has achievement
try:
userAchievement = UserAchievement.objects.get(userprofile=userprofile, achievement=referralsAchievement)
except ObjectDoesNotExist:
if userprofile.referrals >= count :
userAchievement = UserAchievement(userprofile=userprofile, achievement=referralsAchievement)
userAchievement.save()
award = True
return award
@@ -78,6 +78,9 @@ class UserProfile(models.Model):
portal_inst_complete = models.BooleanField(default=False)
task_inst_complete = models.BooleanField(default=False)
experiment = models.ForeignKey(Experiment, null=True, blank=True)
referrals = models.IntegerField(default=0)
bestGenAccuracy = models.IntegerField(default=0)
bestDevAccuracy = models.IntegerField(default=0)

def __unicode__(self):
return self.user.username
@@ -1,6 +1,7 @@
from django.test import TestCase
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password
from django.core.exceptions import ObjectDoesNotExist

from op_tasks.models import Dataset, \
Product, OpTask, UserProfile, TaskListItem, Experiment, Achievement, UserAchievement
@@ -52,6 +53,7 @@ def test_saving_and_retrieving_product(self):
dataset = Dataset()
dataset.version = '1'
dataset.name = 'test'
dataset.save()

product = Product()
product.dataset = dataset
@@ -238,6 +240,9 @@ def test_can_assign_user_achievements(self):
achievement = Achievement(name='One')
achievement.save()

achievement2 = Achievement(name='Two')
achievement2.save()

userachievement = UserAchievement()
userachievement.achievement = achievement
userachievement.userprofile = userprofile
@@ -249,7 +254,7 @@ def test_can_assign_user_achievements(self):
userachievementTwo.save()

saved_achievements = Achievement.objects.all()
self.assertEqual(saved_achievements.count(), 1)
self.assertEqual(saved_achievements.count(), 2)

saved_userachievements = UserAchievement.objects.all()
self.assertEqual(saved_userachievements.count(), 2)
@@ -258,6 +263,43 @@ def test_can_assign_user_achievements(self):
if saved_userachievement.userprofile.user.username == 'Paul':
self.assertEqual(saved_userachievement.achievement.name, 'One')

try:
johnAchievement = UserAchievement.objects.get(userprofile=userprofile, achievement=achievement2)
self.assertEqual(johnAchievement.userprofile, userprofile)

except ObjectDoesNotExist:
print 'object does not exist'
johnAchievements = UserAchievement.objects.get(userprofile=userprofile)
print johnAchievements


def test_can_check_achievement_conditions(self):
user = User(username='John', password=make_password('John'))
user.save()
userprofile = UserProfile(user=user)
userprofile.referrals = 3
userprofile.save()

achievement1 = Achievement(name='referralsOne')
achievement1.save()
achievement2 = Achievement(name='referralsTwo')
achievement2.save()
achievement3 = Achievement(name='referralsThree')
achievement3.save()

self.assertEqual(achievements.referralsOne(user), True)

userprofile.referrals = 6
userprofile.save()

self.assertEqual(achievements.referralsTwo(user), True)

userprofile.referrals = 9
userprofile.save()

self.assertEqual(achievements.referralsThree(user), True)

johnsAchievements = UserAchievement.objects.filter(userprofile=userprofile)
self.assertEqual(3, johnsAchievements.count())


def test_can_check_achievements(self):
self.assertEqual(True, True)

0 comments on commit 3e371a0

Please sign in to comment.