-
Notifications
You must be signed in to change notification settings - Fork 24
STREAMS-1982: Add singleton compat for MinKey #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| load(__dirname + '/index.js'); | ||
|
|
||
| const bson = require('bson'); | ||
|
|
||
| assert.strictEqual(ObjectId('0123456789abcdef01234567').tojson(), 'ObjectId("0123456789abcdef01234567")'); | ||
|
|
||
| assert.strictEqual(BinData(4, 'abcdefgh').toString(), 'BinData(4,"abcdefgh")'); | ||
|
|
@@ -87,7 +89,7 @@ const tsFromStr = Timestamp.fromString('ff', 16); | |
| assert.strictEqual(tsFromStr.i, 255); | ||
| assert.strictEqual(tsFromStr.t, 0); | ||
| assert.strictEqual(Timestamp.MAX_VALUE._bsontype, 'Long'); | ||
| assert.strictEqual(Timestamp.MAX_VALUE, Long.MAX_UNSIGNED_VALUE); | ||
| assert.strictEqual(Timestamp.MAX_VALUE, Long.MAX_UNSIGNED_VALUE); | ||
|
|
||
| const id = ObjectId('68ffa28b77bba38c9ddcf376'); | ||
| const dbRef = DBRef('testColl', id, 'testDb'); | ||
|
|
@@ -161,3 +163,19 @@ assert(sortedArrayJson.indexOf('"a"') < sortedArrayJson.indexOf('"z"'), 'Array.t | |
| assert(sortedArrayJson.indexOf('"b"') < sortedArrayJson.indexOf('"y"'), 'Array.tojson with sortedKeys=true should sort object keys in array elements'); | ||
| assert(unsortedArrayJson.indexOf('"z"') < unsortedArrayJson.indexOf('"a"'), 'Array.tojson with sortedKeys=false should not sort keys'); | ||
| assert(defaultArrayJson.indexOf('"z"') < defaultArrayJson.indexOf('"a"'), 'Array.tojson without sortedKeys should not sort keys'); | ||
|
|
||
| // Test MinKey | ||
| const minKey = new MinKey(); | ||
| assert(minKey instanceof MinKey, "minKey should be an instance of MinKey"); | ||
| assert.strictEqual(minKey.tojson(), '{ "$minKey" : 1 }', "minKey should serialize correctly"); | ||
| assert.strictEqual(minKey.toString(), "[object Function]", "minKey toString should work"); | ||
| assert.strictEqual(minKey.toJSON(), '{ "$minKey" : 1 }', "minKey toJSON should work"); | ||
|
|
||
| // Test that multiple references return the same instance | ||
| const anotherMinKeyRef = new MinKey(); | ||
| assert.strictEqual(minKey, anotherMinKeyRef, "minKey should be a singleton - v1"); | ||
| assert.strictEqual(MinKey(), MinKey(), "minKey should be a singleton - v2"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll need to make sure that
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you clarify what you mean by "without calling it"? If you have a reference test I can look at, that would be helpful
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have something like this, but I am not certain if it accomplishes what you are looking for 😓
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
One oddity of MongoDB JS environments like the legacy shell and $function is that db.coll.insertOne({ a: MinKey, b: MaxKey })and db.coll.insertOne({ a: MinKey(), b: MaxKey() })are equivalent – and it's not immediately true whether that's still the case after this patch or not; and even if it's the case already, I'd say we should still add a test to make sure it keeps being true regardless of future modifications to this code
Right, doing that with those values should be fine 🙂 (Aside from the fact that you almost certainly want
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me know what you think of the changes. I adjusted the test from what I originally included in the thread.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. EJSON would be kind of nice to test since it's built into mongosh (unlike direct bson serialization/deserialization) but that's fine, yeah |
||
|
|
||
| const serializedBsonMinKey = bson.serialize({ key1: MinKey, key2: MinKey() }); | ||
| const deserializedBsonMinKey = bson.deserialize(serializedBsonMinKey); | ||
| assert.deepStrictEqual(deserializedBsonMinKey.key1, deserializedBsonMinKey.key2, "should be equal after bson serialization"); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal, but passing a static string as a message for
assert.strictEqual()and friends is an antipattern since it will hide information about the actual comparison results – if you want to add this type of information, add a comment