@@ -392,3 +392,69 @@ def test_draw_compound_image_request_fails(self):
392392 result = cheminfo_api .draw_compound ("caffeine" )
393393 assert result is None
394394 mock_print .assert_called ()
395+
396+
397+ class TestFetchScalar :
398+ def test_property_with_invalid_type_returns_none (self ):
399+ """Test that properties with invalid types return None."""
400+ with mock .patch .object (
401+ cheminfo_api , "_resolve_to_single_cid" , return_value = 2519
402+ ):
403+ with mock .patch (
404+ "ChemInformant.api_helpers.get_batch_properties" ,
405+ return_value = {2519 : {"MolecularWeight" : ["invalid" , "list" ]}},
406+ ):
407+ result = cheminfo_api ._fetch_scalar (2519 , "molecular_weight" )
408+ assert result is None
409+
410+
411+ class TestGetSynonyms :
412+ def test_get_synonyms_returns_empty_list_on_not_found (self ):
413+ """Test that get_synonyms returns empty list when compound not found."""
414+ result = cheminfo_api .get_synonyms ("nonexistent" )
415+ assert result == []
416+
417+ def test_get_synonyms_returns_empty_list_on_ambiguous (self ):
418+ """Test that get_synonyms returns empty list when identifier is ambiguous."""
419+ result = cheminfo_api .get_synonyms ("ambiguous" )
420+ assert result == []
421+
422+
423+ class TestCompoundModel :
424+ def test_safe_float_handles_invalid_types (self ):
425+ """Test that _safe_float returns None for invalid values."""
426+ # Test with valid values
427+ compound = Compound (
428+ input_identifier = "test" ,
429+ cid = "123" ,
430+ status = "OK" ,
431+ molecular_weight = 100.5 ,
432+ )
433+ assert compound .molecular_weight == 100.5
434+
435+ # Test with None
436+ compound = Compound (
437+ input_identifier = "test" , cid = "123" , status = "OK" , molecular_weight = None
438+ )
439+ assert compound .molecular_weight is None
440+
441+ # Test with empty string
442+ compound = Compound (
443+ input_identifier = "test" , cid = "123" , status = "OK" , molecular_weight = ""
444+ )
445+ assert compound .molecular_weight is None
446+
447+ # Test with invalid string (ValueError)
448+ compound = Compound (
449+ input_identifier = "test" ,
450+ cid = "123" ,
451+ status = "OK" ,
452+ molecular_weight = "not_a_number" ,
453+ )
454+ assert compound .molecular_weight is None
455+
456+ # Test with invalid type (TypeError) - using a complex object
457+ compound = Compound (
458+ input_identifier = "test" , cid = "123" , status = "OK" , molecular_weight = {"key" : "value" }
459+ )
460+ assert compound .molecular_weight is None
0 commit comments