<commit_before>import should from 'should/as-function';
import Syncano from '../../src/syncano';
import {suffix, credentials, createCleaner } from './utils';
import _ from 'lodash';

describe('BatchManager', function() {
  this.timeout(15000);

  const cleaner = createCleaner();
  let connection = null;
  let Class = null;
  let Model = null;
  let Objects = null;
  let Instance = null;
  let Manager = null;
  const instanceName = suffix.get('name');
  const className = suffix.get('class');
  const classData = {
    name: className,
    instanceName,
    schema: [
      { name: 'name', type: 'string'}
    ]
  };

  before(function() {
    connection = Syncano(credentials.getCredentials());
    Instance = connection.Instance;
    Class = connection.Class;
    Model = connection.DataObject;
    Manager = connection.BatchManager({instanceName});

    Objects = _.map(_.range(10), (int) => {
      return { object: Model({ name: `name-${int}`, instanceName, className, id: int+1 }), action: 'create' }
    });

    return Instance.please().create({name: instanceName}).then(() => {
      return Class.please().create(classData);
    });
  });

  after(function() {
    return Instance.please().delete({name: instanceName});
  });

  afterEach(function() {
    Manager.removeObjects();
    return cleaner.clean();
  });

  it('should validate objects added via the #addObjects() method', function() {
    should(() => {
      Manager.addObjects({ something: 'thing' })
    }).throw(/properly formatted object models/);
  });

  it('should validate objects added via the #addSingleObject() method', function() {
    should(() => {
      Manager.addSingleObject({ something: 'thing' }, 'save');
    }).throw(/not a valid model/);
  });

  it('should be able to batch create objects', function() {
    return Manager.addObjects(Objects).batch()
      .then((objects) => {
        should(objects).be.an.Array().with.length(10);
      })
   });

  it('should be able to batch update objects', function() {
   const updateObjects = _.map(_.take(Objects, 2), (obj) => {
     return _.assign({}, obj, { object: Model({instanceName, className, id: obj.object.id, name: 'new_name'}), action: 'update'})
   });
   return new Promise((resolve) => {
     setInterval(() => resolve(), 1000);
   })
   .then(() => {
     return Manager.addObjects(updateObjects).batch()
   })
   .then(() => {
     return Model.please().list({instanceName, className}).pageSize(2);
   })
   .then((objects) => {
     should(objects).be.an.Array().with.length(2);
     should(objects[0]).have.property('name').which.is.String().equal('new_name');
     should(objects[1]).have.property('name').which.is.String().equal('new_name');
   })
  });

  it('should be able to batch different actions', function() {
    const mixedObjects = [
      { object: Objects[0].object, action: 'delete'},
      { object: Model({ instanceName, className, name: 'new_name2', id: 2}), action: 'update'},
      { object: Model({ name: 'created_object', instanceName, className}), action: 'create' }
    ]
    return new Promise((resolve) => {
      setInterval(() => resolve(), 1000);
    })
    .then(() => {
      return Manager.addObjects(mixedObjects).batch()
    })
    .then(() => {
      return Model.please().list({instanceName, className});
    })
    .then((objects) => {
      should(objects).be.an.Array().with.length(10);
      should(objects[0]).have.property('name').which.is.String().equal('new_name2');<commit_msg>[LIB-804] Test indents<commit_after>import should from 'should/as-function';
import Syncano from '../../src/syncano';
import {suffix, credentials, createCleaner } from './utils';
import _ from 'lodash';

describe('BatchManager', function() {
  this.timeout(15000);

  const cleaner = createCleaner();
  let connection = null;
  let Class = null;
  let Model = null;
  let Objects = null;
  let Instance = null;
  let Manager = null;
  const instanceName = suffix.get('name');
  const className = suffix.get('class');
  const classData = {
    name: className,
    instanceName,
    schema: [
      { name: 'name', type: 'string'}
    ]
  };

  before(function() {
    connection = Syncano(credentials.getCredentials());
    Instance = connection.Instance;
    Class = connection.Class;
    Model = connection.DataObject;
    Manager = connection.BatchManager({instanceName});

    Objects = _.map(_.range(10), (int) => {
      return { object: Model({ name: `name-${int}`, instanceName, className, id: int+1 }), action: 'create' }
    });

    return Instance.please().create({name: instanceName}).then(() => {
      return Class.please().create(classData);
    });
  });

  after(function() {
    return Instance.please().delete({name: instanceName});
  });

  afterEach(function() {
    Manager.removeObjects();
    return cleaner.clean();
  });

  it('should validate objects added via the #addObjects() method', function() {
    should(() => {
      Manager.addObjects({ something: 'thing' })
    }).throw(/properly formatted object models/);
  });

  it('should validate objects added via the #addSingleObject() method', function() {
    should(() => {
      Manager.addSingleObject({ something: 'thing' }, 'save');
    }).throw(/not a valid model/);
  });

  it('should be able to batch create objects', function() {
    return Manager.addObjects(Objects).batch()
      .then((objects) => {
        should(objects).be.an.Array().with.length(10);
      })
   });

  it('should be able to batch update objects', function() {
    const updateObjects = _.map(_.take(Objects, 2), (obj) => {
      return _.assign({}, obj, { object: Model({instanceName, className, id: obj.object.id, name: 'new_name'}), action: 'update'})
    });
    return new Promise((resolve) => {
      setInterval(() => resolve(), 1000);
    })
    .then(() => {
      return Manager.addObjects(updateObjects).batch()
    })
    .then(() => {
      return Model.please().list({instanceName, className}).pageSize(2);
    })
    .then((objects) => {
      should(objects).be.an.Array().with.length(2);
      should(objects[0]).have.property('name').which.is.String().equal('new_name');
      should(objects[1]).have.property('name').which.is.String().equal('new_name');
    })
  });

  it('should be able to batch different actions', function() {
    const mixedObjects = [
      { object: Objects[0].object, action: 'delete'},
      { object: Model({ instanceName, className, name: 'new_name2', id: 2}), action: 'update'},
      { object: Model({ name: 'created_object', instanceName, className}), action: 'create' }
    ]
    return new Promise((resolve) => {
      setInterval(() => resolve(), 1000);
    })
    .then(() => {
      return Manager.addObjects(mixedObjects).batch()
    })
    .then(() => {
      return Model.please().list({instanceName, className});
    })
    .then((objects) => {
      should(objects).be.an.Array().with.length(10);
      should(objects[0]).have.property('name').which.is.String().equal('new_name2');
{'fraction_non_alphanumeric': 0.1852116704805492, 'fraction_numerical': 0.009010297482837529, 'mean_word_length': 3.474088291746641, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '2960843', 'n_tokens_mistral': 2124, 'n_tokens_neox': 2046, 'n_words': 465}