<commit_before>var assert = require('assert');
var _ = require('lodash');

describe('unique attribute feature', function() {

  /////////////////////////////////////////////////////
  // TEST SETUP
  ////////////////////////////////////////////////////

  var Waterline = require('waterline');
  var defaults = { migrate: 'alter' };
  var waterline;

  var UniqueFixture = require('./support/unique.fixture.js')
  var UniqueModel;

  var id0, id1, email0;


  before(function(done) {
    waterline = new Waterline();
    waterline.loadCollection(UniqueFixture);

    var connections = { uniqueConn: _.clone(Connections.test) };

    waterline.initialize({ adapters: { wl_tests: Adapter }, connections: connections, defaults: defaults }, function(err, ontology) {
      if(err) return done(err);
      UniqueModel = ontology.collections['unique'];

      // Insert 3 Records
      var records = [];
      for(var i=0; i<3; i++) {
        records.push({name: 'testUnique' + i, email: 'email' + i, type: 'unique'});
      }

      UniqueModel.createEach(records, function(err, records) {
        if(err) return done(err);
        id0 = records[0].id;
        id1 = records[1].id;
        email0 = records[0].email.toString();
        done();
      });
    });
  });

  after(function(done) {
    if(!Adapter.hasOwnProperty('drop')) {
      waterline.teardown(done);
    } else {
      UniqueModel.drop(function(err1) {
        waterline.teardown(function(err2) {
          return done(err1 || err2);
        });
      });
    }
  });


  /////////////////////////////////////////////////////
  // TEST METHODS
  ////////////////////////////////////////////////////

  it('should error when creating with a duplicate value', function(done) {
    UniqueModel.create({ email: email0 }, function(err, records) {
      assert(err);
      assert(!records);
      UniqueModel.find({type: 'unique'}).exec(function(err, records) {
        assert(!err);
        assert.equal(records.length, 3);
        done();
      });
    });
  });

  it('should error when updating with a duplicate value', function(done) {
    UniqueModel.update(id1, { email: email0 }).exec(function(err, records) {
      assert(err);
      assert(!records);
      UniqueModel.findOne(id1).exec(function(err, record) {
        assert(!err);
        assert.notEqual(record.email, email0);
        done();
      });
    });
  });

  it('should work (do nothing) when updating a unique field to the same value', function(done) {
    UniqueModel.update(id0, { email: email0 }).exec(function(err, records) {
      assert(!err, 'Expected no error when updating to the same value');
      assert.equal(records.length, 1);
      assert.equal(records[0].id, id0);
      assert.equal(records[0].email, email0);
      done();
    });
  });

});<commit_msg>Separate tests for updating a single record and updating based on criteria.<commit_after>var assert = require('assert');
var _ = require('lodash');

describe('unique attribute feature', function() {

  /////////////////////////////////////////////////////
  // TEST SETUP
  ////////////////////////////////////////////////////

  var Waterline = require('waterline');
  var defaults = { migrate: 'alter' };
  var waterline;

  var UniqueFixture = require('./support/unique.fixture.js')
  var UniqueModel;

  var id0, id1, email0;


  before(function(done) {
    waterline = new Waterline();
    waterline.loadCollection(UniqueFixture);

    var connections = { uniqueConn: _.clone(Connections.test) };

    waterline.initialize({ adapters: { wl_tests: Adapter }, connections: connections, defaults: defaults }, function(err, ontology) {
      if(err) return done(err);
      UniqueModel = ontology.collections['unique'];

      // Insert 3 Records
      var records = [];
      for(var i=0; i<3; i++) {
        records.push({name: 'testUnique' + i, email: 'email' + i, type: 'unique'});
      }

      UniqueModel.createEach(records, function(err, records) {
        if(err) return done(err);
        id0 = records[0].id;
        id1 = records[1].id;
        email0 = records[0].email.toString();
        done();
      });
    });
  });

  after(function(done) {
    if(!Adapter.hasOwnProperty('drop')) {
      waterline.teardown(done);
    } else {
      UniqueModel.drop(function(err1) {
        waterline.teardown(function(err2) {
          return done(err1 || err2);
        });
      });
    }
  });


  /////////////////////////////////////////////////////
  // TEST METHODS
  ////////////////////////////////////////////////////

  it('should error when creating with a duplicate value', function(done) {
    UniqueModel.create({ email: email0 }, function(err, records) {
      assert(err);
      assert(!records);
      UniqueModel.find({type: 'unique'}).exec(function(err, records) {
        assert(!err);
        assert.equal(records.length, 3);
        done();
      });
    });
  });

  it('should error when updating with a duplicate value', function(done) {
    UniqueModel.update(id1, { email: email0 }).exec(function(err, records) {
      assert(err);
      assert(!records);
      UniqueModel.findOne(id1).exec(function(err, record) {
        assert(!err);
        assert.notEqual(record.email, email0);
        done();
      });
    });
  });

  it('should work (do nothing) when updating the field of an existing record to the same value', function(done) {
    UniqueModel.update(id0, { id: id0, name: 'testUnique0', email: email0, type: 'unique' }).exec(function(err, records) {
      assert(!err, 'Expected no error when updating to the same value');
      assert.equal(records.length, 1);
      assert.equal(records[0].id, id0);
      assert.equal(records[0].email, email0);
      done();
    });
  });

  it('should work when updating a unique field to the same value based on search parameters', function(done) {
    UniqueModel.update({email: email0}, { email: email0 }).exec(function(err, records) {
      assert(!err, 'Expected no error when updating to the same value on searched records');
      assert.equal(records.length, 1);
      assert.equal(records[0].id, id0);
      assert.equal(records[0].email, email0);
      done();
    });
  });

});
{'fraction_non_alphanumeric': 0.21100323624595468, 'fraction_numerical': 0.010841423948220064, 'mean_word_length': 2.277306468716861, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '7462046', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1727, 'n_words': 412}