<commit_before>'use strict';

var assert = require('assert');

var helpers = require('./helpers');

var Account = require('../../lib/resource/Account');
var CustomData = require('../../lib/resource/CustomData');

describe('Account', function() {
  var client, directory, account, creationResult;

  before(function(done) {
    helpers.getClient(function(_client) {
      client = _client;

      client.createDirectory({ name: helpers.uniqId() }, function(err, _directory) {
        directory = _directory;

        directory.createAccount(helpers.fakeAccount(), function(err,_account) {
          account = _account;
          creationResult = [err, _account];
          done();
        });
      });
    });
  });

  after(function(done) {
    account.delete(function(err) {
      if (err) {
        return done(err);
      }

      directory.delete(done);
    });
  });

  it('should be create-able from a directory instance', function() {
    assert.equal(creationResult[0], null); // did not error
    assert(account instanceof Account);
  });

  describe('customData', function() {
    describe('via customData.get', function() {
      var customData;

      before(function(done) {
        account.customData.get(function(err, _customData) {
          if (err) {
            return done(err);
          }

          customData = _customData;
          done();
        });
      });

      it('should be get-able', function() {
        assert(customData instanceof CustomData);
        assert.equal(customData.href, account.href + '/customData');
      });

      describe('when saved and re-fetched', function() {
        var customDataAfterGet;
        var propertyName = helpers.uniqId();
        var propertyValue = helpers.uniqId();

        before(function(done) {
          customData[propertyName] = propertyValue;

          customData.save(function(err) {
            if (err) {
              return done(err);
            }

            account.customData.get(function(err, customData) {
              if (err) {
                return done(err);
              }

              customDataAfterGet = customData;
              done();
            });
          });
        });

        it('should have the new property persisted', function() {
          assert.equal(customDataAfterGet[propertyName], propertyValue);
        });
      });
    });

    describe('via getCustomData', function() {
      var customData;

      before(function(done) {
        account.getCustomData(function(err, _customData) {
          if (err) {
            return done(err);
          }

          customData = _customData;
          done();
        });
      });

      it('should be get-able', function() {
        assert(customData instanceof CustomData);
        assert.equal(customData.href, account.href + '/customData');
      });

      describe('when saved and re-fetched', function() {
        var customDataAfterGet;
        var propertyName = helpers.uniqId();
        var propertyValue = helpers.uniqId();

        before(function(done) {
          customData[propertyName] = propertyValue;

          customData.save(function(err) {
            if (err) {
              return done(err);
            }

            account.getCustomData(function(err, customData) {
              if (err) {
                return done(err);
              }

              customDataAfterGet = customData;
              done();
            });
          });
        });

        it('should have the new property persisted', function() {
          assert.equal(customDataAfterGet[propertyName], propertyValue);
        });
      });
    });

    describe('via resource expansion', function() {
      function getExpandedAccount(cb) {
        client.getAccount(account.href, { expand: 'customData' }, function(err, account) {
          if (err) {
            throw err;
          }

          cb(account);
        });
      }

      var customData;

      before(function(done) {
        getExpandedAccount(function(account) {
          customData = account.customData;
          done();
        });
      });

      it('should be get-able', function() {
        assert(customData instanceof CustomData);
        assert.equal(customData.href, account.href + '/customData');
      });

      describe('when saved and re-fetched', function() {
        var customDataAfterGet;
        var propertyName = helpers.uniqId();
        var propertyValue = helpers.uniqId();

        before(function(done) {
          customData[propertyName] = propertyValue;

          customData.save(function(err) {
            if (err) {
              return done(err);
            }

            getExpandedAccount(function(account) {
              customDataAfterGet = account.customData;
              done();
            });
          });
        });

        it('should have the new property persisted', function() {
          assert.equal(customDataAfterGet[propertyName], propertyValue);
        });
      });
    });
  });
});
<commit_msg>Adding test to ensure we can remove all customData fields without error.<commit_after>'use strict';

var assert = require('assert');

var helpers = require('./helpers');

var Account = require('../../lib/resource/Account');
var CustomData = require('../../lib/resource/CustomData');

