<commit_before>var util = require('util')
  , fs = require('fs')
  , assert = require('assert')
  , nano = require('nano')
  , clone = require('clone')
  , request = require('request')
  , path = require('path');

var nano = require('nano')('http://seb:seb@127.0.0.1:5984'); //connect as admin
var registry = nano.db.use('registry')
  , _users = nano.db.use('_users');

function rurl(path){
  return 'http://127.0.0.1:3000' + path
};

function rm(db, id, cb){
  db['head'](id, function(err, _, headers){
    var etag = (headers && headers.etag.replace(/^"(.*)"$/, '$1')) || '';    
    db['destroy'](id, etag, function(err, _, _){
      cb();
    });
  });
};

var pass = '<PASSWORD>';
var userData = {
  name: 'user_a',
  salt: '<PASSWORD>',
  password_sha: '<PASSWORD>',
  email: '<EMAIL>'
};

var dpkg = {
  name: 'test-dpkg',
  version: '0.0.0',
  resources: [
    {
      'name': 'inline',
      'format': 'json',
      'schema': { 'fields': [ {'name': 'a', 'type': 'string'}, {'name': 'b', 'type': 'integer'}] },
      'data': [{'a': 'a', 'b': 1}, {'a': 'x', 'b': 2} ]
    }
  ],
  date: (new Date()).toISOString()
};

var maintainers = [{'name': 'user_a', 'email': '<EMAIL>'}, {'name': 'user_b','email': '<EMAIL>'}];

function createFixture(done){
  request.put({url: rurl('/adduser/user_a'), json: userData}, function(err, resp, body){
    var userB = clone(userData);
    userB.name = 'user_b';
    request.put({url: rurl('/adduser/user_b'), json: userB}, function(err, resp, body){
      var userC = clone(userData);
      userC.name = 'user_c';
      request.put({url: rurl('/adduser/user_c'), json: userC}, function(err, resp, body){

        request.put( { url: rurl('/test-dpkg/0.0.0'), auth: {user:'user_a', pass: <PASSWORD>}, json: dpkg }, function(err, resp, body){
          var mydpkg = clone(dpkg);
          mydpkg.version = '0.0.1';
          request.put( { url: rurl('/test-dpkg/0.0.1'), auth: {user:'user_a', pass: <PASSWORD>}, json: mydpkg }, function(err, resp, body){

            request.post( {url: rurl('/owner/add'), auth: {user:'user_a', pass: <PASSWORD>},  json: {username: 'user_b', dpkgName: 'test-dpkg'}}, function(err, resp, body){
              done();
            });

          });
        });
      });
    });
  });
};

function rmAll(done){
  rm(_users, 'org.couchdb.user:user_a', function(){
    rm(_users, 'org.couchdb.user:user_b', function(){
      rm(_users, 'org.couchdb.user:user_c', function(){
        rm(registry, 'test-dpkg@0.0.0', function(){
          rm(registry, 'test-dpkg@0.0.1', function(){
            rm(registry, 'test-dpkg@0.0.2', function(){
              done();
            });
          });
        });
      });
    });
  });
};


