Added some very basic tests for the sidebar and access modules
authorSverre Rabbelier <srabbelier@gmail.com>
Tue, 27 Jan 2009 11:15:49 +0000
changeset 1009 7abbbfc79f3a
parent 1008 ae1a36ef7cff
child 1010 aeed003f50c5
Added some very basic tests for the sidebar and access modules Also updated run.py to set the USER_EMAIL variable. Patch by: Sverre Rabbelier
tests/app/soc/__init__.py
tests/app/soc/cache/__init__.py
tests/app/soc/cache/test_base.py
tests/app/soc/cache/test_sidebar.py
tests/app/soc/views/__init__.py
tests/app/soc/views/helper/__init__.py
tests/app/soc/views/helper/test_access.py
tests/app/soc/views/sidebar/__init__.py
tests/app/soc/views/sidebar/test_sidebar.py
tests/run.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/app/soc/cache/test_base.py	Tue Jan 27 11:15:49 2009 +0000
@@ -0,0 +1,67 @@
+#!/usr/bin/python2.5
+#
+# Copyright 2008 the Melange authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+__authors__ = [
+  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
+  ]
+
+
+import unittest
+
+from google.appengine.api import memcache
+
+from soc.cache import base
+
+
+class CacheDecoratorTest(unittest.TestCase):
+  """Tests that the @cache decorator caches the result
+  """
+
+  def setUp(self):
+    self.called = 0
+    decorator = base.getCacher(self.get, self.put)
+
+    @decorator
+    def failOnSecondCall():
+      self.called = self.called + 1
+      if self.called > 1:
+        self.fail("method got called twice")
+
+    self.failOnSecondCall = failOnSecondCall
+
+  def tearDown(self):
+    memcache.flush_all()
+
+  def get(self):
+    return memcache.get('answer_to_life')
+
+  def put(self, answer):
+    memcache.add('answer_to_life', 42)
+
+  def testMemcache(self):
+    """Santiy check to see if memcache is working
+    """
+
+    memcache.add('answer_to_life', 42)
+    self.assertEqual(memcache.get('answer_to_life'), 42)
+
+  def testSidebarCaching(self):
+    """Test that the caching decorator caches
+    """
+
+    self.failOnSecondCall()
+    self.failOnSecondCall()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/app/soc/cache/test_sidebar.py	Tue Jan 27 11:15:49 2009 +0000
@@ -0,0 +1,88 @@
+#!/usr/bin/python2.5
+#
+# Copyright 2008 the Melange authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+__authors__ = [
+  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
+  ]
+
+
+from google.appengine.api import users
+from google.appengine.api import memcache
+
+import unittest
+
+from soc.cache import sidebar
+
+
+class SidebarCacheTest(unittest.TestCase):
+  """Tests that the sidebar properly uses caching
+  """
+
+  def setUp(self):
+    self.called = 0
+    self.user = users.get_current_user()
+
+  def tearDown(self):
+    memcache.flush_all()
+
+  def testGetCurrentUser(self):
+    """Santiy check to see if get_current_user returns same value
+    """
+
+    self.assertEqual(self.user, users.get_current_user())
+
+  def testKey(self):
+    """Test that the key method returns a unique key
+    """
+
+    self.assertEqual("sidebar_for_users.User(email='test@example.com')",
+                     sidebar.key(self.user))
+
+  def testGet(self):
+    """Test that get without setting something returns None
+    """
+    self.assertEqual(None, sidebar.get())
+
+  def testGetPut(self):
+    """Test that getting after putting gives back what you put in
+    """
+
+    sidebar.put(42)
+    self.assertEqual(42, sidebar.get())
+
+  def testFlush(self):
+    """Test that getting after putting and flushing returns None
+    """
+
+    sidebar.put(42)
+    sidebar.flush()
+    self.assertEqual(None, sidebar.get())
+
+  def testCache(self):
+    """Test that the result of a cached sidebar is cached
+    """
+
+    @sidebar.cache
+    def getAnswer():
+      self.called = self.called + 1
+      return 42
+
+    self.assertEqual(42, getAnswer())
+    self.assertEqual(42, getAnswer())
+    self.assertEqual(self.called, 1)
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/app/soc/views/helper/test_access.py	Tue Jan 27 11:15:49 2009 +0000
@@ -0,0 +1,48 @@
+#!/usr/bin/python2.5
+#
+# Copyright 2008 the Melange authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+__authors__ = [
+  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
+  ]
+
+
+import unittest
+
+from soc.views import out_of_band
+from soc.views.helper import access
+
+
+class AccessTest(unittest.TestCase):
+  def setUp(self):
+    self.test_context = {'TEST_KEY': 'TEST_VALUE'}
+    self.rights = access.Checker(None)
+
+  def testAllow(self):
+    try:
+      self.rights.allow(self.test_context)
+    except out_of_band.Error:
+      self.fail("allow should not raise on any request")
+
+  def testDeny(self):
+    kwargs = {}
+    kwargs['context'] = self.test_context
+    try:
+      self.rights.deny(kwargs)
+      self.fail("deny should raise out_of_band.Error")
+    except out_of_band.Error, e:
+      self.assertEqual(e.context, self.test_context,
+                       "context should pass through context")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/app/soc/views/sidebar/test_sidebar.py	Tue Jan 27 11:15:49 2009 +0000
@@ -0,0 +1,46 @@
+#!/usr/bin/python2.5
+#
+# Copyright 2008 the Melange authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+__authors__ = [
+  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
+  ]
+
+
+import unittest
+
+from google.appengine.api import users
+from google.appengine.api import memcache
+
+from soc.views.sitemap import sidebar
+
+class SidebarTest(unittest.TestCase):
+  def setUp(self):
+    pass
+
+  def testSidebarCallbacksAreAdded(self):
+    """Test that the sidebar callbacks are added when importing 'build'
+    """
+
+    self.assertEqual([], sidebar.SIDEBAR)
+    from soc.views.sitemap import build
+    self.assertNotEqual([], sidebar.SIDEBAR)
+
+  def testSidebarBuilds(self):
+    """test that the sidebar builds and does not return None
+    """
+
+    self.assertNotEqual(None, sidebar.getSidebar())
--- a/tests/run.py	Tue Jan 27 04:09:57 2009 +0000
+++ b/tests/run.py	Tue Jan 27 11:15:49 2009 +0000
@@ -41,6 +41,7 @@
   os.environ['SERVER_NAME'] = 'Foo'
   os.environ['SERVER_PORT'] = '8080'
   os.environ['APPLICATION_ID'] = 'test-app-run'
+  os.environ['USER_EMAIL'] = 'test@example.com'
   import main as app_main
   from google.appengine.api import apiproxy_stub_map
   from google.appengine.api import datastore_file_stub