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

Refactor map comparison in Python and Node #1319

Merged
merged 3 commits into from May 12, 2024

Conversation

shohamazon
Copy link
Member

Issue #, if available:

Description of changes:

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@shohamazon shohamazon requested a review from a team as a code owner April 21, 2024 08:42
@shohamazon shohamazon force-pushed the map_ordering branch 2 times, most recently from 1fa0c2b to 9d039bf Compare April 21, 2024 08:50
Copy link
Collaborator

@Yury-Fridlyand Yury-Fridlyand left a comment

Choose a reason for hiding this comment

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

Please have a look on #1257.
LinkedHashMap in java ensures the order, while regular HashMap - don't. Probably, you need to do similar fix in python and node clients.

node/tests/SharedTests.ts Outdated Show resolved Hide resolved
python/python/tests/test_async_client.py Show resolved Hide resolved
Copy link
Contributor

@barshaul barshaul left a comment

Choose a reason for hiding this comment

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

+1 to what Yuri commented, lets make the comparison clearer

@shohamazon shohamazon force-pushed the map_ordering branch 4 times, most recently from 079dcb5 to 3390553 Compare May 5, 2024 08:24
@shohamazon
Copy link
Member Author

+1 to what Yuri commented, lets make the comparison clearer

@barshaul round

@shohamazon shohamazon requested a review from barshaul May 5, 2024 08:36
Comment on lines 63 to 69
if (
map[key] !== null &&
typeof map[key] === "object" &&
map2[key] !== null &&
typeof map2[key] === "object" &&
!Array.isArray(map[key]) &&
!Array.isArray(map2[key])
Copy link
Contributor

Choose a reason for hiding this comment

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

this is really not readable.
I think map[key] !== null can be removed if we check for typeof map2[key] === "object".
Try to check if there's a better way of checking if it's a map, and add a comment explaining what you do there

) {
// If the value is a nested map, recursively compare them
compareMaps(
map[key] as Record<string, unknown>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it sure that the record's key is a string?

@@ -117,6 +117,14 @@ async def check_if_server_version_lt(client: TRedisClient, min_version: str) ->
return version.parse(redis_version) < version.parse(min_version)


def compare_maps(map, map2):
Copy link
Contributor

Choose a reason for hiding this comment

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

same- add description

Copy link
Contributor

Choose a reason for hiding this comment

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

and move it to a utils file

Copy link
Contributor

@barshaul barshaul left a comment

Choose a reason for hiding this comment

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

Please add unit tests for the map compare functions. make sure recursive calls and maps with same items but different order return the expected results

Copy link
Collaborator

@Yury-Fridlyand Yury-Fridlyand left a comment

Choose a reason for hiding this comment

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

LGTM, but one small comment.
Thanks for testing the test tools - that is a really good approach!

python/python/tests/utils/utils.py Outdated Show resolved Hide resolved
import { describe, expect, it } from "@jest/globals";
import { compareMaps } from "./TestUtilities";

describe("test compareMaps", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

All of the tests here should be under TestUtilities, please remove this file

import { compareMaps } from "./TestUtilities";

describe("test compareMaps", () => {
it("should compare two empty maps", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

"should compare " doesn't imply what the test checks for - should it be equal? not equal? instead write something like "Map comparison of empty maps should return true"

map: Record<string | number | symbol, unknown>,
map2: Record<string | number | symbol, unknown>,
) {
expect(JSON.stringify(map)).toEqual(JSON.stringify(map2));
Copy link
Contributor

Choose a reason for hiding this comment

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

return true for equal and false if not. then do the expect true or false inside the tests. it is more readable from the function name and from reading the test

python/python/tests/utils/utils.py Outdated Show resolved Hide resolved
* ```
*/
export function compareMaps(
map: Record<string | number | symbol, unknown>,
Copy link
Contributor

Choose a reason for hiding this comment

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

why symbol, unknown?

Copy link
Member Author

Choose a reason for hiding this comment

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

All possible values of a key of map in typescript

Copy link
Member Author

Choose a reason for hiding this comment

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

I can remove that

expect(compareMaps(map1, map2)).toBe(true);
});

it("Map comparison of maps with mixed types of values should return true", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

wrong description

expect(compareMaps(map1, map2)).toBe(true);
});

it("Map comparison of maps with nested maps having different values should return true", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

wrong description

RangeByScore(start=InfBound.NEG_INF, stop=InfBound.POS_INF),
)
expected_map = {"one": 1.0, "two": 2.0, "three": 3.0}
assert compare_maps(zrange_map, expected_map)
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: it's more readable when explicit
assert compare_maps(zrange_map, expected_map) is True
(throughout all)

"f": [1, "2", False],
"e": "string",
}
assert not compare_maps(map1, map2)
Copy link
Contributor

Choose a reason for hiding this comment

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

same - make it more readable with is True is False everywhere

return version.parse(redis_version) < version.parse(min_version)


def compare_maps(map, map2):
Copy link
Contributor

Choose a reason for hiding this comment

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

what about typing?

Copy link
Contributor

@barshaul barshaul left a comment

Choose a reason for hiding this comment

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

minor comments

*/
export function compareMaps(
map: Record<string | number | symbol, unknown>,
map2: Record<string | number | symbol, unknown>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

optional:

Suggested change
map2: Record<string | number | symbol, unknown>,
map2: Record<string, unknown>,

AFAIK all maps in redis have string keys.

@@ -18,6 +18,7 @@
int,
None,
Dict[str, T],
Mapping[str, "TResult"],
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 possible?

Suggested change
Mapping[str, "TResult"],
Mapping[str, TResult],

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope 😕 , this is the syntax of what you're suggesting

@shohamazon shohamazon force-pushed the map_ordering branch 2 times, most recently from 95580f5 to 1e23215 Compare May 12, 2024 14:11
@shohamazon shohamazon merged commit 16a60ad into aws:main May 12, 2024
12 checks passed
@shohamazon shohamazon deleted the map_ordering branch May 12, 2024 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants