Mongo稳定吗?

时间:2015-09-17 04:15:25

标签: mongodb chai

我有一些摩卡测试如下所示。当我运行它们时,它们起作用。然后我再次运行它们中的一个会失败。然后我运行它们,然后它们工作,然后下一次,其他一些(看似)随机测试将失败。如果我的结构很差或编码错误,我很乐意听到。

有谁能告诉我为什么这些测试会随机失败?这让我想起了mongo的稳定性。

另一件事是:测试永远不会说出错了,只是它超时并且我应该确保调用done()。 / data / mongoAccess代码位于测试块之下。

(function() {
    var assert = require("chai").assert;
    var mongo = require("../data/semRushMongoAccess");

    describe("insertApiCall", function() {
    it("should insert a new record.", function(done) {
        var data = {
            "data": "insertApiCall"
        };
        mongo.removeApiCall("uri1").then(function() {
            return mongo.insertApiCall("uri1", "type", 2, "domain", "target", "url", "phrase", "d1|d2", data);
        }).then(function() {
            return mongo.findApiCall("uri1");
        }).then(function(result) {
            assert.equal("insertApiCall", result[0].data.data);
            return;
        }).then(function() {
            return mongo.removeApiCall("uri1");
        }).then(function() {
            done();
        });
    });
    });


    describe("removeApiCall", function() {
    it("should remove a record specified by uri.", function(done) {
        var data = {
            "data": "removeApiCall1"
        };

        mongo.removeApiCall("uri1").then(function() {
            return mongo.insertApiCall("uri2", "type", 2, "domain", "target", "url", "phrase", "d1|d2", data);
        }).then(function() {
            return mongo.findApiCall("uri2");
        }).then(function(result1) {
            assert.equal("removeApiCall1", result1[0].data.data);
        }).then(function() {
            return mongo.removeApiCall("uri2");
        }).then(function() {
            return mongo.findApiCall("uri2");
        }).then(function(result2) {
            assert.isArray(result2);
            assert.equal(0, result2.length, "Data was not removed.");
            done();
        });
    });
    it("should remove a record specified by apiType", function(done) {
        var data = {
            "data": "removeApiCall2"
        };

        mongo.removeApiCall("uri3").then(function() {
            return mongo.insertApiCall("uri3", "type", 2, "domain", "target", "url", "phrase", "d1|d2", data);
        }).then(function() {
            return mongo.findApiCall("uri3");
        }).then(function(result1) {
            assert.equal("removeApiCall2", result1[0].data.data);
        }).then(function() {
            return mongo.removeApiCall("uri3");
        }).then(function() {
            return mongo.findApiCall("uri3");
        }).then(function(result2) {
            assert.isArray(result2);
            assert.equal(0, result2.length, "Data was not removed.");
            done();
        });
    });
    it("should remove a record specified by domain.", function(done) {
        var data = {
            "data": "removeApiCall3"
        };

        mongo.removeApiCall("uri4").then(function() {
            return mongo.insertApiCall("uri4", "type", 2, "domain", "target", "url", "phrase", "d1|d2", data);
        }).then(function() {
            return mongo.findApiCall("uri4");
        }).then(function(result1) {
            assert.equal("removeApiCall3", result1[0].data.data);
            return mongo.removeApiCall(null, null, "domain");
        }).then(function() {
            return mongo.findApiCall("uri4");
        }).then(function(result2) {
            assert.isArray(result2);
            assert.equal(0, result2.length, "Data was not removed.");
            done();
        });
    });
    it("should remove a record specified by target.", function(done) {
        var data = {
            "data": "removeApiCall4"
        };

        mongo.removeApiCall("uri5").then(function() {
            return mongo.insertApiCall("uri5", "type", 2, "domain", "target", "url", "phrase", "d1|d2", data);
        }).then(function() {
            return mongo.findApiCall("uri5");
        }).then(function(result1) {
            assert.equal("removeApiCall4", result1[0].data.data);
            return mongo.removeApiCall(null, null, null, "target");
        }).then(function() {
            return mongo.findApiCall("uri5");
        }).then(function(result2) {
            assert.isArray(result2);
            assert.equal(0, result2.length, "Data was not removed.");
            done();
        });
    });
    it("should remove a record specified by url.", function(done) {
        var data = {
            "data": "removeApiCall5"
        };

        mongo.removeApiCall("uri6").then(function() {
            return mongo.insertApiCall("uri6", "type", 2, "domain", "target", "url", "phrase", "d1|d2", data);
        }).then(function() {
            return mongo.findApiCall("uri6");
        }).then(function(result1) {
            assert.equal("removeApiCall5", result1[0].data.data);
            return mongo.removeApiCall(null, null, null, null, "url");
        }).then(function() {
            return mongo.findApiCall("uri6");
        }).then(function(result2) {
            assert.isArray(result2);
            assert.equal(0, result2.length, "Data was not removed.");
            done();
        });
    });
    it("should remove a record specified by phrase.", function(done) {
        var data = {
            "data": "removeApiCall6"
        };

        mongo.removeApiCall("uri7").then(function() {
            return mongo.insertApiCall("uri7", "type", 2, "domain", "target", "url", "phrase", "d1|d2", data);
        }).then(function() {
            return mongo.findApiCall("uri7");
        }).then(function(result1) {
            assert.equal("removeApiCall6", result1[0].data.data);
            return mongo.removeApiCall(null, null, null, null, null, "phrase");
        }).then(function() {
            return mongo.findApiCall("uri7");
        }).then(function(result2) {
            assert.isArray(result2);
            assert.equal(0, result2.length, "Data was not removed.");
            done();
        });
    });
    it("should remove a record specified by domains", function(done) {
        var data = {
            "data": "removeApiCall7"
        };
        mongo.removeApiCall("uri8").then(function() {
            return mongo.insertApiCall("uri8", "type", 2, "domain", "target", "url", "phrase", "d1|d2", data);
        }).then(function() {
            return mongo.findApiCall("uri8");
        }).then(function(result1) {
            assert.equal("removeApiCall7", result1[0].data.data);
            return mongo.removeApiCall(null, null, null, null, null, null, "d1|d2");
        }).then(function() {
            return mongo.findApiCall("uri8");
        }).then(function(result2) {
            assert.isArray(result2);
            assert.equal(0, result2.length, "Data was not removed.");
            done();
        });
    });
    it("should remove records using OR when isOr is true.", function(done) {
        var data = {
            "data": "removeApiCall8"
        };
        mongo.removeApiCall("uri9").then(function() {
            return mongo.removeApiCall("uri10");
        }).then(function() {
            return mongo.insertApiCall("uri9", "type1", 2, "domain", "target", "url", "phrase", "d1|d1", data);
        }).then(function() {
            return mongo.insertApiCall("uri10", "type2", 2, "domain", "target", "url", "phrase", "d1|d2");
        }).then(function() {
            return mongo.removeApiCall("uri9", "type2", null, null, null, null, null, true);
        }).then(function() {
            return mongo.findApiCall("uri9", "type2", null, null, null, null, null, true)
        }).then(function(result) {
            assert.isArray(result);
            assert.equal(0, result.length, "Array length is " + result.length);
            done();
        });
    });
    it("should remove records using AND when isOr is false.", function(done) {
        var data = {
            "data": "removeApiCall9"
        };

        // Insert uri11/type1, uri12/type1, and uri12/type2
        // Remove uri12 + type1.
        // Should leave uri11 and uri12/type2.
        mongo.removeApiCall("uri11").then(function() {
            return mongo.removeApiCall("uri12");
        }).then(function() {
            return mongo.insertApiCall("uri11", "type1", 2, "domain", "target", "url", "phrase", "d1|d1", data);
        }).then(function() {
            return mongo.insertApiCall("uri12", "type1", 2, "domain", "target", "url", "phrase", "d1|d2");
        }).then(function() {
            return mongo.insertApiCall("uri12", "type2", 2, "domain", "target", "url", "phrase", "d1|d2")
        }).then(function() {
            return mongo.removeApiCall("uri12", "type1", null, null, null, null, null, false);
        }).then(function() {
            return mongo.findApiCall("uri12");
        }).then(function(result) {
            assert.isArray(result);
            assert.equal(1, result.length, "Array length is " + result.length);
            assert.equal("type2", result[0].apiType);
        }).then(function() {
            return mongo.findApiCall("uri11");
        }).then(function(result2) {
            assert.isArray(result2);
            assert.equal(1, result2.length);
            return mongo.removeApiCall("uri11");
        }).then(function() {
            return mongo.removeApiCall("uri12");
        }).then(function() {
            done();
        });
    });
    it("should remove records using AND when isOr is not specified (is null).", function(done) {
        var data = {
            "data": "removeApiCall9"
        };

        // Identical to previous test, only instead of explicit false for isOr, it's excluded.
        // Insert uri11/type1, uri12/type1, and uri12/type2
        // Remove uri12 + type1.
        // Should leave uri11 and uri12/type2.
        mongo.removeApiCall("uri11").then(function() {
            return mongo.removeApiCall("uri12");
        }).then(function() {
            return mongo.insertApiCall("uri11", "type1", 2, "domain", "target", "url", "phrase", "d1|d1", data);
        }).then(function() {
            return mongo.insertApiCall("uri12", "type1", 2, "domain", "target", "url", "phrase", "d1|d2");
        }).then(function() {
            return mongo.insertApiCall("uri12", "type2", 2, "domain", "target", "url", "phrase", "d1|d2")
        }).then(function() {
            return mongo.removeApiCall("uri12", "type1", null, null, null, null, null);
        }).then(function() {
            return mongo.findApiCall("uri12");
        }).then(function(result) {
            assert.isArray(result);
            assert.equal(1, result.length, "Array length is " + result.length);
            assert.equal("type2", result[0].apiType);
        }).then(function() {
            return mongo.findApiCall("uri11");
        }).then(function(result2) {
            assert.isArray(result2);
            assert.equal(1, result2.length);
            return mongo.removeApiCall("uri11");
        }).then(function() {
            return mongo.removeApiCall("uri12");
        }).then(function() {
            done();
        });
    });
    });
})();

