Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Commit

Permalink
Added new test for UPDATE query builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddentao committed Nov 2, 2012
1 parent d5d7203 commit 901a2cf
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 17 deletions.
5 changes: 1 addition & 4 deletions docs/squel.html
Expand Up @@ -261,10 +261,7 @@

<p>Note that the query builder does not check the final query string for correctness.</p>

<p>All the build methods in this object return the object instance for chained method calling purposes.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">Update</span> <span class="k">extends</span> <span class="nx">WhereOrderLimit</span>
<span class="nv">tables: </span><span class="kc">null</span>
<span class="nv">fields: </span><span class="kc">null</span>
<span class="nv">options: </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">&#182;</a> </div> <p>options: see DefaultBuilderOptions</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<p>All the build methods in this object return the object instance for chained method calling purposes.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">Update</span> <span class="k">extends</span> <span class="nx">WhereOrderLimit</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">&#182;</a> </div> <p>options: see DefaultBuilderOptions</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">constructor: </span><span class="nf">(options) -&gt;</span>
<span class="k">super</span>
<span class="vi">@tables = </span><span class="p">[]</span>
<span class="vi">@fields = </span><span class="p">{}</span>
Expand Down
6 changes: 0 additions & 6 deletions squel.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion squel.min.js

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions src/squel.coffee
Expand Up @@ -466,10 +466,6 @@ class Select extends JoinWhereOrderLimit
#
# All the build methods in this object return the object instance for chained method calling purposes.
class Update extends WhereOrderLimit
tables: null
fields: null
options: null

# options: see DefaultBuilderOptions
constructor: (options) ->
super
Expand Down
2 changes: 0 additions & 2 deletions test/select.test.coffee
Expand Up @@ -207,8 +207,6 @@ test['SELECT builder'] =

'build query':
beforeEach: ->
@_joinString = @_whereString = @_orderString = @_limitString = ''

test.mocker.spy(@inst, '_joinString')
test.mocker.spy(@inst, '_whereString')
test.mocker.spy(@inst, '_orderString')
Expand Down
185 changes: 185 additions & 0 deletions test/update.test.coffee
@@ -0,0 +1,185 @@
###
Copyright (c) 2012 Ramesh Nair (hiddentao.com)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
###


{test, assert, expect, should} = require './testbase'
squel = require "../src/squel"



test['UPDATE builder'] =
beforeEach: ->
@inst = squel.update()

'instanceof WhereOrderLimit': ->
assert.instanceOf @inst, squel.WhereOrderLimit

'default field values': ->
assert.same [], @inst.tables
assert.same {}, @inst.fields
assert.same { usingValuePlaceholders: false }, @inst.options

'constructor':
'override options': ->
@inst = squel.update
usingValuePlaceholders: true
dummy: true

assert.same [], @inst.tables
assert.same {}, @inst.fields
assert.same { usingValuePlaceholders: true, dummy: true }, @inst.options


'>> table()':
beforeEach: ->
test.mocker.spy(@inst, '_sanitizeTable')
test.mocker.spy(@inst, '_sanitizeAlias')

'args: ()': ->
assert.throws (=> @inst.table()), 'table name must be a string'
assert.ok @inst._sanitizeTable.calledWithExactly(undefined)

'args: (table)':
beforeEach: ->
@ret = @inst.table('table')

'update internal state': ->
assert.same @ret, @inst
assert.same @inst.tables, [
{
name: 'table'
alias: null
}
]

assert.ok @inst._sanitizeTable.calledWithExactly('table')
assert.ok @inst._sanitizeAlias.notCalled

'>> args(table2)': ->
assert.same @inst.table('table2'), @inst
assert.same @inst.tables, [
{
name: 'table'
alias: null
}
{
name: 'table2'
alias: null
}
]

'args: (table, alias)': ->
@inst.table('table', 'alias')

assert.same @inst.tables, [
{
name: 'table'
alias: 'alias'
}
]

assert.ok @inst._sanitizeTable.calledWithExactly('table')
assert.ok @inst._sanitizeAlias.calledWithExactly('alias')


'>> set()':
beforeEach: ->
test.mocker.spy(@inst, '_sanitizeField')
test.mocker.spy(@inst, '_sanitizeValue')

'args: ()': ->
assert.throws (=> @inst.set()), 'field name must be a string'
assert.ok @inst._sanitizeField.calledWithExactly(undefined)

'args: (field)': ->
assert.throws (=> @inst.set('field')), 'field value must be a string, number, boolean or null'
assert.ok @inst._sanitizeField.calledWithExactly('field')
assert.ok @inst._sanitizeValue.calledWithExactly(undefined)

'args: (field, null)':
beforeEach: ->
@ret = @inst.set('field', null)

'update internal state': ->
assert.same @ret, @inst
assert.same @inst.fields, { 'field': null }
assert.ok @inst._sanitizeField.calledWithExactly('field')
assert.ok @inst._sanitizeValue.calledWithExactly(null)

'>> args: (field, 1)':
beforeEach: ->
@ret = @inst.set('field', 1)

'update internal state': ->
assert.same @ret, @inst
assert.same @inst.fields, { 'field': 1 }
assert.ok @inst._sanitizeField.calledWithExactly('field')
assert.ok @inst._sanitizeValue.calledWithExactly(1)


'build query':
beforeEach: ->
test.mocker.spy(@inst, '_whereString')
test.mocker.spy(@inst, '_orderString')
test.mocker.spy(@inst, '_limitString')

'need to call table() first': ->
assert.throws (=> @inst.toString()), 'table() needs to be called'

'need to call set() first': ->
@inst.table('table')
assert.throws (=> @inst.toString()), 'set() needs to be called'

'>> from(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'
assert.ok @inst._whereString.calledOnce
assert.ok @inst._orderString.calledOnce
assert.ok @inst._limitString.calledOnce

'>> table(table2) >> set(field2, 2)':
beforeEach: -> @inst.table('table2').set('field2', 2)
toString: ->
assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = 2'

'>> where(a = 1)':
beforeEach: -> @inst.where('a = 1')
toString: ->
assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = 2 WHERE (a = 1)'

'>> order(a, true)':
beforeEach: -> @inst.order('a', true)
toString: ->
assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = 2 WHERE (a = 1) ORDER BY a ASC'

'>> limit(2)':
beforeEach: -> @inst.limit(2)
toString: ->
assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = 2 WHERE (a = 1) ORDER BY a ASC LIMIT 2'



module?.exports[require('path').basename(__filename)] = test

0 comments on commit 901a2cf

Please sign in to comment.