1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
12 class MkdirDashPTest(unittest.TestCase):
15 os.path.mkdir('./tmp')
20 os.unlink('./tmp/bar')
26 arvados.util.mkdir_dash_p('./tmp/foo')
27 with open('./tmp/bar', 'wb') as f:
29 self.assertRaises(OSError, arvados.util.mkdir_dash_p, './tmp/bar')
32 class RunCommandTestCase(unittest.TestCase):
33 def test_success(self):
34 stdout, stderr = arvados.util.run_command(['echo', 'test'],
35 stderr=subprocess.PIPE)
36 self.assertEqual("test\n".encode(), stdout)
37 self.assertEqual("".encode(), stderr)
39 def test_failure(self):
40 with self.assertRaises(arvados.errors.CommandFailedError):
41 arvados.util.run_command(['false'])
43 class KeysetTestHelper:
44 def __init__(self, expect):
48 def fn(self, **kwargs):
49 if self.expect[self.n][0] != kwargs:
50 raise Exception("Didn't match %s != %s" % (self.expect[self.n][0], kwargs))
53 def execute(self, num_retries):
55 return self.expect[self.n-1][1]
57 class KeysetListAllTestCase(unittest.TestCase):
59 ks = KeysetTestHelper([[
60 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": []},
64 ls = list(arvados.util.keyset_list_all(ks.fn))
65 self.assertEqual(ls, [])
67 def test_oneitem(self):
68 ks = KeysetTestHelper([[
69 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": []},
70 {"items": [{"created_at": "1", "uuid": "1"}]}
72 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", "=", "1"], ["uuid", ">", "1"]]},
75 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">", "1"]]},
79 ls = list(arvados.util.keyset_list_all(ks.fn))
80 self.assertEqual(ls, [{"created_at": "1", "uuid": "1"}])
82 def test_onepage2(self):
83 ks = KeysetTestHelper([[
84 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": []},
85 {"items": [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}]}
87 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "2"], ["uuid", "!=", "2"]]},
91 ls = list(arvados.util.keyset_list_all(ks.fn))
92 self.assertEqual(ls, [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}])
94 def test_onepage3(self):
95 ks = KeysetTestHelper([[
96 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": []},
97 {"items": [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}, {"created_at": "3", "uuid": "3"}]}
99 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "3"], ["uuid", "!=", "3"]]},
103 ls = list(arvados.util.keyset_list_all(ks.fn))
104 self.assertEqual(ls, [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}, {"created_at": "3", "uuid": "3"}])
107 def test_twopage(self):
108 ks = KeysetTestHelper([[
109 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": []},
110 {"items": [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}]}
112 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "2"], ["uuid", "!=", "2"]]},
113 {"items": [{"created_at": "3", "uuid": "3"}, {"created_at": "4", "uuid": "4"}]}
115 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "4"], ["uuid", "!=", "4"]]},
119 ls = list(arvados.util.keyset_list_all(ks.fn))
120 self.assertEqual(ls, [{"created_at": "1", "uuid": "1"},
121 {"created_at": "2", "uuid": "2"},
122 {"created_at": "3", "uuid": "3"},
123 {"created_at": "4", "uuid": "4"}
126 def test_repeated_key(self):
127 ks = KeysetTestHelper([[
128 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": []},
129 {"items": [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}, {"created_at": "2", "uuid": "3"}]}
131 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "2"], ["uuid", "!=", "3"]]},
132 {"items": [{"created_at": "2", "uuid": "2"}, {"created_at": "2", "uuid": "4"}]}
134 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", "=", "2"], ["uuid", ">", "4"]]},
137 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">", "2"]]},
138 {"items": [{"created_at": "3", "uuid": "5"}, {"created_at": "4", "uuid": "6"}]}
140 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "4"], ["uuid", "!=", "6"]]},
145 ls = list(arvados.util.keyset_list_all(ks.fn))
146 self.assertEqual(ls, [{"created_at": "1", "uuid": "1"},
147 {"created_at": "2", "uuid": "2"},
148 {"created_at": "2", "uuid": "3"},
149 {"created_at": "2", "uuid": "4"},
150 {"created_at": "3", "uuid": "5"},
151 {"created_at": "4", "uuid": "6"}
154 def test_onepage_withfilter(self):
155 ks = KeysetTestHelper([[
156 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["foo", ">", "bar"]]},
157 {"items": [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}]}
159 {"limit": 1000, "count": "none", "order": ["created_at asc", "uuid asc"], "filters": [["created_at", ">=", "2"], ["uuid", "!=", "2"], ["foo", ">", "bar"]]},
163 ls = list(arvados.util.keyset_list_all(ks.fn, filters=[["foo", ">", "bar"]]))
164 self.assertEqual(ls, [{"created_at": "1", "uuid": "1"}, {"created_at": "2", "uuid": "2"}])
167 def test_onepage_desc(self):
168 ks = KeysetTestHelper([[
169 {"limit": 1000, "count": "none", "order": ["created_at desc", "uuid desc"], "filters": []},
170 {"items": [{"created_at": "2", "uuid": "2"}, {"created_at": "1", "uuid": "1"}]}
172 {"limit": 1000, "count": "none", "order": ["created_at desc", "uuid desc"], "filters": [["created_at", "<=", "1"], ["uuid", "!=", "1"]]},
176 ls = list(arvados.util.keyset_list_all(ks.fn, ascending=False))
177 self.assertEqual(ls, [{"created_at": "2", "uuid": "2"}, {"created_at": "1", "uuid": "1"}])