mongoAccess:

(function() {
    var moment = require("moment");
    var appConfig = require("../config/appConfig");
    var mongo = require("promised-mongo");
    var db = mongo("mongodb://localhost:27017/" + appConfig.options.dbName, ["apiCalls"]);

    exports.insertApiCall = function(uri, apiType, lineLimit, domain, target, url, phrase, domains, data) {
    return db.apiCalls.insert({
        uri: uri,
        apiType: apiType,
        lineLimit: lineLimit,
        domain: domain,
        target: target,
        url: url,
        phrase: phrase,
        domains: domains,
        data: data,
        timestamp: moment.utc().format()
    });
    };

    exports.findApiCall = function(uri, apiType, domain, target, url, phrase, domains, isOr) {
    var query = buildQuery(uri, apiType, domain, target, url, phrase, domains, isOr);
    var resultSet = buildResultSet(uri, apiType, domain, target, url, phrase, domains);
    return db.apiCalls.find(query, resultSet);
    };

    exports.removeApiCall = function(uri, apiType, domain, target, url, phrase, domains, isOr) {
    var query = buildQuery(uri, apiType, domain, target, url, phrase, domains, isOr);
    return db.apiCalls.remove(query);
    };

    function buildResultSet(uri, domain, target, url, phrase, domains) {
    var resultSet = {
        "apiType": true,
        "timestamp": true,
        "data": true
    };

    if (uri != null) {
        resultSet["uri"] = true;
    }
    if (domain != null) {
        resultSet["domain"] = true;
    }
    if (target != null) {
        resultSet["target"] = true;
    }
    if (url != null) {
        resultSet["url"] = true;
    }
    if (phrase != null) {
        resultSet["phrase"] = true;
    }
    if (domains != null) {
        resultSet["domains"] = true;
    }
    return resultSet;
    }

    function buildQuery(uri, apiType, domain, target, url, phrase, domains, isOr) {
    var query = [];

    if (uri != null) {
        query.push({
            uri: uri
        });
    }
    if (apiType != null) {
        query.push({
            apiType: apiType
        });
    }
    if (domain != null) {
        query.push({
            domain: domain
        });
    }
    if (target != null) {
        query.push({
            target: target
        });
    }
    if (url != null) {
        query.push({
            url: url
        });
    }
    if (phrase != null) {
        query.push({
            phrase: phrase
        });
    }
    if (domains != null) {
        query.push({
            domains: domains
        });
    }

    return isOr ? {
        $or: query
    } : {
        $and: query
    };
}
})();

