Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
documentdb-mock
Advanced tools
Mock for testing Stored Procedures in Microsoft Azure's DocumentDB
Copyright (c) 2015, Lawrence S. Maccherone, Jr.
Mock for testing Stored Procedures in Microsoft Azure's DocumentDB
Microsoft Azure's DocumentDB is a great PaaS NoSQL database. My absolute favorite feature is that you can write stored procedures in JavaScript (CoffeeScript in my case) but it's missing a mature way to test your stored procedures. Luckily, JavaScript runs just fine on node.js so your stored procedures will run there with mock data.
This package implements a thin mock for testing stored procedures.
ServerSideMock implements many of the methods in the DocumentDB's Collection class including:
ClientSideMock is fairly limited at this point. It only implements the executeNext(), hasMoreResults(), and toArray() methods for reads() or querys() calls.
npm install -save documentdb-mock
You can look at the code in the test and stored-procedure folders of documentdb-utils to see how to use ServerSideMock.
Basically:
exports
your function(s).mock = new ServerSideMock('path/to/stored/procedure')
mock.nextResources
, mock.nextError
, mock.nextOptions
, and/or mock.nextCollectionOperationQueued
to control
the response that your stored procedure will see to the next collection operation. Note, nextCollectionOperationQueued
is the Boolean that is immediately returned from collection operation calls. Setting this to false
allows you to test
situations where your stored procedure is defensively timed out by DocumentDB.mock.package.your-stored-procedure()
mock.lastBody
to see the output of your stored procedure. You can also inspect mock.lastResponseOptions
mock.lastCollectionLink
, and mock.lastQueryFilter
to see the last values that your stored procedure sent into
the most recent collection operation.As an example, here is a stored procedure that will count all of the documents in a collection:
count = (memo) ->
collection = getContext().getCollection()
unless memo?
memo = {}
unless memo.count?
memo.count = 0
unless memo.continuation?
memo.continuation = null
unless memo.example?
memo.example = null
stillQueuingOperations = true
query = () ->
if stillQueuingOperations
responseOptions =
continuation: memo.continuation
pageSize: 1000
if memo.filterQuery?
stillQueuingOperations = collection.queryDocuments(collection.getSelfLink(), memo.filterQuery, responseOptions, onReadDocuments)
else
stillQueuingOperations = collection.readDocuments(collection.getSelfLink(), responseOptions, onReadDocuments)
setBody()
onReadDocuments = (err, resources, options) ->
if err
throw err
count = resources.length
memo.count += count
memo.example = resources[0]
if options.continuation?
memo.continuation = options.continuation
query()
else
memo.continuation = null
setBody()
setBody = () ->
getContext().getResponse().setBody(memo)
query()
return memo
exports.count = count
Here is a simple nodeunit test of the above stored procedure:
{ServerSideMock} = require('documentdb-mock')
mock = new ServerSideMock('./stored-procedures/countDocuments')
exports.countTest =
basicTest: (test) ->
mock.nextResources = [
{id: 1, value: 10}
{id: 2, value: 20}
{id: 3, value: 30}
]
mock.package.count()
test.equal(mock.lastBody.count, 3)
test.ok(!mock.lastBody.continuation?)
test.done()
If you want to test the ability of a stored procedure to be restarted:
testContinuation: (test) ->
firstBatch = [
{id: 1, value: 10}
{id: 2, value: 20}
]
secondBatch = [
{id: 3, value: 30}
{id: 4, value: 40}
]
mock.resourcesList = [firstBatch, secondBatch]
firstOptions = {continuation: 'ABC123'}
secondOptions = {}
mock.optionsList = [firstOptions, secondOptions]
mock.package.count()
test.equal(mock.lastBody.count, 4)
test.ok(!mock.lastBody.continuation?)
# Note, lastResponseOptions is NOT the options returned from a collection operation.
# It is the last one you sent in.
test.equal(mock.lastOptions.continuation, 'ABC123')
test.done()
Here's an example of testing a stored procedure being forceably timed out by DocumentDB and then restarted by you:
testTimeout: (test) ->
firstBatch = [
{id: 1, value: 10}
{id: 2, value: 20}
]
secondBatch = [
{id: 3, value: 30}
{id: 4, value: 40}
]
mock.resourcesList = [firstBatch, secondBatch]
firstOptions = {continuation: 'ABC123'}
secondOptions = {}
mock.optionsList = [firstOptions, secondOptions]
mock.collectionOperationQueuedList = [true, false, true]
mock.package.count()
memo = mock.lastBody
test.equal(memo.count, 2)
test.equal(memo.continuation, 'ABC123')
mock.package.count(memo)
test.equal(memo.count, 4)
test.done()
I'd be willing to accept pull requests implementing any unimplemented functionality listed as "Unimplemented" above. Also, I'd love to hear feedback from other people using it.
Copyright (c) 2015 Lawrence S. Maccherone, Jr.
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.
FAQs
Mock for testing Stored Procedures in Microsoft Azure's DocumentDB
The npm package documentdb-mock receives a total of 8 weekly downloads. As such, documentdb-mock popularity was classified as not popular.
We found that documentdb-mock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.