Permalink
Browse files

Started writing tests for WhereOrderLimit builder base class.

  • Loading branch information...
1 parent 364b141 commit 550286539ea791b7200eba4df9807d029622e5b2 @hiddentao committed Nov 2, 2012
View
@@ -119,14 +119,11 @@
<span class="k">if</span> <span class="o">not</span> <span class="nx">options</span> <span class="o">or</span> <span class="kc">false</span> <span class="o">is</span> <span class="nx">options</span><span class="p">.</span><span class="nx">usingValuePlaceholders</span>
<span class="nv">value = </span><span class="s">&quot;&#39;</span><span class="si">#{</span><span class="nx">value</span><span class="si">}</span><span class="s">&#39;&quot;</span>
<span class="nx">value</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">&#182;</a> </div> <p>Base class for query builders which support WHERE, ORDER and LIMIT clauses.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">WhereOrderLimit</span> <span class="k">extends</span> <span class="nx">QueryBuilder</span>
- <span class="nv">wheres: </span><span class="kc">null</span>
- <span class="nv">orders: </span><span class="kc">null</span>
- <span class="nv">limits: </span><span class="kc">null</span>
-
<span class="nv">constructor: </span><span class="o">-&gt;</span>
<span class="k">super</span>
<span class="vi">@wheres = </span><span class="p">[]</span>
- <span class="vi">@orders = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">&#182;</a> </div> <p>Add a WHERE condition.</p>
+ <span class="vi">@orders = </span><span class="p">[]</span>
+ <span class="vi">@limits = </span><span class="kc">null</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">&#182;</a> </div> <p>Add a WHERE condition.</p>
<p>When the final query is constructed all the WHERE conditions are combined using the intersection (AND) operator.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">where: </span><span class="p">(</span><span class="nx">condition</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="nv">condition = </span><span class="nx">@_sanitizeCondition</span><span class="p">(</span><span class="nx">condition</span><span class="p">)</span>
@@ -358,6 +355,8 @@
<span class="k">delete</span><span class="o">:</span> <span class="o">-&gt;</span> <span class="k">new</span> <span class="nx">Delete</span>
<span class="nx">Expression</span>
<span class="nx">QueryBuilder</span>
+ <span class="nx">WhereOrderLimit</span>
+ <span class="nx">JoinWhereOrderLimit</span>
<span class="nx">Select</span>
<span class="nx">Update</span>
<span class="nx">Insert</span>
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -212,14 +212,11 @@ class QueryBuilder
# Base class for query builders which support WHERE, ORDER and LIMIT clauses.
class WhereOrderLimit extends QueryBuilder
- wheres: null
- orders: null
- limits: null
-
constructor: ->
super
@wheres = []
@orders = []
+ @limits = null
# Add a WHERE condition.
@@ -650,6 +647,8 @@ _export = {
delete: -> new Delete
Expression
QueryBuilder
+ WhereOrderLimit
+ JoinWhereOrderLimit
Select
Update
Insert
@@ -29,7 +29,7 @@ squel = require "../src/squel"
-test['Query builder base class'] =
+test['QueryBuilder base class'] =
beforeEach: ->
@inst = new squel.QueryBuilder()
@@ -181,4 +181,95 @@ test['Query builder base class'] =
+test['WhereOrderLimit base class'] =
+ beforeEach: ->
+ @inst = new squel.WhereOrderLimit()
+
+ 'instanceof QueryBuilder': ->
+ assert.instanceOf @inst, squel.QueryBuilder
+
+ 'default field values': ->
+ assert.same [], @inst.wheres
+ assert.same [], @inst.orders
+ assert.same null, @inst.limits
+
+ '>> where()':
+ beforeEach: ->
+ test.mocker.spy(@inst, '_sanitizeCondition')
+
+ 'with empty string': ->
+ assert.same @inst, @inst.where("")
+
+ assert.ok @inst._sanitizeCondition.calledWithExactly ""
+ assert.same [], @inst.wheres
+
+ 'with Expression': ->
+ e = squel.expr().or('a = 5')
+
+ assert.same @inst, @inst.where(e)
+
+ assert.ok @inst._sanitizeCondition.calledWithExactly e
+ assert.same [e.toString()], @inst.wheres
+
+ 'with non-empty string':
+ beforeEach: ->
+ @ret = @inst.where("a")
+
+ 'updates internal state': ->
+ assert.same @ret, @inst
+ assert.ok @inst._sanitizeCondition.calledWithExactly "a"
+ assert.same ['a'], @inst.wheres
+
+ 'with non-empty string again':
+ beforeEach: ->
+ @ret = @inst.where("b")
+
+ 'adds to internal state': ->
+ assert.ok @inst._sanitizeCondition.calledWithExactly "b"
+ assert.same ['a', 'b'], @inst.wheres
+
+
+ '>> order()':
+ beforeEach: ->
+ test.mocker.spy(@inst, '_sanitizeField')
+
+ 'args empty': ->
+ assert.throws (=> @inst.order()), 'field name must be a string'
+
+ assert.ok @inst._sanitizeField.calledWithExactly undefined
+ assert.same [], @inst.orders
+
+ 'args (field)':
+ beforeEach: ->
+ @ret = @inst.order("field")
+
+ 'updates internal state': ->
+ assert.same @ret, @inst
+
+ assert.ok @inst._sanitizeField.calledWithExactly 'field'
+ assert.same [ { field: 'field', dir: 'ASC' } ], @inst.orders
+
+ 'args (field, true)':
+ beforeEach: ->
+ @ret = @inst.order("field", true)
+
+ 'updates internal state': ->
+ assert.same @ret, @inst
+
+ assert.ok @inst._sanitizeField.calledWithExactly 'field'
+ assert.same [ { field: 'field', dir: 'ASC' } ], @inst.orders
+
+ 'args (field2, false)':
+ beforeEach: ->
+ @ret = @inst.order("field2", false)
+
+ 'adds to internal state': ->
+ assert.same @ret, @inst
+
+ assert.ok @inst._sanitizeField.calledWithExactly 'field2'
+ assert.same [ { field: 'field', dir: 'ASC' }, { field: 'field2', dir: 'DESC' } ], @inst.orders
+
+
+
+
module?.exports[require('path').basename(__filename)] = test
File renamed without changes.
View
@@ -43,22 +43,6 @@ assert.same = (actual, expected, message) ->
assert.deepEqual(actual, expected, message)
-###
-Assert that given function calls throws given error
-
-@param fn
-@param errorMsg expected error message
-###
-assert.throws = (fn, errorMsg) ->
- errMsg = undefined
-
- try
- fn.call()
- catch err
- errMsg = err.toString()
- finally
- assert.same errMsg, "Error: #{errorMsg}"
-
test =

0 comments on commit 5502865

Please sign in to comment.