MCPcopy Index your code
hub / github.com/tensorflow/datasets / test_mock_fs

Function test_mock_fs

tensorflow_datasets/testing/test_utils_test.py:131–181  ·  view source on GitHub ↗
(as_path_fn)

Source from the content-addressed store, hash-verified

129 'as_path_fn', [pathlib.Path, str] # Test both PathLike and str
130)
131def test_mock_fs(as_path_fn):
132 _p = as_path_fn # pylint: disable=invalid-name
133 fs = test_utils.MockFs()
134 with fs.mock():
135 fs.add_file(_p('/path/to/file1'), 'Content of file 1')
136 fs.add_file(_p('/path/file.txt'), 'Content of file.txt')
137
138 # Test `tf.io.gfile.exists`
139 assert tf.io.gfile.exists(_p('/path/to/file1'))
140 assert tf.io.gfile.exists(_p('/path/to/'))
141 assert tf.io.gfile.exists(_p('/path/to'))
142 assert tf.io.gfile.exists(_p('/path'))
143 assert not tf.io.gfile.exists(_p('/pat'))
144 assert not tf.io.gfile.exists(_p('/path/to/file1_nonexisting'))
145 assert not tf.io.gfile.exists(_p('/path/to_file1_nonexisting'))
146
147 # Test `tf.io.gfile.exists` (relative path)
148 fs.add_file(_p('relative_path/to/file.txt'), 'Content')
149 assert tf.io.gfile.exists(_p('relative_path/to/file.txt'))
150 assert tf.io.gfile.exists(_p('relative_path/to/'))
151 assert tf.io.gfile.exists(_p('relative_path/to'))
152 assert tf.io.gfile.exists(_p('relative_path'))
153
154 # Test `tf.io.gfile.GFile` (write and read mode)
155 with tf.io.gfile.GFile(_p('/path/to/file2'), 'w') as f:
156 f.write('Content of file 2 (old)')
157 assert fs.read_file('/path/to/file2') == 'Content of file 2 (old)'
158 with tf.io.gfile.GFile(_p('/path/to/file2'), 'w') as f:
159 f.write('Content of file 2 (new)')
160 assert fs.read_file('/path/to/file2') == 'Content of file 2 (new)'
161 with tf.io.gfile.GFile(_p('/path/to/file2'), 'r') as f:
162 assert f.read() == 'Content of file 2 (new)'
163
164 # Test `tf.io.gfile.rename`
165 assert fs.read_file('/path/to/file1') == 'Content of file 1'
166 tf.io.gfile.rename(_p('/path/to/file1'), _p('/path/to/file1_moved'))
167 assert not tf.io.gfile.exists('/path/to/file1')
168 assert fs.read_file('/path/to/file1_moved') == 'Content of file 1'
169
170 # Test `tf.io.gfile.listdir`
171 assert set(tf.io.gfile.listdir(_p('/path/to'))) == set(
172 tf.io.gfile.listdir(_p('/path/to/'))
173 )
174 assert set(tf.io.gfile.listdir(_p('/path/to'))) == {'file1_moved', 'file2'}
175 assert set(tf.io.gfile.listdir(_p('/path'))) == {'file.txt', 'to'}
176
177 # Test `MockFs.files`
178 assert fs.read_file('/path/to/file2') == 'Content of file 2 (new)'
179 assert fs.read_file('/path/to/file1_moved') == 'Content of file 1'
180 assert fs.read_file('/path/file.txt') == 'Content of file.txt'
181 assert fs.read_file('relative_path/to/file.txt') == 'Content'
182
183
184def test_mock_fs_gcs():

Callers

nothing calls this directly

Calls 6

mockMethod · 0.95
add_fileMethod · 0.95
read_fileMethod · 0.95
existsMethod · 0.80
writeMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected