kay / kay / management / test.py

# -*- coding: utf-8 -*-
#
# Copyright 2007 Google Inc.
#
# 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.
#
"""
Kay test management commands.

:Copyright: (c) 2009 Accense Technology, Inc. All rights reserved.
:license: BSD, see LICENSE for more details.
"""

import logging
import os
import sys
import unittest

import kay
kay.setup()
from kay.misc import get_appid

from werkzeug.utils import import_string
from google.appengine.ext import db
from google.appengine.api import apiproxy_stub_map
from google.appengine.api import datastore_file_stub
from google.appengine.api import mail_stub
from google.appengine.api import urlfetch_stub
from google.appengine.api.memcache import memcache_stub
from google.appengine.api import user_service_stub
from google.appengine.api.labs.taskqueue import taskqueue_stub
try:
  from google.appengine.api.images import images_stub
except ImportError:
  pass

from kay.conf import settings

def setup_env():
  try:
    os.environ['APPLICATION_ID'] = get_appid()
  except Exception:
    fake_appid = os.path.basename(
      os.path.dirname(
        os.path.dirname(
          os.path.dirname(os.path.abspath(__file__)))))
    os.environ['APPLICATION_ID'] = fake_appid
  os.environ['USER_EMAIL'] = ''
  os.environ['SERVER_NAME'] = 'localhost'
  os.environ['SERVER_PORT'] = '80'
  logging.getLogger().setLevel(logging.ERROR)


def setup_stub():
  apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
  stub = datastore_file_stub.DatastoreFileStub('test','/dev/null',
                                               '/dev/null', trusted=True)
  apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)

  apiproxy_stub_map.apiproxy.RegisterStub(
    'user', user_service_stub.UserServiceStub())

  apiproxy_stub_map.apiproxy.RegisterStub(
    'memcache', memcache_stub.MemcacheServiceStub())

  apiproxy_stub_map.apiproxy.RegisterStub(
    'urlfetch', urlfetch_stub.URLFetchServiceStub())

  apiproxy_stub_map.apiproxy.RegisterStub(
    'taskqueue', taskqueue_stub.TaskQueueServiceStub())

  try:
    apiproxy_stub_map.apiproxy.RegisterStub(
      'images', images_stub.ImagesServiceStub())
  except NameError:
    pass


def runtest(target='', verbosity=0):
  suite = unittest.TestSuite()
  if target:
    tests_mod = import_string("%s.tests" % target)
    suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(
        tests_mod))
  else:
    for app_name in settings.INSTALLED_APPS:
      if app_name.startswith('kay.'):
        continue
      try:
        tests_mod = import_string("%s.tests" % app_name)
      except (ImportError, AttributeError), e:
        logging.error("Loading module %s.tests failed: '%s'." % 
                      (app_name, e))
      else:
        suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(
            tests_mod))
  unittest.TextTestRunner(verbosity=verbosity).run(suite)

def do_runtest(target=('t', ''), verbosity=("v", 0)):
  """
  Run test for installed applications.
  """
  setup_env()
  setup_stub()
  runtest(target, verbosity)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.