| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 # This will attempt to import the actual App Engine modules, and if it fails, | 5 # This will attempt to import the actual App Engine modules, and if it fails, |
| 6 # they will be replaced with fake modules. This is useful during testing. | 6 # they will be replaced with fake modules. This is useful during testing. |
| 7 try: | 7 try: |
| 8 import google.appengine.ext.blobstore as blobstore | 8 import google.appengine.ext.blobstore as blobstore |
| 9 from google.appengine.ext.blobstore.blobstore import BlobReferenceProperty | 9 from google.appengine.ext.blobstore.blobstore import BlobReferenceProperty |
| 10 import google.appengine.ext.db as db | 10 import google.appengine.ext.db as db |
| 11 import google.appengine.ext.webapp as webapp | 11 import google.appengine.ext.webapp as webapp |
| 12 import google.appengine.api.files as files | 12 import google.appengine.api.files as files |
| 13 import google.appengine.api.memcache as memcache | 13 import google.appengine.api.memcache as memcache |
| 14 import google.appengine.api.urlfetch as urlfetch | 14 import google.appengine.api.urlfetch as urlfetch |
| 15 except ImportError: | 15 except ImportError: |
| 16 import re | 16 import re |
| 17 from StringIO import StringIO |
| 17 | 18 |
| 18 FAKE_URL_FETCHER_CONFIGURATION = None | 19 FAKE_URL_FETCHER_CONFIGURATION = None |
| 19 | 20 |
| 20 def ConfigureFakeUrlFetch(configuration): | 21 def ConfigureFakeUrlFetch(configuration): |
| 21 """|configuration| is a dictionary mapping strings to fake urlfetch classes. | 22 """|configuration| is a dictionary mapping strings to fake urlfetch classes. |
| 22 A fake urlfetch class just needs to have a fetch method. The keys of the | 23 A fake urlfetch class just needs to have a fetch method. The keys of the |
| 23 dictionary are treated as regex, and they are matched with the URL to | 24 dictionary are treated as regex, and they are matched with the URL to |
| 24 determine which fake urlfetch is used. | 25 determine which fake urlfetch is used. |
| 25 """ | 26 """ |
| 26 global FAKE_URL_FETCHER_CONFIGURATION | 27 global FAKE_URL_FETCHER_CONFIGURATION |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 return _RPC() | 61 return _RPC() |
| 61 | 62 |
| 62 def make_fetch_call(self, rpc, url): | 63 def make_fetch_call(self, rpc, url): |
| 63 rpc.result = self.fetch(url) | 64 rpc.result = self.fetch(url) |
| 64 urlfetch = FakeUrlFetch() | 65 urlfetch = FakeUrlFetch() |
| 65 | 66 |
| 66 class NotImplemented(object): | 67 class NotImplemented(object): |
| 67 def __getattr__(self, attr): | 68 def __getattr__(self, attr): |
| 68 raise NotImplementedError() | 69 raise NotImplementedError() |
| 69 | 70 |
| 70 blobstore = NotImplemented() | 71 _BLOBS = {} |
| 71 files = NotImplemented() | 72 class FakeBlobstore(object): |
| 73 class BlobReader(object): |
| 74 def __init__(self, blob_key): |
| 75 self._data = _BLOBS[blob_key].getvalue() |
| 76 |
| 77 def read(self): |
| 78 return self._data |
| 79 |
| 80 blobstore = FakeBlobstore() |
| 81 |
| 82 class FakeFileInterface(object): |
| 83 """This class allows a StringIO object to be used in a with block like a |
| 84 file. |
| 85 """ |
| 86 def __init__(self, io): |
| 87 self._io = io |
| 88 |
| 89 def __exit__(self, *args): |
| 90 pass |
| 91 |
| 92 def write(self, data): |
| 93 self._io.write(data) |
| 94 |
| 95 def __enter__(self, *args): |
| 96 return self._io |
| 97 |
| 98 class FakeFiles(object): |
| 99 _next_blobstore_key = 0 |
| 100 class blobstore(object): |
| 101 @staticmethod |
| 102 def create(): |
| 103 FakeFiles._next_blobstore_key += 1 |
| 104 return FakeFiles._next_blobstore_key |
| 105 |
| 106 @staticmethod |
| 107 def get_blob_key(filename): |
| 108 return filename |
| 109 |
| 110 def open(self, filename, mode): |
| 111 _BLOBS[filename] = StringIO() |
| 112 return FakeFileInterface(_BLOBS[filename]) |
| 113 |
| 114 def GetBlobKeys(self): |
| 115 return _BLOBS.keys() |
| 116 |
| 117 def finalize(self, filename): |
| 118 pass |
| 119 |
| 120 files = FakeFiles() |
| 72 | 121 |
| 73 class InMemoryMemcache(object): | 122 class InMemoryMemcache(object): |
| 74 """A fake memcache that does nothing. | 123 """A fake memcache that does nothing. |
| 75 """ | 124 """ |
| 76 class Client(object): | 125 class Client(object): |
| 77 def set_multi_async(self, mapping, namespace='', time=0): | 126 def set_multi_async(self, mapping, namespace='', time=0): |
| 78 return | 127 return |
| 79 | 128 |
| 80 def get_multi_async(self, keys, namespace='', time=0): | 129 def get_multi_async(self, keys, namespace='', time=0): |
| 81 return _RPC(result=dict((k, None) for k in keys)) | 130 return _RPC(result=dict((k, None) for k in keys)) |
| 82 | 131 |
| 83 def set(self, key, value, namespace='', time=0): | 132 def set(self, key, value, namespace='', time=0): |
| 84 return | 133 return |
| 85 | 134 |
| 86 def get(self, key, namespace='', time=0): | 135 def get(self, key, namespace='', time=0): |
| 87 return None | 136 return None |
| 88 | 137 |
| 89 def delete(self, key, namespace): | 138 def delete(self, key, namespace): |
| 90 return | 139 return |
| 140 |
| 91 memcache = InMemoryMemcache() | 141 memcache = InMemoryMemcache() |
| 92 | 142 |
| 93 class webapp(object): | 143 class webapp(object): |
| 94 class RequestHandler(object): | 144 class RequestHandler(object): |
| 95 """A fake webapp.RequestHandler class for Handler to extend. | 145 """A fake webapp.RequestHandler class for Handler to extend. |
| 96 """ | 146 """ |
| 97 def __init__(self, request, response): | 147 def __init__(self, request, response): |
| 98 self.request = request | 148 self.request = request |
| 99 self.response = response | 149 self.response = response |
| 100 | 150 |
| 101 def redirect(self, path): | 151 def redirect(self, path): |
| 102 self.request.path = path | 152 self.request.path = path |
| 103 | 153 |
| 104 class _Db_Result(object): | 154 class _Db_Result(object): |
| 155 def __init__(self, data): |
| 156 self._data = data |
| 157 |
| 158 class _Result(object): |
| 159 def __init__(self, value): |
| 160 self.value = value |
| 161 |
| 105 def get(self): | 162 def get(self): |
| 106 return [] | 163 return self._Result(self._data) |
| 107 | 164 |
| 108 class db(object): | 165 class db(object): |
| 166 _store = {} |
| 109 class StringProperty(object): | 167 class StringProperty(object): |
| 110 pass | 168 pass |
| 111 | 169 |
| 112 class Model(object): | 170 class Model(object): |
| 171 def __init__(self, key_='', value=''): |
| 172 self._key = key_ |
| 173 self._value = value |
| 174 |
| 113 @staticmethod | 175 @staticmethod |
| 114 def gql(*args): | 176 def gql(query, key): |
| 115 return _Db_Result() | 177 return _Db_Result(db._store.get(key, None)) |
| 178 |
| 179 def put(self): |
| 180 db._store[self._key] = self._value |
| 116 | 181 |
| 117 class BlobReferenceProperty(object): | 182 class BlobReferenceProperty(object): |
| 118 pass | 183 pass |
| OLD | NEW |