Skip to content

Commit 3bc1602

Browse files
committed
test: add tests for _get_type_name
1 parent 449d8c4 commit 3bc1602

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

tests/unit/test_variable_explorer.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
ExportDataframeError,
1414
ExportSizeDataframeError,
1515
_get_elements_of,
16+
_get_type_name,
1617
_get_variable_dict_entry,
1718
deepnote_export_df,
1819
)
@@ -368,6 +369,81 @@ def test_variable_types(self, name, input_value, expected):
368369
assert result.get(key) == value, f"Failed at {name} with {key}"
369370

370371

372+
class TestGetTypeName:
373+
"""Test cases for the _get_type_name function, focusing on exception handling."""
374+
375+
def test_get_type_name_normal_types(self):
376+
"""Test that normal types return their expected names."""
377+
assert _get_type_name(5) == "int"
378+
assert _get_type_name(3.14) == "float"
379+
assert _get_type_name("hello") == "str"
380+
assert _get_type_name([1, 2, 3]) == "list"
381+
assert _get_type_name({1: 2}) == "dict"
382+
383+
def test_get_type_name_numpy_bool(self):
384+
"""Test that numpy bool_ type returns 'bool_'."""
385+
np_bool_value = np.bool_(True)
386+
assert _get_type_name(np_bool_value) == "bool_"
387+
388+
@patch("deepnote_toolkit.variable_explorer.np")
389+
def test_get_type_name_attribute_error(self, mock_np):
390+
"""Test that AttributeError is caught when np.bool_ doesn't exist."""
391+
# Mock np to raise AttributeError when accessing bool_
392+
mock_np.bool_ = property(lambda self: (_ for _ in ()).throw(AttributeError("bool_ not found")))
393+
394+
# Should fall back to regular type name
395+
value = 42
396+
assert _get_type_name(value) == "int"
397+
398+
@patch("deepnote_toolkit.variable_explorer.np")
399+
def test_get_type_name_type_error(self, mock_np):
400+
"""Test that TypeError is caught during isinstance check."""
401+
# Create a mock bool_ class that raises TypeError on isinstance
402+
class MockBool:
403+
def __instancecheck__(self, instance):
404+
raise TypeError("Cannot check instance")
405+
406+
mock_np.bool_ = MockBool()
407+
408+
# Should fall back to regular type name
409+
value = "test"
410+
assert _get_type_name(value) == "str"
411+
412+
@patch("deepnote_toolkit.variable_explorer.np", None)
413+
def test_get_type_name_numpy_none(self):
414+
"""Test that the function handles when numpy is None."""
415+
# This should not raise an error, just return the regular type name
416+
value = [1, 2, 3]
417+
assert _get_type_name(value) == "list"
418+
419+
def test_get_type_name_edge_case_with_custom_type(self):
420+
"""Test with a custom type that might cause issues."""
421+
class WeirdType:
422+
"""A custom type that behaves unusually."""
423+
pass
424+
425+
weird_obj = WeirdType()
426+
# Should return the custom class name
427+
assert _get_type_name(weird_obj) == "WeirdType"
428+
429+
@patch("deepnote_toolkit.variable_explorer.np")
430+
def test_get_type_name_numpy_comparison_error(self, mock_np):
431+
"""Test when isinstance itself causes issues with numpy types."""
432+
# Create a value that causes isinstance to fail
433+
class ProblematicValue:
434+
def __class__(self):
435+
raise TypeError("Cannot get class")
436+
437+
# Mock np.bool_ to exist but cause TypeError when used with isinstance
438+
mock_np.bool_ = np.bool_ # Use real np.bool_
439+
440+
# Create a regular value that should work
441+
normal_value = 123
442+
443+
# Even if there's an issue with numpy comparison, should fall back safely
444+
assert _get_type_name(normal_value) == "int"
445+
446+
371447
class TestDeepnoteExportDf:
372448
"""Test cases for the deepnote_export_df function."""
373449

0 commit comments

Comments
 (0)