describe('data-registry', function(){

  describe('no side effect', function(){

    before(function(done){
      createFixture(done);
    });
    
    it('should have an user', function(done){
      _users.get('org.couchdb.user:user_a', function(err, body){
        assert.equal(body.name, userData.name);
        done();
      });
    });

    it('should have a dpkg', function(done){
      registry.get('test-dpkg@0.0.0', function(err, body){
        assert.equal(body._id, 'test-dpkg@0.0.0');      
        done();
      });
    });

    it('should retrieve dpkg with url instead of resources', function(done){
      request(rurl('/test-dpkg/0.0.0'), function(err, resp, body){
        var expected = clone(dpkg);
        delete expected.resources[0].data;
        delete expected.date;
        expected.resources[0].url = 'http://localhost:3000/test-dpkg/0.0.0/inline'
        assert.deepEqual(JSON.parse(body), expected);      
        done();
      });
    });

    it('should retrieve dpkg as is', function(done){
      request(rurl('/test-dpkg/0.0.0?clone=true'), function(err, resp, body){
        var expected = clone(dpkg);
        delete expected.date;
        assert.deepEqual(JSON.parse(body), expected);      
        done();
      });
    });

    it('should retrieve a resource', function(done){
      request(rurl('/test-dpkg/0.0.0/inline'), function(err, resp, body){
        assert.deepEqual(JSON.parse(body), dpkg.resources[0].data);      
        done();
      });
    });

    it('should retrieve all the versions of test-dpkg', function(done){
      request(rurl('/test-dpkg'), function(err, resp, body){
        assert.deepEqual(JSON.parse(body), ['0.0.0', '0.0.1']);      
        done();
      });
    });

    it('should retrieve the latest version of test-dpkg', function(done){
      request(rurl('/test-dpkg/latest'), function(err, resp, body){
        assert.equal(JSON.parse(body).version, '0.0.1');      
        done();<commit_msg>test meta=true<commit_after>var util = require('util')
  , fs = require('fs')
  , assert = require('assert')
  , nano = require('nano')
  , clone = require('clone')
  , request = require('request')
  , path = require('path');

var nano = require('nano')('http://seb:seb@127.0.0.1:5984'); //connect as admin
var registry = nano.db.use('registry')
  , _users = nano.db.use('_users');

function rurl(path){
  return 'http://127.0.0.1:3000' + path
};

function rm(db, id, cb){
  db['head'](id, function(err, _, headers){
    var etag = (headers && headers.etag.replace(/^"(.*)"$/, '$1')) || '';    
    db['destroy'](id, etag, function(err, _, _){
      cb();
    });
  });
};

var pass = '<PASSWORD>';
var userData = {
  name: 'user_a',
  salt: '209c14190cf00f0fed293a666c46aa617957dfff23d30afd2615cc28<PASSWORD>4',
  password_<PASSWORD>: '<PASSWORD>',
  email: '<EMAIL>'
};

var dpkg = {
  name: 'test-dpkg',
  version: '0.0.0',
  resources: [
    {
      'name': 'inline',
      'format': 'json',
      'schema': { 'fields': [ {'name': 'a', 'type': 'string'}, {'name': 'b', 'type': 'integer'}] },
      'data': [{'a': 'a', 'b': 1}, {'a': 'x', 'b': 2} ]
    }
  ],
  date: (new Date()).toISOString()
};

var maintainers = [{'name': 'user_a', 'email': '<EMAIL>'}, {'name': 'user_b','email': '<EMAIL>'}];

function createFixture(done){
  request.put({url: rurl('/adduser/user_a'), json: userData}, function(err, resp, body){
    var userB = clone(userData);
    userB.name = 'user_b';
    request.put({url: rurl('/adduser/user_b'), json: userB}, function(err, resp, body){
      var userC = clone(userData);
      userC.name = 'user_c';
      request.put({url: rurl('/adduser/user_c'), json: userC}, function(err, resp, body){

        request.put( { url: rurl('/test-dpkg/0.0.0'), auth: {user:'user_a', pass: <PASSWORD>}, json: dpkg }, function(err, resp, body){
          var mydpkg = clone(dpkg);
          mydpkg.version = '0.0.1';
          request.put( { url: rurl('/test-dpkg/0.0.1'), auth: {user:'user_a', pass: <PASSWORD>}, json: mydpkg }, function(err, resp, body){

            request.post( {url: rurl('/owner/add'), auth: {user:'user_a', pass: <PASSWORD>},  json: {username: 'user_b', dpkgName: 'test-dpkg'}}, function(err, resp, body){
              done();
            });

          });
        });
      });
    });
  });
};

function rmAll(done){
  rm(_users, 'org.couchdb.user:user_a', function(){
    rm(_users, 'org.couchdb.user:user_b', function(){
      rm(_users, 'org.couchdb.user:user_c', function(){
        rm(registry, 'test-dpkg@0.0.0', function(){
          rm(registry, 'test-dpkg@0.0.1', function(){
            rm(registry, 'test-dpkg@0.0.2', function(){
              done();
            });
          });
        });
      });
    });
  });
};


describe('data-registry', function(){

  describe('no side effect', function(){

    before(function(done){
      createFixture(done);
    });
    
    it('should have an user', function(done){
      _users.get('org.couchdb.user:user_a', function(err, body){
        assert.equal(body.name, userData.name);
        done();
      });
    });

    it('should have a dpkg', function(done){
      registry.get('test-dpkg@0.0.0', function(err, body){
        assert.equal(body._id, 'test-dpkg@0.0.0');      
        done();
      });
    });

    it('should retrieve dpkg with url instead of resources', function(done){
      request(rurl('/test-dpkg/0.0.0'), function(err, resp, body){
        var expected = clone(dpkg);
        delete expected.resources[0].data;
        delete expected.date;
        expected.resources[0].url = 'http://localhost:3000/test-dpkg/0.0.0/inline'
        assert.deepEqual(JSON.parse(body), expected);      
        done();
      });
    });

    it('should retrieve dpkg as is', function(done){
      request(rurl('/test-dpkg/0.0.0?clone=true'), function(err, resp, body){
        var expected = clone(dpkg);
        delete expected.date;
        assert.deepEqual(JSON.parse(body), expected);      
        done();
      });
    });

    it('should retrieve a resource', function(done){
      request(rurl('/test-dpkg/0.0.0/inline'), function(err, resp, body){
        assert.deepEqual(JSON.parse(body), dpkg.resources[0].data);      
        done();
      });
    });

    it('should retrieve the meta data of a resource only', function(done){
      request(rurl('/test-dpkg/0.0.0/inline?meta=true'), function(err, resp, body){
        var expected = clone(dpkg.resources[0]);
        delete expected.data;
        assert.deepEqual(JSON.parse(body), expected);      
        done();
      });
    });

    it('should retrieve all the versions of test-dpkg', function(done){
      request(rurl('/test-dpkg'), function(err, resp, body){
        assert.deepEqual(JSON.parse(body), ['0.0.0', '0.0.1']);      
        done();
      });
    });

    it('should retrieve the latest version of test-dpkg', function(done){
      request(rurl('/test-dpkg/latest'), function(err, resp, body){
        assert.equal(JSON.parse(body).version, '0.0.1');      
        done();
{'fraction_non_alphanumeric': 0.21091021162934045, 'fraction_numerical': 0.020135607150195193, 'mean_word_length': 2.779114906832298, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4349529', 'n_tokens_mistral': 3425, 'n_tokens_neox': 3340, 'n_words': 656}