describe('Account', function() {
  var client, directory, account, creationResult;

  before(function(done) {
    helpers.getClient(function(_client) {
      client = _client;

      client.createDirectory({ name: helpers.uniqId() }, function(err, _directory) {
        directory = _directory;

        directory.createAccount(helpers.fakeAccount(), function(err,_account) {
          account = _account;
          creationResult = [err, _account];
          done();
        });
      });
    });
  });

  after(function(done) {
    account.delete(function(err) {
      if (err) {
        return done(err);
      }

      directory.delete(done);
    });
  });

  it('should be create-able from a directory instance', function() {
    assert.equal(creationResult[0], null); // did not error
    assert(account instanceof Account);
  });

  describe('customData', function() {
    describe('via customData.get', function() {
      var customData;

      before(function(done) {
        account.customData.get(function(err, _customData) {
          if (err) {
            return done(err);
          }

          customData = _customData;
          done();
        });
      });

      it('should be get-able', function() {
        assert(customData instanceof CustomData);
        assert.equal(customData.href, account.href + '/customData');
      });

      describe('when saved and re-fetched', function() {
        var customDataAfterGet;
        var propertyName = helpers.uniqId();
        var propertyValue = helpers.uniqId();

        before(function(done) {
          customData[propertyName] = propertyValue;

          customData.save(function(err) {
            if (err) {
              return done(err);
            }

            account.customData.get(function(err, customData) {
              if (err) {
                return done(err);
              }

              customDataAfterGet = customData;
              done();
            });
          });
        });

        it('should have the new property persisted', function() {
          assert.equal(customDataAfterGet[propertyName], propertyValue);
        });

        it('should allow all added keys to be deleted', function(done) {
          customDataAfterGet.remove(propertyName);
          customDataAfterGet.save(function(err) {
            done(err);
          });
        });
      });
    });

    describe('via getCustomData', function() {
      var customData;

      before(function(done) {
        account.getCustomData(function(err, _customData) {
          if (err) {
            return done(err);
          }

          customData = _customData;
          done();
        });
      });

      it('should be get-able', function() {
        assert(customData instanceof CustomData);
        assert.equal(customData.href, account.href + '/customData');
      });

      describe('when saved and re-fetched', function() {
        var customDataAfterGet;
        var propertyName = helpers.uniqId();
        var propertyValue = helpers.uniqId();

        before(function(done) {
          customData[propertyName] = propertyValue;

          customData.save(function(err) {
            if (err) {
              return done(err);
            }

            account.getCustomData(function(err, customData) {
              if (err) {
                return done(err);
              }

              customDataAfterGet = customData;
              done();
            });
          });
        });

        it('should have the new property persisted', function() {
          assert.equal(customDataAfterGet[propertyName], propertyValue);
        });
      });
    });

    describe('via resource expansion', function() {
      function getExpandedAccount(cb) {
        client.getAccount(account.href, { expand: 'customData' }, function(err, account) {
          if (err) {
            throw err;
          }

          cb(account);
        });
      }

      var customData;

      before(function(done) {
        getExpandedAccount(function(account) {
          customData = account.customData;
          done();
        });
      });

      it('should be get-able', function() {
        assert(customData instanceof CustomData);
        assert.equal(customData.href, account.href + '/customData');
      });

      describe('when saved and re-fetched', function() {
        var customDataAfterGet;
        var propertyName = helpers.uniqId();
        var propertyValue = helpers.uniqId();

        before(function(done) {
          customData[propertyName] = propertyValue;

          customData.save(function(err) {
            if (err) {
              return done(err);
            }

            getExpandedAccount(function(account) {
              customDataAfterGet = account.customData;
              done();
            });
          });
        });

        it('should have the new property persisted', function() {
          assert.equal(customDataAfterGet[propertyName], propertyValue);
        });
      });
    });
  });
});

{'fraction_non_alphanumeric': 0.12755646021130174, 'fraction_numerical': 0.0001938548027527382, 'mean_word_length': 1.9229461756373938, 'pattern_counts': {'":': 0, '<': 3, '<?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': '77548', 'n_tokens_mistral': 2747, 'n_tokens_neox': 2656, 'n_words': 543}