tests/app/soc/cache/test_base.py
author Sverre Rabbelier <srabbelier@gmail.com>
Fri, 13 Feb 2009 19:22:34 +0000
changeset 1298 a04ab9bd300a
parent 1027 9633a6a5e5f9
child 1308 35b75ffcbb37
permissions -rw-r--r--
Fixes as requested by Leslie on behalf of the Legal team Upped the copyright statement from 2008 to 2009, and link to the GAE Terms of Service instead of any custom ToS. Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1009
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
__authors__ = [
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
  ]
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
import unittest
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
from google.appengine.api import memcache
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
from soc.cache import base
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
class CacheDecoratorTest(unittest.TestCase):
1027
9633a6a5e5f9 Style and import sorting fixes in tests modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1009
diff changeset
    31
  """Tests that the @cache decorator caches the result.
1009
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
  """
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
  def setUp(self):
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
    self.called = 0
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
    decorator = base.getCacher(self.get, self.put)
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
    @decorator
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
    def failOnSecondCall():
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
      self.called = self.called + 1
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
      if self.called > 1:
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
        self.fail("method got called twice")
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
    self.failOnSecondCall = failOnSecondCall
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
  def tearDown(self):
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
    memcache.flush_all()
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
  def get(self):
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
    return memcache.get('answer_to_life')
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
  def put(self, answer):
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
    memcache.add('answer_to_life', 42)
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
  def testMemcache(self):
1027
9633a6a5e5f9 Style and import sorting fixes in tests modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1009
diff changeset
    56
    """Sanity check to see if memcache is working.
1009
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
    """
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
    memcache.add('answer_to_life', 42)
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
    self.assertEqual(memcache.get('answer_to_life'), 42)
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
  def testSidebarCaching(self):
1027
9633a6a5e5f9 Style and import sorting fixes in tests modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1009
diff changeset
    63
    """Test that the caching decorator caches.
1009
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
    """
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
    self.failOnSecondCall()
7abbbfc79f3a Added some very basic tests for the sidebar and access modules
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
    self.failOnSecondCall()