1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
11 from unittest import mock
16 class MkdirDashPTest(unittest.TestCase):
19 os.path.mkdir('./tmp')
24 os.unlink('./tmp/bar')
30 arvados.util.mkdir_dash_p('./tmp/foo')
31 with open('./tmp/bar', 'wb') as f:
33 self.assertRaises(OSError, arvados.util.mkdir_dash_p, './tmp/bar')
36 class RunCommandTestCase(unittest.TestCase):
37 def test_success(self):
38 stdout, stderr = arvados.util.run_command(['echo', 'test'],
39 stderr=subprocess.PIPE)
40 self.assertEqual("test\n".encode(), stdout)
41 self.assertEqual("".encode(), stderr)
43 def test_failure(self):
44 with self.assertRaises(arvados.errors.CommandFailedError):
45 arvados.util.run_command(['false'])
47 class KeysetTestHelper:
48 def __init__(self, expect):
52 def fn(self, **kwargs):
53 if self.expect[self.n][0] != kwargs:
54 raise Exception("Didn't match %s != %s" % (self.expect[self.n][0], kwargs))
57 def execute(self, num_retries):
59 return self.expect[self.n-1][1]
62 'uuid': 'zzzzz-zyyyz-zzzzzyyyyywwwww',
63 'name': 'KeysetListAllTestCase.test_select mock',
64 'created_at': '2023-08-28T12:34:56.123456Z',
67 class KeysetListAllTestCase(unittest.TestCase):
69 ks = KeysetTestHelper([[
70 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": []},
74 ls = list(arvados.util.keyset_list_all(ks.fn))
75 self.assertEqual(ls, [])
77 def test_oneitem(self):
78 ks = KeysetTestHelper([[
79 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": []},
80 {"items": [{"created_at": "1", "uuid": "1"}]}
82 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", "=", "1"], ["uuid", ">", "1"]]},
85 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">", "1"]]},
89 ls = list(arvados.util.keyset_list_all(ks.fn))
90 self.assertEqual(ls, [{"created_at": "1", "uuid": "1"}])
92 def test_onepage2(self):
93 ks = KeysetTestHelper([[
94 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": []},
95 {"items": [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}]}
97 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "2"], ["uuid", "!=", "2"]]},
101 ls = list(arvados.util.keyset_list_all(ks.fn))
102 self.assertEqual(ls, [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}])
104 def test_onepage3(self):
105 ks = KeysetTestHelper([[
106 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": []},
107 {"items": [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}, {"created_at": "3", "uuid": "3"}]}
109 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "3"], ["uuid", "!=", "3"]]},
113 ls = list(arvados.util.keyset_list_all(ks.fn))
114 self.assertEqual(ls, [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}, {"created_at": "3", "uuid": "3"}])
117 def test_twopage(self):
118 ks = KeysetTestHelper([[
119 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": []},
120 {"items": [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}]}
122 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "2"], ["uuid", "!=", "2"]]},
123 {"items": [{"created_at": "3", "uuid": "3"}, {"created_at": "4", "uuid": "4"}]}
125 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "4"], ["uuid", "!=", "4"]]},
129 ls = list(arvados.util.keyset_list_all(ks.fn))
130 self.assertEqual(ls, [{"created_at": "1", "uuid": "1"},
131 {"created_at": "2", "uuid": "2"},
132 {"created_at": "3", "uuid": "3"},
133 {"created_at": "4", "uuid": "4"}
136 def test_repeated_key(self):
137 ks = KeysetTestHelper([[
138 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": []},
139 {"items": [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}, {"created_at": "2", "uuid": "3"}]}
141 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "2"], ["uuid", "!=", "3"]]},
142 {"items": [{"created_at": "2", "uuid": "2"}, {"created_at": "2", "uuid": "4"}]}
144 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", "=", "2"], ["uuid", ">", "4"]]},
147 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">", "2"]]},
148 {"items": [{"created_at": "3", "uuid": "5"}, {"created_at": "4", "uuid": "6"}]}
150 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "4"], ["uuid", "!=", "6"]]},
155 ls = list(arvados.util.keyset_list_all(ks.fn))
156 self.assertEqual(ls, [{"created_at": "1", "uuid": "1"},
157 {"created_at": "2", "uuid": "2"},
158 {"created_at": "2", "uuid": "3"},
159 {"created_at": "2", "uuid": "4"},
160 {"created_at": "3", "uuid": "5"},
161 {"created_at": "4", "uuid": "6"}
164 def test_onepage_withfilter(self):
165 ks = KeysetTestHelper([[
166 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["foo", ">", "bar"]]},
167 {"items": [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}]}
169 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "2"], ["uuid", "!=", "2"], ["foo", ">", "bar"]]},
173 ls = list(arvados.util.keyset_list_all(ks.fn, filters=[["foo", ">", "bar"]]))
174 self.assertEqual(ls, [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}])
176 def test_onepage_desc(self):
177 ks = KeysetTestHelper([[
178 {"limit": 1000, "count": "none", "order": ["created_at desc", "uuid desc"], "filters": []},
179 {"items": [{"created_at": "2", "uuid": "2"}, {"created_at": "1", "uuid": "1"}]}
181 {"limit": 1000, "count": "none", "order": ["created_at desc", "uuid desc"], "filters": [["created_at", "<=", "1"], ["uuid", "!=", "1"]]},
185 ls = list(arvados.util.keyset_list_all(ks.fn, ascending=False))
186 self.assertEqual(ls, [{"created_at": "2", "uuid": "2"}, {"created_at": "1", "uuid": "1"}])
188 @parameterized.parameterized.expand(zip(
189 itertools.cycle(_SELECT_FAKE_ITEM),
190 itertools.chain.from_iterable(
191 itertools.combinations(_SELECT_FAKE_ITEM, count)
192 for count in range(len(_SELECT_FAKE_ITEM) + 1)
195 def test_select(self, order_key, select):
196 # keyset_list_all must have both uuid and order_key to function.
197 # Test that it selects those fields along with user-specified ones.
198 expect_select = {'uuid', order_key, *select}
201 for key, value in _SELECT_FAKE_ITEM.items()
202 if key in expect_select
204 list_func = mock.Mock()
205 list_func().execute = mock.Mock(
212 list_func.reset_mock()
213 actual = list(arvados.util.keyset_list_all(list_func, order_key, select=list(select)))
214 self.assertEqual(actual, [item])
215 calls = list_func.call_args_list
216 self.assertTrue(len(calls) >= 2, "list_func() not called enough to exhaust items")
217 for args, kwargs in calls:
218 self.assertEqual(set(kwargs.get('select', ())), expect_select)