DatabaseTransaction / QueryBuilder spec fixes

This commit is contained in:
Ben Gotow 2016-01-25 11:11:57 -08:00
parent 9fc11512ad
commit a2c901559e
4 changed files with 21 additions and 21 deletions

View file

@ -20,7 +20,7 @@ CategoryPicker = require '../lib/category-picker'
{Categories} = require 'nylas-observables'
fdescribe 'CategoryPicker', ->
describe 'CategoryPicker', ->
beforeEach ->
CategoryStore._categoryCache = {}

View file

@ -1,4 +1,4 @@
Label = require '../../src/flux/models/label'
Category = require '../../src/flux/models/category'
Model = require '../../src/flux/models/model'
Attributes = require '../../src/flux/attributes'
@ -66,10 +66,10 @@ TestModel.configureWithCollectionAttribute = ->
queryable: true
modelKey: 'serverId'
jsonKey: 'server_id'
'labels': Attributes.Collection
'categories': Attributes.Collection
queryable: true
modelKey: 'labels'
itemClass: Label
modelKey: 'categories'
itemClass: Category
TestModel.configureWithJoinedDataAttribute = ->

View file

@ -31,9 +31,9 @@ describe "DatabaseSetupQueryBuilder", ->
expected = [
'CREATE TABLE IF NOT EXISTS `TestModel` (id TEXT PRIMARY KEY,data BLOB,client_id TEXT,server_id TEXT)',
'CREATE UNIQUE INDEX IF NOT EXISTS `TestModel_id` ON `TestModel` (`id`)',
'CREATE TABLE IF NOT EXISTS `TestModel-Label` (id TEXT KEY, `value` TEXT)'
'CREATE INDEX IF NOT EXISTS `TestModel_Label_id` ON `TestModel-Label` (`id` ASC)'
'CREATE UNIQUE INDEX IF NOT EXISTS `TestModel_Label_val_id` ON `TestModel-Label` (`value` ASC, `id` ASC)',
'CREATE TABLE IF NOT EXISTS `TestModel-Category` (id TEXT KEY, `value` TEXT)'
'CREATE INDEX IF NOT EXISTS `TestModel_Category_id` ON `TestModel-Category` (`id` ASC)'
'CREATE UNIQUE INDEX IF NOT EXISTS `TestModel_Category_val_id` ON `TestModel-Category` (`value` ASC, `id` ASC)',
]
for query,i in queries
expect(query).toBe(expected[i])

View file

@ -1,6 +1,6 @@
_ = require 'underscore'
Label = require '../../src/flux/models/label'
Category = require '../../src/flux/models/category'
Thread = require '../../src/flux/models/thread'
TestModel = require '../fixtures/db-test-model'
ModelQuery = require '../../src/flux/models/query'
@ -59,7 +59,7 @@ describe "DatabaseTransaction", ->
it "should throw an exception if the models are not the same class,\
since it cannot be specified by the trigger payload", ->
expect(=> @transaction.persistModels([testModelInstanceA, new Label()])).toThrow()
expect(=> @transaction.persistModels([testModelInstanceA, new Category()])).toThrow()
it "should throw an exception if the models are not a subclass of Model", ->
expect(=> @transaction.persistModels([{id: 'asd', subject: 'bla'}])).toThrow()
@ -174,7 +174,7 @@ describe "DatabaseTransaction", ->
.then =>
expect(@performed.length).toBe(4)
expect(@performed[0].query).toBe("BEGIN IMMEDIATE TRANSACTION")
expect(@performed[2].query).toBe("DELETE FROM `TestModel-Label` WHERE `id` = ?")
expect(@performed[2].query).toBe("DELETE FROM `TestModel-Category` WHERE `id` = ?")
expect(@performed[2].values[0]).toBe('1234')
expect(@performed[3].query).toBe("COMMIT")
@ -229,49 +229,49 @@ describe "DatabaseTransaction", ->
beforeEach ->
TestModel.configureWithCollectionAttribute()
@m = new TestModel(id: 'local-6806434c-b0cd')
@m.labels = [new Label(id: 'a'),new Label(id: 'b')]
@m.categories = [new Category(id: 'a'),new Category(id: 'b')]
@transaction._writeModels([@m])
it "should delete all association records for the model from join tables", ->
expect(@performed[1].query).toBe('DELETE FROM `TestModel-Label` WHERE `id` IN (\'local-6806434c-b0cd\')')
expect(@performed[1].query).toBe('DELETE FROM `TestModel-Category` WHERE `id` IN (\'local-6806434c-b0cd\')')
it "should insert new association records into join tables in a single query", ->
expect(@performed[2].query).toBe('INSERT OR IGNORE INTO `TestModel-Label` (`id`, `value`) VALUES (?,?),(?,?)')
expect(@performed[2].query).toBe('INSERT OR IGNORE INTO `TestModel-Category` (`id`, `value`) VALUES (?,?),(?,?)')
expect(@performed[2].values).toEqual(['local-6806434c-b0cd', 'a','local-6806434c-b0cd', 'b'])
describe "model collection attributes query building", ->
beforeEach ->
TestModel.configureWithCollectionAttribute()
@m = new TestModel(id: 'local-6806434c-b0cd')
@m.labels = []
@m.categories = []
it "should page association records into multiple queries correctly", ->
@m.labels.push(new Label(id: "id-#{i}")) for i in [0..199]
@m.categories.push(new Category(id: "id-#{i}")) for i in [0..199]
@transaction._writeModels([@m])
collectionAttributeQueries = _.filter @performed, (i) ->
i.query.indexOf('INSERT OR IGNORE INTO `TestModel-Label`') == 0
i.query.indexOf('INSERT OR IGNORE INTO `TestModel-Category`') == 0
expect(collectionAttributeQueries.length).toBe(1)
expect(collectionAttributeQueries[0].values[399]).toEqual('id-199')
it "should page association records into multiple queries correctly", ->
@m.labels.push(new Label(id: "id-#{i}")) for i in [0..200]
@m.categories.push(new Category(id: "id-#{i}")) for i in [0..200]
@transaction._writeModels([@m])
collectionAttributeQueries = _.filter @performed, (i) ->
i.query.indexOf('INSERT OR IGNORE INTO `TestModel-Label`') == 0
i.query.indexOf('INSERT OR IGNORE INTO `TestModel-Category`') == 0
expect(collectionAttributeQueries.length).toBe(2)
expect(collectionAttributeQueries[0].values[399]).toEqual('id-199')
expect(collectionAttributeQueries[1].values[1]).toEqual('id-200')
it "should page association records into multiple queries correctly", ->
@m.labels.push(new Label(id: "id-#{i}")) for i in [0..201]
@m.categories.push(new Category(id: "id-#{i}")) for i in [0..201]
@transaction._writeModels([@m])
collectionAttributeQueries = _.filter @performed, (i) ->
i.query.indexOf('INSERT OR IGNORE INTO `TestModel-Label`') == 0
i.query.indexOf('INSERT OR IGNORE INTO `TestModel-Category`') == 0
expect(collectionAttributeQueries.length).toBe(2)
expect(collectionAttributeQueries[0].values[399]).toEqual('id-199')