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

Commit

Permalink
Added INSERT builder tests.
Browse files Browse the repository at this point in the history
Fixed Base Query builders tests.
  • Loading branch information
hiddentao committed Nov 2, 2012
1 parent 9d61eaa commit 5050a42
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 42 deletions.
6 changes: 2 additions & 4 deletions docs/squel.html
Expand Up @@ -312,11 +312,9 @@

<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">Insert</span> <span class="k">extends</span> <span class="nx">QueryBuilder</span>
<span class="nv">table: </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-78"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-78">&#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">Insert</span> <span class="k">extends</span> <span class="nx">QueryBuilder</span></pre></div> </td> </tr> <tr id="section-78"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-78">&#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">@table = </span><span class="kc">null</span>
<span class="vi">@fields = </span><span class="p">{}</span>
<span class="vi">@options = </span><span class="nx">_extend</span> <span class="p">{},</span> <span class="nx">DefaultInsertBuilderOptions</span><span class="p">,</span> <span class="nx">options</span></pre></div> </td> </tr> <tr id="section-79"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-79">&#182;</a> </div> <p>The table to insert into.
This will override any previously set value.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">into: </span><span class="p">(</span><span class="nx">table</span><span class="p">)</span> <span class="o">=&gt;</span>
Expand Down
7 changes: 1 addition & 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.

5 changes: 1 addition & 4 deletions src/squel.coffee
Expand Up @@ -578,13 +578,10 @@ class Delete extends JoinWhereOrderLimit
#
# All the build methods in this object return the object instance for chained method calling purposes.
class Insert extends QueryBuilder
table: null
fields: null
options: null

# options: see DefaultBuilderOptions
constructor: (options) ->
super
@table = null
@fields = {}
@options = _extend {}, DefaultInsertBuilderOptions, options

Expand Down
30 changes: 15 additions & 15 deletions test/BaseQueryBuilders.test.coffee
Expand Up @@ -322,42 +322,42 @@ test['WhereOrderLimit base class'] =
assert.same 2, @inst.limits


'>> whereString()':
'>> _whereString()':
'no clauses': ->
@inst.wheres = []
assert.same @inst.whereString(), ""
assert.same @inst._whereString(), ""

'1 clause': ->
@inst.wheres = ['a']
assert.same @inst.whereString(), " WHERE (a)"
assert.same @inst._whereString(), " WHERE (a)"

'>1 clauses': ->
@inst.wheres = ['a', 'b']
assert.same @inst.whereString(), " WHERE (a) AND (b)"
assert.same @inst._whereString(), " WHERE (a) AND (b)"


'>> orderString()':
'>> _orderString()':
'no clauses': ->
@inst.orders = []
assert.same @inst.orderString(), ""
assert.same @inst._orderString(), ""

'1 clause': ->
@inst.orders = [{ field: 'a', dir: 'ASC' }]
assert.same @inst.orderString(), " ORDER BY a ASC"
assert.same @inst._orderString(), " ORDER BY a ASC"

'>1 clauses': ->
@inst.orders = [{ field: 'a', dir: 'ASC' }, { field: 'b', dir: 'DESC' }]
assert.same @inst.orderString(), " ORDER BY a ASC, b DESC"
assert.same @inst._orderString(), " ORDER BY a ASC, b DESC"


'>> limitString()':
'>> _limitString()':
'not set': ->
@inst.limits = null
assert.same @inst.limitString(), ""
assert.same @inst._limitString(), ""

'set': ->
@inst.limits = 2
assert.same @inst.limitString(), " LIMIT 2"
assert.same @inst._limitString(), " LIMIT 2"



Expand Down Expand Up @@ -507,21 +507,21 @@ test['JoinWhereOrderLimit base class'] =
assert.ok @inst.join.calledWithExactly('table', 'alias', 'condition', 'OUTER')


'>> joinString()':
'>> _joinString()':
beforeEach: -> @inst.joins = []

'no joins': ->
assert.same @inst.joinString(), ""
assert.same @inst._joinString(), ""

'1 join': ->
@inst.left_join('table')
assert.same @inst.joinString(), " LEFT JOIN table"
assert.same @inst._joinString(), " LEFT JOIN table"

'>1 joins': ->
@inst.left_join('table')
@inst.right_join('table2', 'a2')
@inst.join('table3', null, 'c3')
assert.same @inst.joinString(), " LEFT JOIN table RIGHT JOIN table2 `a2` INNER JOIN table3 ON (c3)"
assert.same @inst._joinString(), " LEFT JOIN table RIGHT JOIN table2 `a2` INNER JOIN table3 ON (c3)"



Expand Down
153 changes: 153 additions & 0 deletions test/insert.test.coffee
@@ -0,0 +1,153 @@
###
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['INSERT builder'] =
beforeEach: ->
@inst = squel.insert()

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

'default field values': ->
assert.same null, @inst.table
assert.same {}, @inst.fields
assert.same { usingValuePlaceholders: false }, @inst.options

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

assert.same null, @inst.table
assert.same {}, @inst.fields
assert.same { usingValuePlaceholders: true, dummy: true }, @inst.options


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

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

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

'update internal state': ->
assert.same @ret, @inst
assert.same @inst.table, 'table'

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

'>> args(table2)': ->
assert.same @inst.into('table2'), @inst
assert.same @inst.table, 'table2'


'>> 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':
'need to call into() first': ->
assert.throws (=> @inst.toString()), 'into() needs to be called'

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

'>> into(table).set(field, 1)':
beforeEach: -> @inst.into('table').set('field', 1)
toString: ->
assert.same @inst.toString(), 'INSERT INTO table (field) VALUES (1)'

'>> set(field2, 1.2)':
beforeEach: -> @inst.set('field2', 1.2)
toString: ->
assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, 1.2)'

'>> set(field2, "str")':
beforeEach: -> @inst.set('field2', 'str')
toString: ->
assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, \'str\')'

'and when using value placeholders': ->
@inst.options.usingValuePlaceholders = true
@inst.set('field2', 'str')
assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, str)'

'>> set(field2, true)':
beforeEach: -> @inst.set('field2', true)
toString: ->
assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, TRUE)'

'>> set(field2, null)':
beforeEach: -> @inst.set('field2', null)
toString: ->
assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, NULL)'



module?.exports[require('path').basename(__filename)] = test
49 changes: 37 additions & 12 deletions test/update.test.coffee
Expand Up @@ -160,25 +160,50 @@ test['UPDATE builder'] =
assert.ok @inst._orderString.calledOnce
assert.ok @inst._limitString.calledOnce

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

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

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

'and when using value placeholders': ->
@inst.options.usingValuePlaceholders = true
@inst.set('field2', 'str')
assert.same @inst.toString(), 'UPDATE table AS `t1` SET field = 1, field2 = str'

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

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

'>> order(a, true)':
beforeEach: -> @inst.order('a', true)
'>> 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 BY a ASC'
assert.same @inst.toString(), 'UPDATE table AS `t1`, table2 SET field = 1, field2 = NULL WHERE (a = 1)'

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



Expand Down

0 comments on commit 5050a42

Please sign in to comment.