Skip to content

Instantly share code, notes, and snippets.

@iampeterbanjo
Created November 13, 2013 11:10
Show Gist options
  • Save iampeterbanjo/7447346 to your computer and use it in GitHub Desktop.
Save iampeterbanjo/7447346 to your computer and use it in GitHub Desktop.
Errors testing Backbone.Collection with TiTouchDB
# Test results
# task model:
# - has working migration for development (FAILED)
# - Expected 0 to equal 6.
# - can set and save a new task (FAILED)
# - Expected 0 to equal 7.
# - can mark a task as complete (ok)
# - can fetch complete tasks (FAILED)
# - Error: Assertion failed: Cannot reindex view 'completed' which has no map block set
# - can remove completed tasks (FAILED)
# - Expected 6 to equal 0.
# - Error: Assertion failed: Cannot reindex view 'completed' which has no map block set
# - can reset all tasks (FAILED)
# - Expected 0 to equal 6.
# - saves reset tasks (FAILED)
# - Expected 0 to equal 6.
# - Expected 0 to equal 1.
# app/models/task.coffee
Alloy = require('alloy')
definition =
config:
adapter:
type: "titouchdb"
modelname: "task"
collection_name: "task"
dbname: "#{Alloy.CFG.db_name}"
views: [
{ name: "default", map: (doc) -> emit(doc.id, null) if doc.id }
{ name: "completed", map: (doc) -> emit(doc.id, null) if doc.complete? is "1" }
]
view_options:
prefetch: true
extendModel: (Model) ->
_.extend(Model.prototype,
# extended functions and properties go here
isComplete: ->
return this.get('complete') is 1
markAsComplete: ->
this.set('complete', 1)
)
return Model
extendCollection: (Collection) ->
_.extend(Collection.prototype,
map_row: (Model, row) ->
result = new Model(row.document.properties)
fetchComplete: ->
this.fetch({ view: "completed" })
return
removeComplete: ->
return this.remove(this.fetchComplete())
)
return Collection
exports.definition = definition
# app/lib/spec/task_spec.coffee
require("/tijasmine/tijasmine").infect this
describe "task model", ->
Alloy = require("alloy")
data =
taskId: 77
building: "Wickmere Happy Centre"
type: "Personal"
address: "Happy Headquarter Donnington Isle of Wight RG35 7QB"
testData = [
{
id: 1
building: "Wickmere Happy Centre"
type: "Business"
address: "Happy Headquarter \n Donnington Isle of Wight \n RG35 7QB"
}
, {
id: 2
building: "Henley pool"
type: "Trap"
address: "The Priory \n Doirlinn Head Road \n Alberbury Cade \n B25 5DT"
}
, {
id: 3
building: "Henley pool"
type: "Trap"
address: "The Priory \n Doirlinn Head Road \n Alberbury \n Cade \n B25 5DT"
}
, {
id: 4
building: "Cade Castle"
type: "Journey"
address: "Happy Headquarter \n Donnington Isle of Wight \n RG35 7QB"
}
, {
id: 5
building: "Cade Castle"
type: "Home"
address: "Happy Headquarter \n Donnington Isle of Wight \n RG35 7QB"
}
, {
id: 6
building: "Wickmere Happy Centre"
type: "Travel"
address: "Happy Headquarter \n Donnington Isle of Wight \n RG35 7QB"
}
]
collection = null
item = null
view = view: "default", add: true
model = Alloy.createModel "task"
testTasks = Alloy.createCollection "task"
addTask = (t) ->
testTasks.add new Alloy.createModel "task", t
# add test data to a collection to use for tests
addTask t for t in testData
beforeEach ->
collection = Alloy.createCollection "task", testTasks
#collection.reset testTasks
item = Alloy.createModel "task"
# collection.reset testData
# collection.fetch view
it "has working migration for development", ->
collection.fetch view
expect(collection.length).toEqual 6
it "can set and save a new task", ->
item.set data
item.save()
testTasks.fetch view
expect(testTasks.length).toEqual 7
it "can mark a task as complete", ->
item.set data
item.save()
expect(item.isComplete()).toBeFalsy()
# mark the item as read
item.markAsComplete()
expect(item.isComplete()).toBeTruthy()
it "can fetch complete tasks", ->
item.set data
item.save()
collection.fetchComplete()
expect(0).toEqual collection.length
item.markAsComplete()
item.save()
collection.fetchComplete()
expect(1).toEqual collection.length
it "can remove completed tasks", ->
# we have 6 items
collection.fetch view
expect(6).toEqual collection.length
# there are no completed items
collection.fetchComplete()
expect(0).toEqual collection.length
item.set data
item.save()
item.markAsComplete()
item.save()
# we have 7 items 1 of which is complete
collection.fetch view
expect(7).toEqual collection.length
collection.removeComplete()
# after removing the complete item we should have 6 left
collection.fetch view
expect(6).toEqual collection.length
it "can reset all tasks", ->
collection.fetch view
expect(collection.length).toEqual 6
collection.reset data
expect(collection.length).toEqual 1
it "saves reset tasks", ->
collection.fetch view
expect(collection.length).toEqual 6
collection.reset data
collection.fetch view
expect(collection.length).toEqual 1
afterEach ->
item.destroy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment