<commit_before>/*jslint indent: 2, nomen: true, maxlen: 120, todo: true, white: false, sloppy: false */
/*global require, describe, beforeEach, it, expect, spyOn, createSpy, createSpyObj */

var FoxxRepository = require("org/arangodb/foxx/repository").Repository,
  Model = require("org/arangodb/foxx/model").Model;

describe('Repository', function () {
  'use strict';
  var TestRepository, instance, prefix, collection, ModelPrototype, model, modelData;

  beforeEach(function () {
    prefix = "myApp";
    collection = null;
    ModelPrototype = createSpy('ModelPrototype');
    model = new ModelPrototype();
    modelData = { fancy: 1 };
    model.forDB = function () {
      return modelData;
    };
  });

  it('should initialize with collection only', function () {
    instance = new FoxxRepository(collection);

    expect(instance.collection).toEqual(collection);
    expect(instance.modelPrototype).toEqual(Model);
  });

  it('should initialize with collection and model prototype', function () {
    instance = new FoxxRepository(collection, {
      model: ModelPrototype
    });

    expect(instance.collection).toEqual(collection);
    expect(instance.modelPrototype).toEqual(ModelPrototype);
  });

  it('should initialize with collection and prefix', function () {
    instance = new FoxxRepository(collection, {
      prefix: prefix
    });

    expect(instance.collection).toEqual(collection);
    expect(instance.modelPrototype).toEqual(Model);
    expect(instance.prefix).toEqual(prefix);
  });

  it('should add a method with extend', function () {
    TestRepository = FoxxRepository.extend({
      test: function () {
        return "test";
      }
    });

    instance = new TestRepository(collection);

    expect(instance.test()).toEqual("test");
  });
});

describe('Repository Methods', function () {
  'use strict';
  var collection,
    instance;

  beforeEach(function () {
    collection = createSpyObj('collection', [
      'all',<commit_msg>Added tests for Repository index magic. Fixes #899.<commit_after>/*jslint indent: 2, nomen: true, maxlen: 120, todo: true, white: false, sloppy: false */
/*global require, describe, beforeEach, it, expect, spyOn, createSpy, createSpyObj */

var FoxxRepository = require("org/arangodb/foxx/repository").Repository,
  Model = require("org/arangodb/foxx/model").Model;

describe('Repository', function () {
  'use strict';
  var TestRepository, instance, prefix, collection, ModelPrototype, model, modelData;

  beforeEach(function () {
    prefix = "myApp";
    collection = null;
    ModelPrototype = createSpy('ModelPrototype');
    model = new ModelPrototype();
    modelData = { fancy: 1 };
    model.forDB = function () {
      return modelData;
    };
  });

  it('should initialize with collection only', function () {
    instance = new FoxxRepository(collection);

    expect(instance.collection).toEqual(collection);
    expect(instance.modelPrototype).toEqual(Model);
  });

  it('should initialize with collection and model prototype', function () {
    instance = new FoxxRepository(collection, {
      model: ModelPrototype
    });

    expect(instance.collection).toEqual(collection);
    expect(instance.modelPrototype).toEqual(ModelPrototype);
  });

  it('should initialize with collection and prefix', function () {
    instance = new FoxxRepository(collection, {
      prefix: prefix
    });

    expect(instance.collection).toEqual(collection);
    expect(instance.modelPrototype).toEqual(Model);
    expect(instance.prefix).toEqual(prefix);
  });

  it('should add a method with extend', function () {
    TestRepository = FoxxRepository.extend({
      test: function () {
        return "test";
      }
    });

    instance = new TestRepository(collection);

    expect(instance.test()).toEqual("test");
  });
});

describe('Repository Indexes', function () {
  'use strict';
  it('should create indexes on instantiation', function () {
    var collection = createSpyObj('collection', [
        'ensureIndex'
      ]),
      indexes = [
        {type: 'skiplist', xyz: 'abcdef'},
        {type: 'geo', more: 'args'},
        {type: 'foo', bar: 'qux'}
      ],
      Repository = FoxxRepository.extend({
        indexes: indexes
      });

    new Repository(collection, {model: Model});

    expect(collection.ensureIndex.calls.count()).toEqual(3);
    expect(collection.ensureIndex).toHaveBeenCalledWith(indexes[0]);
    expect(collection.ensureIndex).toHaveBeenCalledWith(indexes[1]);
    expect(collection.ensureIndex).toHaveBeenCalledWith(indexes[2]);
  });

  it('should add skiplist methods to the prototype', function () {
    expect(FoxxRepository.prototype.range).toBeUndefined();
    var Repository = FoxxRepository.extend({
      indexes: [
        {type: 'skiplist'}
      ]
    });

    expect(typeof Repository.prototype.range).toBe('function');
  });

  it('should add geo methods to the prototype', function () {
    expect(FoxxRepository.prototype.near).toBeUndefined();
    expect(FoxxRepository.prototype.within).toBeUndefined();
    var Repository = FoxxRepository.extend({
      indexes: [
        {type: 'geo'}
      ]
    });
    expect(typeof Repository.prototype.near).toBe('function');
    expect(typeof Repository.prototype.within).toBe('function');
  });

  it('should add fulltext methods to the prototype', function () {
    expect(FoxxRepository.prototype.fulltext).toBeUndefined();
    var Repository = FoxxRepository.extend({
      indexes: [
        {type: 'fulltext'}
      ]
    });
    expect(typeof Repository.prototype.fulltext).toBe('function');
  });
});

describe('Repository Methods', function () {
  'use strict';
  var collection,
    instance;

  beforeEach(function () {
    collection = createSpyObj('collection', [
      'all',
{'fraction_non_alphanumeric': 0.13988919667590027, 'fraction_numerical': 0.002943213296398892, 'mean_word_length': 3.7902155887230515, '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': '7580292', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1611, 'n_words': 352}