Skip to content
Browse files

[tests] doc list

  • Loading branch information...
1 parent 74549d7 commit 22d401fd414d353181592e0a1eb17e45943c05e6 @dscape committed May 14, 2012
Showing with 93 additions and 205 deletions.
  1. +1 −1 package.json
  2. +53 −204 tests/doc/list.js
  3. +39 −0 tests/fixtures/doc/list.json
View
2 package.json
@@ -27,7 +27,7 @@
, "nock" : "0.13.x"
}
, "scripts" :
- { "test" : "NOCK=on ls -tr tests/*/*.js | xargs -I{} node {}" }
+ { "test" : "ls -tr tests/*/*.js | NOCK=on xargs -I{} node {}" }
, "main" : "./nano.js"
, "engines" : { "node": ">=0.3.6" }
}
View
257 tests/doc/list.js
@@ -1,217 +1,66 @@
-var ensure = require('ensure')
- , nock = require('nock')
+var specify = require('specify')
, async = require('async')
- , cfg = require('../../cfg/tests.js')
- , nano = require('../../nano')(cfg)
- , db_name = require('../utils').db_name("doc_li")
- , tests = exports
- , couch
+ , helpers = require('../helpers')
+ , timeout = helpers.timeout
+ , nano = helpers.nano
+ , nock = helpers.nock
;
- couch = nock(cfg.url)
- .put('/' + db_name('a'))
- .reply(201, "{\"ok\":true}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('a'),
- date: 'Fri, 02 Dec 2011 20:26:29 GMT',
- 'content-type': 'application/json',
- 'content-length': '12',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('c'))
- .reply(201, "{\"ok\":true}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('c'),
- date: 'Fri, 02 Dec 2011 20:26:29 GMT',
- 'content-type': 'application/json',
- 'content-length': '12',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('b'))
- .reply(201, "{\"ok\":true}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('b'),
- date: 'Fri, 02 Dec 2011 20:26:29 GMT',
- 'content-type': 'application/json',
- 'content-length': '12',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('a') + '/foobar', {"foo": "bar"})
- .reply(201, "{\"ok\": true,\"id\": \"foobar\",\"rev\": \"1-4c6114c65e295552ab1019e2b046b10e\"}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('a') + '/foobar',
- etag: '"1-4c6114c65e295552ab1019e2b046b10e"',
- date: 'Fri, 02 Dec 2011 20:26:29 GMT',
- 'content-type': 'application/json',
- 'content-length': '69',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('a') + '/barfoo', {"bar": "foo"})
- .reply(201, "{\"ok\": true,\"id\": \"barfoo\",\"rev\": \"1-41412c293dade3fe73279cba8b4cece4\"}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('a') + '/barfoo',
- etag: '"1-41412c293dade3fe73279cba8b4cece4"',
- date: 'Fri, 02 Dec 2011 20:26:29 GMT',
- 'content-type': 'application/json',
- 'content-length': '69',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('c') + '/foobar', {"foo": "bar"})
- .reply(201, "{\"ok\": true,\"id\": \"foobar\",\"rev\": \"1-4c6114c65e295552ab1019e2b046b10e\"}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('c') + '/foobar',
- etag: '"1-4c6114c65e295552ab1019e2b046b10e"',
- date: 'Fri, 02 Dec 2011 20:26:29 GMT',
- 'content-type': 'application/json',
- 'content-length': '69',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('a') + '/foobaz', {"foo": "baz"})
- .reply(201, "{\"ok\": true,\"id\": \"foobaz\",\"rev\": \"1-cfa20dddac397da5bf0be2b50fb472fe\"}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('a') + '/foobaz',
- etag: '"1-cfa20dddac397da5bf0be2b50fb472fe"',
- date: 'Fri, 02 Dec 2011 20:26:30 GMT',
- 'content-type': 'application/json',
- 'content-length': '69',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('c') + '/barfoo', {"bar": "foo"})
- .reply(201, "{\"ok\": true,\"id\": \"barfoo\",\"rev\": \"1-41412c293dade3fe73279cba8b4cece4\"}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('c') + '/barfoo',
- etag: '"1-41412c293dade3fe73279cba8b4cece4"',
- date: 'Fri, 02 Dec 2011 20:26:30 GMT',
- 'content-type': 'application/json',
- 'content-length': '69',
- 'cache-control': 'must-revalidate' })
- .get('/' + db_name('a') + '/_all_docs')
- .reply(200, "{\"total_rows\": 3,\"offset\": 0,\"rows\": [\r\n{\"id\": \"barfoo\",\"key\": \"barfoo\",\"value\": {\"rev\": \"1-41412c293dade3fe73279cba8b4cece4\"}},\r\n{\"id\" :\"foobar\",\"key\": \"foobar\",\"value\": {\"rev\":\"1-4c6114c65e295552ab1019e2b046b10e\"}},\r\n{\"id\": \"foobaz\",\"key\": \"foobaz\",\"value\": {\"rev\":\"1-cfa20dddac397da5bf0be2b50fb472fe\"}}\r\n]}\n",
- { 'transfer-encoding': 'chunked',
- server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- etag: '"AXA5PYPHH9S5SIRQ1NH1N0896"',
- date: 'Fri, 02 Dec 2011 20:26:30 GMT',
- 'content-type': 'application/json',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('b') + '/foobar', {"foo": "bar"})
- .reply(201, "{\"ok\": true,\"id\": \"foobar\",\"rev\": \"1-4c6114c65e295552ab1019e2b046b10e\"}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('b') + '/foobar',
- etag: '"1-4c6114c65e295552ab1019e2b046b10e"',
- date: 'Fri, 02 Dec 2011 20:26:30 GMT',
- 'content-type': 'application/json',
- 'content-length': '69',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('b') + '/barfoo', {"bar": "foo"})
- .reply(201, "{\"ok\": true,\"id\": \"barfoo\",\"rev\": \"1-41412c293dade3fe73279cba8b4cece4\"}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('b') + '/barfoo',
- etag: '"1-41412c293dade3fe73279cba8b4cece4"',
- date: 'Fri, 02 Dec 2011 20:26:30 GMT',
- 'content-type': 'application/json',
- 'content-length': '69',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('b') + '/foobaz', {"foo": "baz"})
- .reply(201, "{\"ok\": true,\"id\": \"foobaz\",\"rev\": \"1-cfa20dddac397da5bf0be2b50fb472fe\"}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('b') + '/foobaz',
- etag: '"1-cfa20dddac397da5bf0be2b50fb472fe"',
- date: 'Fri, 02 Dec 2011 20:26:30 GMT',
- 'content-type': 'application/json',
- 'content-length': '69',
- 'cache-control': 'must-revalidate' })
- .put('/' + db_name('c') + '/foobaz', {"foo": "baz"})
- .reply(201, "{\"ok\": true,\"id\": \"foobaz\",\"rev\": \"1-cfa20dddac397da5bf0be2b50fb472fe\"}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- location: cfg.url + '/' + db_name('c') + '/foobaz',
- etag: '"1-cfa20dddac397da5bf0be2b50fb472fe"',
- date: 'Fri, 02 Dec 2011 20:26:30 GMT',
- 'content-type': 'application/json',
- 'content-length': '69',
- 'cache-control': 'must-revalidate' })
- .delete('/' + db_name('a'))
- .reply(200, "{\"ok\":true}\n",
- { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- date: 'Fri, 02 Dec 2011 20:26:30 GMT',
- 'content-type': 'application/json',
- 'content-length': '12',
- 'cache-control': 'must-revalidate' })
- .get('/' + db_name('b') + '/_all_docs?limit=1')
- .reply(200, "{\"total_rows\": 3,\"offset\": 0,\"rows\": [\r\n{\"id\": \"barfoo\",\"key\": \"barfoo\",\"value\": {\"rev\": \"1-41412c293dade3fe73279cba8b4cece4\"}}\r\n]}\n",
- { 'transfer-encoding': 'chunked',
- server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- etag: '"7FE6ZA8SPA15QUXDRRHGRLWKO"',
- date: 'Fri, 02 Dec 2011 20:26:30 GMT',
- 'content-type': 'application/json',
- 'cache-control': 'must-revalidate' })
- .get('/' + db_name('c') + '/_all_docs?startkey=%22c%22')
- .reply(200, "{\"total_rows\": 3,\"offset\": 1,\"rows\": [\r\n{\"id\": \"foobar\",\"key\": \"foobar\",\"value\": {\"rev\": \"1-4c6114c65e295552ab1019e2b046b10e\"}},\r\n{\"id\": \"foobaz\",\"key\": \"foobaz\",\"value\": {\"rev\":\"1-cfa20dddac397da5bf0be2b50fb472fe\"}}\r\n]}\n",
- { 'transfer-encoding': 'chunked',
- server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
- etag: '"B5NTIXF7OO6RFK10FU94G5O3Y"',
- date: 'Fri, 02 Dec 2011 20:26:31 GMT',
- 'content-type': 'application/json',
- 'cache-control': 'must-revalidate' });
-
-function db(i) { return nano.use(db_name(i)); }
+var mock = nock(helpers.couch, "doc/list")
+ , db = nano.use("doc_list")
+ ;
-tests.list_doc = function (callback) {
- nano.db.create(db_name('a'), function () {
+specify("doc_list:setup", timeout, function (assert) {
+ nano.db.create("doc_list", function (err) {
+ assert.equal(err, undefined, "Failed to create database");
async.parallel(
- [ function(cb) { db('a').insert({"foo": "bar"}, "foobar", cb); }
- , function(cb) { db('a').insert({"bar": "foo"}, "barfoo", cb); }
- , function(cb) { db('a').insert({"foo": "baz"}, "foobaz", cb); }
- ],
- function(err, results){
- db('a').list(callback);
- });
+ [ function(cb) { db.insert({"foo": "bar"}, "foobar", cb); }
+ , function(cb) { db.insert({"bar": "foo"}, "barfoo", cb); }
+ , function(cb) { db.insert({"foo": "baz"}, "foobaz", cb); }
+ ]
+ , function(error, results) {
+ assert.equal(error, undefined, "Should have stored docs");
+ });
});
-};
+});
-tests.list_doc_ok = function (e,b) {
- this.t.notOk(e, 'No err');
- this.t.equal(b.total_rows,3, '3 Rows');
- this.t.ok(b.rows, 'Got rows');
- this.t.ok(couch.isDone(), 'Nock is done');
-};
-
-tests.ns_list_doc = function (callback) {
- nano.db.create(db_name('b'), function () {
- async.parallel(
- [ function(cb) { db('b').insert({"foo": "bar"}, "foobar", cb); }
- , function(cb) { db('b').insert({"bar": "foo"}, "barfoo", cb); }
- , function(cb) { db('b').insert({"foo": "baz"}, "foobaz", cb); }
- ],
- function(err, results){
- nano.request( { db: db_name('b')
- , doc: "_all_docs"
- , method: "GET"
- , params: {limit: 1}
- }, callback);
- });
+specify("doc_list:list", timeout, function (assert) {
+ db.list(function (error, docs) {
+ assert.equal(error, undefined, "List didn't work");
+ assert.equal(docs.total_rows, 3, "Got total three rows");
+ assert.ok(docs.rows, "Got rows");
});
-};
+});
-tests.ns_list_doc_ok = function (e,b) {
- this.t.notOk(e, 'Not Err');
- this.t.equal(b.rows.length,1, 'One Row');
- this.t.equal(b.total_rows,3, 'Out of 3');
- this.t.ok(b.rows, 'I got Rows');
- this.t.ok(couch.isDone(), 'Nock is done');
-};
+specify("doc_list:relaxed", timeout, function (assert) {
+ nano.relax(
+ { db : "doc_list"
+ , doc : "_all_docs"
+ , method : "GET"
+ , params : {limit: 1}
+ }, function (error, docs) {
+ assert.equal(error, undefined, "Relax didn't work");
+ assert.ok(docs.rows, "Got rows");
+ assert.equal(docs.rows.length, 1, "Only one row");
+ assert.equal(docs.total_rows, 3, "Got total three rows");
+ });
+});
-tests.list_doc_params = function (callback) {
- nano.db.create(db_name('c'), function () {
- async.parallel(
- [ function(cb) { db('c').insert({"foo": "bar"}, "foobar", cb); }
- , function(cb) { db('c').insert({"bar": "foo"}, "barfoo", cb); }
- , function(cb) { db('c').insert({"foo": "baz"}, "foobaz", cb); }
- ],
- function(err, results){
- db('c').list({startkey: 'c'},callback);
- });
+specify("doc_list:relaxed", timeout, function (assert) {
+ db.list({startkey: 'c'}, function (error, docs) {
+ assert.equal(error, undefined, "Startkey didn't work");
+ assert.ok(docs.rows, "Got rows");
+ assert.equal(docs.rows.length, 2, "Started in row two");
+ assert.equal(docs.total_rows, 3, "Got total three rows");
+ assert.equal(docs.offset, 1, "Offset by 1");
});
-};
+});
-tests.list_doc_params_ok = function (e,b) {
- this.t.notOk(e, 'No errs');
- this.t.equal(b.rows.length,2, 'Two rows returned');
- this.t.equal(b.total_rows,3, 'Out of 3');
- this.t.ok(b.rows, 'That means we got rows');
- this.t.ok(couch.isDone(), 'Nock is done');
-};
+specify("doc_list:teardown", timeout, function (assert) {
+ nano.db.destroy("doc_list", function (err) {
+ assert.equal(err, undefined, "Failed to destroy database");
+ assert.ok(mock.isDone(), "Some mocks didn't run");
+ });
+});
-ensure(__filename,tests,module,process.argv[2]);
+specify.run(process.argv.slice(2));
View
39 tests/fixtures/doc/list.json
@@ -0,0 +1,39 @@
+[
+ { "method" : "put"
+ , "path" : "/doc_list"
+ , "status" : 201
+ , "response" : "{ \"ok\": true }"
+ }
+, { "method" : "put"
+ , "status" : 201
+ , "path" : "/doc_list/foobar"
+ , "body" : "{\"foo\":\"bar\"}"
+ , "response" : "{\"ok\":true,\"id\":\"foobar\",\"rev\":\"1-4c6114\"}"
+ }
+, { "method" : "put"
+ , "status" : 201
+ , "path" : "/doc_list/foobaz"
+ , "body" : "{\"foo\":\"baz\"}"
+ , "response" : "{\"ok\":true,\"id\":\"foobaz\",\"rev\":\"1-611488\"}"
+ }
+, { "method" : "put"
+ , "status" : 201
+ , "path" : "/doc_list/barfoo"
+ , "body" : "{\"bar\":\"foo\"}"
+ , "response" : "{\"ok\":true,\"id\":\"barfoo\",\"rev\":\"1-3cde10\"}"
+ }
+, { "path" : "/doc_list/_all_docs"
+ , "response" : "{\"total_rows\":3,\"offset\": 0, \"rows\": []}"
+ }
+, { "path" : "/doc_list/_all_docs?limit=1"
+ , "response" : "{\"total_rows\":3,\"offset\": 0,\"rows\":[\r\n{\"id\":\"foobar\",\"key\":\"foobar\",\"value\":{\"_id\":\"foobar\",\"_rev\":\"1-4c6114c65e295552ab1019e2b046b10e\",\"foo\":\"bar\"}}]}"
+ }
+, { "path" : "/doc_list/_all_docs?startkey=%22c%22"
+ , "response" : "{\"total_rows\":3,\"offset\": 1,\"rows\":[\r\n{\"id\":\"foobar\",\"key\":\"foobar\",\"value\":{\"_id\":\"foobar\",\"_rev\":\"1-4c6114c65e295552ab1019e2b046b10e\",\"foo\":\"bar\"},\"doc\":{\"_id\":\"foobar\",\"_rev\":\"1-4c6114c65e295552ab1019e2b046b10e\",\"foo\":\"bar\"}},\r\n{\"id\":\"barfoo\",\"key\":\"barfoo\",\"value\":{\"_id\":\"barfoo\",\"_rev\":\"1-41412c293dade3fe73279cba8b4cece4\",\"bar\":\"foo\"},\"doc\":{\"_id\":\"barfoo\",\"_rev\":\"1-41412c293dade3fe73279cba8b4cece4\",\"bar\":\"foo\"}}\r\n]}"
+ }
+, { "method" : "delete"
+ , "path" : "/doc_list"
+ , "status" : 200
+ , "response" : "{ \"ok\": true }"
+ }
+]

0 comments on commit 22d401f

Please sign in to comment.
Something went wrong with that request. Please try again.