mirror of https://github.com/mongodb/mongo
SERVER-115258: Convert some shell tests to mochalite (#45093)
GitOrigin-RevId: 507bbaa46db3b118c5dc4cd6346ebafd879aaf99
This commit is contained in:
parent
94ff76d9fe
commit
807332806c
|
|
@ -1,174 +1,105 @@
|
||||||
|
import {it} from "jstests/libs/mochalite.js";
|
||||||
|
|
||||||
// check that constructor also works without "new"
|
// check that constructor also works without "new"
|
||||||
let a;
|
it("ObjectId", () => {
|
||||||
let b;
|
let a = new ObjectId();
|
||||||
a = new ObjectId();
|
let b = ObjectId(a.valueOf());
|
||||||
b = ObjectId(a.valueOf());
|
printjson(a);
|
||||||
printjson(a);
|
assert.eq(tojson(a), tojson(b), "oid");
|
||||||
assert.eq(tojson(a), tojson(b), "oid");
|
});
|
||||||
|
|
||||||
a = new DBRef("test", "theid");
|
it("DBRef", () => {
|
||||||
b = DBRef(a.getRef(), a.getId());
|
let a = new DBRef("test", "theid");
|
||||||
printjson(a);
|
let b = DBRef(a.getRef(), a.getId());
|
||||||
assert.eq(tojson(a), tojson(b), "dbref");
|
printjson(a);
|
||||||
|
assert.eq(tojson(a), tojson(b), "dbref");
|
||||||
|
|
||||||
a = new DBRef("test", "theid", "testdb");
|
a = new DBRef("test", "theid", "testdb");
|
||||||
b = DBRef(a.getRef(), a.getId(), a.getDb());
|
b = DBRef(a.getRef(), a.getId(), a.getDb());
|
||||||
printjson(a);
|
printjson(a);
|
||||||
assert.eq(tojson(a), tojson(b), "dbref");
|
assert.eq(tojson(a), tojson(b), "dbref");
|
||||||
|
});
|
||||||
|
|
||||||
a = new DBPointer("test", new ObjectId());
|
it("DBPointer", () => {
|
||||||
b = DBPointer(a.getCollection(), a.getId());
|
let a = new DBPointer("test", new ObjectId());
|
||||||
printjson(a);
|
let b = DBPointer(a.getCollection(), a.getId());
|
||||||
assert.eq(tojson(a), tojson(b), "dbpointer");
|
printjson(a);
|
||||||
|
assert.eq(tojson(a), tojson(b), "dbpointer");
|
||||||
|
});
|
||||||
|
|
||||||
a = new Timestamp(10, 20);
|
it("BinData", () => {
|
||||||
b = Timestamp(a.t, a.i);
|
let a = new BinData(3, "VQ6EAOKbQdSnFkRmVUQAAA==");
|
||||||
printjson(a);
|
let b = BinData(a.type, a.base64());
|
||||||
assert.eq(tojson(a), tojson(b), "timestamp");
|
printjson(a);
|
||||||
|
assert.eq(tojson(a), tojson(b), "bindata");
|
||||||
|
});
|
||||||
|
|
||||||
assert.throws(
|
it("UUID", () => {
|
||||||
function () {
|
let a = new UUID("550e8400e29b41d4a716446655440000");
|
||||||
Timestamp(-2, 3);
|
let b = UUID(a.hex());
|
||||||
},
|
printjson(a);
|
||||||
[],
|
assert.eq(tojson(a), tojson(b), "uuid");
|
||||||
"Timestamp time must not accept negative time",
|
});
|
||||||
);
|
|
||||||
assert.throws(
|
|
||||||
function () {
|
|
||||||
Timestamp(0, -1);
|
|
||||||
},
|
|
||||||
[],
|
|
||||||
"Timestamp increment must not accept negative time",
|
|
||||||
);
|
|
||||||
assert.throws(
|
|
||||||
function () {
|
|
||||||
Timestamp(0x10000 * 0x10000, 0);
|
|
||||||
},
|
|
||||||
[],
|
|
||||||
"Timestamp time must not accept values larger than 2**32 - 1",
|
|
||||||
);
|
|
||||||
assert.throws(
|
|
||||||
function () {
|
|
||||||
Timestamp(0, 0x10000 * 0x10000);
|
|
||||||
},
|
|
||||||
[],
|
|
||||||
"Timestamp increment must not accept values larger than 2**32 - 1",
|
|
||||||
);
|
|
||||||
|
|
||||||
a = new Timestamp(0x80008000, 0x80008000 + 0.5);
|
it("MD5", () => {
|
||||||
b = Timestamp(a.t, Math.round(a.i));
|
let a = new MD5("550e8400e29b41d4a716446655440000");
|
||||||
printjson(a);
|
let b = MD5(a.hex());
|
||||||
assert.eq(tojson(a), tojson(b), "timestamp");
|
printjson(a);
|
||||||
|
assert.eq(tojson(a), tojson(b), "md5");
|
||||||
|
});
|
||||||
|
|
||||||
a = new BinData(3, "VQ6EAOKbQdSnFkRmVUQAAA==");
|
it("HexData", () => {
|
||||||
b = BinData(a.type, a.base64());
|
let a = new HexData(4, "550e8400e29b41d4a716446655440000");
|
||||||
printjson(a);
|
let b = HexData(a.type, a.hex());
|
||||||
assert.eq(tojson(a), tojson(b), "bindata");
|
printjson(a);
|
||||||
|
assert.eq(tojson(a), tojson(b), "hexdata");
|
||||||
|
});
|
||||||
|
|
||||||
a = new UUID("550e8400e29b41d4a716446655440000");
|
it("NumberLong", () => {
|
||||||
b = UUID(a.hex());
|
let a = new NumberLong(100);
|
||||||
printjson(a);
|
let b = NumberLong(a.toNumber());
|
||||||
assert.eq(tojson(a), tojson(b), "uuid");
|
printjson(a);
|
||||||
|
assert.eq(tojson(a), tojson(b), "long");
|
||||||
|
});
|
||||||
|
|
||||||
a = new MD5("550e8400e29b41d4a716446655440000");
|
it("NumberInt", () => {
|
||||||
b = MD5(a.hex());
|
let a = new NumberInt(100);
|
||||||
printjson(a);
|
let b = NumberInt(a.toNumber());
|
||||||
assert.eq(tojson(a), tojson(b), "md5");
|
printjson(a);
|
||||||
|
assert.eq(tojson(a), tojson(b), "int");
|
||||||
|
});
|
||||||
|
|
||||||
a = new HexData(4, "550e8400e29b41d4a716446655440000");
|
it("ObjectId.fromDate", () => {
|
||||||
b = HexData(a.type, a.hex());
|
let a = new ObjectId();
|
||||||
printjson(a);
|
let timestampA = a.getTimestamp();
|
||||||
assert.eq(tojson(a), tojson(b), "hexdata");
|
let dateA = new Date(timestampA.getTime());
|
||||||
|
|
||||||
a = new NumberLong(100);
|
// ObjectId.fromDate - invalid input types
|
||||||
b = NumberLong(a.toNumber());
|
assert.throws(() => ObjectId.fromDate(undefined), [], "ObjectId.fromDate should error on undefined date");
|
||||||
printjson(a);
|
|
||||||
assert.eq(tojson(a), tojson(b), "long");
|
|
||||||
|
|
||||||
a = new NumberInt(100);
|
assert.throws(() => ObjectId.fromDate(12345), [], "ObjectId.fromDate should error on numerical value");
|
||||||
b = NumberInt(a.toNumber());
|
|
||||||
printjson(a);
|
|
||||||
assert.eq(tojson(a), tojson(b), "int");
|
|
||||||
|
|
||||||
// ObjectId.fromDate
|
assert.throws(() => ObjectId.fromDate(dateA.toISOString()), [], "ObjectId.fromDate should error on string value");
|
||||||
|
|
||||||
a = new ObjectId();
|
// SERVER-14623 dates less than or equal to 1978-07-04T21:24:15Z fail
|
||||||
let timestampA = a.getTimestamp();
|
let checkFromDate = function (millis, expected, comment) {
|
||||||
let dateA = new Date(timestampA.getTime());
|
let oid = ObjectId.fromDate(new Date(millis));
|
||||||
|
assert.eq(oid.valueOf(), expected, comment);
|
||||||
|
};
|
||||||
|
checkFromDate(Math.pow(2, 28) * 1000, "100000000000000000000000", "1978-07-04T21:24:16Z");
|
||||||
|
checkFromDate(Math.pow(2, 28) * 1000 - 1, "0fffffff0000000000000000", "1978-07-04T21:24:15Z");
|
||||||
|
checkFromDate(0, "000000000000000000000000", "start of epoch");
|
||||||
|
|
||||||
// ObjectId.fromDate - invalid input types
|
// test date upper limit
|
||||||
assert.throws(
|
checkFromDate(Math.pow(2, 32) * 1000 - 1, "ffffffff0000000000000000", "last valid date");
|
||||||
function () {
|
assert.throws(
|
||||||
ObjectId.fromDate(undefined);
|
() => ObjectId.fromDate(new Date(Math.pow(2, 32) * 1000)),
|
||||||
},
|
[],
|
||||||
[],
|
"ObjectId limited to 4 bytes for seconds",
|
||||||
"ObjectId.fromDate should error on undefined date",
|
);
|
||||||
);
|
|
||||||
|
|
||||||
assert.throws(
|
// ObjectId.fromDate - Date
|
||||||
function () {
|
let b = ObjectId.fromDate(dateA);
|
||||||
ObjectId.fromDate(12345);
|
printjson(a);
|
||||||
},
|
assert.eq(tojson(a.getTimestamp()), tojson(b.getTimestamp()), "ObjectId.fromDate - Date");
|
||||||
[],
|
});
|
||||||
"ObjectId.fromDate should error on numerical value",
|
|
||||||
);
|
|
||||||
|
|
||||||
assert.throws(
|
|
||||||
function () {
|
|
||||||
ObjectId.fromDate(dateA.toISOString());
|
|
||||||
},
|
|
||||||
[],
|
|
||||||
"ObjectId.fromDate should error on string value",
|
|
||||||
);
|
|
||||||
|
|
||||||
// SERVER-14623 dates less than or equal to 1978-07-04T21:24:15Z fail
|
|
||||||
let checkFromDate = function (millis, expected, comment) {
|
|
||||||
let oid = ObjectId.fromDate(new Date(millis));
|
|
||||||
assert.eq(oid.valueOf(), expected, comment);
|
|
||||||
};
|
|
||||||
checkFromDate(Math.pow(2, 28) * 1000, "100000000000000000000000", "1978-07-04T21:24:16Z");
|
|
||||||
checkFromDate(Math.pow(2, 28) * 1000 - 1, "0fffffff0000000000000000", "1978-07-04T21:24:15Z");
|
|
||||||
checkFromDate(0, "000000000000000000000000", "start of epoch");
|
|
||||||
|
|
||||||
// test date upper limit
|
|
||||||
checkFromDate(Math.pow(2, 32) * 1000 - 1, "ffffffff0000000000000000", "last valid date");
|
|
||||||
assert.throws(
|
|
||||||
function () {
|
|
||||||
ObjectId.fromDate(new Date(Math.pow(2, 32) * 1000));
|
|
||||||
},
|
|
||||||
[],
|
|
||||||
"ObjectId limited to 4 bytes for seconds",
|
|
||||||
);
|
|
||||||
|
|
||||||
// ObjectId.fromDate - Date
|
|
||||||
b = ObjectId.fromDate(dateA);
|
|
||||||
printjson(a);
|
|
||||||
assert.eq(tojson(a.getTimestamp()), tojson(b.getTimestamp()), "ObjectId.fromDate - Date");
|
|
||||||
|
|
||||||
// tojsonObject
|
|
||||||
|
|
||||||
// Empty object
|
|
||||||
assert.eq("{\n\t\n}", tojsonObject({}, "", false));
|
|
||||||
assert.eq("{ }", tojsonObject({}, "", true));
|
|
||||||
assert.eq("{\n\t\t\t\n\t\t}", tojsonObject({}, "\t\t", false));
|
|
||||||
|
|
||||||
// Single field
|
|
||||||
assert.eq('{\n\t"a" : 1\n}', tojsonObject({a: 1}, "", false));
|
|
||||||
assert.eq('{ "a" : 1 }', tojsonObject({a: 1}, "", true));
|
|
||||||
assert.eq('{\n\t\t\t"a" : 1\n\t\t}', tojsonObject({a: 1}, "\t\t", false));
|
|
||||||
|
|
||||||
// Multiple fields
|
|
||||||
assert.eq('{\n\t"a" : 1,\n\t"b" : 2\n}', tojsonObject({a: 1, b: 2}, "", false));
|
|
||||||
assert.eq('{ "a" : 1, "b" : 2 }', tojsonObject({a: 1, b: 2}, "", true));
|
|
||||||
assert.eq('{\n\t\t\t"a" : 1,\n\t\t\t"b" : 2\n\t\t}', tojsonObject({a: 1, b: 2}, "\t\t", false));
|
|
||||||
|
|
||||||
// Nested fields
|
|
||||||
assert.eq(
|
|
||||||
'{\n\t"a" : 1,\n\t"b" : {\n\t\t"bb" : 2,\n\t\t"cc" : 3\n\t}\n}',
|
|
||||||
tojsonObject({a: 1, b: {bb: 2, cc: 3}}, "", false),
|
|
||||||
);
|
|
||||||
assert.eq('{ "a" : 1, "b" : { "bb" : 2, "cc" : 3 } }', tojsonObject({a: 1, b: {bb: 2, cc: 3}}, "", true));
|
|
||||||
assert.eq(
|
|
||||||
'{\n\t\t\t"a" : 1,\n\t\t\t"b" : {\n\t\t\t\t"bb" : 2,\n\t\t\t\t"cc" : 3\n\t\t\t}\n\t\t}',
|
|
||||||
tojsonObject({a: 1, b: {bb: 2, cc: 3}}, "\t\t", false),
|
|
||||||
);
|
|
||||||
|
|
|
||||||
|
|
@ -424,3 +424,35 @@ describe("tojson", function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("tojsonObject", () => {
|
||||||
|
it("empty object", () => {
|
||||||
|
assert.eq(tojsonObject({}, "", false), "{\n\t\n}");
|
||||||
|
assert.eq(tojsonObject({}, "", true), "{ }");
|
||||||
|
assert.eq(tojsonObject({}, "\t\t", false), "{\n\t\t\t\n\t\t}");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("single field", () => {
|
||||||
|
assert.eq(tojsonObject({a: 1}, "", false), '{\n\t"a" : 1\n}');
|
||||||
|
assert.eq(tojsonObject({a: 1}, "", true), '{ "a" : 1 }');
|
||||||
|
assert.eq(tojsonObject({a: 1}, "\t\t", false), '{\n\t\t\t"a" : 1\n\t\t}');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("multiple fields", () => {
|
||||||
|
assert.eq(tojsonObject({a: 1, b: 2}, "", false), '{\n\t"a" : 1,\n\t"b" : 2\n}');
|
||||||
|
assert.eq(tojsonObject({a: 1, b: 2}, "", true), '{ "a" : 1, "b" : 2 }');
|
||||||
|
assert.eq(tojsonObject({a: 1, b: 2}, "\t\t", false), '{\n\t\t\t"a" : 1,\n\t\t\t"b" : 2\n\t\t}');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("nested fields", () => {
|
||||||
|
assert.eq(
|
||||||
|
tojsonObject({a: 1, b: {bb: 2, cc: 3}}, "", false),
|
||||||
|
'{\n\t"a" : 1,\n\t"b" : {\n\t\t"bb" : 2,\n\t\t"cc" : 3\n\t}\n}',
|
||||||
|
);
|
||||||
|
assert.eq(tojsonObject({a: 1, b: {bb: 2, cc: 3}}, "", true), '{ "a" : 1, "b" : { "bb" : 2, "cc" : 3 } }');
|
||||||
|
assert.eq(
|
||||||
|
tojsonObject({a: 1, b: {bb: 2, cc: 3}}, "\t\t", false),
|
||||||
|
'{\n\t\t\t"a" : 1,\n\t\t\t"b" : {\n\t\t\t\t"bb" : 2,\n\t\t\t\t"cc" : 3\n\t\t\t}\n\t\t}',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,33 @@ describe("Timestamp shims and polyfills", function () {
|
||||||
assert.eq(ts, new Timestamp(1, 2));
|
assert.eq(ts, new Timestamp(1, 2));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Constructs without 'new'", () => {
|
||||||
|
let a = new Timestamp(10, 20);
|
||||||
|
let b = Timestamp(a.t, a.i);
|
||||||
|
printjson(a);
|
||||||
|
assert.eq(tojson(a), tojson(b), "timestamp");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("edge-case inputs", () => {
|
||||||
|
assert.throws(() => Timestamp(-2, 3), [], "Timestamp time must not accept negative time");
|
||||||
|
assert.throws(() => Timestamp(0, -1), [], "Timestamp increment must not accept negative time");
|
||||||
|
assert.throws(
|
||||||
|
() => Timestamp(0x10000 * 0x10000, 0),
|
||||||
|
[],
|
||||||
|
"Timestamp time must not accept values larger than 2**32 - 1",
|
||||||
|
);
|
||||||
|
assert.throws(
|
||||||
|
() => Timestamp(0, 0x10000 * 0x10000),
|
||||||
|
[],
|
||||||
|
"Timestamp increment must not accept values larger than 2**32 - 1",
|
||||||
|
);
|
||||||
|
|
||||||
|
let a = new Timestamp(0x80008000, 0x80008000 + 0.5);
|
||||||
|
let b = Timestamp(a.t, Math.round(a.i));
|
||||||
|
printjson(a);
|
||||||
|
assert.eq(tojson(a), tojson(b), "timestamp");
|
||||||
|
});
|
||||||
|
|
||||||
it("getTime", function () {
|
it("getTime", function () {
|
||||||
const ts = new Timestamp(1, 2);
|
const ts = new Timestamp(1, 2);
|
||||||
assert.eq(ts.getTime(), 1);
|
assert.eq(ts.getTime(), 1);
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue