コード例 #1
0
    def test_get_file_model(self, m_getFile: mock.MagicMock) -> None:
        # Check static expected values in model. This happens to be actual
        # return values, however we are mocking out any actual network
        # communication.
        expected_m = {
            '_id': self.EXAMPLE_ITEM_ID,
            '_modelType': 'file',
            'exts': ['png'],
            'mimeType': 'image/png',
            'name': 'Lenna.png',
            'sha512': self.EXAMPLE_SHA512,
            'size': 473831
        }

        m_getFile.return_value = expected_m

        e = GirderDataElement(self.EXAMPLE_ITEM_ID,
                              self.EXAMPLE_GIRDER_API_ROOT)
        m = e.get_file_model()
        assert m is not None
        self.assertEqual(m['_id'], expected_m['_id'])
        self.assertEqual(m['_modelType'], expected_m['_modelType'])
        self.assertEqual(m['exts'], expected_m['exts'])
        self.assertEqual(m['mimeType'], expected_m['mimeType'])
        self.assertEqual(m['name'], expected_m['name'])
        self.assertEqual(m['sha512'], expected_m['sha512'])
        self.assertEqual(m['size'], expected_m['size'])
コード例 #2
0
    def test_get_bytes(self, _m_downloadFile: mock.MagicMock) -> None:
        """ Test that getting bytes is driven by the girder_client downloadFile
        method.
        """
        _m_downloadFile.side_effect = lambda fid, buf: buf.write(b'foo')

        e = GirderDataElement('someId')
        actual_bytes = e.get_bytes()
        assert actual_bytes == b'foo'
コード例 #3
0
    def test_get_file_model_item_no_exists(self,
                                           m_getFile: mock.MagicMock) -> None:
        def raise_http_error(*_: Tuple, **__: Tuple) -> None:
            raise girder_client.HttpError(None, None, None, None)

        m_getFile.side_effect = raise_http_error

        e = GirderDataElement('foo', self.EXAMPLE_GIRDER_API_ROOT)
        m = e.get_file_model()
        self.assertIsNone(m)
コード例 #4
0
    def test_set_bytes_http_errors(self) -> None:
        gde = GirderDataElement('someId')
        gde.writable = mock.MagicMock(return_value=True)  # type: ignore

        # Test access denied throws ReadOnlyError
        gde.gc.uploadFileContents = mock.MagicMock(
            side_effect=girder_client.HttpError(401, '', None, None))
        self.assertRaises(ReadOnlyError, gde.set_bytes, b=b'foo')

        # Test any other error (like a 500) re-raises the HttpError
        gde.gc.uploadFileContents = mock.MagicMock(
            side_effect=girder_client.HttpError(500, '', None, None))
        self.assertRaises(girder_client.HttpError, gde.set_bytes, b=b'foo')
コード例 #5
0
    def test_content_type_no_cache(self, m_getFile: mock.MagicMock) -> None:
        # Mocking such that we simulate a valid API root and an existing
        # item reference

        # Dummy requests return value
        expected_mimetype = 'some/type'
        m_getFile.return_value = {'mimeType': expected_mimetype}

        e = GirderDataElement('foo')
        actual_type = e.content_type()
        self.assertEqual(actual_type, expected_mimetype)
        m_getFile.assert_called_once()

        # Ensure that calling content_type a second time doesn't call
        # getFile again.
        self.assertEqual(e.content_type(), expected_mimetype)
        m_getFile.assert_called_once()
コード例 #6
0
 def test_new_fileId(self) -> None:
     expected_id = "some id"
     e = GirderDataElement(expected_id)
     self.assertEqual(e.file_id, expected_id)
     self.assertEqual(e.api_root, self.LOCAL_APIROOT)
     self.assertIsNone(e.api_key)
     self.assertIsNone(e.token)
     self.assertIsNone(e._content_type)
     self.assertIsInstance(e.gc, girder_client.GirderClient)
コード例 #7
0
 def test_from_config_full_constructor(
         self, _mock_authenticate: mock.MagicMock) -> None:
     expected_file_id = '34uhki34gh2345ghjk'
     expected_api_root = 'https://some.other.server/api/v1'
     expected_api_key = '1234ghk135hlg23435'
     inst = GirderDataElement(expected_file_id,
                              api_root=expected_api_root,
                              api_key=expected_api_key)
     for i in configuration_test_helper(inst):  # type: GirderDataElement
         assert i.file_id == expected_file_id
         assert i.api_root == expected_api_root
         assert i.api_key == expected_api_key
