Skip to content
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

redis add jSet function #716

Merged
merged 9 commits into from
Aug 9, 2024
Merged

redis add jSet function #716

merged 9 commits into from
Aug 9, 2024

Conversation

mtuchi
Copy link
Collaborator

@mtuchi mtuchi commented Aug 9, 2024

Summary

Add jSet(key: string, values: object) function.

Ref #709

Notes
changeset is in release/redis branch

Review Checklist

  • Does the PR do what it claims to do?
  • Are there any unit tests? Should there be?
  • Is there a changeset associated with this PR?

@mtuchi mtuchi changed the base branch from main to release/redis August 9, 2024 09:08
@mtuchi mtuchi changed the title add jSet function redis add jSet function Aug 9, 2024
@mtuchi mtuchi marked this pull request as ready for review August 9, 2024 09:12
@@ -191,6 +191,31 @@ export function hset(key, value) {
};
}

/**
* Creates a JSON object at the key
* @example <caption>Set a field and value for the patient key</caption>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are they still called fields and values in JSON?

I think this is just "setting an object to the value of key". And you only need one example, you don't need a multi-key example because that's not how JSON works

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 be a JSON object or a JSON string, let me update the docs.

packages/redis/src/Adaptor.js Outdated Show resolved Hide resolved
* @function
* @public
* @param {string} key - The key to modify.
* @param {object} value - The JSON value to set
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it a json object, rather than a JSON value? Can you set an array, string, or boolean value?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You can set a JSON string or a JSON object, A JSON can be deeply nested with values that are boolean or an array

packages/redis/src/Utils.js Show resolved Hide resolved
@@ -411,3 +420,57 @@ describe('hGetAll', () => {
});
});
});

describe('jSet', () => {
const jGetClient = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Needs renaming to jSetClient

const jGetClient = {
json: {
set: async (key, path, values) => {
expect(path).to.eql('$');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, this is important. Probably important enough for an explicit test, like jSet always sets at the document root

An explicit test would remind us and other developer that we are hard-coding the path to the root, which is an important detail. Test-as-documentation.

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 added a test for should always sets results at the document root. Please share feedback if that's the right way of testing

setMockClient({
json: {
set: (key, path, values) => {
expect(path).to.eql('$');
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think these assertions are valuable or useful in this test. We've already proved that the right values get passed through

expect(path).to.eql('$');
expect(key).to.eql('hash-key');
expect(values).to.eql({ name: 'mammoth' });
throw new Error('Existing key has wrong Redis type');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this exactly the same error as Redis throws?

I'm a bit torn on this test. Ok, it's good that it's documenting a certain behavior. But this isn't a behavior driven by our adaptor code

I suspect redis might throw a whole bunch of errors. What's special about this one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah that is thrown by redis when you try to update a key that has a different redis type. For example if you already have an existing key that is a has-key and you want to update the values for that key, Redis will throw Existing key has wrong Redis type

}
});

it('should always sets results at the document root', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@mtuchi I've updated this test to what I meant.

Notice that it's only asserting one thing. That's the only thing this test cares about. Super focused.

It's technically redundant because we've already got an implied test for it, but for me it's worth calling out like this

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks @josephjclark that makes lot of sense

@josephjclark josephjclark merged commit 22c4b62 into release/redis Aug 9, 2024
1 check passed
@josephjclark josephjclark deleted the 709-jset branch August 9, 2024 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants