Skip to content
This repository
Browse code

[tests] migrating to specify

  • Loading branch information...
commit f9999f04d4f4443007508677dbe7b012689fdf14 1 parent e1aa043
Nuno Job authored May 12, 2012
21  cfg/couch.example.js
... ...
@@ -1,21 +0,0 @@
1  
-var cfg = { host: "mydb.iriscouch.com"
2  
-          , port: "80"
3  
-          , ssl:  false
4  
-          , proxy: "http://someproxy.com"
5  
-          , user: "admin"
6  
-          , pass: "admin"
7  
-          };
8  
-
9  
-cfg.credentials = function credentials() {
10  
-  if (cfg.user && cfg.pass) {
11  
-    return cfg.user + ":" + cfg.pass + "@";
12  
-  }
13  
-  else { return ""; }
14  
-}();
15  
-
16  
-cfg.url = function () {
17  
-  return "http" + (cfg.ssl ? "s" : "") + "://" + cfg.credentials + cfg.host + 
18  
-    ":" + cfg.port;
19  
-}();
20  
-
21  
-module.exports = exports = cfg;
15  package.json
@@ -2,7 +2,7 @@
2 2
 , "description"     : "minimalistic couchdb driver for node.js"
3 3
 , "homepage"        : "http://github.com/dscape/nano"
4 4
 , "repository"      : "git://github.com/dscape/nano"
5  
-, "version"         : "2.1.0"
  5
+, "version"         : "3.0.0"
6 6
 , "author"          : "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)"
7 7
 , "contributors"    :
