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
Conversation
1fa0c2b
to
9d039bf
Compare
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.
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.
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.
+1 to what Yuri commented, lets make the comparison clearer
079dcb5
to
3390553
Compare
@barshaul round |
node/tests/TestUtilities.ts
Outdated
if ( | ||
map[key] !== null && | ||
typeof map[key] === "object" && | ||
map2[key] !== null && | ||
typeof map2[key] === "object" && | ||
!Array.isArray(map[key]) && | ||
!Array.isArray(map2[key]) |
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.
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
node/tests/TestUtilities.ts
Outdated
) { | ||
// If the value is a nested map, recursively compare them | ||
compareMaps( | ||
map[key] as Record<string, unknown>, |
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.
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): |
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.
same- add description
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.
and move it to a utils file
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.
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
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.
LGTM, but one small comment.
Thanks for testing the test tools - that is a really good approach!
node/tests/UnitTests.test.ts
Outdated
import { describe, expect, it } from "@jest/globals"; | ||
import { compareMaps } from "./TestUtilities"; | ||
|
||
describe("test compareMaps", () => { |
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.
All of the tests here should be under TestUtilities, please remove this file
node/tests/UnitTests.test.ts
Outdated
import { compareMaps } from "./TestUtilities"; | ||
|
||
describe("test compareMaps", () => { | ||
it("should compare two empty maps", () => { |
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.
"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"
node/tests/TestUtilities.ts
Outdated
map: Record<string | number | symbol, unknown>, | ||
map2: Record<string | number | symbol, unknown>, | ||
) { | ||
expect(JSON.stringify(map)).toEqual(JSON.stringify(map2)); |
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.
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
node/tests/TestUtilities.ts
Outdated
* ``` | ||
*/ | ||
export function compareMaps( | ||
map: Record<string | number | symbol, unknown>, |
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.
why symbol, unknown
?
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.
All possible values of a key of map in typescript
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.
I can remove that
node/tests/UtilsTests.test.ts
Outdated
expect(compareMaps(map1, map2)).toBe(true); | ||
}); | ||
|
||
it("Map comparison of maps with mixed types of values should return true", () => { |
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.
wrong description
node/tests/UtilsTests.test.ts
Outdated
expect(compareMaps(map1, map2)).toBe(true); | ||
}); | ||
|
||
it("Map comparison of maps with nested maps having different values should return true", () => { |
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.
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) |
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.
minor: it's more readable when explicit
assert compare_maps(zrange_map, expected_map) is True
(throughout all)
python/python/tests/test_utils.py
Outdated
"f": [1, "2", False], | ||
"e": "string", | ||
} | ||
assert not compare_maps(map1, map2) |
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.
same - make it more readable with is True is False everywhere
python/python/tests/utils/utils.py
Outdated
return version.parse(redis_version) < version.parse(min_version) | ||
|
||
|
||
def compare_maps(map, map2): |
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.
what about typing?
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.
minor comments
node/tests/TestUtilities.ts
Outdated
*/ | ||
export function compareMaps( | ||
map: Record<string | number | symbol, unknown>, | ||
map2: Record<string | number | symbol, unknown>, |
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.
optional:
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"], |
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.
Is it possible?
Mapping[str, "TResult"], | |
Mapping[str, TResult], |
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.
Nope 😕 , this is the syntax of what you're suggesting
95580f5
to
1e23215
Compare
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.