Commits

Anonymous committed d20ca6a

Fixed several issues with testing.

Comments (0)

Files changed (6)

kay/ext/testutils/gae_test_base.py

         return not getattr(self.__class__, 'KIND_NAME_UNSWAPPED', GAETestBase.DEFAULT_KIND_NAME_UNSWAPPED)
 
     def swap_model_kind(self):
+        self.kind_method_swapped = True
+        self.original_class_for_kind_method = db.class_for_kind
         kprefix = self.kind_prefix()
 
         def kind_for_test_with_store_kinds(cls):
         def kind_for_test(cls):
             return kprefix + cls._meta.db_table
             
+        def class_for_kind_for_test(kind):
+            if kind.find(kprefix) == 0 and \
+                  db._kind_map.has_key(kind[len(kprefix):]):
+                return db._kind_map[kind[len(kprefix):]]
+            else:
+                try:
+                    return db._kind_map[kind]
+                except KeyError:
+                    import logging
+                    logging.error(db._kind_map)
+                    raise KindError('No implementation for kind \'%s\'' % kind)
+
         if self.may_cleanup_used_kind():
             db.Model.kind = classmethod(kind_for_test_with_store_kinds)
         else:
             db.Model.kind = classmethod(kind_for_test)
-
-        self._original_kind_map = db._kind_map
-        db._kind_map = {}
-        for k, v in self._original_kind_map.items():
-            db._kind_map[kprefix+k] = v
+        db.class_for_kind = class_for_kind_for_test
 
     def reswap_model_kind(self):
         @classmethod
         def original_kind(cls):
             return cls._meta.db_table
         db.Model.kind = original_kind
-        if hasattr(self, '_original_kind_map'):
-          db._kind_map = self._original_kind_map
+        db.class_for_kind = self.original_class_for_kind_method
+        delete_keys = []
+        elms = {}
+        kprefix = self.kind_prefix()
+        for key in db._kind_map.keys():
+            if key.startswith(kprefix):
+                 elms[key[len(kprefix):]] = db._kind_map[key]
+                 delete_keys.append(key)
+        for delete_key in delete_keys:
+            del db._kind_map[delete_key]
+        db._kind_map.update(elms)
         
 
     def _env_setUp(self):
+        self.kind_method_swapped = False
         self.original_kind_method = None
 
         if self.may_cleanup_used_kind():
             self.delete_all_entities_of_used_kind()
             global kind_names_for_test
             del kind_names_for_test
-        self.reswap_model_kind()
+        if self.kind_method_swapped:
+            self.reswap_model_kind()
         restore_environments(self)

kay/ext/testutils/gaeunit.py

         except (ImportError, AttributeError), e:
             logging.warn("Loading module %s.tests failed: '%s'." %
                          (app_name, e))
-            raise
+            pass
     return modules
 
 
             for model in models:
                 self.check_authority(request, OP_UPDATE, obj=model,
                                      model_name=model_name, prop_name=None)
-        db.put(models)
+        for model in models:
+          def txn():
+            db.put(model)
+          db.run_in_transaction(txn)
 
         # if input was not a list, convert single element models list
         # back to single element

kay/tests/auth_test.py

   KIND_NAME_UNSWAPPED = False
   USE_PRODUCTION_STUBS = True
   CLEANUP_USED_KIND = True
+  KIND_PREFIX_IN_TEST = 't1'
   
   def setUp(self):
     try:
     s = LazySettings(settings_module='kay.tests.google_settings')
     app = get_application(settings=s)
     self.client = Client(app, BaseResponse)
+    self.client.test_logout()
 
   def tearDown(self):
-    if hasattr(self, "original_user"):
-      os.environ["USER_EMAIL"] = self.original_user
-    if hasattr(self, "original_is_admin"):
-      os.environ["USER_IS_ADMIN"] = self.original_is_admin
+    self.client.test_logout()
 
   def test_login(self):
     response = self.client.get(url_for('auth_testapp/index'))
   KIND_NAME_UNSWAPPED = False
   USE_PRODUCTION_STUBS = True
   CLEANUP_USED_KIND = True