8 8
   [ "Thiago Arrais <thiago.arrais@gmail.com> (http://thiagoarrais.com)"
@@ -22,16 +22,9 @@
22 22
   ["couchdb", "data", "request", "json", "nosql", "micro", "nano", "database"]
23 23
 , "dependencies"    : {"request": "2.9.x"}
24 24
 , "devDependencies" : 
25  
-  { "async"         : "0.1.15"
26  
-  , "ensure"        : "0.4.6"
27  
-  , "nock"          : "0.5.5"
28  
-  , "tap-runner"    : "0.0.7"
29  
-  , "tap-producer"  : "0.0.1"
30  
-  , "tap"           : "0.1.3"
31  
-  , "inherits"      : "1.0.0"
32  
-  , "yamlish"       : "0.0.2"
33  
-  , "slide"         : "1.1.3"
34  
-  , "underscore"    : "1.3.1"
  25
+  { "async"         : "0.1.x"
  26
+  , "specify"       : "0.4.x"
  27
+  , "nock"          : "0.10.x"
35 28
   }
36 29
 , "scripts"         : 
37 30
   { "test"          : "./node_modules/ensure/bin/tap.js tests/*/*.js" }
89  tests/att/destroy.js
... ...
@@ -1,63 +1,38 @@
1  
-var ensure   = require('ensure')
2  
-  , cfg      = require('../../cfg/tests.js')
3  
-  , nano     = require('../../nano')(cfg)
4  
-  , db_name  = require('../utils').db_name("att_de")
5  
-  , tests    = exports
6  
-  , nock     = require('nock')
7  
-  , couchdb
8  
-  ;
9  
-  
10  
-  path = '/' + db_name("a");
11  
-  location = cfg.url + path;
12  
-  couchdb  = nock(cfg.url)
13  
-    .put('/' + db_name("a")
14  
-        , ''
15  
-        , { 'content-type': 'application/json'
16  
-          , "accept": 'application/json' })
17  
-    .reply(201, { ok: true },
18  
-      { location: cfg.url + '/' + db_name("a")
19  
-      , date: 'Wed, 30 Nov 2011 15:21:58 GMT'
20  
-      , 'content-type': 'application/json'
21  
-      , 'cache-control': 'must-revalidate'
22  
-      , 'status-code': 201 
23  
-      })
24  
-      
25  
-    .put(path + '/new/att', "\"Hello World!\"")
26  
-    .reply(201, { ok: true, id: 'new', rev: '1-921bd51ccdef5ab4c84b07bab7b80e7e' },
27  
-      { location: location + '/new/att'
28  
-      , date: 'Wed, 30 Nov 2011 15:21:58 GMT'
29  
-      , 'content-type': 'application/json'
30  
-      , 'cache-control': 'must-revalidate'
31  
-      , 'status-code': 201 
32  
-      })
33  
-      
34  
-     .delete(path + '/new/att?rev=1-921bd51ccdef5ab4c84b07bab7b80e7e')
35  
-     .reply(201, { ok: true, id:'new' },
36  
-       { location: location + '/new/att?rev=1-921bd51ccdef5ab4c84b07bab7b80e7e'
37  
-       , date: 'Wed, 30 Nov 2011 15:21:58 GMT'
38  
-       , 'content-type': 'application/json'
39  
-       , 'cache-control': 'must-revalidate'
40  
-       , 'status-code': 201 
41  
-       })
  1
+var specify  = require('specify')
  2
+  , helpers  = require('../helpers')
  3
+  , timeout  = helpers.timeout
  4
+  , nano     = helpers.nano
  5
+  , nock     = helpers.nock
42 6
   ;
43 7
 
44  
-function db(i) { return nano.use(db_name(i)); }
  8
+var mock = nock(helpers.couch, "att/destroy");
  9
+
  10
+specify("att_destroy:setup", timeout, function (assert) {
  11
+  nano.db.create("att_destroy", function (err) {
  12
+    assert.equal(err, undefined, "Failed to create database");
  13
+  });
  14
+});
45 15
 
46  
-tests.att_des = function (callback) {
47  
-  console.log(require('../../nano'))
48  
-  nano.db.create(db_name("a"), function () {
49  
-    db("a").attachment.insert("new", "att", "Hello World!", "text/plain",
50  
-      function (e,b) {
51  
-        db("a").attachment.destroy("new", "att", b.rev, callback);
52  
-    });
  16
+specify("att_destroy:storeAtt", timeout, function (assert) {
  17
+  var db = nano.use("att_destroy");
  18
+  db.attachment.insert("new", "att", "Hello World!", "text/plain",
  19
+    function (error, att) {
  20
+      assert.equal(error, undefined, "Should store the attachment");
  21
+      assert.equal(att.ok, true, "Response should be ok");
  22
+      assert.ok(att.rev, "Should have a revision number");
  23
+      db.attachment.destroy("new", "att", att.rev, function(error, response) {
  24
+        assert.equal(error, undefined, "Should delete the attachment");
  25
+        assert.equal(response.ok, true, "Response should be ok");
  26
+        assert.equal(response.id, "new", "Id should be new");
  27
+      });
53 28
   });
54  
-};
  29
+});
55 30
 
56  
-tests.att_des_ok = function (e,b) {
57  
-  this.t.notOk(e, 'No Error');
58  
-  this.t.ok(b.ok, 'Ok is there');
59  
-  this.t.equal(b.id, "new", 'Id is "new"');
60  
-  this.t.ok(couchdb.isDone(), 'Nock not done');
61  
-};
  31
+specify("att_destroy:teardown", timeout, function (assert) {
  32
+  nano.db.destroy("att_destroy", function (err) {
  33
+    assert.equal(err, undefined, "Failed to destroy database");
  34
+    assert.ok(mock.isDone(), "Some mocks didn't run");
  35
+  });
  36
+});
62 37
 
63  
-ensure(__filename,tests,module,process.argv[2]);
  38
+specify.run(process.argv.slice(2));
74  tests/att/get.js
... ...
@@ -1,13 +1,14 @@
1  
-var ensure   = require('ensure')
2  
-  , nock     = require('nock')
3  
-  , cfg      = require('../../cfg/tests.js')
4  
-  , nano     = require('../../nano')(cfg)
5  
-  , db_name  = require('../utils').db_name("att_ge")
6  
-  , tests    = exports
7  
-  , pixel    = "Qk06AAAAAAAAADYAAAAoAAAAAQAAAP////8BABgAAAAAAAAAAAATCwAAEwsAAAAAAAAAAAAAWm2CAA=="
8  
-  , couch
  1
+var specify  = require('specify')
  2
+  , helpers  = require('../helpers')
  3
+  , timeout  = helpers.timeout
  4
+  , nano     = helpers.nano
  5
+  , nock     = helpers.nock
  6
+  , pixel    = helpers.pixel
9 7
   ;
10 8
 
  9
+var mock = nock(helpers.couch, "att/get");
  10
+
  11
+/*
11 12
 couch = nock(cfg.url)
12 13
     .put('/' + db_name('a'))
13 14
     .reply(201, "{\"ok\":true}\n", { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
@@ -16,6 +17,7 @@ couch = nock(cfg.url)
16 17
     'content-type': 'application/json',
17 18
     'content-length': '12',
18 19
     'cache-control': 'must-revalidate' })
  20
+    
19 21
     .put('/' + db_name('a') + '/new/att', '"Hello"')
20 22
     .reply(201, "{\"ok\":true,\"id\":\"new\",\"rev\":\"1-5142a2e74e1ec33e6e5b621418210283\"}\n", { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
21 23
     location: cfg.url + '/' + db_name('a') + '/new/att',
@@ -24,6 +26,7 @@ couch = nock(cfg.url)
24 26
     'content-type': 'text/plain;charset=utf-8',
25 27
     'content-length': '66',
26 28
     'cache-control': 'must-revalidate' })
  29
+    
27 30
     .put('/' + db_name('a') + '/new/att?rev=1-5142a2e74e1ec33e6e5b621418210283'
28 31
     , new Buffer(pixel, 'base64').toString())
29 32
     .reply(201, "{\"ok\":true,\"id\":\"new\",\"rev\":\"2-3b1f88c637fde74a486cf3ce5558b47e\"}\n", { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
@@ -33,6 +36,7 @@ couch = nock(cfg.url)
33 36
     'content-type': 'text/plain;charset=utf-8',
34 37
     'content-length': '66',
35 38
     'cache-control': 'must-revalidate' })
  39
+
36 40
     .get('/' + db_name('a') +  '/new/att?rev=2-3b1f88c637fde74a486cf3ce5558b47e')
37 41
     .reply(200, new Buffer(pixel, 'base64'), { server: 'CouchDB/1.1.1 (Erlang OTP/R14B04)',
38 42
     etag: '"2-3b1f88c637fde74a486cf3ce5558b47e"',
@@ -44,28 +48,42 @@ couch = nock(cfg.url)
44 48
     'accept-ranges': 'bytes' });
45 49
 
46 50
 function db(i) { return nano.use(db_name(i)); }
  51
+*/
  52
+
  53
+specify("att_get:setup", timeout, function (assert) {
  54
+  nano.db.create("att_get", function (err) {
  55
+    assert.equal(err, undefined, "Failed to create database");
  56
+  });
  57
+});
47 58
 
48  
-tests.att_get = function (callback) {
49  
-  var buffer = new Buffer(pixel, 'base64');
50  
-  nano.db.create(db_name("a"), function () {
51  
-    db("a").attachment.insert("new", "att", "Hello", "text/plain",
52  
-      function(e,b) {
53  
-        if(e) { callback(e); }
54  
-        db("a").attachment.insert("new", "att", buffer, "image/bmp", {rev: b.rev},
55  
-          function (e2,b2) {
56  
-          if(e2) { callback(e2); }
57  
-          db("a").attachment.get("new", "att", {rev: b2.rev}, callback);
  59
+specify("att_get:pixelAtt", timeout, function (assert) {
  60
+  var db     = nano.use("att_get")
  61
+    , buffer = new Buffer(pixel, 'base64')
  62
+    ;
  63
+
  64
+    db.attachment.insert("new", "att", "Hello", "text/plain", 
  65
+    function(error, hello) {
  66
+      assert.equal(error, undefined, "Should store hello");
  67
+      assert.equal(hello.ok, true, "Response should be ok");
  68
+      assert.ok(hello.rev, "Should have a revision number");
  69
+      db.attachment.insert("new", "att", buffer, "image/bmp", 
  70
+      { rev: hello.rev }, function (error, bmp) {
  71
+        assert.equal(error, undefined, "Should store the pixel");
  72
+        db.attachment.get("new", "att", {rev: bmp.rev}, 
  73
+        function (error, bmp) {
  74
+          assert.equal(error, undefined, "Should get the pixel");
  75
+          assert.equal(bmp.toString("base64"), pixel, "Base64 is reflexive");
58 76
         });
  77
+      });
59 78
     });
60  
-  });
61  
-};
62 79
 
63  
-tests.att_get_ok = function (e,b) {
64  
-  this.t.notOk(e, 'No Error');
65  
-  var from_buffer = new Buffer(b, "binary").toString("base64");
66  
-  this.t.equal(from_buffer, pixel, 'Buffer is pixel');
67  
-  this.t.equal(b.toString("base64"), pixel, 'Result buffer is pixel');
68  
-  this.t.ok(couch.isDone(), 'Nock is done');
69  
-};
  80
+});
  81
+
  82
+specify("att_get:teardown", timeout, function (assert) {
  83
+  nano.db.destroy("att_get", function (err) {
  84
+    assert.equal(err, undefined, "Failed to destroy database");
  85
+    assert.ok(mock.isDone(), "Some mocks didn't run");
  86
+  });
  87
+});
70 88
 
71  
-ensure(__filename,tests,module,process.argv[2]);
  89
+specify.run(process.argv.slice(2));
23  tests/fixtures/att/destroy.json
... ...
@@ -0,0 +1,23 @@
  1
+[
  2
+  { "method"   : "put"
  3
+  , "path"     : "/att_destroy"
  4
+  , "status"   : 201
  5
+  , "response" : "{ ok: true }" 
  6
+  }
  7
+, { "method"   : "put"
  8
+  , "path"     : "/att_destroy/new/att"
  9
+  , "body"     : "\"Hello World!\""
  10
+  , "status"   : 201
  11
+  , "response" : "{ok: true, id: 'new', rev: '1-921bd51' }"
  12
+  }
  13
+, { "method"   : "delete"
  14
+  , "path"     : "/att_destroy/new/att?rev=1-921bd51"
  15
+  , "response" : "{ ok: true, id:'new' }"
  16
+  , "status"   : 201
  17
+  }
  18
+, { "method"   : "delete"
  19
+  , "path"     : "/att_destroy"
  20
+  , "status"   : 200
  21
+  , "response" : "{ ok: true }" 
  22
+  }
  23
+]
28  tests/fixtures/att/get.json
... ...
@@ -0,0 +1,28 @@
  1
+[
  2
+  { "method"   : "put"
  3
+  , "path"     : "/att_get"
  4
+  , "status"   : 201
  5
+  , "response" : "{ok: true}" 
  6
+  }
  7
+, { "method"   : "put"
  8
+  , "path"     : "/att_get/new/att"
  9
+  , "body"     : "\"Hello\""
  10
+  , "status"   : 201
  11
+  , "response" : "{\"ok\":true,\"id\":\"new\",\"rev\":\"1-5142a2\"}"
  12
+  }
  13
+, { "method"   : "put"
  14
+  , "path"     : "/att_get/new/att?rev=1-5142a2"
  15
+  , "body"     : "BM:\u0000\u0000\u0000\u0000\u0000\u0000\u00006\u0000\u0000\u0000(\u0000\u0000\u0000\u0001\u0000\u0000\u0000????\u0001\u0000\u0018\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0013\u000b\u0000\u0000\u0013\u000b\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000Zm?\u0000"
  16
+  , "status"   : 201
  17
+  , "response" : "{\"ok\":true,\"id\":\"new\",\"rev\":\"2-3b1f88\"}\n"
  18
+  }
  19
+, { "path"     : "/att_get/new/att?rev=2-3b1f88"
  20
+  , "status"   : 200
  21
+  , "buffer"   : "Qk06AAAAAAAAADYAAAAoAAAAAQAAAP////8BABgAAAAAAAAAAAATCwAAEwsAAAAAAAAAAAAAWm2CAA=="
  22
+  }
  23
+, { "method"   : "delete"
  24
+  , "path"     : "/att_get"
  25
+  , "status"   : 200
  26
+  , "response" : "{ ok: true }" 
  27
+  }
  28
+]
3  tests/fixtures/cfg.json
... ...
@@ -0,0 +1,3 @@
  1
+{ "timeout" : 20000
  2
+, "couch"   : "http://localhost:5984"
  3
+}
71  tests/helpers.js
... ...
@@ -0,0 +1,71 @@
  1
+var path    = require('path')
  2
+  , fs      = require('fs')
  3
+  , cfg      = require('./fixtures/cfg.json')
  4
+  , nano     = require('../nano')
  5
+  , helpers  = exports
  6
+  ;
  7
+
  8
+function endsWith (string, ending) {
  9
+  return string.length >= ending.length && 
  10
+    string.substr(string.length - ending.length) == ending;
  11
+}
  12
+
  13
+function noop(){}
  14
+
  15
+function fake_chain() {
  16
+  return {
  17
+      "get"                  : fake_chain
  18
+    , "post"                 : fake_chain
  19
+    , "delete"               : fake_chain
  20
+    , "put"                  : fake_chain
  21
+    , "intercept"            : fake_chain
  22
+    , "done"                 : fake_chain
  23
+    , "isDone"               : function () { return true; }
  24
+    , "filteringPath"        : fake_chain
  25
+    , "filteringRequestBody" : fake_chain
  26
+    , "matchHeader"          : fake_chain
  27
+    , "defaultReplyHeaders"  : fake_chain
  28
+    , "log"                  : fake_chain
  29
+  };
  30
+}
  31
+
  32
+helpers.timeout = cfg.timeout;
  33
+helpers.nano = nano(cfg.couch);
  34
+helpers.couch = cfg.couch;
  35
+helpers.pixel = "Qk06AAAAAAAAADYAAAAoAAAAAQAAAP////8BABgAAAAA" + 
  36
+                "AAAAAAATCwAAEwsAAAAAAAAAAAAAWm2CAA==";
  37
+
  38
+helpers.loadFixture = function helpersLoadFixture(filename, json) {
  39
+  var contents = fs.readFileSync(
  40
+    path.join(__dirname, 'fixtures', filename), 'ascii');
  41
+  return json ? JSON.parse(contents): contents;
  42
+};
  43
+
  44
+helpers.nock = function helpersNock(url, fixture) {
  45
+  if(process.env.NOCK) {
  46
+    var nock    = require('nock')
  47
+      , nocks   = helpers.loadFixture(fixture + '.json', true)
  48
+      ;
  49
+    nocks.forEach(function(n) {
  50
+      var path     = n.path
  51
+        , method   = n.method   || "get"
  52
+        , status   = n.status   || 200
  53
+        , response = n.response || ""
  54
+        , headers  = n.headers  || {}
  55
+        , body     = n.buffer    ? new Buffer(n.buffer, 'base64') : n.body
  56
+        ;
  57
+
  58
+      if(typeof response === "string" && endsWith(response, '.json')) {
  59
+        response = helpers.loadFixture(path.join(fixture, response));
  60
+      }
  61
+      if(typeof headers === "string" && endsWith(headers, '.json')) {
  62
+        headers = helpers.loadFixture(path.join(fixture, headers));
  63
+      }
  64
+      nock(url)[method](path, body).reply(status, response, headers);
  65
+    });
  66
+    nock(url).log(console.log);
  67
+    return nock(url);
  68
+  } else {
  69
+    return fake_chain();
  70
+  }
  71
+};
6  tests/utils.js
... ...
@@ -1,6 +0,0 @@
1  
-module.exports = 
2  
-  { db_name : function (prefix) {
3  
-      return function db_name(i) { 
4  
-        return process.version.replace(/\./g,'') + '_' +prefix + i; };
5  
-    }
6  
-  };

0 notes on commit f9999f0

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