<commit_before>require("internal").flushModuleCache();

var jsunity = require("jsunity"),
  FoxxRepository = require("org/arangodb/foxx/repository").Repository,
  Model = require("org/arangodb/foxx/model").Model,
  stub_and_mock = require("org/arangodb/stub_and_mock"),
  stub = stub_and_mock.stub,
  allow = stub_and_mock.allow,
  expect = stub_and_mock.expect,
  mockConstructor = stub_and_mock.mockConstructor;

function RepositorySpec () {
  var TestRepository, instance, prefix, collection, modelPrototype, model, modelData;

  return {
    setUp: function () {
      prefix = "myApp";
      collection = function () {};
      modelPrototype = function () {};
      model = new modelPrototype();
      modelData = { fancy: 1 };
      model.forDB = function () {
        return modelData;
      };
    },

    testInitializeWithCollectionOnly: function () {
      instance = new FoxxRepository(collection);

      assertEqual(instance.collection, collection);
      assertEqual(instance.modelPrototype, Model);
    },

    testInitializeWithCollectionAndModelPrototype: function () {
      instance = new FoxxRepository(collection, {
        model: modelPrototype
      });

      assertEqual(instance.collection, collection);
      assertEqual(instance.modelPrototype, modelPrototype);
    },

    testInitializeWithCollectionAndPrefix: function () {
      instance = new FoxxRepository(collection, {
        prefix: prefix
      });

      assertEqual(instance.collection, collection);
      assertEqual(instance.modelPrototype, Model);
      assertEqual(instance.prefix, prefix);
    },

    testAddingAMethodWithExtend: function () {
      TestRepository = FoxxRepository.extend({
        test: function() {
          return "test";
        }
      });

      instance = new TestRepository(collection);

      assertEqual(instance.test(), "test");
    },

    testNothing: function () {
      [
        "save",

        "removeById",
        "removeByExample",

        "replaceById",
        "replaceByExample",

        "updateById",
        "updateByExample",

        "byId",
        "byExample",
        "firstExample"
      ].forEach(function (f) {
        instance = new FoxxRepository(collection);
        assertTrue(instance[f] !== undefined);
      });
    }
  };
}

function RepositoryMethodsSpec() {
  var instance,
    collection,
    ModelPrototype,
    model,
    modelData,
    data,
    example,
    id,
    id_and_rev;

  return {
    setUp: function () {
      ModelPrototype = stub();
      id_and_rev = stub();
      modelData = stub();
      example = stub();
      model = stub();
      data = stub();
      collection = stub();
      id = stub();

      allow(model)
        .toReceive("forDB")
        .andReturn(modelData);

      instance = new FoxxRepository(collection, { model: ModelPrototype });
    },

    testSave: function () {
      expect(model)
        .toReceive("set")
        .withArguments(id_and_rev);

      expect(collection)
        .toReceive("save")
        .withArguments(modelData)
        .andReturn(id_and_rev);

      instance.save(model);

      model.assertIsSatisfied();
      collection.assertIsSatisfied();
    },

    testRemoveById: function () {
      expect(collection)
        .toReceive("remove")
        .withArguments(id);

      instance.removeById(id);

      collection.assertIsSatisfied();
    },

    testRemoveByExample: function () {
      expect(collection)
        .toReceive("removeByExample")
        .withArguments(example);

      instance.removeByExample(example);

      collection.assertIsSatisfied();
    },

    testById: function () {
      expect(collection)
        .toReceive("document")
        .withArguments(id);

      instance.byId(id);

      collection.assertIsSatisfied();
    },

    testByIdReturnValue: function () {
      allow(collection)
        .toReceive("document")
        .andReturn(data);

      ModelPrototype = mockConstructor(data);
      instance = new FoxxRepository(collection, { model: ModelPrototype });

      model = instance.byId(id);

      ModelPrototype.assertIsSatisfied();
    }
  };
}

jsunity.run(RepositorySpec);
jsunity.run(RepositoryMethodsSpec);

return jsunity.done();<commit_msg>Foxx Repository: Also check the returned object of byId<commit_after>require("internal").flushModuleCache();

