intermediate

This commit is contained in:
Ruben Vereecken 2014-06-13 18:04:25 +02:00
parent 63401025f6
commit 488d49e286
4 changed files with 30 additions and 30 deletions

View file

@ -1,12 +0,0 @@
// Fixtures
db.achievements.insert({
query: '{"level.original": "52d97ecd32362bc86e004e87"}',
index: true,
slug: 'dungeon-arena-started',
name: 'Dungeon Arena started',
worth: 1,
collection: 'level.session',
description: 'Started playing Dungeon Arena.',
userField: 'creator'
});

View file

@ -3,6 +3,7 @@ jsonschema = require('../../app/schemas/models/achievement')
log = require 'winston'
util = require '../../app/lib/utils'
plugins = require('../plugins/plugins')
AchievablePlugin = require '../plugins/achievements'
# `pre` and `post` are not called for update operations executed directly on the database,
# including `Model.update`,`.findByIdAndUpdate`,`.findOneAndUpdate`, `.findOneAndRemove`,and `.findByIdAndRemove`.order
@ -23,23 +24,22 @@ AchievementSchema.methods.objectifyQuery = ->
AchievementSchema.methods.stringifyQuery = ->
@set('query', JSON.stringify(@get('query'))) if typeof @get('query') != "string"
getExpFunction: ->
kind = @get('function')?.kind or jsonschema.function.default.kind
parameters = @get('function')?.parameters or jsonschema.function.default.parameters
return utils.functionCreators[kind](parameters) if kind of utils.functionCreators
AchievementSchema.methods.getExpFunction: ->
kind = @get('function')?.kind or jsonschema.function.default.kind
parameters = @get('function')?.parameters or jsonschema.function.default.parameters
return utils.functionCreators[kind](parameters) if kind of utils.functionCreators
AchievementSchema.post('init', (doc) -> doc.objectifyQuery())
AchievementSchema.post 'init', (doc) -> doc.objectifyQuery()
AchievementSchema.pre('save', (next) ->
AchievementSchema.pre 'save', (next) ->
@stringifyQuery()
next()
)
# Reload achievements upon save
AchievementSchema.post 'save', -> AchievablePlugin.loadAchievements()
AchievementSchema.plugin(plugins.NamedPlugin)
AchievementSchema.plugin(plugins.SearchablePlugin, {searchable: ['name']})
module.exports = Achievement = mongoose.model('Achievement', AchievementSchema)
# Reload achievements upon save
AchievablePlugin = require '../plugins/achievements'
AchievementSchema.post 'save', (doc) -> AchievablePlugin.loadAchievements()

View file

@ -10,9 +10,6 @@ module.exports = AchievablePlugin = (schema, options) ->
User = require '../users/User' # Avoid mutual inclusion cycles
Achievement = require('../achievements/Achievement')
checkForAchievement = (doc) ->
collectionName = doc.constructor.modelName
before = {}
schema.post 'init', (doc) ->
@ -84,6 +81,7 @@ module.exports = AchievablePlugin = (schema, options) ->
return
module.exports.loadAchievements = ->
log.debug 'Reloading all achievements'
achievements = {}
Achievement = require('../achievements/Achievement')
query = Achievement.find({})

View file

@ -51,6 +51,9 @@ describe 'Achievement', ->
request.post {uri: url, json: repeatable}, (err, res, body) ->
expect(res.statusCode).toBe(200)
repeatable._id = body._id
Achievement.find {}, (err, docs) ->
expect(docs.length).toBe(2)
done()
it 'can get all for ordinary users', (done) ->
@ -93,18 +96,29 @@ describe 'Achieving Achievements', ->
it 'allows users to unlock one-time Achievements', (done) ->
loginJoe (joe) ->
levelSession =
session = new LevelSession(
permissions: simplePermissions
creator: joe._id
level: original: 'dungeon-arena'
)
request.post {uri:getURL('/db/level.session'), json:levelSession}, (session) ->
session.save (err, doc) ->
expect(err).toBeNull()
expect(doc).toBeDefined()
expect(doc.creator).toBe(session.creator)
done()
EarnedAchievement.find {}, (err, docs) ->
expect(err).toBeNull()
console.log docs
expect(docs.length).toBe(1)
done()
xit 'cleaning up test: deleting all Achievements and relates', (done) ->
it 'cleaning up test: deleting all Achievements and relates', (done) ->
clearModels [Achievement, EarnedAchievement, LevelSession], (err) ->
expect(err).toBeNull()
done()