2012-06-22 11 views
8

क्या परीक्षण चौखटे विचारों का परीक्षण करने के लिए उपलब्ध हैं और यह मेरे CouchDB के map और reduce काम करता है, फिल्टर, सूचियों, शो है आदि?परीक्षण CouchDB दृश्य, फ़िल्टर, सूचियों, शो आदि

आदर्श रूप से ढांचा प्रत्येक कार्य को इकाई परीक्षण करने की अनुमति देता है, साथ ही साथ सोफेडबी उदाहरण पर डेटासेट के खिलाफ दिए गए दृश्यों, फ़िल्टर इत्यादि के परीक्षण के लिए समर्थन प्रदान करता है।

मुझे writing a test suite for the CouchDB API पर एक ब्लॉग पोस्ट मिला है लेकिन यह 2010 से है और मैं सोच रहा था कि तब से क्या हुआ है।

उत्तर

4

मैं Expresso Node.js. के लिए टीडीडी ढांचे का उपयोग करूंगा। एक नोड.जेएस आवेदन लिखने का उपरांत बर्बाद प्रयास नहीं होगा।

डाउनलोड पृष्ठ से Node.js स्थापित करें: nodejs.org/download

सुनिश्चित करें कि आप भी NPM (Node.js पैकेज प्रबंधक) प्राप्त करें।

एक्सप्रेसो फ्रेमवर्क के साथ आप आसानी से कॉच डीबी में किसी भी विश्वसनीय कार्यों का परीक्षण कर सकते हैं।

var http = require('http'); 
var url = require('url'); 
var log_level = 5; 
var base_url = 'http://localhost:5984/'; 
var delete_db_at_end = false; 

/** 
* This test fetches all the data from a database and verifies that the data is 
* returned as it should. 
*/ 
exports.testCouchDBCartoonsAllDocs = function(beforeExit, assert) { 
    curl('GET', base_url + 'cartoons/_all_docs', null, function(response) { 
     assert.equal(7, response.total_rows); 
     assert.equal('Donald Duck', response.rows[1].id); 
    }); 
} 

/** 
* This test creates a database for this test only and deletes it at the end if the 
* global parameter delete_db_at_end is set to true, otherwise it is left as is for 
* human inspection. 
* 
*/ 
exports.testCreateDatabaseTestAddAndDestroy = function(beforeExit, assert) { 
    var dbname = 'test_db_cartoon'; 
    deleteExistingDatabase(dbname, assert, function(response) { 
     /* Create the database */ 
     curl('PUT', base_url + dbname, null, function(response) { 
      assert.equal(true, response.ok); 
      curl('PUT', base_url + dbname + '/Donald+Duck', '{"publisher":"Walt Disney"}', function(response){ 
       assert.equal(true, response.ok); 
       assert.equal('Donald Duck', response.id); 
       curl('GET', base_url + dbname + '/Donald+Duck', null, function(response) { 
        assert.equal('Donald Duck', response._id); 
        assert.equal('Walt Disney', response.publisher); 
        /* Finally we delete the database from CouchDB */ 
        if (delete_db_at_end) { 
         deleteExistingDatabase(dbname, assert, function(response) { 
          assert.equal(true, response.ok); 
         }); 
        } 
       }); 
      }); 
     }); 
    }); 
} 

/** 
* This is a helper function that deletes the database if it exists so 
* that the tests can run even if they where interrupted. 
*/ 
function deleteExistingDatabase(dbname, assert, callback) { 
    curl('GET', base_url + dbname, null, function(response) { 
     if (response.db_name === dbname) { 
      log(1, 'About to delete the database ' + dbname); 
      curl('DELETE', base_url + '/' + dbname, null, function(response) { 
       callback(response); 
      }); 
     } else { 
      callback(response); 
     } 
    }); 
} 

/** 
* This is a helper method to manage the RESTful sending to the database 
*/ 
function curl(method, urlString, payload, callback) { 
    log(1, method + ' ' + urlString); 
    var auth = 'Basic ' + new Buffer('username:password').toString('base64'); 
    log(7, auth); 

    var options = url.parse(urlString); 
    options.method = method; 
    options.headers = { 
      'Content-Encoding': 'UTF8', 
      'Content-Type': 'application/json', 
      'Authorization' : auth 
    }; 
    log(7, options); 

    var req = http.request(options, function (res) { 
     var data = ""; 
     res.setEncoding('UTF8'); 
     res.on('data', function (chunk) { 
      data += chunk; 
     }); 
     res.on('end', function (chunk) { 
      var response = JSON.parse(data); 
      log(5, response); 
      callback(response); 
     }); 
    }); 
    if (payload) { 
     req.write(payload); 
    } 
    req.end(); 
} 


/** 
* This simple logger logs message depending on the log_level set at 
* the top of this file. 
*/ 
function log(level, message) { 
    if (level<=log_level) { 
     console.log(message); 
    } 
} 

एक ही फ़ोल्डर में आप इस फाइल को संग्रहीत की गई इस आदेश पर अमल:

npm install expresso 

जारी करके परीक्षण चलाएँ

यहां नमूने Node.js परीक्षण है कि CouchDB के साथ संचार है यह आदेश:

node_modules/expresso/bin/expresso test.js 

यह उपरोक्त आदेश से कंसोल आउटपुट है:

GET http://localhost:5984/cartoons/_all_docs 
GET http://localhost:5984/test_db_cartoon 
{ error: 'not_found', reason: 'no_db_file' } 
PUT http://localhost:5984/test_db_cartoon 
{ total_rows: 7, 
    offset: 0, 
    rows: 
    [ { id: 'Batman', key: 'Batman', value: [Object] }, 
    { id: 'Donald Duck', key: 'Donald Duck', value: [Object] }, 
    { id: 'Iron Man', key: 'Iron Man', value: [Object] }, 
    { id: 'Mickey Mouse', key: 'Mickey Mouse', value: [Object] }, 
    { id: 'Spider-Man', key: 'Spider-Man', value: [Object] }, 
    { id: 'Superman', key: 'Superman', value: [Object] }, 
    { id: '_design/select', key: '_design/select', value: [Object] } ] } 
{ ok: true } 
PUT http://localhost:5984/test_db_cartoon/Donald+Duck 
{ ok: true, 
    id: 'Donald Duck', 
    rev: '1-1c431dfb2c46991ec999743830a5363b' } 
GET http://localhost:5984/test_db_cartoon/Donald+Duck 
{ _id: 'Donald Duck', 
    _rev: '1-1c431dfb2c46991ec999743830a5363b', 
    publisher: 'Walt Disney' } 

    100% 2 tests 

आप आसानी से अतिरिक्त

exports.testname = function(beforeExit, assert) { 
} 
के साथ परीक्षण का विस्तार कर सकते