Skip to content

Commit b0ca5c6

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

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

tests/unit/test_variable_explorer.py

Lines changed: 82 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,87 @@ 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(
393+
lambda self: (_ for _ in ()).throw(AttributeError("bool_ not found"))
394+
)
395+
396+
# Should fall back to regular type name
397+
value = 42
398+
assert _get_type_name(value) == "int"
399+
400+
@patch("deepnote_toolkit.variable_explorer.np")
401+
def test_get_type_name_type_error(self, mock_np):
402+
"""Test that TypeError is caught during isinstance check."""
403+
404+
# Create a mock bool_ class that raises TypeError on isinstance
405+
class MockBool:
406+
def __instancecheck__(self, instance):
407+
raise TypeError("Cannot check instance")
408+
409+
mock_np.bool_ = MockBool()
410+
411+
# Should fall back to regular type name
412+
value = "test"
413+
assert _get_type_name(value) == "str"
414+
415+
@patch("deepnote_toolkit.variable_explorer.np", None)
416+
def test_get_type_name_numpy_none(self):
417+
"""Test that the function handles when numpy is None."""
418+
# This should not raise an error, just return the regular type name
419+
value = [1, 2, 3]
420+
assert _get_type_name(value) == "list"
421+
422+
def test_get_type_name_edge_case_with_custom_type(self):
423+
"""Test with a custom type that might cause issues."""
424+
425+
class WeirdType:
426+
"""A custom type that behaves unusually."""
427+
428+
pass
429+
430+
weird_obj = WeirdType()
431+
# Should return the custom class name
432+
assert _get_type_name(weird_obj) == "WeirdType"
433+
434+
@patch("deepnote_toolkit.variable_explorer.np")
435+
def test_get_type_name_numpy_comparison_error(self, mock_np):
436+
"""Test when isinstance itself causes issues with numpy types."""
437+
438+
# Create a value that causes isinstance to fail
439+
class ProblematicValue:
440+
def __class__(self):
441+
raise TypeError("Cannot get class")
442+
443+
# Mock np.bool_ to exist but cause TypeError when used with isinstance
444+
mock_np.bool_ = np.bool_ # Use real np.bool_
445+
446+
# Create a regular value that should work
447+
normal_value = 123
448+
449+
# Even if there's an issue with numpy comparison, should fall back safely
450+
assert _get_type_name(normal_value) == "int"
451+
452+
371453
class TestDeepnoteExportDf:
372454
"""Test cases for the deepnote_export_df function."""
373455

0 commit comments

Comments
 (0)