Last active
August 29, 2015 13:59
-
-
Save iandouglas/10441406 to your computer and use it in GitHub Desktop.
How do I write a unit test to check that my code throws a CapabilityDisabledError when writing to GAE's datastore during an outage?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding=utf-8 | |
| import unittest | |
| from google.appengine.api import users, capabilities | |
| from google.appengine.ext import testbed | |
| from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError | |
| from models.user import User | |
| class TestDatastoreDown(unittest.TestCase): | |
| def setUp(self): | |
| self.tb = testbed.Testbed() | |
| self.tb.activate() | |
| self.tb.setup_env(USER_EMAIL='[email protected]', USER_ID='1', USER_IS_ADMIN='0', overwrite=True) | |
| self.tb.init_capability_stub() | |
| self.tb.init_memcache_stub() | |
| self.tb.init_datastore_v3_stub() | |
| self.stub = self.tb.get_stub('capability_service') | |
| self.stub.SetPackageEnabled('datastore_v3', False) | |
| self.guser = users.get_current_user() | |
| def tearDown(self): | |
| self.tb.deactivate() | |
| def test_stuff(self): | |
| # this test always passes, making you think that the capability for datastore is temporarily turned off | |
| self.assertFalse(capabilities.CapabilitySet('datastore_v3').is_enabled()) | |
| # but then trying to actually write to the datastore actually works just fine and no exception is raised | |
| self.assertRaises(CapabilityDisabledError, lambda: User( | |
| guser=self.guser, user_handle='foo').put()) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stack trace of error:
Failure
Traceback (most recent call last):
File "/tmp/test_datastore_offline.py", line 28, in test_stuff
self.assertRaises(CapabilityDisabledError, lambda: User(
AssertionError: CapabilityDisabledError not raised