Spaces:
Sleeping
Sleeping
File size: 8,209 Bytes
5caedb4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
import os
import pandas as pd
import pytest
from llm_studio.src.possible_values import (
Columns,
DatasetValue,
Files,
Number,
String,
_scan_dirs,
_scan_files,
strip_common_prefix,
)
# Helper function to create a temporary directory structure
@pytest.fixture
def temp_dir_structure(tmp_path):
base_dir = tmp_path / "test_dir"
base_dir.mkdir()
(base_dir / "subdir1").mkdir()
(base_dir / "subdir2").mkdir()
(base_dir / "subdir1" / "subsubdir").mkdir()
(base_dir / "file1.csv").touch()
(base_dir / "file2.json").touch()
(base_dir / "__meta_info__train.json").touch()
(base_dir / "subdir1" / "file3.parquet").touch()
(base_dir / "subdir1" / "__meta_info__train.pq.csv").touch()
return base_dir
def test_scan_dirs(temp_dir_structure):
dirs = _scan_dirs(str(temp_dir_structure))
expected = [
str(temp_dir_structure / "subdir1") + "/",
str(temp_dir_structure / "subdir2") + "/",
str(temp_dir_structure / "subdir1" / "subsubdir") + "/",
]
assert set(dirs) == set(expected)
def test_scan_files(temp_dir_structure):
files = _scan_files(str(temp_dir_structure))
expected = [
str(temp_dir_structure / "file1.csv"),
str(temp_dir_structure / "file2.json"),
str(temp_dir_structure / "subdir1" / "file3.parquet"),
]
assert set(files) == set(expected)
def test_strip_common_prefix():
paths = ["/a/b/c/file1.txt", "/a/b/c/file2.txt", "/a/b/d/file3.txt"]
stripped = strip_common_prefix(paths)
assert stripped == ("c/file1.txt", "c/file2.txt", "d/file3.txt")
# Test with ignore_set
paths_with_ignore = paths + ["ignore_this"]
stripped_with_ignore = strip_common_prefix(
paths_with_ignore, ignore_set={"ignore_this"}
)
assert stripped_with_ignore == (
"c/file1.txt",
"c/file2.txt",
"d/file3.txt",
"ignore_this",
)
def test_strip_common_prefix_empty_paths():
paths = []
stripped = strip_common_prefix(paths)
assert stripped == tuple([])
def test_number_slider():
num = Number(min=0, max=10, step=0.5)
assert num.min == 0
assert num.max == 10
assert num.step == 0.5
def test_number_spinbox():
num = Number(min=0, step=0.5)
assert num.min == 0
assert num.max is None
assert num.step == 0.5
def test_number_impossible_values():
with pytest.raises(ValueError):
Number(min=0, max=10, step="a")
with pytest.raises(ValueError):
Number(min=0, max="a", step=0.5)
with pytest.raises(ValueError):
Number(min="a", max=10, step=0.5)
with pytest.raises(ValueError):
Number(min=0, max=10)
with pytest.raises(ValueError):
Number(min=10, max=1, step=1)
with pytest.raises(ValueError):
Number(min=10, max=0, step=1)
def test_string_tuple_of_strings():
s = String(
values=("a", "b", "c"), allow_custom=True, placeholder="Select an option"
)
assert s.values == ("a", "b", "c")
assert s.allow_custom is True
assert s.placeholder == "Select an option"
def test_string_tuple_of_tuples():
s = String(
values=(("a", "hello there"), ("b", "hello there"), ("c", "hello there")),
allow_custom=True,
placeholder="Select an option",
)
assert s.values == (
("a", "hello there"),
("b", "hello there"),
("c", "hello there"),
)
assert s.allow_custom is True
assert s.placeholder == "Select an option"
def test_string_impossible_values():
with pytest.raises(ValueError):
String(values=("a", "b", "c"), allow_custom="a")
with pytest.raises(ValueError):
String(values=("a", "b", "c"), placeholder=True)
class TestDatasetValue:
def test_get_value(self):
"""
Test that NotImplementedError is raised when get_value is called directly
This is a base class and should not be used directly.
get_value is an abstract method.
"""
dataset_value = DatasetValue()
with pytest.raises(NotImplementedError):
dataset_value.get_value(None, None, None)
@pytest.mark.parametrize(
"current_values, possible_values, prefer_with, expected",
[
(["a", "b"], ["a", "b", "c"], None, ["a", "b"]),
(["a", "d"], ["a", "b", "c"], None, ["a"]),
([], ["a", "b", "c"], None, ["a"]),
(["d", "e"], ["a", "b", "c"], None, ["a"]),
([], [], None, [""]),
(["a", "b"], [], None, [""]),
],
)
def test_compute_current_values_basic(
self, current_values, possible_values, prefer_with, expected
):
result = DatasetValue._compute_current_values(
current_values, possible_values, prefer_with
)
assert result == expected
def test_compute_current_values_with_prefer_function(self):
current_values = []
possible_values = ["a", "b", "c", "d"]
def prefer_with(x):
return x in ["b", "c"]
result = DatasetValue._compute_current_values(
current_values, possible_values, prefer_with
)
assert result == ["b", "c"]
def test_compute_current_values_with_prefer_function_single_match(self):
current_values = []
possible_values = ["a", "b", "d"]
def prefer_with(x):
return x in ["b", "c"]
result = DatasetValue._compute_current_values(
current_values, possible_values, prefer_with
)
assert result == ["b"]
def test_compute_current_values_prefer_function_no_match(self):
current_values = []
possible_values = ["a", "b", "c"]
def prefer_with(x):
return x == "d"
result = DatasetValue._compute_current_values(
current_values, possible_values, prefer_with
)
assert result == [
"a"
] # Should return first possible value when no preference matches
def test_compute_current_values_all_filtered_out(self):
current_values = ["d", "e"]
possible_values = ["a", "b", "c"]
result = DatasetValue._compute_current_values(current_values, possible_values)
assert result == [
"a"
] # Should return first possible value when all current values are filtered out
@pytest.mark.parametrize(
"current_values, possible_values",
[
(["a", "a", "b"], ["a", "b", "c"]),
(["a", "b", "a"], ["a", "b", "c"]),
(["a", "b", "a"], ["a", "b", "c", "a"]),
(["a", "b"], ["a", "a", "b"]),
],
)
def test_compute_current_values_duplicates(self, current_values, possible_values):
with pytest.raises(ValueError):
DatasetValue._compute_current_values(current_values, possible_values)
def test_compute_current_values_type_check(self):
current_values = ["a", "b"]
possible_values = ["a", "b", "c"]
result = DatasetValue._compute_current_values(current_values, possible_values)
assert isinstance(result, list)
assert all(isinstance(item, str) for item in result)
# Mock dataset for testing
@pytest.fixture
def mock_dataset(temp_dir_structure):
return {
"path": str(temp_dir_structure),
"dataframe": pd.DataFrame(
{"col1": [1, 2, 3], "col2": ["a", "b", "c"], "col3": [True, False, True]}
),
}
@pytest.mark.parametrize(
"filename", ["file1.csv", "file2.json", "subdir1/file3.parquet", "non-existant"]
)
def test_files(mock_dataset, filename):
files = Files()
result, value = files.get_value(mock_dataset, filename, str)
assert isinstance(result, String)
assert value == os.path.join(mock_dataset["path"], "file1.csv")
@pytest.mark.parametrize("col", ["col1", "col2", "col3", "non-existant"])
def test_columns(mock_dataset, col):
cols = Columns()
result, value = cols.get_value(mock_dataset, col, str)
assert isinstance(result, String)
assert set(result.values) == {"col1", "col2", "col3"}
if col == "non-existant":
assert value == "col1"
else:
assert value == col
|