Tests for build_dataloaders function.
| 15 | |
| 16 | |
| 17 | class TestBuildDataloaders: |
| 18 | """Tests for build_dataloaders function.""" |
| 19 | |
| 20 | @patch("datastudio.datasets.builder.build_from_cfg") |
| 21 | @patch("datastudio.datasets.builder.ConfigLoader") |
| 22 | def test_build_with_yaml_config( |
| 23 | self, mock_config_loader, mock_build, temp_dir, mock_logger |
| 24 | ): |
| 25 | """Test building dataloaders from YAML config.""" |
| 26 | # Setup mocks |
| 27 | mock_config = MagicMock() |
| 28 | mock_config.datasets = [MagicMock()] |
| 29 | mock_config.get_file_paths.return_value = ["/data/a.json", "/data/b.json"] |
| 30 | mock_config_loader.load.return_value = mock_config |
| 31 | mock_build.return_value = MagicMock() |
| 32 | |
| 33 | dataset_config = { |
| 34 | "type": "StandardDataLoader", |
| 35 | "dataset": "config.yaml", |
| 36 | "data_root": temp_dir, |
| 37 | "batch_size": 32, |
| 38 | "num_workers": 4, |
| 39 | } |
| 40 | |
| 41 | # Create yaml file |
| 42 | yaml_path = os.path.join(temp_dir, "config.yaml") |
| 43 | with open(yaml_path, "w") as f: |
| 44 | f.write("datasets:\n - file_path: a.json\n") |
| 45 | |
| 46 | loaders = build_dataloaders( |
| 47 | dataset_config, logger=mock_logger, work_dir=temp_dir |
| 48 | ) |
| 49 | |
| 50 | assert isinstance(loaders, list) |
| 51 | |
| 52 | @patch("datastudio.datasets.builder.build_from_cfg") |
| 53 | def test_build_with_single_file(self, mock_build, temp_dir, mock_logger): |
| 54 | """Test building dataloaders from single file.""" |
| 55 | mock_build.return_value = MagicMock() |
| 56 | |
| 57 | dataset_config = { |
| 58 | "type": "StandardDataLoader", |
| 59 | "dataset": ["/data/test.json"], |
| 60 | "data_root": "/data", |
| 61 | "batch_size": 32, |
| 62 | "num_workers": 4, |
| 63 | } |
| 64 | |
| 65 | loaders = build_dataloaders( |
| 66 | dataset_config, logger=mock_logger, work_dir=temp_dir |
| 67 | ) |
| 68 | |
| 69 | assert isinstance(loaders, list) |
| 70 | assert len(loaders) == 1 |
| 71 | |
| 72 | @patch("datastudio.datasets.builder.build_from_cfg") |
| 73 | def test_build_with_file_list(self, mock_build, temp_dir, mock_logger): |
| 74 | """Test building dataloaders from file list.""" |
nothing calls this directly
no outgoing calls
no test coverage detected