Skip to content
This repository has been archived by the owner on Jun 5, 2019. It is now read-only.

Commit

Permalink
Wrote test for slug renaming in Buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Vereecken committed Dec 18, 2014
1 parent ff38a15 commit c90f657
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
9 changes: 5 additions & 4 deletions server/models/bucket.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fieldSchema = new mongoose.Schema
settings: {}
,
toJSON:
getters: true
getters: yes

fieldSchema
.path 'slug'
Expand Down Expand Up @@ -52,15 +52,16 @@ fieldSchema.post 'init', ->
@set 'slug.old', @get 'slug'

fieldSchema.post 'save', ->
if @get('slug.old') isnt @get('slug')
# There is a very annoying case where slug.old doesn't get filled in properly
if @get('slug.old') and @get('slug.old') isnt @get('slug')
oldPath = "content.#{@get 'slug.old'}"
newPath = "content.#{@get 'slug'}"
q = {}
q[oldPath] = $exists: yes
u = {$rename: {}}
u = $rename: {}
u.$rename[oldPath] = newPath

# TODO will probably have to change due to #168
# careful with this, updates circumvent middleware
mongoose.model('Entry').update q, u, {}, (err) ->
winston.error err if err?

Expand Down
56 changes: 56 additions & 0 deletions test/server/models/bucket.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,62 @@ describe 'Model#Bucket', ->
expect(bucket.singular).to.equal 'Article'
done()

describe 'Update', ->
Entry = require '../../../server/models/entry'

before reset.db

user = null
before (done) ->
User.create {name: 'Bucketer', email: '[email protected]', password: 'S3cr3ts'}, (err, u) ->
expect(err).to.not.exist
user = u
done()

bucket = null
beforeEach (done) ->
bucket = new Bucket
name: 'Articles'
slug: 'articles'
#fields: [
# fieldType: 'markdown'
# slug: 'body'
# name: 'body'
#]
bucket.fields.push
fieldType: 'markdown'
slug: 'body'
name: 'body'
bucket.save (err, b) ->
expect(err).to.not.exist
done()

afterEach (done) -> Bucket.remove {}, -> done()

it 'updates entry fields when slug changes', (done) ->
Entry.create
title: 'Some Entry'
bucket: bucket._id
author: user._id
content: body: 'Bodyslam'
, (err, entry) ->
expect(err).to.not.exist
expect(entry.get 'content.body').to.exist

Bucket.findById bucket._id, (err, bucket) ->
field = bucket.get('fields')[0]
field.set 'slug', 'new-body'

bucket.save (err, field) ->
expect(err).to.not.exist
Entry.find {bucket: bucket._id}, (err, entries) ->
expect(err).to.not.exist
expect(entries.length).to.not.equal 0
for entry in entries
expect(entry.get 'content.body').to.not.exist
expect(entry.get 'content.new-body').to.exist
done()

describe '#getMembers', ->
u = null
b = null
Expand Down
2 changes: 1 addition & 1 deletion test/server/models/entry.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ reset = require '../../reset'

{expect} = require 'chai'

describe 'Entry', ->
describe 'Model#Entry', ->

user = null

Expand Down

0 comments on commit c90f657

Please sign in to comment.