Пример #1
0
def test_file_update():
    account = Account.create_from_data(json.loads(helpers.account))
    file_data = json.loads(helpers.file_data)
    file_obj = File.create_from_data(file_data, parent_resource=account)
    with patch('sdk.resources.request') as mock_req:
        resp = Response()
        new_data = file_data.copy()
        new_data['name'] = 'NewFileName'
        resp._content = json.dumps(new_data)
        account_resp = Response()
        account_resp._content = helpers.account
        mock_req.side_effect = (resp,account_resp)
        file_obj.name = 'NewFileName'
        file_obj.parent_id = 'root'
        file_obj.save()
        expected_calls = [
                          # This is updating the file
                          call(file_obj._api_session.patch,
                               'accounts/%s/files/%s' % (account.id,
                                                         file_data['id']),
                               params={},
                               data={'name': u'NewFileName',
                                     'parent_id': 'root'},
                               configuration=file_obj._configuration),
                          # This is refreshing the parent resource
                          call(account._api_session.get,
                               'accounts/%s' % account.id,
                               configuration=account._configuration),
                         ]
        mock_req.assert_has_calls(expected_calls)
Пример #2
0
def test_file_delete():
    account = Account.create_from_data(json.loads(helpers.account))
    file_data = json.loads(helpers.file_data)
    file_obj = File.create_from_data(file_data, parent_resource=account)
    with patch('sdk.resources.request') as mock_req:
        resp = Response()
        resp.status_code = 204
        mock_req.return_value = resp
        file_obj.delete()
        mock_req.assert_called_with(file_obj._api_session.delete,
                                    ('accounts/%s/files/%s'
                                        % (account.id, file_data['id'])),
                                    configuration=file_obj._configuration,
                                    params={})
Пример #3
0
def test_file_contents():
    account = Account.create_from_data(json.loads(helpers.account))
    file_data = json.loads(helpers.file_data)
    file_obj = File.create_from_data(file_data, parent_resource=account)
    with patch('sdk.resources.request') as mock_req:
        resp = Response()
        resp._content = helpers.file_contents
        mock_req.return_value = resp
        file_contents = file_obj.contents()
        assert isinstance(file_contents, Response)
        mock_req.assert_called_with(file_obj._api_session.get,
                                    ('accounts/%s/files/%s/contents'
                                        % (account.id, file_data['id'])),
                                    configuration=file_obj._configuration,
                                    stream=True)
Пример #4
0
def test_file_metadata():
    account = Account.create_from_data(json.loads(helpers.account))
    file_data = json.loads(helpers.file_data)
    with patch('sdk.resources.request') as mock_req:
        resp = Response()
        resp._content = helpers.file_data
        mock_req.return_value = resp
        file_obj = File.retrieve(id=file_data['id'],
                                 parent_resource=account)
        assert isinstance(file_obj, File)
        for attr in ['id', 'name', 'type', 'size', 'account']:
            assert file_data[attr] == getattr(file_obj, attr)
        mock_req.assert_called_with(File._api_session.get,
                                    ('accounts/%s/files/%s'
                                        % (account.id, file_data['id'])),
                                    configuration=None,
                                    params={})
Пример #5
0
def test_file_upload():
    account = Account.create_from_data(json.loads(helpers.account))
    file_data = json.loads(helpers.file_data)
    with patch('sdk.resources.request') as mock_req:
        resp = Response()
        resp._content = helpers.file_data
        mock_req.return_value = resp
        file_obj = File.create(parent_resource=account,
                               file_name=file_data['name'],
                               parent_id='root',
                               file_data=helpers.file_contents)
        assert isinstance(file_obj, File)
        for attr in ['id', 'name', 'type', 'size', 'account']:
            assert file_data[attr] == getattr(file_obj, attr)
        mock_req.assert_called_with(File._api_session.post,
                                    'accounts/%s/files' % account.id,
                                    data={'metadata':json.dumps({
                                                'name': file_data['name'],
                                                'parent_id': 'root'})},
                                    files={'file': helpers.file_contents},
                                    params={},
                                    configuration=None)