1 个答案:

答案 0 :(得分:0)

好的,我明白了。不幸的是,在我意识到我已经解决了改变的问题之前,我改变了我的问题的文本。在修复我的promise语法之后,我再也无法重现这个问题了。

我做的是这样的事情:

<EntityType Name="Attachment">
  <Key>
    <PropertyRef Name="Id"/>
  </Key>
  <Property Name="Id" Unicode="false" Type="Edm.String" Nullable="false"/>
  <Property Name="Name" Unicode="false" Type="Edm.String"/>
  <Property Name="ContentType" Unicode="false" Type="Edm.String"/>
  <Property Name="Size" Type="Edm.Int32" Nullable="false"/>
  <Property Name="IsInline" Type="Edm.Boolean" Nullable="false"/>
  <Property Name="DateTimeLastModified" Type="Edm.DateTimeOffset"/>
</EntityType>

<EntityType Name="FileAttachment" BaseType="Microsoft.Graph.Attachment">
  <Property Name="ContentId" Unicode="false" Type="Edm.String"/>
  <Property Name="ContentLocation" Unicode="false" Type="Edm.String"/>
  <Property Name="IsContactPhoto" Type="Edm.Boolean" Nullable="false"/>
  <Property Name="ContentBytes" Type="Edm.Binary"/>
