def test_fileobj(self): data = "foo bar boo" obj = StringIO(data) with utils.read(obj) as fh: self.assertEqual(obj, fh) self.assertEqual(data, fh.read())
def test_path(self): name = "/path/to/file" path = MockPath(name) with utils.read(path) as fh: pass # close() is on the fh returned by open() calls = [mock.call(), mock.call().close()] self.assertEqual(path.open.mock_calls, calls)
def test_path_close(self): name = "/path/to/file" path = MockPath(name) with self.assertRaises(Exception): with utils.read(path) as fh: raise Exception() # close() is on the fh returned by open() calls = [mock.call(), mock.call().close()] self.assertEqual(path.open.mock_calls, calls)
def test_unsuported(self): with self.assertRaises(Exception): with utils.read(None) as fh: pass
def test_string(self): data = "foo bar boo" with utils.read(data) as fh: self.assertEqual(data, fh.read()) self.assertEqual('<string>', fh.name)
def test_unsuported(self): with self.assertRaises(ValueError): with utils.read(None) as fh: pass