コード例 #8
0
    def test_repr(self, _mock_requests: mock.MagicMock) -> None:
        expected_file_id = 'some_file id'
        expected_api_root = 'https://some.server/api/v1'
        expected_api_key = 'someKeyHere'

        e = GirderDataElement(expected_file_id, expected_api_root,
                              expected_api_key)
        actual_repr = repr(e)

        expected_repr = "GirderDataElement{file_id: %s, " \
                        "api_root: %s, " \
                        "api_key: %s, token: }" \
                        % (expected_file_id, expected_api_root,
                           expected_api_key)
        self.assertEqual(actual_repr, expected_repr)
コード例 #9
0
    def test_writable(self, m_getItem: mock.MagicMock,
                      m_getFolder: mock.MagicMock,
                      _m_get_file_model: mock.MagicMock) -> None:
        m_getItem.return_value = {'folderId': 'someFolderId'}
        m_getFolder.return_value = {'_accessLevel': 1}

        self.assertTrue(GirderDataElement('someId').writable())

        # Access level 0 should cause it to be unwritable
        m_getFolder.return_value = {'_accessLevel': 0}
        self.assertFalse(GirderDataElement('someId').writable())

        # A nonexistent file model should make writable return false
        gde = GirderDataElement('someId')
        gde.get_file_model = mock.MagicMock(return_value=None)  # type: ignore
        self.assertFalse(gde.writable())
コード例 #10
0
 def test_from_uri_full_url(self) -> None:
     e = GirderDataElement.from_uri(self.EXAMPLE_GIRDER_FULL_URI)
     self.assertEqual(e.file_id, self.EXAMPLE_ITEM_ID)
コード例 #11
0
 def test_set_bytes_non_writable(self) -> None:
     gde = GirderDataElement('someId')
     gde.writable = mock.MagicMock(return_value=False)  # type: ignore
     self.assertRaises(ReadOnlyError, gde.set_bytes, b=None)
コード例 #12
0
 def test_set_bytes(self, m_uploadFileContents: mock.MagicMock) -> None:
     gde = GirderDataElement('someId')
     gde.writable = mock.MagicMock(return_value=True)  # type: ignore
     gde.set_bytes(b'foo')
     m_uploadFileContents.assert_called_once()
コード例 #13
0
 def test_is_empty_nonzero_bytes(self) -> None:
     e = GirderDataElement('someId')
     e.get_file_model = mock.MagicMock(return_value={'size':
                                                     7})  # type: ignore
     self.assertFalse(e.is_empty())
コード例 #14
0
 def test_is_empty_zero_size(self) -> None:
     # Uses model return size parameter
     e = GirderDataElement('someId')
     e.get_file_model = mock.MagicMock(return_value={'size':
                                                     0})  # type: ignore
     self.assertTrue(e.is_empty())
コード例 #15
0
 def test_is_empty_none_model(self) -> None:
     # Uses model return, empty if no model return (no item in girder by
     # ID)
     e = GirderDataElement('someId')
     e.get_file_model = mock.MagicMock(return_value=None)  # type: ignore
     self.assertTrue(e.is_empty())
コード例 #16
0
    _ = r.content
except requests.ConnectionError:
    internet_available = False


def gen_response(content: mock.MagicMock,
                 status_code: int = 200) -> requests.Response:
    resp = requests.Response()
    resp._content = content
    resp.status_code = status_code
    resp.headers['content-length'] = str(len(content))
    return resp


# Only perform these tests if the implementation is usable.
@pytest.mark.skipif(not GirderDataElement.is_usable(),
                    reason="GirderDataElement reports as not usable.")
class TestGirderDataElement(unittest.TestCase):
    """
    Tests for the GirderDataElement plugin implementation

    """

    LOCAL_APIROOT = "http://localhost:8080/api/v1"
    EXAMPLE_ITEM_ID = '5820bbeb8d777f10f26efc2f'
    EXAMPLE_GIRDER_API_ROOT = "%s/api/v1" % DATA_KITWARE_URL
    EXAMPLE_GIRDER_FULL_URI = (
        EXAMPLE_GIRDER_API_ROOT.replace('https', 'girder') +
        '/file/%s' % EXAMPLE_ITEM_ID)
    EXAMPLE_PTH = os.path.join(TEST_DATA_DIR, 'Lenna.png')
    EXAMPLE_SHA512 = 'ca2be093f4f25d2168a0afebe013237efce02197bcd8a87f41f' \