</EntityType>

<EntityType Name="ItemAttachment" BaseType="Microsoft.Graph.Attachment">
  <NavigationProperty Name="Item" Type="Microsoft.Graph.OutlookItem" Nullable="false" ContainsTarget="true"/>
</EntityType>

当我把它切换到这个时:

 mongo.removeApiCall("uri11").then(
     mongo.removeApiCall("uri12").then(
         mongo.insertApiCall("uri11", "type1", 2, "domain", "target", "url", "phrase", "d1|d1", data).then(
             mongo.insertApiCall("uri12", "type1", 2, "domain", "target", "url", "phrase", "d1|d2").then(
                 mongo.insertApiCall("uri12", "type2", 2, "domain", "target", "url", "phrase", "d1|d2").then(
                     mongo.removeApiCall("uri12", "type1", null, null, null, null, null, false).then(
                         mongo.findApiCall("uri12").then(function(result) {
                             assert.isArray(result);
                             assert.equal(1, result.length, "Array length is " + result.length);
                             assert.equal("type2", result[0].apiType);
                             mongo.findApiCall("uri11").then(function(result2) {
                                 assert.isArray(result2);
                                 assert.equal(1, result2.length);
                                 mongo.removeApiCall("uri11").then(
                                     mongo.removeApiCall("uri12").then(function() {
                                         done();
                                     }));
                             });
                         })))))));
问题消失了。我不确定为什么会出现这种情况,但就像我说的那样,自从做出这样的改变后,我还没有能够重现这个问题。

相关问题