| 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 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 def _GetConfiguration(key): | 30 def _GetConfiguration(key): |
| 31 if not FAKE_URL_FETCHER_CONFIGURATION: | 31 if not FAKE_URL_FETCHER_CONFIGURATION: |
| 32 raise ValueError('No fake fetch paths have been configured. ' | 32 raise ValueError('No fake fetch paths have been configured. ' |
| 33 'See ConfigureFakeUrlFetch in appengine_wrappers.py.') | 33 'See ConfigureFakeUrlFetch in appengine_wrappers.py.') |
| 34 for k, v in FAKE_URL_FETCHER_CONFIGURATION.iteritems(): | 34 for k, v in FAKE_URL_FETCHER_CONFIGURATION.iteritems(): |
| 35 if k.match(key): | 35 if k.match(key): |
| 36 return v | 36 return v |
| 37 return None | 37 return None |
| 38 | 38 |
| 39 class _RPC(object): |
| 40 def __init__(self, result=None): |
| 41 self.result = result |
| 42 |
| 43 def get_result(self): |
| 44 return self.result |
| 45 |
| 39 class FakeUrlFetch(object): | 46 class FakeUrlFetch(object): |
| 40 """A fake urlfetch module that uses the current | 47 """A fake urlfetch module that uses the current |
| 41 |FAKE_URL_FETCHER_CONFIGURATION| to map urls to fake fetchers. | 48 |FAKE_URL_FETCHER_CONFIGURATION| to map urls to fake fetchers. |
| 42 """ | 49 """ |
| 43 class _Response(object): | 50 class _Response(object): |
| 44 def __init__(self, content): | 51 def __init__(self, content): |
| 45 self.content = content | 52 self.content = content |
| 46 self.headers = { 'content-type': 'none' } | 53 self.headers = { 'content-type': 'none' } |
| 47 self.status_code = 200 | 54 self.status_code = 200 |
| 48 | 55 |
| 49 class _RPC(object): | |
| 50 def __init__(self): | |
| 51 self.result = None | |
| 52 | |
| 53 def wait(self): | |
| 54 pass | |
| 55 | |
| 56 def get_result(self): | |
| 57 return self.result | |
| 58 | |
| 59 def fetch(self, url): | 56 def fetch(self, url): |
| 60 return self._Response(_GetConfiguration(url).fetch(url)) | 57 return self._Response(_GetConfiguration(url).fetch(url)) |
| 61 | 58 |
| 62 def create_rpc(self): | 59 def create_rpc(self): |
| 63 return self._RPC() | 60 return _RPC() |
| 64 | 61 |
| 65 def make_fetch_call(self, rpc, url): | 62 def make_fetch_call(self, rpc, url): |
| 66 rpc.result = self.fetch(url) | 63 rpc.result = self.fetch(url) |
| 67 urlfetch = FakeUrlFetch() | 64 urlfetch = FakeUrlFetch() |
| 68 | 65 |
| 69 class NotImplemented(object): | 66 class NotImplemented(object): |
| 70 def __getattr__(self, attr): | 67 def __getattr__(self, attr): |
| 71 raise NotImplementedError() | 68 raise NotImplementedError() |
| 72 | 69 |
| 73 blobstore = NotImplemented() | 70 blobstore = NotImplemented() |
| 74 files = NotImplemented() | 71 files = NotImplemented() |
| 75 | 72 |
| 76 class InMemoryMemcache(object): | 73 class InMemoryMemcache(object): |
| 77 """A memcache that stores items in memory instead of using the memcache | 74 """A fake memcache that does nothing. |
| 78 module. | |
| 79 """ | 75 """ |
| 80 def __init__(self): | 76 class Client(object): |
| 81 self._cache = {} | 77 def set_multi_async(self, mapping, namespace='', time=0): |
| 78 return |
| 82 | 79 |
| 83 def set(self, key, value, namespace, time=60): | 80 def get_multi_async(self, keys, namespace='', time=0): |
| 84 if namespace not in self._cache: | 81 return _RPC(result=dict((k, None) for k in keys)) |
| 85 self._cache[namespace] = {} | |
| 86 self._cache[namespace][key] = value | |
| 87 | 82 |
| 88 def get(self, key, namespace): | 83 def set(self, key, value, namespace='', time=0): |
| 89 if namespace not in self._cache: | 84 return |
| 90 return None | 85 |
| 91 return self._cache[namespace].get(key, None) | 86 def get(self, key, namespace='', time=0): |
| 87 return None |
| 92 | 88 |
| 93 def delete(self, key, namespace): | 89 def delete(self, key, namespace): |
| 94 if namespace in self._cache: | 90 return |
| 95 self._cache[namespace].pop(key) | |
| 96 memcache = InMemoryMemcache() | 91 memcache = InMemoryMemcache() |
| 97 | 92 |
| 98 # A fake webapp.RequestHandler class for Handler to extend. | |
| 99 class webapp(object): | 93 class webapp(object): |
| 100 class RequestHandler(object): | 94 class RequestHandler(object): |
| 95 """A fake webapp.RequestHandler class for Handler to extend. |
| 96 """ |
| 101 def __init__(self, request, response): | 97 def __init__(self, request, response): |
| 102 self.request = request | 98 self.request = request |
| 103 self.response = response | 99 self.response = response |
| 104 | 100 |
| 105 def redirect(self, path): | 101 def redirect(self, path): |
| 106 self.request.path = path | 102 self.request.path = path |
| 107 | 103 |
| 108 class _Db_Result(object): | 104 class _Db_Result(object): |
| 109 def get(self): | 105 def get(self): |
| 110 return [] | 106 return [] |
| 111 | 107 |
| 112 class db(object): | 108 class db(object): |
| 113 class StringProperty(object): | 109 class StringProperty(object): |
| 114 pass | 110 pass |
| 115 | 111 |
| 116 class Model(object): | 112 class Model(object): |
| 117 @staticmethod | 113 @staticmethod |
| 118 def gql(*args): | 114 def gql(*args): |
| 119 return _Db_Result() | 115 return _Db_Result() |
| 120 | 116 |
| 121 class BlobReferenceProperty(object): | 117 class BlobReferenceProperty(object): |
| 122 pass | 118 pass |
| OLD | NEW |