forked from bazelbuild/bazel-central-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_selector_test.py
91 lines (74 loc) · 3.62 KB
/
module_selector_test.py
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
import unittest
from unittest.mock import MagicMock
from registry import RegistryClient
from module_selector import select_modules
class TestSelectModules(unittest.TestCase):
def setUp(self):
# Create a mock registry client
self.registry = RegistryClient("/fake")
self.registry.get_all_modules = MagicMock(return_value=["foo_module", "bar_module", "baz_module", "qux_module"])
self.registry.get_module_versions = MagicMock(side_effect=self.mock_get_module_versions)
def mock_get_module_versions(self, module_name):
versions = {
"foo_module": [("foo_module", "1.0.0"), ("foo_module", "1.1.0"), ("foo_module", "1.2.0")],
"bar_module": [("bar_module", "2.0.0"), ("bar_module", "2.1.0")],
"baz_module": [("baz_module", "0.9.0"), ("baz_module", "1.0.0")],
"qux_module": [("qux_module", "3.0.0")],
}
return versions.get(module_name, [])
def test_select_specific_version(self):
selections = ["[email protected]"]
result = select_modules(self.registry, selections)
expected = ["[email protected]"]
self.assertEqual(result, expected)
def test_select_latest_version(self):
selections = ["foo_module@latest"]
result = select_modules(self.registry, selections)
expected = ["[email protected]"]
self.assertEqual(result, expected)
def test_select_version_greater_than(self):
selections = ["foo_module@>1.0.0"]
result = select_modules(self.registry, selections)
expected = ["[email protected]", "[email protected]"]
self.assertEqual(result, expected)
def test_select_version_less_than_or_equal(self):
selections = ["bar_module@<=2.0.0"]
result = select_modules(self.registry, selections)
expected = ["[email protected]"]
self.assertEqual(result, expected)
def test_select_with_wildcard(self):
selections = ["ba*_module@latest"]
result = select_modules(self.registry, selections)
expected = ["[email protected]", "[email protected]"]
self.assertEqual(sorted(result), sorted(expected))
def test_select_random_percentage(self):
selections = ["foo_module@>0"]
result = select_modules(self.registry, selections, random_percentage=50)
# Ensure that the result is a subset of possible_versions
self.assertTrue(set(result).issubset(set(possible_versions)))
# Check that the number of selected modules is correct
expected_count = max(1, (50 * len(possible_versions)) // 100)
self.assertEqual(len(result), expected_count)
def test_invalid_selection_pattern_missing_at(self):
selections = ["foo_module"]
with self.assertRaises(ValueError):
select_modules(self.registry, selections)
def test_invalid_selection_pattern_empty_module(self):
selections = ["@1.0.0"]
with self.assertRaises(ValueError):
select_modules(self.registry, selections)
def test_invalid_version_not_found(self):
selections = ["[email protected]"]
with self.assertRaises(ValueError):
select_modules(self.registry, selections)
def test_no_matching_modules(self):
selections = ["nonexistent_module@latest"]
with self.assertRaises(ValueError):
select_modules(self.registry, selections)
def test_empty_selections(self):
selections = []
with self.assertRaises(ValueError):
select_modules(self.registry, selections)
if __name__ == "__main__":
unittest.main()