<commit_before>'use strict';

var _ = require('lodash');
var util = require('util');
var expect = require('chai').expect;
var path = require('path');
var BluebirdPromise = require('bluebird');
var Condition = require('../../lib/condition'),
  w = Condition.w;

require('../helpers/model');

var shared = {};

shared.shouldRunMigrationsAndQueries = function(it) {
  var db; before(function() { db = this.db; });

  describe('with migrations applied', function() {
    before(function(done) {
      var migration =
        path.join(__dirname, '../fixtures/migrations/blog');
      this.migrator = db.migrator(migration);
      this.migrator.migrate().then(function() { done(); }, done);
    });

    after(function(done) {
      this.migrator.rollback().then(function() { done(); }, done);
    });

    afterEach(function(done) {
      this.resetSequence('articles').then(function() { done(); }, done);
    });

    it('can insert, update, and delete data', function(done) {
      BluebirdPromise.bind({})
      .then(function() {
        return db
          .insert('articles', { title: 'Title 1', body: 'Contents 1'});
      }).get('rows').get('0')
      .then(function(article) { expect(article).to.not.exist; })
      .then(function() {
        return db
          .insert('articles', { title: 'Title 2', body: 'Contents 2'})
          .returning('id');
      }).get('rows').get('0')
      .then(function(details) { expect(details).to.eql({ id: 2 }); })
      .then(function() { return db.select('articles'); }).get('rows')
      .then(function(articles) {
        expect(_.sortBy(articles, 'id')).to.eql([
          { id: 1, title: 'Title 1', body: 'Contents 1'},
          { id: 2, title: 'Title 2', body: 'Contents 2'}
        ]);
      })
      .then(function() {
        return db.update('articles', { title: 'Updated' }).where({ id: 1 });
      })
      .then(function() { return db.select('articles'); }).get('rows')
      .then(function(articles) {
        expect(_.sortBy(articles, 'id')).to.eql([
          { id: 1, title: 'Updated', body: 'Contents 1'},
          { id: 2, title: 'Title 2', body: 'Contents 2'}
        ]);
      })
      .then(function() { return db.delete('articles'); })
      .then(function() { return db.select('articles'); }).get('rows')
      .then(function(articles) {
        expect(articles).to.eql([]);
      })
      .done(done, done);
    });

    it('can create, update, read, and delete models', function(done) {

      var Article = db.model('article').reopen({
        title: db.attr(),
        body: db.attr(),
        comments: db.hasMany()
      });

      var Comment = db.model('comment').reopen({
        email: db.attr(),
        body: db.attr(),
        article: db.belongsTo()
      });

      BluebirdPromise.bind({})
      .then(function() {
        this.article1 = Article.create({ title: 'News', body: 'Azul 1.0' });
        return this.article1.save();
      })
      .then(function() {
        this.article2 = Article.create({ title: 'Update', body: 'Azul 2.0' });
        return this.article2.save();
      })
      .then(function() {
        this.comment1 = this.article1.createComment({
          email: '<EMAIL>', body: 'Sweet initial release.'
        });
        return this.comment1.save();
      })
      .then(function() {
        this.comment2 = this.article1.createComment({
          email: '<EMAIL>', body: 'Great initial release!'
        });
        return this.comment2.save();
      })
      .then(function() {
        this.comment3 = this.article2.createComment({
          email: '<EMAIL>', body: 'Good update.'
        });
        return this.comment3.save();
      })
      .then(function() {
        this.comment4 = this.article2.createComment({
          email: '<EMAIL>', body: 'Rolex watches.'
        });
        return this.comment4.save();
      })
      .then(function() { return Article.objects.fetch(); })
      .then(function(articles) {
        expect(_.map(articles, 'attrs')).to.eql([
          { id: 1, title: 'News', body: 'Azul 1.0' },
          { id: 2, title: 'Update', body: 'Azul 2.0' },
        ]);
      })
      .then(function() {
        return Article.objects.where({ 'comments.body[icontains]': 'rolex' });
      })
      .then(function(articles) {
        expect(_.map(articles, 'attrs')).to.eql([
          { id: 2, title: 'Update', body: 'Azul 2.0' },
        ]);
      })
      .then(function() {
        return Article.objects
          .where({ 'comments.body[icontains]': 'initial' });
      })
      .then(function(articles) {
        expect(_.map(articles, 'attrs')).to.eql([
          { id: 1, title: 'News', body: 'Azul 1.0' },
          { id: 1, title: 'News', body: 'Azul 1.0' },
        ]);
      })
      .then(function() {
        return Comment.objects.where({ 'article.title': 'News' });
      })
      .then(function(articles) {
        expect(_.map(articles, 'attrs')).to.eql([
          { id: 1, article_id: 1,
            email: '<EMAIL>', body: 'Sweet initial release.' },
          { id: 2, article_id: 1,
            email: '<EMAIL>', body: 'Great initial release!' }
        ]);<commit_msg>Fixed JSHint warning.<commit_after>'use strict';

var _ = require('lodash');
var util = require('util');
var expect = require('chai').expect;
var path = require('path');
var BluebirdPromise = require('bluebird');
var Condition = require('../../lib/condition'),
  w = Condition.w;

require('../helpers/model');

var shared = {};

shared.shouldRunMigrationsAndQueries = function(it) {
  var db; before(function() { db = this.db; });

  describe('with migrations applied', function() {
    before(function(done) {
      var migration =
        path.join(__dirname, '../fixtures/migrations/blog');
      this.migrator = db.migrator(migration);
      this.migrator.migrate().then(function() { done(); }, done);
    });

    after(function(done) {
      this.migrator.rollback().then(function() { done(); }, done);
    });

    afterEach(function(done) {
      this.resetSequence('articles').then(function() { done(); }, done);
    });

    it('can insert, update, and delete data', function(done) {
      BluebirdPromise.bind({})
      .then(function() {
        return db
          .insert('articles', { title: 'Title 1', body: 'Contents 1'});
      }).get('rows').get('0')
      .then(function(article) { expect(article).to.not.exist; })
      .then(function() {
        return db
          .insert('articles', { title: 'Title 2', body: 'Contents 2'})
          .returning('id');
      }).get('rows').get('0')
      .then(function(details) { expect(details).to.eql({ id: 2 }); })
      .then(function() { return db.select('articles'); }).get('rows')
      .then(function(articles) {
        expect(_.sortBy(articles, 'id')).to.eql([
          { id: 1, title: 'Title 1', body: 'Contents 1'},
          { id: 2, title: 'Title 2', body: 'Contents 2'}
        ]);
      })
      .then(function() {
        return db.update('articles', { title: 'Updated' }).where({ id: 1 });
      })
      .then(function() { return db.select('articles'); }).get('rows')
      .then(function(articles) {
        expect(_.sortBy(articles, 'id')).to.eql([
          { id: 1, title: 'Updated', body: 'Contents 1'},
          { id: 2, title: 'Title 2', body: 'Contents 2'}
        ]);
      })
      .then(function() { return db.delete('articles'); })
      .then(function() { return db.select('articles'); }).get('rows')
      .then(function(articles) {
        expect(articles).to.eql([]);
      })
      .done(done, done);
    });

    it('can create, update, read, and delete models', function(done) {

      var Article = db.model('article').reopen({
        title: db.attr(),
        body: db.attr(),
        comments: db.hasMany()
      });

      var Comment = db.model('comment').reopen({
        email: db.attr(),
        body: db.attr(),
        article: db.belongsTo()
      });

      BluebirdPromise.bind({})
      .then(function() {
        this.article1 = Article.create({ title: 'News', body: 'Azul 1.0' });
        return this.article1.save();
      })
      .then(function() {
        this.article2 = Article.create({ title: 'Update', body: 'Azul 2.0' });
        return this.article2.save();
      })
      .then(function() {
        this.comment1 = this.article1.createComment({
          email: '<EMAIL>', body: 'Sweet initial release.'
        });
        return this.comment1.save();
      })
      .then(function() {
        this.comment2 = this.article1.createComment({
          email: '<EMAIL>', body: 'Great initial release!'
        });
        return this.comment2.save();
      })
      .then(function() {
        this.comment3 = this.article2.createComment({
          email: '<EMAIL>', body: 'Good update.'
        });
        return this.comment3.save();
      })
      .then(function() {
        this.comment4 = this.article2.createComment({
          email: '<EMAIL>', body: 'Rolex watches.'
        });
        return this.comment4.save();
      })
      .then(function() { return Article.objects.fetch(); })
      .then(function(articles) {
        expect(_.map(articles, 'attrs')).to.eql([
          { id: 1, title: 'News', body: 'Azul 1.0' },
          { id: 2, title: 'Update', body: 'Azul 2.0' },
        ]);
      })
      .then(function() {
        return Article.objects.where({ 'comments.body[icontains]': 'rolex' });
      })
      .then(function(articles) {
        expect(_.map(articles, 'attrs')).to.eql([
          { id: 2, title: 'Update', body: 'Azul 2.0' },
        ]);
      })
      .then(function() {
        return Article.objects
          .where({ 'comments.body[icontains]': 'initial' });
      })
      .then(function(articles) {
        expect(_.map(articles, 'attrs')).to.eql([
          { id: 1, title: 'News', body: 'Azul 1.0' },
          { id: 1, title: 'News', body: 'Azul 1.0' },
        ]);
      })
      .then(function() {
        return Comment.objects.where({ 'article.title': 'News' });
      })
      .then(function(articles) {
        expect(_.map(articles, 'attrs')).to.eql([
          { id: 1, 'article_id': 1,
            email: '<EMAIL>', body: 'Sweet initial release.' },
          { id: 2, 'article_id': 1,
            email: '<EMAIL>', body: 'Great initial release!' }
        ]);
{'fraction_non_alphanumeric': 0.19651208106001558, 'fraction_numerical': 0.011301636788776305, 'mean_word_length': 2.294287548138639, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5698719', 'n_tokens_mistral': 3269, 'n_tokens_neox': 3176, 'n_words': 660}