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 |
| 9 from google.appengine.ext.blobstore.blobstore import BlobReferenceProperty |
| 10 import google.appengine.ext.db as db |
8 import google.appengine.ext.webapp as webapp | 11 import google.appengine.ext.webapp as webapp |
| 12 import google.appengine.api.files as files |
9 import google.appengine.api.memcache as memcache | 13 import google.appengine.api.memcache as memcache |
10 import google.appengine.api.urlfetch as urlfetch | 14 import google.appengine.api.urlfetch as urlfetch |
11 except ImportError: | 15 except ImportError: |
12 class FakeUrlFetch(object): | 16 class NotImplemented(object): |
13 """A fake urlfetch module that errors for all method calls. | 17 def __getattr__(self, attr): |
14 """ | |
15 def fetch(self, url): | |
16 raise NotImplementedError() | 18 raise NotImplementedError() |
17 | 19 |
18 def create_rpc(self): | 20 blobstore = NotImplemented() |
19 raise NotImplementedError() | 21 files = NotImplemented() |
20 | |
21 def make_fetch_call(self): | |
22 raise NotImplementedError() | |
23 urlfetch = FakeUrlFetch() | |
24 | 22 |
25 class InMemoryMemcache(object): | 23 class InMemoryMemcache(object): |
26 """A memcache that stores items in memory instead of using the memcache | 24 """A memcache that stores items in memory instead of using the memcache |
27 module. | 25 module. |
28 """ | 26 """ |
29 def __init__(self): | 27 def __init__(self): |
30 self._cache = {} | 28 self._cache = {} |
31 | 29 |
32 def set(self, key, value, namespace, time=60): | 30 def set(self, key, value, namespace, time=60): |
33 if namespace not in self._cache: | 31 if namespace not in self._cache: |
34 self._cache[namespace] = {} | 32 self._cache[namespace] = {} |
35 self._cache[namespace][key] = value | 33 self._cache[namespace][key] = value |
36 | 34 |
37 def get(self, key, namespace): | 35 def get(self, key, namespace): |
38 if namespace not in self._cache: | 36 if namespace not in self._cache: |
39 return None | 37 return None |
40 return self._cache[namespace].get(key, None) | 38 return self._cache[namespace].get(key, None) |
41 | 39 |
42 def delete(self, key, namespace): | 40 def delete(self, key, namespace): |
43 if namespace in self._cache: | 41 if namespace in self._cache: |
44 self._cache[namespace].pop(key) | 42 self._cache[namespace].pop(key) |
45 memcache = InMemoryMemcache() | 43 memcache = InMemoryMemcache() |
46 | 44 |
| 45 class FakeUrlfetch(object): |
| 46 class _RPC(object): |
| 47 def wait(self): |
| 48 pass |
| 49 |
| 50 def get_result(self): |
| 51 return FakeUrlfetch._Result() |
| 52 |
| 53 class _Result(object): |
| 54 def __init__(self): |
| 55 self.content = '{ "commit": { "tree": { "sha": 0 } } }' |
| 56 |
| 57 def fetch(self, url): |
| 58 return self._Result() |
| 59 |
| 60 def create_rpc(self): |
| 61 return self._RPC() |
| 62 |
| 63 def make_fetch_call(self, rpc, url): |
| 64 pass |
| 65 urlfetch = FakeUrlfetch() |
| 66 |
47 # A fake webapp.RequestHandler class for Handler to extend. | 67 # A fake webapp.RequestHandler class for Handler to extend. |
48 class webapp(object): | 68 class webapp(object): |
49 class RequestHandler(object): | 69 class RequestHandler(object): |
50 def __init__(self, request, response): | 70 def __init__(self, request, response): |
51 self.request = request | 71 self.request = request |
52 self.response = response | 72 self.response = response |
53 | 73 |
54 def redirect(self, path): | 74 def redirect(self, path): |
55 self.request.path = path | 75 self.request.path = path |
| 76 |
| 77 class _Db_Result(object): |
| 78 def get(self): |
| 79 return [] |
| 80 |
| 81 class db(object): |
| 82 class StringProperty(object): |
| 83 pass |
| 84 |
| 85 class Model(object): |
| 86 @staticmethod |
| 87 def gql(*args): |
| 88 return _Db_Result() |
| 89 |
| 90 class BlobReferenceProperty(object): |
| 91 pass |
OLD | NEW |