Stay organized with collections
Save and categorize content based on your preferences.
Source code for google.appengine.api.files.testutil
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Files API.
.. deprecated:: 1.8.1
Use Google Cloud Storage Client library instead.
Testing utils for writing tests involving Files API."""
__all__ = ['TestFileServiceStub']
from google.appengine.api import apiproxy_stub
from google.appengine.api.files import file_service_pb
[docs]class TestFileServiceStub(apiproxy_stub.APIProxyStub):
"""A FileServiceStub to be used with tests.
Doesn't perform any kind of file validation and stores all
file content in memory.
Can be used to test low-level file calls only, because it doesn't
support all features (like blobstore files).
"""
def __init__(self):
super(TestFileServiceStub, self).__init__('file')
self._file_content = {}
def _Dynamic_Open(self, request, response):
pass
def _Dynamic_Close(self, request, response):
pass
def _Dynamic_Append(self, request, response):
self._file_content[request.filename()] = (
self.get_content(request.filename()) + request.data())
def _Dynamic_Read(self, request, response):
content = self._file_content[request.filename()]
pos = request.pos()
response.set_data(content[pos:pos + request.max_bytes()])
def _Dynamic_Stat(self, request, response):
file_stat = response.add_stat()
file_stat.set_length(len(self.get_content(request.filename())))
file_stat.set_filename(request.filename())
file_stat.set_content_type(file_service_pb.FileContentType.RAW)
file_stat.set_finalized(True)
response.set_more_files_found(False)
[docs] def get_content(self, filename):
"""Get current in-memory file content."""
return self._file_content.get(filename, '')
[docs] def set_content(self, filename, content):
"""Set current in-memory file content."""
self._file_content[filename] = content
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-16 UTC."],[[["This code provides a testing utility, `TestFileServiceStub`, for the Google App Engine Files API, designed for use in testing environments."],["`TestFileServiceStub` simulates file operations in memory without performing actual file validation, allowing for testing of low-level file calls."],["The Files API is deprecated, with the recommendation to use the Google Cloud Storage Client library instead."],["The `TestFileServiceStub` class has methods for dynamic file operations like `Open`, `Close`, `Append`, `Read`, and `Stat`, and additional methods `get_content` and `set_content` for managing file content."]]],[]]