<commit_before>var test = require('tape');
var RestCollectionMixins = require('../ampersand-collection-rest-mixin');
var AmpersandCollection = require('ampersand-collection');
var AmpersandModel = require('ampersand-model');


function endAfter (t, after) {
    var count = 0;
    return function () {
        count++;
        if (after === count) {
            t.end();
        }
    };
}

var Model = AmpersandModel.extend({
    props: {
        name: 'string'
    },
    sync: function () {
        this.__syncArgs = arguments;
        return AmpersandModel.prototype.sync.apply(this, arguments);
    }
});

var Collection = AmpersandCollection.extend(RestCollectionMixins, {
    model: Model,
    sync: function () {
        this.__syncArgs = arguments;
        return RestCollectionMixins.sync.apply(this, arguments);
    }
});


test('Existence of methods', function (t) {
    t.plan(5);

    var collection = new Collection();

    t.ok(typeof collection.fetch === 'function');
    t.ok(typeof collection.create === 'function');
    t.ok(typeof collection.sync === 'function');
    t.ok(typeof collection.getOrFetch === 'function');
    t.ok(typeof collection.fetchById === 'function');

    t.end();
});

// ============================
// Tested ported from Backbone
// that have to do with REST stuff
// ============================

test('Fetch', 4, function (t) {
    t.plan(4);

    var collection = new Collection();
    collection.url = '/test';
    collection.fetch();

    t.equal(collection.__syncArgs[0], 'read');
    t.equal(collection.__syncArgs[1], collection);
    t.equal(collection.__syncArgs[2].parse, true);

    collection.fetch({parse: false});
    t.equal(collection.__syncArgs[2].parse, false);

    t.end();
});

test('fetch with an error response triggers an error event', function (t) {
    t.plan(1);

    var collection = new Collection();
    collection.on('error', function () {
        t.ok(true);
        t.end();
    });

    collection.sync = function (method, model, options) { options.error(); };
    collection.fetch();
});

test('ensure fetch only parses once', function (t) {
    t.plan(1);

    var collection = new Collection();

    var counter = 0;
    collection.parse = function(models) {
        counter++;
        return models;
    };
    collection.url = '/test';
    collection.fetch();

    collection.__syncArgs[2].success();

    t.equal(counter, 1);
    t.end();
});

test('create', function (t) {
    t.plan(4);

    var collection = new Collection();
    collection.url = '/test';

    var model = collection.create({name: 'f'}, {wait: true});

    t.equal(model.__syncArgs[0], 'create');
    t.equal(model.__syncArgs[1], model);
    t.equal(model.name, 'f');
    t.equal(model.collection, collection);

    t.end();
});

test('create with validate:true enforces validation', 3, function (t) {
    t.plan(3);

    var ValidatingModel = Model.extend({
        validate: function () {
            return 'fail';
        }
    });

    var ValidatingCollection = Collection.extend({
        model: ValidatingModel
    });

    var collection = new ValidatingCollection();

    collection.on('invalid', function (collection, error, options) {
        t.equal(error, 'fail');
        t.equal(options.validationError, 'fail');
    });

    // TODO:
    // The Backbone test expects it to return false
    // Is that a behavior we want to change?
    // t.equal(collection.create({name: 'foo'}, {validate: true}), false);
    //
    // Substitue a passing test for now
    t.equal(collection.create({name: 'foo'}, {validate: true}), collection.at(0));

    t.end();
});

test('a failing create returns model with errors', function (t) {
    t.plan(2);

    var ValidatingModel = Model.extend({
        validate: function () {
            return 'fail';
        }
    });

    var ValidatingCollection = Collection.extend({
        model: ValidatingModel
    });

    var collection = new ValidatingCollection();
<commit_msg>properly test for model validation on create<commit_after>var test = require('tape');
var RestCollectionMixins = require('../ampersand-collection-rest-mixin');
var AmpersandCollection = require('ampersand-collection');
var AmpersandModel = require('ampersand-model');


function endAfter (t, after) {
    var count = 0;
    return function () {
        count++;
        if (after === count) {
            t.end();
        }
    };
}

var Model = AmpersandModel.extend({
    props: {
        name: 'string'
    },
    sync: function () {
        this.__syncArgs = arguments;
        return AmpersandModel.prototype.sync.apply(this, arguments);
    }
});

var Collection = AmpersandCollection.extend(RestCollectionMixins, {
    model: Model,
    sync: function () {
        this.__syncArgs = arguments;
        return RestCollectionMixins.sync.apply(this, arguments);
    }
});


test('Existence of methods', function (t) {
    t.plan(5);

    var collection = new Collection();

    t.ok(typeof collection.fetch === 'function');
    t.ok(typeof collection.create === 'function');
    t.ok(typeof collection.sync === 'function');
    t.ok(typeof collection.getOrFetch === 'function');
    t.ok(typeof collection.fetchById === 'function');

    t.end();
});

// ============================
// Tested ported from Backbone
// that have to do with REST stuff
// ============================

test('Fetch', 4, function (t) {
    t.plan(4);

    var collection = new Collection();
    collection.url = '/test';
    collection.fetch();

    t.equal(collection.__syncArgs[0], 'read');
    t.equal(collection.__syncArgs[1], collection);
    t.equal(collection.__syncArgs[2].parse, true);

    collection.fetch({parse: false});
    t.equal(collection.__syncArgs[2].parse, false);

    t.end();
});

test('fetch with an error response triggers an error event', function (t) {
    t.plan(1);

    var collection = new Collection();
    collection.on('error', function () {
        t.ok(true);
        t.end();
    });

    collection.sync = function (method, model, options) { options.error(); };
    collection.fetch();
});

test('ensure fetch only parses once', function (t) {
    t.plan(1);

    var collection = new Collection();

    var counter = 0;
    collection.parse = function(models) {
        counter++;
        return models;
    };
    collection.url = '/test';
    collection.fetch();

    collection.__syncArgs[2].success();

    t.equal(counter, 1);
    t.end();
});

test('create', function (t) {
    t.plan(4);

    var collection = new Collection();
    collection.url = '/test';

    var model = collection.create({name: 'f'}, {wait: true});

    t.equal(model.__syncArgs[0], 'create');
    t.equal(model.__syncArgs[1], model);
    t.equal(model.name, 'f');
    t.equal(model.collection, collection);

    t.end();
});

test('create with validate:true enforces validation', function (t) {
    t.plan(3);

    var ValidatingModel = Model.extend({
        validate: function () {
            return 'fail';
        }
    });

    var ValidatingCollection = Collection.extend({
        model: ValidatingModel
    });

    var collection = new ValidatingCollection();

    collection.on('invalid', function (collection, error) {
        t.equal(error, 'fail');
    });

    t.equal(collection.create({name: 'foo'}, {validate: true}), false);
    t.equal(collection.length, 0);

    t.end();
});

test('a failing create returns model with errors', function (t) {
    t.plan(2);

    var ValidatingModel = Model.extend({
        validate: function () {
            return 'fail';
        }
    });

    var ValidatingCollection = Collection.extend({
        model: ValidatingModel
    });

    var collection = new ValidatingCollection();

{'fraction_non_alphanumeric': 0.16988217014113685, 'fraction_numerical': 0.0050498510941344036, 'mean_word_length': 2.9049544994944387, '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': '3162550', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2239, 'n_words': 544}