DocumentDBMock - 模拟测试DocumentDB存储过程的工具

[复制链接]
查看11 | 回复7 | 2014-2-19 11:55:14 | 显示全部楼层 |阅读模式
DocumentDBMock
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.

回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
Features
Working
DocumentDBMock implements many of the methods in the DocumentDB's Collection class including:
getResponse.setBody
getSelfLink
createDocument
readDocument
replaceDocument
deleteDocument
queryDocuments
readDocuments
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
Unimplemented
Attachment operations - should be easy to implement following the patterns for document operations
Right now, you pretty much have to pre-configure the mock with every response that you expect to get from DocumentDB operations.
Installnpm install -save documentdb-mock复制代码
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
Usage
You can look at the code in the test and stored-procedure folders to see how to use DocumentDBMock.
Basically:
Create a module to hold one or more stored procedures. You simply need to exports your function(s).
Create your mock with mock = new DocumentDBMock('path/to/stored/procedure')
Set 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.
Call your stored procedure like it was a function from within your test with mock.package.your-stored-procedure()
Inspect mock.lastBody to see the output of your stored procedure. You can also inspect mock.lastResponseOptions 'mock.lastCollectionLink, andmock.lastQueryFilter` to see the last values that your stored procedure sent into the most recent collection operation.
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
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 = 0unless memo.continuation?memo.continuation = nullunless memo.example?memo.example = nullstillQueuingOperations = truequery = () ->if stillQueuingOperationsresponseOptions =continuation: memo.continuationpageSize: 1000if memo.filterQuery?stillQueuingOperations = collection.queryDocuments(collection.getSelfLink(), memo.filterQuery, responseOptions, onReadDocuments)elsestillQueuingOperations = collection.readDocuments(collection.getSelfLink(), responseOptions, onReadDocuments)setBody()onReadDocuments = (err, resources, options) ->if errthrow errcount = resources.lengthmemo.count += countmemo.example = resources[0]if options.continuation?memo.continuation = options.continuationquery()elsememo.continuation = nullsetBody()setBody = () ->getContext().getResponse().setBody(memo)query()return memoexports.count = count复制代码
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
Here is a simple nodeunit test of the above stored procedure:DocumentDBMock = require('documentdb-mock')mock = new DocumentDBMock('./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()复制代码
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
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()复制代码
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
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.lastBodytest.equal(memo.count, 2)test.equal(memo.continuation, 'ABC123')mock.package.count(memo)test.equal(memo.count, 4)test.done()复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行