fix KeyError in os.environ check
[arvados.git] / sdk / python / test_collections.py
1 # usage example:
2 #
3 # ARVADOS_API_TOKEN=abc ARVADOS_API_HOST=arvados.local python -m unittest discover
4
5 import unittest
6 import arvados
7 import os
8
9 class KeepLocalStoreTest(unittest.TestCase):
10     def setUp(self):
11         os.environ['KEEP_LOCAL_STORE'] = '/tmp'
12     def runTest(self):
13         self.assertEqual(arvados.Keep.put('foo'), 'acbd18db4cc2f85cedef654fccc4a4d8+3', 'wrong md5 hash from Keep.put')
14         self.assertEqual(arvados.Keep.get('acbd18db4cc2f85cedef654fccc4a4d8+3'), 'foo', 'wrong data from Keep.get')
15
16 class LocalCollectionWriterTest(unittest.TestCase):
17     def setUp(self):
18         os.environ['KEEP_LOCAL_STORE'] = '/tmp'
19     def runTest(self):
20         cw = arvados.CollectionWriter()
21         self.assertEqual(cw.current_stream_name(), '.',
22                          'current_stream_name() should be "." now')
23         cw.set_current_file_name('foo.txt')
24         cw.write('foo')
25         self.assertEqual(cw.current_file_name(), 'foo.txt',
26                          'current_file_name() should be foo.txt now')
27         cw.start_new_file('bar.txt')
28         cw.write('bar')
29         cw.start_new_stream('baz')
30         cw.write('baz')
31         cw.set_current_file_name('baz.txt')
32         hash = cw.finish()
33         self.assertEqual(hash,
34                          'a4d26dddc10ad8b5eb39347c916de16c+112',
35                          'resulting manifest hash is not what I expected')
36
37 class LocalCollectionReaderTest(unittest.TestCase):
38     def setUp(self):
39         os.environ['KEEP_LOCAL_STORE'] = '/tmp'
40         LocalCollectionWriterTest().runTest()
41     def runTest(self):
42         cr = arvados.CollectionReader('a4d26dddc10ad8b5eb39347c916de16c+112')
43         got = []
44         for s in cr.all_streams():
45             for f in s.all_files():
46                 got += [[f.size(), f.stream_name(), f.name(), f.read(2**26)]]
47         expected = [[3, '.', 'foo.txt', 'foo'],
48                     [3, '.', 'bar.txt', 'bar'],
49                     [3, 'baz', 'baz.txt', 'baz']]
50         self.assertEqual(got,
51                          expected,
52                          'resulting file list is not what I expected')
53         stream0 = cr.all_streams()[0]
54         self.assertEqual(stream0.read(0),
55                          '',
56                          'reading zero bytes should have returned empty string')
57         self.assertEqual(stream0.read(2**26),
58                          'foobar',
59                          'reading entire stream failed')
60         self.assertEqual(stream0.read(2**26),
61                          None,
62                          'reading past end of stream should have returned None')
63         self.assertEqual(stream0.read(0),
64                          '',
65                          'reading zero bytes should have returned empty string')
66
67 class LocalCollectionManifestSubsetTest(unittest.TestCase):
68     def setUp(self):
69         os.environ['KEEP_LOCAL_STORE'] = '/tmp'
70         LocalCollectionWriterTest().runTest()
71     def runTest(self):
72         cr = arvados.CollectionReader('a4d26dddc10ad8b5eb39347c916de16c+112')
73         manifest_subsets = []
74         for s in cr.all_streams():
75             for f in s.all_files():
76                 manifest_subsets += [f.as_manifest()]
77         got = []
78         for m in manifest_subsets:
79             cr = arvados.CollectionReader(m)
80             for f in cr.all_files():
81                 got += [[f.size(), f.stream_name(), f.name(), f.read(2**26)]]
82         expected = [[3, '.', 'foo.txt', 'foo'],
83                     [3, '.', 'bar.txt', 'bar'],
84                     [3, 'baz', 'baz.txt', 'baz']]
85         self.assertEqual(got,
86                          expected,
87                          'all_files|as_manifest did not preserve manifest contents')
88
89 class LocalCollectionReadlineTest(unittest.TestCase):
90     def setUp(self):
91         os.environ['KEEP_LOCAL_STORE'] = '/tmp'
92     def _runTest(self, what_in, what_out):
93         cw = arvados.CollectionWriter()
94         cw.start_new_file('test.txt')
95         cw.write(what_in)
96         test1 = cw.finish()
97         cr = arvados.CollectionReader(test1)
98         got = []
99         for x in list(cr.all_files())[0].readlines():
100             got += [x]
101         self.assertEqual(got,
102                          what_out,
103                          "readlines did not split lines correctly: %s" % got)
104     def runTest(self):
105         self._runTest("\na\nbcd\n\nefg\nz",
106                       ["\n", "a\n", "bcd\n", "\n", "efg\n", "z"])
107         self._runTest("ab\ncd\n",
108                       ["ab\n", "cd\n"])