var jsunity = require("jsunity"),
  FoxxRepository = require("org/arangodb/foxx/repository").Repository,
  Model = require("org/arangodb/foxx/model").Model,
  stub_and_mock = require("org/arangodb/stub_and_mock"),
  stub = stub_and_mock.stub,
  allow = stub_and_mock.allow,
  expect = stub_and_mock.expect,
  mockConstructor = stub_and_mock.mockConstructor;

function RepositorySpec () {
  var TestRepository, instance, prefix, collection, modelPrototype, model, modelData;

  return {
    setUp: function () {
      prefix = "myApp";
      collection = function () {};
      modelPrototype = function () {};
      model = new modelPrototype();
      modelData = { fancy: 1 };
      model.forDB = function () {
        return modelData;
      };
    },

    testInitializeWithCollectionOnly: function () {
      instance = new FoxxRepository(collection);

      assertEqual(instance.collection, collection);
      assertEqual(instance.modelPrototype, Model);
    },

    testInitializeWithCollectionAndModelPrototype: function () {
      instance = new FoxxRepository(collection, {
        model: modelPrototype
      });

      assertEqual(instance.collection, collection);
      assertEqual(instance.modelPrototype, modelPrototype);
    },

    testInitializeWithCollectionAndPrefix: function () {
      instance = new FoxxRepository(collection, {
        prefix: prefix
      });

      assertEqual(instance.collection, collection);
      assertEqual(instance.modelPrototype, Model);
      assertEqual(instance.prefix, prefix);
    },

    testAddingAMethodWithExtend: function () {
      TestRepository = FoxxRepository.extend({
        test: function() {
          return "test";
        }
      });

      instance = new TestRepository(collection);

      assertEqual(instance.test(), "test");
    },

    testNothing: function () {
      [
        "save",

        "removeById",
        "removeByExample",

        "replaceById",
        "replaceByExample",

        "updateById",
        "updateByExample",

        "byId",
        "byExample",
        "firstExample"
      ].forEach(function (f) {
        instance = new FoxxRepository(collection);
        assertTrue(instance[f] !== undefined);
      });
    }
  };
}

function RepositoryMethodsSpec() {
  var instance,
    collection,
    ModelPrototype,
    model,
    modelData,
    data,
    example,
    id,
    id_and_rev;

  return {
    setUp: function () {
      ModelPrototype = stub();
      id_and_rev = stub();
      modelData = stub();
      example = stub();
      model = stub();
      data = stub();
      collection = stub();
      id = stub();

      allow(model)
        .toReceive("forDB")
        .andReturn(modelData);

      instance = new FoxxRepository(collection, { model: ModelPrototype });
    },

    testSave: function () {
      expect(model)
        .toReceive("set")
        .withArguments(id_and_rev);

      expect(collection)
        .toReceive("save")
        .withArguments(modelData)
        .andReturn(id_and_rev);

      instance.save(model);

      model.assertIsSatisfied();
      collection.assertIsSatisfied();
    },

    testRemoveById: function () {
      expect(collection)
        .toReceive("remove")
        .withArguments(id);

      instance.removeById(id);

      collection.assertIsSatisfied();
    },

    testRemoveByExample: function () {
      expect(collection)
        .toReceive("removeByExample")
        .withArguments(example);

      instance.removeByExample(example);

      collection.assertIsSatisfied();
    },

    testById: function () {
      expect(collection)
        .toReceive("document")
        .withArguments(id);

      instance.byId(id);

      collection.assertIsSatisfied();
    },

    testByIdConstructsModelAndReturnsIt: function () {
      allow(collection)
        .toReceive("document")
        .andReturn(data);

      ModelPrototype = mockConstructor(data);
      instance = new FoxxRepository(collection, { model: ModelPrototype });

      model = instance.byId(id);

      assertTrue(model instanceof ModelPrototype);
      ModelPrototype.assertIsSatisfied();
    }
  };
}

jsunity.run(RepositorySpec);
jsunity.run(RepositoryMethodsSpec);

return jsunity.done();
{'fraction_non_alphanumeric': 0.12051012051012051, 'fraction_numerical': 0.000234000234000234, 'mean_word_length': 2.694036300777874, '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': '4925876', 'n_tokens_mistral': 2411, 'n_tokens_neox': 2339, 'n_words': 416}