|
| 1 | +import pytest |
| 2 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 3 | + |
| 4 | +from transformerlab.services import experiment_service as svc |
| 5 | + |
| 6 | + |
| 7 | +def test_normalize_tags_lowercases_and_trims(): |
| 8 | + assert svc.normalize_tags([" Foo ", "BAR"]) == ["foo", "bar"] |
| 9 | + |
| 10 | + |
| 11 | +def test_normalize_tags_deduplicates_preserving_order(): |
| 12 | + assert svc.normalize_tags(["foo", "Bar", "FOO", "bar"]) == ["foo", "bar"] |
| 13 | + |
| 14 | + |
| 15 | +def test_normalize_tags_allows_dot_dash_underscore_digits(): |
| 16 | + assert svc.normalize_tags(["fine-tune", "v1.0", "exp_2"]) == [ |
| 17 | + "fine-tune", |
| 18 | + "v1.0", |
| 19 | + "exp_2", |
| 20 | + ] |
| 21 | + |
| 22 | + |
| 23 | +def test_normalize_tags_rejects_spaces_inside_tag(): |
| 24 | + with pytest.raises(ValueError, match="hello world"): |
| 25 | + svc.normalize_tags(["hello world"]) |
| 26 | + |
| 27 | + |
| 28 | +def test_normalize_tags_rejects_punctuation(): |
| 29 | + with pytest.raises(ValueError, match="bad!"): |
| 30 | + svc.normalize_tags(["bad!"]) |
| 31 | + |
| 32 | + |
| 33 | +def test_normalize_tags_rejects_unicode(): |
| 34 | + with pytest.raises(ValueError, match="café"): |
| 35 | + svc.normalize_tags(["café"]) |
| 36 | + |
| 37 | + |
| 38 | +def test_normalize_tags_rejects_over_32_chars(): |
| 39 | + long_tag = "a" * 33 |
| 40 | + with pytest.raises(ValueError, match="32"): |
| 41 | + svc.normalize_tags([long_tag]) |
| 42 | + |
| 43 | + |
| 44 | +def test_normalize_tags_accepts_exactly_32_chars(): |
| 45 | + long_tag = "a" * 32 |
| 46 | + assert svc.normalize_tags([long_tag]) == [long_tag] |
| 47 | + |
| 48 | + |
| 49 | +def test_normalize_tags_rejects_empty_after_strip(): |
| 50 | + with pytest.raises(ValueError): |
| 51 | + svc.normalize_tags([" "]) |
| 52 | + |
| 53 | + |
| 54 | +def test_normalize_tags_empty_input_returns_empty_list(): |
| 55 | + assert svc.normalize_tags([]) == [] |
| 56 | + |
| 57 | + |
| 58 | +def _mock_experiment(current_tags=None): |
| 59 | + """Build a mock Experiment whose get_json_data returns config.tags=current_tags.""" |
| 60 | + mock_exp = MagicMock() |
| 61 | + mock_exp.get_json_data = AsyncMock(return_value={"config": {"tags": list(current_tags) if current_tags else []}}) |
| 62 | + mock_exp.update_config_field = AsyncMock() |
| 63 | + return mock_exp |
| 64 | + |
| 65 | + |
| 66 | +@patch("transformerlab.services.experiment_service.cache.invalidate", new_callable=AsyncMock) |
| 67 | +@patch("transformerlab.services.experiment_service.Experiment.get", new_callable=AsyncMock) |
| 68 | +async def test_experiment_add_tags_merges_with_existing(mock_get, _mock_cache): |
| 69 | + mock_exp = _mock_experiment(current_tags=["foo"]) |
| 70 | + mock_get.return_value = mock_exp |
| 71 | + |
| 72 | + result = await svc.experiment_add_tags("exp1", ["Bar", "BAZ"]) |
| 73 | + |
| 74 | + assert result == ["foo", "bar", "baz"] |
| 75 | + mock_exp.update_config_field.assert_awaited_once_with("tags", ["foo", "bar", "baz"]) |
| 76 | + |
| 77 | + |
| 78 | +@patch("transformerlab.services.experiment_service.cache.invalidate", new_callable=AsyncMock) |
| 79 | +@patch("transformerlab.services.experiment_service.Experiment.get", new_callable=AsyncMock) |
| 80 | +async def test_experiment_add_tags_is_idempotent(mock_get, _mock_cache): |
| 81 | + mock_exp = _mock_experiment(current_tags=["foo", "bar"]) |
| 82 | + mock_get.return_value = mock_exp |
| 83 | + |
| 84 | + result = await svc.experiment_add_tags("exp1", ["FOO", "bar"]) |
| 85 | + |
| 86 | + assert result == ["foo", "bar"] |
| 87 | + mock_exp.update_config_field.assert_awaited_once_with("tags", ["foo", "bar"]) |
| 88 | + |
| 89 | + |
| 90 | +@patch("transformerlab.services.experiment_service.cache.invalidate", new_callable=AsyncMock) |
| 91 | +@patch("transformerlab.services.experiment_service.Experiment.get", new_callable=AsyncMock) |
| 92 | +async def test_experiment_add_tags_handles_missing_tags_field(mock_get, _mock_cache): |
| 93 | + mock_exp = MagicMock() |
| 94 | + mock_exp.get_json_data = AsyncMock(return_value={"config": {}}) |
| 95 | + mock_exp.update_config_field = AsyncMock() |
| 96 | + mock_get.return_value = mock_exp |
| 97 | + |
| 98 | + result = await svc.experiment_add_tags("exp1", ["foo"]) |
| 99 | + |
| 100 | + assert result == ["foo"] |
| 101 | + |
| 102 | + |
| 103 | +@patch("transformerlab.services.experiment_service.Experiment.get", new_callable=AsyncMock) |
| 104 | +async def test_experiment_add_tags_enforces_max_cap(mock_get): |
| 105 | + existing = [f"t{i}" for i in range(19)] |
| 106 | + mock_exp = _mock_experiment(current_tags=existing) |
| 107 | + mock_get.return_value = mock_exp |
| 108 | + |
| 109 | + with pytest.raises(ValueError, match="20"): |
| 110 | + await svc.experiment_add_tags("exp1", ["new1", "new2"]) |
| 111 | + mock_exp.update_config_field.assert_not_awaited() |
| 112 | + |
| 113 | + |
| 114 | +@patch("transformerlab.services.experiment_service.cache.invalidate", new_callable=AsyncMock) |
| 115 | +@patch("transformerlab.services.experiment_service.Experiment.get", new_callable=AsyncMock) |
| 116 | +async def test_experiment_remove_tags_removes_present(mock_get, _mock_cache): |
| 117 | + mock_exp = _mock_experiment(current_tags=["foo", "bar", "baz"]) |
| 118 | + mock_get.return_value = mock_exp |
| 119 | + |
| 120 | + result = await svc.experiment_remove_tags("exp1", ["Bar"]) |
| 121 | + |
| 122 | + assert result == ["foo", "baz"] |
| 123 | + mock_exp.update_config_field.assert_awaited_once_with("tags", ["foo", "baz"]) |
| 124 | + |
| 125 | + |
| 126 | +@patch("transformerlab.services.experiment_service.cache.invalidate", new_callable=AsyncMock) |
| 127 | +@patch("transformerlab.services.experiment_service.Experiment.get", new_callable=AsyncMock) |
| 128 | +async def test_experiment_remove_tags_absent_is_noop(mock_get, _mock_cache): |
| 129 | + mock_exp = _mock_experiment(current_tags=["foo"]) |
| 130 | + mock_get.return_value = mock_exp |
| 131 | + |
| 132 | + result = await svc.experiment_remove_tags("exp1", ["missing"]) |
| 133 | + |
| 134 | + assert result == ["foo"] |
| 135 | + mock_exp.update_config_field.assert_awaited_once_with("tags", ["foo"]) |
| 136 | + |
| 137 | + |
| 138 | +def test_aggregate_tags_dedupes_and_sorts(): |
| 139 | + experiments = [ |
| 140 | + {"id": "a", "config": {"tags": ["foo", "bar"]}}, |
| 141 | + {"id": "b", "config": {"tags": ["bar", "baz"]}}, |
| 142 | + {"id": "c", "config": {}}, |
| 143 | + {"id": "d", "config": {"tags": []}}, |
| 144 | + ] |
| 145 | + assert svc.aggregate_tags(experiments) == ["bar", "baz", "foo"] |
| 146 | + |
| 147 | + |
| 148 | +def test_aggregate_tags_handles_string_config_blob(): |
| 149 | + experiments = [{"id": "a", "config": '{"tags": ["zeta", "alpha"]}'}] |
| 150 | + assert svc.aggregate_tags(experiments) == ["alpha", "zeta"] |
| 151 | + |
| 152 | + |
| 153 | +def test_aggregate_tags_skips_non_string_entries(): |
| 154 | + experiments = [{"id": "a", "config": {"tags": ["foo", 42, None, "bar"]}}] |
| 155 | + assert svc.aggregate_tags(experiments) == ["bar", "foo"] |
| 156 | + |
| 157 | + |
| 158 | +def test_aggregate_tags_empty_input(): |
| 159 | + assert svc.aggregate_tags([]) == [] |
0 commit comments