Skip to content
Browse files

Added new DELETE builder tests.

  • Loading branch information...
1 parent 901a2cf commit 9d61eaad9fe844532db2c93afde2d1217a4031f6 @hiddentao committed Nov 2, 2012
Showing with 70 additions and 12 deletions.
  1. +69 −11 test/delete.test.coffee
  2. +1 −1 test/update.test.coffee
View
80 test/delete.test.coffee
@@ -29,20 +29,78 @@ squel = require "../src/squel"
-test['DELETE query builder'] =
- 'when the builder is initialized':
+test['DELETE builder'] =
+ beforeEach: ->
+ @inst = squel.delete()
+
+ 'instanceof JoinWhereOrderLimit': ->
+ assert.instanceOf @inst, squel.JoinWhereOrderLimit
+
+ 'default field values': ->
+ assert.same null, @inst.table
+
+ '>> from()':
beforeEach: ->
- @inst = squel.delete()
+ test.mocker.spy(@inst, '_sanitizeTable')
+
+ 'args: ()': ->
+ assert.throws (=> @inst.from()), 'table name must be a string'
+ assert.ok @inst._sanitizeTable.calledWithExactly(undefined)
+
+ 'args: (table)':
+ beforeEach: ->
+ @ret = @inst.from('table')
+
+ 'update internal state': ->
+ assert.same @ret, @inst
+ assert.same @inst.table, 'table'
+
+ assert.ok @inst._sanitizeTable.calledWithExactly('table')
+
+
+ 'build query':
+ beforeEach: ->
+ test.mocker.spy(@inst, '_joinString')
+ test.mocker.spy(@inst, '_whereString')
+ test.mocker.spy(@inst, '_orderString')
+ test.mocker.spy(@inst, '_limitString')
+
+ 'need to call from() first': ->
+ assert.throws (=> @inst.toString()), 'from() needs to be called'
+
+ '>> from(table)':
+ beforeEach: -> @inst.from('table')
+ toString: ->
+ assert.same @inst.toString(), 'DELETE FROM table'
+ assert.ok @inst._joinString.calledOnce
+ assert.ok @inst._whereString.calledOnce
+ assert.ok @inst._orderString.calledOnce
+ assert.ok @inst._limitString.calledOnce
+
+ '>> table(table2)':
+ beforeEach: -> @inst.from('table2')
+ toString: ->
+ assert.same @inst.toString(), 'DELETE FROM table2'
+
+ '>> where(a = 1)':
+ beforeEach: -> @inst.where('a = 1')
+ toString: ->
+ assert.same @inst.toString(), 'DELETE FROM table2 WHERE (a = 1)'
+
+ '>> join(other_table)':
+ beforeEach: -> @inst.join('other_table')
+ toString: ->
+ assert.same @inst.toString(), 'DELETE FROM table2 INNER JOIN other_table WHERE (a = 1)'
- 'toString() throws an error': ->
- test.mocker.spy(@inst, 'toString')
+ '>> order(a, true)':
+ beforeEach: -> @inst.order('a', true)
+ toString: ->
+ assert.same @inst.toString(), 'DELETE FROM table2 INNER JOIN other_table WHERE (a = 1) ORDER BY a ASC'
- try
- @inst.toString()
- catch err
- assert.same err.toString(), 'Error: from() needs to be called'
- finally
- assert.ok @inst.toString.threw()
+ '>> limit(2)':
+ beforeEach: -> @inst.limit(2)
+ toString: ->
+ assert.same @inst.toString(), 'DELETE FROM table2 INNER JOIN other_table WHERE (a = 1) ORDER BY a ASC LIMIT 2'
View
2 test/update.test.coffee
@@ -152,7 +152,7 @@ test['UPDATE builder'] =
@inst.table('table')
assert.throws (=> @inst.toString()), 'set() needs to be called'
- '>> from(table, t1).set(field, 1)':
+ '>> table(table, t1).set(field, 1)':
beforeEach: -> @inst.table('table', 't1').set('field', 1)
toString: ->
assert.same @inst.toString(), 'UPDATE table AS `t1` SET field = 1'

0 comments on commit 9d61eaa

Please sign in to comment.
Something went wrong with that request. Please try again.