+  KIND_PREFIX_IN_TEST = 't2'
   
   def setUp(self):
     from kay.auth import create_new_user
     create_new_user("foobar", "password", is_admin=False)
 
   def tearDown(self):
-    pass
+    self.client.test_logout()
 
   def test_login(self):
     response = self.client.get(url_for('auth_testapp/index'))
   KIND_NAME_UNSWAPPED = False
   USE_PRODUCTION_STUBS = True
   CLEANUP_USED_KIND = True
+  KIND_PREFIX_IN_TEST = 't3'
 
   def setUp(self):
     s = LazySettings(settings_module='kay.tests.gaema_settings')
     self.client = Client(app, BaseResponse)
 
   def tearDown(self):
-    pass
+    self.client.test_logout(service='shehas.net')
 
   def test_login(self):
     response = self.client.get(url_for('gaema_testapp/index'))

kay/tests/rest_test.py

 from kay.utils import url_for
 
 from kay.tests.restapp.models import RestModel
-logging.getLogger().setLevel(logging.ERROR)
+logging.getLogger().setLevel(logging.DEBUG)
 
-class RestTestCase(GAETestBase):
+class RestJSONTestCase(GAETestBase):
   KIND_NAME_UNSWAPPED = False
   USE_PRODUCTION_STUBS = True
   CLEANUP_USED_KIND = True
 
   def setUp(self):
-    try:
-      self.original_user = os.environ['USER_EMAIL']
-      self.original_is_admin = os.environ['USER_IS_ADMIN']
-      del os.environ['USER_EMAIL']
-      del os.environ['USER_IS_ADMIN']
-    except Exception:
-      pass
     s = LazySettings(settings_module='kay.tests.rest_settings')
     app = get_application(settings=s)
     self.client = Client(app, BaseResponse)
+    self.client.test_logout()
 
   def tearDown(self):
-    if hasattr(self, "original_user"):
-      os.environ["USER_EMAIL"] = self.original_user
-    if hasattr(self, "original_is_admin"):
-      os.environ["USER_IS_ADMIN"] = self.original_is_admin
+    self.client.test_logout()
 
   def test_rest_json(self):
 
     self.assertEqual(response.status_code, 404)
 
 
+class RestTestCase(GAETestBase):
+  KIND_NAME_UNSWAPPED = False
+  USE_PRODUCTION_STUBS = True
+  CLEANUP_USED_KIND = True
+  KIND_PREFIX_IN_TEST = "t2"
+
+  def setUp(self):
+    s = LazySettings(settings_module='kay.tests.rest_settings')
+    app = get_application(settings=s)
+    self.client = Client(app, BaseResponse)
+    self.client.test_logout()
+
+  def tearDown(self):
+    self.client.test_logout()
+
   def test_rest_operations(self):
     self.client.test_login(email="test@example.com", is_admin="1")
     response = self.client.get('/rest/metadata')

kay/utils/__init__.py

 
 _translations_cache = {}
 _default_translations = None
-_standard_context_processors = None
 
 _timezone_cache = {}
 
 
 def get_standard_processors():
   from kay.conf import settings
-  global _standard_context_processors
-  if _standard_context_processors is None:
-    processors = []
-    for path in settings.CONTEXT_PROCESSORS:
-      try:
-        func = import_string(path)
-      except (ImportError, AttributeError), e:
-        raise ImproperlyConfigured('Error importing request processor module'
-                                   ' %s: "%s"' % (path, e))
-      processors.append(func)
-    _standard_context_processors = tuple(processors)
-  return _standard_context_processors
+  processors = []
+  for path in settings.CONTEXT_PROCESSORS:
+    try:
+      func = import_string(path)
+    except (ImportError, AttributeError), e:
+      raise ImproperlyConfigured('Error importing request processor module'
+                                 ' %s: "%s"' % (path, e))
+    processors.append(func)
+  return tuple(processors)
 
 
 def to_local_timezone(datetime, tzname=None):