Worked on achievement tests

This commit is contained in:
Ruben Vereecken 2014-06-10 20:05:32 +02:00
parent 925594ffe9
commit fddca03417
3 changed files with 62 additions and 32 deletions

View file

@ -40,3 +40,7 @@ AchievementSchema.plugin(plugins.NamedPlugin)
AchievementSchema.plugin(plugins.SearchablePlugin, {searchable: ['name']}) AchievementSchema.plugin(plugins.SearchablePlugin, {searchable: ['name']})
module.exports = Achievement = mongoose.model('Achievement', AchievementSchema) module.exports = Achievement = mongoose.model('Achievement', AchievementSchema)
# Reload achievements upon save
AchievablePlugin = require '../plugins/achievements'
AchievementSchema.post 'save', (doc) -> AchievablePlugin.loadAchievements()

View file

@ -1,5 +1,4 @@
mongoose = require('mongoose') mongoose = require('mongoose')
Achievement = require('../achievements/Achievement')
EarnedAchievement = require '../achievements/EarnedAchievement' EarnedAchievement = require '../achievements/EarnedAchievement'
LocalMongo = require '../../app/lib/LocalMongo' LocalMongo = require '../../app/lib/LocalMongo'
util = require '../../app/lib/utils' util = require '../../app/lib/utils'
@ -7,18 +6,9 @@ log = require 'winston'
achievements = {} achievements = {}
loadAchievements = ->
achievements = {}
query = Achievement.find({})
query.exec (err, docs) ->
_.each docs, (achievement) ->
category = achievement.get 'collection'
achievements[category] = [] unless category of achievements
achievements[category].push achievement
loadAchievements()
module.exports = AchievablePlugin = (schema, options) -> module.exports = AchievablePlugin = (schema, options) ->
User = require '../users/User' User = require '../users/User' # Avoid mutual inclusion cycles
Achievement = require('../achievements/Achievement')
checkForAchievement = (doc) -> checkForAchievement = (doc) ->
collectionName = doc.constructor.modelName collectionName = doc.constructor.modelName
@ -92,3 +82,15 @@ module.exports = AchievablePlugin = (schema, options) ->
delete before[doc.id] unless isNew # This assumes everything we patch has a _id delete before[doc.id] unless isNew # This assumes everything we patch has a _id
return return
module.exports.loadAchievements = ->
achievements = {}
Achievement = require('../achievements/Achievement')
query = Achievement.find({})
query.exec (err, docs) ->
_.each docs, (achievement) ->
category = achievement.get 'collection'
achievements[category] = [] unless category of achievements
achievements[category].push achievement
AchievablePlugin.loadAchievements()

View file

@ -1,25 +1,29 @@
require '../common' require '../common'
unlockable =
name: 'Dungeon Arena Started'
description: 'Started playing Dungeon Arena.'
worth: 3
collection: 'level.session'
query: "{\"level.original\":\"dungeon-arena\"}"
userField: 'creator'
repeatable =
name: 'Simulated'
description: 'Simulated Games.'
worth: 1
collection: 'User'
query: "{\"simulatedBy\":{\"$gt\":\"0\"}}"
userField: '_id'
proportionalTo: 'simulatedBy'
url = getURL('/db/achievement')
describe 'Achievement', -> describe 'Achievement', ->
unlockable =
name: 'One Time Only'
description: 'So you did the really cool thing.'
worth: 6.66
collection: 'level.session'
repeatable =
name: 'Lots of em'
description: 'Oops you did it again.'
worth: 1
collection: 'User'
proportionalTo: '_id'
url = getURL('/db/achievement')
allowHeader = 'GET, POST, PUT, PATCH' allowHeader = 'GET, POST, PUT, PATCH'
it 'preparing test: deleting all Achievements first', (done) -> it 'preparing test: deleting all Achievements first', (done) ->
clearModels [Achievement], (err) -> clearModels [Achievement, EarnedAchievement, LevelSession, User], (err) ->
expect(err).toBeNull() expect(err).toBeNull()
done() done()
@ -43,13 +47,17 @@ describe 'Achievement', ->
request.post {uri: url, json: unlockable}, (err, res, body) -> request.post {uri: url, json: unlockable}, (err, res, body) ->
expect(res.statusCode).toBe(200) expect(res.statusCode).toBe(200)
unlockable._id = body._id unlockable._id = body._id
done()
request.post {uri: url, json: repeatable}, (err, res, body) ->
expect(res.statusCode).toBe(200)
repeatable._id = body._id
done()
it 'can get all for ordinary users', (done) -> it 'can get all for ordinary users', (done) ->
loginJoe -> loginJoe ->
request.get {uri: url, json: unlockable}, (err, res, body) -> request.get {uri: url, json: unlockable}, (err, res, body) ->
expect(res.statusCode).toBe(200) expect(res.statusCode).toBe(200)
expect(body.length).toBe(1) expect(body.length).toBe(2)
done() done()
it 'can be read by ordinary users', (done) -> it 'can be read by ordinary users', (done) ->
@ -80,7 +88,23 @@ describe 'Achievement', ->
expect(body.type).toBeDefined() expect(body.type).toBeDefined()
done() done()
it 'cleaning up test: deleting all Achievements', (done) ->
clearModels [Achievement], (err) -> describe 'Achieving Achievements', ->
it 'allows users to unlock one-time Achievements', (done) ->
loginJoe (joe) ->
levelSession =
creator: joe._id
level: original: 'dungeon-arena'
request.post {uri:getURL('/db/level.session'), json:levelSession}, (session) ->
done()
xit 'cleaning up test: deleting all Achievements and relates', (done) ->
clearModels [Achievement, EarnedAchievement, LevelSession], (err) ->
expect(err).toBeNull() expect(err).toBeNull()
done() done()