Skip to content

Conversation

@tcannon91
Copy link
Collaborator

No description provided.

@tcannon91
Copy link
Collaborator Author

@addaleax 👋 could I get a review when you get a chance? I don't have the ability to add reviewers

// 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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to make sure that MinKey still serializes properly to BSON/EJSON without calling it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 😓

const serializedBsonMinKey = bson.serialize({ key: minKey });
const deserializedBsonMinKey = bson.deserialize(serializedBsonMinKey);
assert.deepEqual(minKey, deserializedBsonMinKey.key, "should be equal after bson serialization");

const serializedEJSONMinKey = EJSON.stringify({ key: minKey });
const deserializedEJSONMinKey = EJSON.parse(serializedEJSONMinKey);
assert.deepEqual(minKey, deserializedEJSONMinKey.key, "should be equal after ejson serialization");

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify what you mean by "without calling it"?

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

I have something like this, but I am not certain if it accomplishes what you are looking for

Right, doing that with those values should be fine 🙂

(Aside from the fact that you almost certainly want assert.deepStrictEqual instead of assert.deepEqual)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

  • I did a different comparison that I think better captures the nuance of the example you shared
  • I removed the ejson stringify/parse because I couldn't get it working (😖) but also I don't think it was important related to your shared example. The bson parsing seemed to be what mattered.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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

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");
Copy link
Collaborator

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

// 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");
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants