forked from nteract/papermill
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import unittest | ||
from unittest.mock import MagicMock, patch | ||
|
||
from ..iorw import HDFSHandler | ||
|
||
|
||
class MockHadoopFileSystem(MagicMock): | ||
def ls(self, path): | ||
return ['test1.ipynb', 'test2.ipynb'] | ||
|
||
def open(self, path, *args): | ||
return MockHadoopFile() | ||
|
||
|
||
class MockHadoopFile(object): | ||
def __init__(self): | ||
self._content = b'Content of notebook' | ||
|
||
def __enter__(self, *args): | ||
return self | ||
|
||
def __exit__(self, *args): | ||
pass | ||
|
||
def read(self): | ||
return self._content | ||
|
||
def write(self, new_content): | ||
self._content = new_content | ||
return 1 | ||
|
||
|
||
@patch('papermill.iorw.HadoopFileSystem', side_effect=MockHadoopFileSystem()) | ||
class HDFSTest(unittest.TestCase): | ||
def setUp(self): | ||
self.hdfs_handler = HDFSHandler() | ||
|
||
def test_hdfs_listdir(self, mock_hdfs_filesystem): | ||
client = self.hdfs_handler._get_client() | ||
self.assertEqual(self.hdfs_handler.listdir("hdfs:///Projects/"), ['test1.ipynb', 'test2.ipynb']) | ||
# Check if client is the same after calling | ||
self.assertIs(client, self.hdfs_handler._get_client()) | ||
|
||
def test_hdfs_read(self, mock_hdfs_filesystem): | ||
client = self.hdfs_handler._get_client() | ||
self.assertEqual(self.hdfs_handler.read("hdfs:///Projects/test1.ipynb"), b'Content of notebook') | ||
self.assertIs(client, self.hdfs_handler._get_client()) | ||
|
||
def test_hdfs_write(self, mock_hdfs_filesystem): | ||
client = self.hdfs_handler._get_client() | ||
self.assertEqual(self.hdfs_handler.write("hdfs:///Projects/test1.ipynb", b'New content'), 1) | ||
self.assertIs(client, self.hdfs_handler._get_client()) |