Skip to content

Commit f3627b7

Browse files
committed
test: Add unit test for get_hash
1 parent 8f2cad2 commit f3627b7

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

tests/unit/functions/test_remote_function_utils.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,78 @@ def test_package_existed_helper():
217217
assert not _utils._package_existed([], "pandas")
218218

219219

220+
def _function_add_one(x):
221+
return x + 1
222+
223+
224+
def _function_add_two(x):
225+
return x + 2
226+
227+
228+
@pytest.mark.parametrize(
229+
"func1, func2, should_be_equal, description",
230+
[
231+
(
232+
_function_add_one,
233+
_function_add_one,
234+
True,
235+
"Identical functions should have the same hash.",
236+
),
237+
(
238+
_function_add_one,
239+
_function_add_two,
240+
False,
241+
"Different functions should have different hashes.",
242+
),
243+
],
244+
)
245+
def test_get_hash_without_package_requirements(
246+
func1, func2, should_be_equal, description
247+
):
248+
"""Tests function hashes without any requirements."""
249+
hash1 = _utils.get_hash(func1)
250+
hash2 = _utils.get_hash(func2)
251+
252+
if should_be_equal:
253+
assert hash1 == hash2, f"FAILED: {description}"
254+
else:
255+
assert hash1 != hash2, f"FAILED: {description}"
256+
257+
258+
@pytest.mark.parametrize(
259+
"reqs1, reqs2, should_be_equal, description",
260+
[
261+
(
262+
None,
263+
["pandas>=1.0"],
264+
False,
265+
"Hash with or without requirements should differ from hash.",
266+
),
267+
(
268+
["pandas", "numpy", "scikit-learn"],
269+
["numpy", "scikit-learn", "pandas"],
270+
True,
271+
"Same requirements should produce the same hash.",
272+
),
273+
(
274+
["pandas==1.0"],
275+
["pandas==2.0"],
276+
False,
277+
"Different requirement versions should produce different hashes.",
278+
),
279+
],
280+
)
281+
def test_get_hash_with_package_requirements(reqs1, reqs2, should_be_equal, description):
282+
"""Tests how package requirements affect the final hash."""
283+
hash1 = _utils.get_hash(_function_add_one, package_requirements=reqs1)
284+
hash2 = _utils.get_hash(_function_add_one, package_requirements=reqs2)
285+
286+
if should_be_equal:
287+
assert hash1 == hash2, f"FAILED: {description}"
288+
else:
289+
assert hash1 != hash2, f"FAILED: {description}"
290+
291+
220292
# Helper functions for signature inspection tests
221293
def _func_one_arg_annotated(x: int) -> int:
222294
"""A function with one annotated arg and an annotated return type."""

0 commit comments

Comments
 (0)