<commit_before>var Collection = typeof Mongo !== "undefined" && typeof Mongo.Collection !== "undefined" ? Mongo.Collection : Meteor.Collection;

_.each([null, "direct_collection_test"], function (ctype) {

  Tinytest.add("direct - hooks should not be fired when using .direct (collection type " + ctype + ")", function (test) {
    // console.log("-------", ctype)

    var collection = new Collection(ctype, {connection: null});
    var hookCount = 0;

    // The server will make a call to find when findOne is called, which adds 2 extra counts
    var hookCountTarget = Meteor.isServer ? 12 : 10;

    // Full permissions on collection
    collection.allow({
      insert: function () { return true; },
      update: function () { return true; },
      remove: function () { return true; }
    });

    collection.before.insert(function (userId, doc) {
      if (doc && doc.test) {
        hookCount++;
        // console.log(ctype, ": before insert", hookCount)
      }
    });

    collection.after.insert(function (userId, doc) {
      if (doc && doc.test) {
        hookCount++;
        // console.log(ctype, ": after insert", hookCount)
      }
    });

    collection.before.update(function (userId, doc, fieldNames, modifier, options) {
      if (options && options.test) {
        hookCount++;
        // console.log(ctype, ": before update", hookCount)
      }
    });

    collection.after.update(function (userId, doc, fieldNames, modifier, options) {
      if (options && options.test) {
        hookCount++;
        // console.log(ctype, ": after update", hookCount)
      }
    });

    collection.before.remove(function (userId, doc) {
      if (doc && doc._id === "test") {
        hookCount++;
        // console.log(ctype, ": before remove", hookCount)
      }
    });

    collection.after.remove(function (userId, doc) {
      if (doc && doc._id === "test") {
        hookCount++;
        // console.log(ctype, ": after remove", hookCount)
      }
    });

    collection.before.find(function (userId, selector, options) {
      if (options && options.test) {
        hookCount++;
        // console.log(ctype, ": before find", hookCount)
      }
    });

    collection.after.find(function (userId, selector, options, result) {
      if (options && options.test) {
        hookCount++;
        // console.log(ctype, ": after find", hookCount)
      }
    });

    collection.before.findOne(function (userId, selector, options) {
      if (options && options.test) {
        hookCount++;
        // console.log(ctype, ": before findOne", hookCount)
      }
    });

    collection.after.findOne(function (userId, selector, options, result) {
      if (options && options.test) {
        hookCount++;
        // console.log(ctype, ": after findOne", hookCount)
      }
    });

    collection.insert({_id: "test", test: 1});
    collection.update({_id: "test"}, {$set: {test: 1}}, {test: 1});
    collection.find({}, {test: 1});
    collection.findOne({}, {test: 1});
    collection.remove({_id: "test"});

    test.equal(hookCount, hookCountTarget);

    // These should in no way affect the hookCount, which is essential in proving
    // that the direct calls are functioning as intended
    collection.direct.insert({_id: "test", test: 1});

    collection.direct.update({_id: "test"}, {$set: {test: 1}}, {test: 1});

    var cursor = collection.direct.find({}, {test: 1});
    var count = cursor.count();
    test.equal(count, 1);

    var doc = collection.direct.findOne({}, {test: 1});
    test.equal(doc.test, 1);

    collection.direct.remove({_id: "test"});

    test.equal(hookCount, hookCountTarget);
  });

  Tinytest.add("direct - update and remove should allow removing by _id string (collection type " + ctype + ")", function (test) {
    var collection = new Collection(ctype, {connection: null});

    function hasCountAndTestValue(count, value) {
      var cursor = collection.direct.find({_id: "test", test: value});
      test.equal(cursor.count(), count);
    }
<commit_msg>slight change to test logic for direct update and remove by string id to be certain we aren't reusing the collection from the test above it<commit_after>var Collection = typeof Mongo !== "undefined" && typeof Mongo.Collection !== "undefined" ? Mongo.Collection : Meteor.Collection;

_.each([null, "direct_collection_test"], function (ctype) {

  Tinytest.add("direct - hooks should not be fired when using .direct (collection type " + ctype + ")", function (test) {
    // console.log("-------", ctype)

    var collection = new Collection(ctype, {connection: null});
    var hookCount = 0;

    // The server will make a call to find when findOne is called, which adds 2 extra counts
    var hookCountTarget = Meteor.isServer ? 12 : 10;

    // Full permissions on collection
    collection.allow({
      insert: function () { return true; },
      update: function () { return true; },
      remove: function () { return true; }
    });

    collection.before.insert(function (userId, doc) {
      if (doc && doc.test) {
        hookCount++;
        // console.log(ctype, ": before insert", hookCount)
      }
    });

    collection.after.insert(function (userId, doc) {
      if (doc && doc.test) {
        hookCount++;
        // console.log(ctype, ": after insert", hookCount)
      }
    });

    collection.before.update(function (userId, doc, fieldNames, modifier, options) {
      if (options && options.test) {
        hookCount++;
        // console.log(ctype, ": before update", hookCount)
      }
    });

    collection.after.update(function (userId, doc, fieldNames, modifier, options) {
      if (options && options.test) {
        hookCount++;
        // console.log(ctype, ": after update", hookCount)
      }
    });

    collection.before.remove(function (userId, doc) {
      if (doc && doc._id === "test") {
        hookCount++;
        // console.log(ctype, ": before remove", hookCount)
      }
    });

    collection.after.remove(function (userId, doc) {
      if (doc && doc._id === "test") {
        hookCount++;
        // console.log(ctype, ": after remove", hookCount)
      }
    });

    collection.before.find(function (userId, selector, options) {
      if (options && options.test) {
        hookCount++;
        // console.log(ctype, ": before find", hookCount)
      }
    });

    collection.after.find(function (userId, selector, options, result) {
      if (options && options.test) {
        hookCount++;
        // console.log(ctype, ": after find", hookCount)
      }
    });

    collection.before.findOne(function (userId, selector, options) {
      if (options && options.test) {
        hookCount++;
        // console.log(ctype, ": before findOne", hookCount)
      }
    });

    collection.after.findOne(function (userId, selector, options, result) {
      if (options && options.test) {
        hookCount++;
        // console.log(ctype, ": after findOne", hookCount)
      }
    });

    collection.insert({_id: "test", test: 1});
    collection.update({_id: "test"}, {$set: {test: 1}}, {test: 1});
    collection.find({}, {test: 1});
    collection.findOne({}, {test: 1});
    collection.remove({_id: "test"});

    test.equal(hookCount, hookCountTarget);

    // These should in no way affect the hookCount, which is essential in proving
    // that the direct calls are functioning as intended
    collection.direct.insert({_id: "test", test: 1});

    collection.direct.update({_id: "test"}, {$set: {test: 1}}, {test: 1});

    var cursor = collection.direct.find({}, {test: 1});
    var count = cursor.count();
    test.equal(count, 1);

    var doc = collection.direct.findOne({}, {test: 1});
    test.equal(doc.test, 1);

    collection.direct.remove({_id: "test"});

    test.equal(hookCount, hookCountTarget);
  });
});

_.each([null, "direct_collection_test_stringid"], function (ctype) {
  Tinytest.add("direct - update and remove should allow removing by _id string (collection type " + ctype + ")", function (test) {
    var collection = new Collection(ctype, {connection: null});

    function hasCountAndTestValue(count, value) {
      var cursor = collection.direct.find({_id: "test", test: value});
      test.equal(cursor.count(), count);
    }

{'fraction_non_alphanumeric': 0.15822707953855494, 'fraction_numerical': 0.004371584699453552, 'mean_word_length': 2.776249426868409, 'pattern_counts': {'":': 20, '<': 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': '6388348', 'n_tokens_mistral': 2464, 'n_tokens_neox': 2356, 'n_words': 645}