Skip to content

Commit 65c2d9b

Browse files
committed
test output format for Tox21MolNet._load_data_from_file
1 parent adedc09 commit 65c2d9b

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

tests/unit/dataset_classes/testTox21MolNet.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
from typing import List
33
from unittest.mock import MagicMock, mock_open, patch
44

5+
import torch
6+
57
from chebai.preprocessing.datasets.tox21 import Tox21MolNet
8+
from chebai.preprocessing.reader import ChemDataReader
69
from tests.unit.mock_data.tox_mock_data import Tox21MolNetMockData
710

811

@@ -16,9 +19,7 @@ def setUpClass(cls, mock_makedirs: MagicMock) -> None:
1619
Args:
1720
mock_makedirs (MagicMock): Mocked `os.makedirs` function.
1821
"""
19-
ReaderMock = MagicMock()
20-
ReaderMock.name.return_value = "MockedReaderTox21MolNet"
21-
Tox21MolNet.READER = ReaderMock
22+
Tox21MolNet.READER = ChemDataReader
2223
cls.data_module = Tox21MolNet()
2324

2425
@patch(
@@ -28,20 +29,38 @@ def setUpClass(cls, mock_makedirs: MagicMock) -> None:
2829
)
2930
def test_load_data_from_file(self, mock_open_file: mock_open) -> None:
3031
"""
31-
Test the `_load_data_from_file` method for correct CSV parsing.
32+
Test the `_load_data_from_file` method for correct output.
3233
3334
Args:
3435
mock_open_file (mock_open): Mocked open function to simulate file reading.
3536
"""
36-
expected_data = Tox21MolNetMockData.get_processed_data()
3737
actual_data = self.data_module._load_data_from_file("fake/file/path.csv")
3838

39-
self.assertEqual(
40-
list(actual_data),
41-
expected_data,
42-
"The loaded data does not match the expected output from the file.",
39+
first_instance = next(actual_data)
40+
41+
# Check for required keys
42+
required_keys = ["features", "labels", "ident"]
43+
for key in required_keys:
44+
self.assertIn(
45+
key, first_instance, f"'{key}' key is missing in the output data."
46+
)
47+
48+
self.assertTrue(
49+
all(isinstance(feature, int) for feature in first_instance["features"]),
50+
"Not all elements in 'features' are integers.",
4351
)
4452

53+
# Check that 'features' can be converted to a tensor
54+
features = first_instance["features"]
55+
try:
56+
tensor_features = torch.tensor(features)
57+
self.assertTrue(
58+
tensor_features.ndim > 0,
59+
"'features' should be convertible to a non-empty tensor.",
60+
)
61+
except Exception as e:
62+
self.fail(f"'features' cannot be converted to a tensor: {str(e)}")
63+
4564
@patch(
4665
"builtins.open",
4766
new_callable=mock_open,

0 commit comments

Comments
 (0)