summaryrefslogtreecommitdiff
path: root/jstests/libs/mql_model_mongod_test_runner.js
blob: 4485c81cdc1509be2be673960a575c97acddf91d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
 * Test runner responsible for parsing and executing a MQL MongoD model test json file.
 */
(function() {
"use strict";

const jsonFilename = jsTestOptions().mqlTestFile;
const mqlRootPath = jsTestOptions().mqlRootPath;

if (jsonFilename === undefined) {
    throw new Error('Undefined JSON file name: MQL Model tests must be run through resmoke.py');
}

// Populate collections with data fetched from the dataFile.
function populateCollections(dataFile) {
    const data = JSON.parse(cat(mqlRootPath + dataFile));

    data.forEach(function(singleColl) {
        assert(singleColl.hasOwnProperty("namespace"), "MQL data model requires a 'namespace'");
        assert(singleColl.hasOwnProperty("data"), "MQL data model requires a 'data'");

        const coll = db.getCollection(singleColl["namespace"]);
        coll.drop();

        singleColl["data"].forEach(function(doc) {
            assert.commandWorked(coll.insert(doc));
        });
    });
}

// Run a single find test.
function runFindTest(testFile, dataFile, expected) {
    populateCollections(dataFile);

    const test = JSON.parse(cat(mqlRootPath + testFile));

    const results = db.getCollection(test["find"]).find(test["filter"], {_id: 0}).toArray();

    assert.eq(results, expected);
}

// Read a list of tests from the jsonFilename and execute them.
const testList = JSON.parse(cat(jsonFilename));
testList.forEach(function(singleTest) {
    if (singleTest.hasOwnProperty("match")) {
        // Skip the match test type as it is not directly supported by mongod.
    } else if (singleTest.hasOwnProperty("find")) {
        // Run the find test type.
        assert(singleTest.hasOwnProperty("data"), "MQL model test requires a 'data'");
        assert(singleTest.hasOwnProperty("expected"), "MQL model test requires a 'expected'");

        runFindTest(singleTest["find"], singleTest["data"], singleTest["expected"]);
    } else {
        throw new Error("Unknown test type: " + tojson(singleTest));
    }
});
}());