[google-app-engine-oil] 2 new revisions pushed by ping.nsr...@gmail.com on 2010-11-27 16:26 GMT

0 views
Skip to first unread message

google-app...@googlecode.com

unread,
Nov 27, 2010, 11:27:31 AM11/27/10
to google-app...@googlegroups.com
2 new revisions:

Revision: 8e06fe21a9
Author: Ping Yeh <pi...@pingyeh.net>
Date: Sat Nov 27 08:22:53 2010
Log: Move the logic of adding paths to a lib, which will be used in unit
te...
http://code.google.com/p/google-app-engine-oil/source/detail?r=8e06fe21a9

Revision: 87102ba2b4
Author: Ping Yeh <pi...@pingyeh.net>
Date: Sat Nov 27 08:25:30 2010
Log: Add unit tests of GaeoRequest class of oildrum/lib/gaeo/app.py.
http://code.google.com/p/google-app-engine-oil/source/detail?r=87102ba2b4

==============================================================================
Revision: 8e06fe21a9
Author: Ping Yeh <pi...@pingyeh.net>
Date: Sat Nov 27 08:22:53 2010
Log: Move the logic of adding paths to a lib, which will be used in unit
tests.
Note that the gaeo_path_util_test.py depends on unittest2 module which is
available with easy_install.
http://code.google.com/p/google-app-engine-oil/source/detail?r=8e06fe21a9

Added:
/lib/__init__.py
/lib/gaeo_path_util.py
/lib/gaeo_path_util_test.py
Modified:
/bin/gaeodoc.py

=======================================
--- /dev/null
+++ /lib/gaeo_path_util.py Sat Nov 27 08:22:53 2010
@@ -0,0 +1,51 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+"""A module to add App Engine and GAEO paths to sys.path.
+
+This module prepares Python PATH for GAEO utilities to run or
+be tested in a standalone environment.
+"""
+
+__author__ = 'Ping Yeh <pi...@pingyeh.net>'
+
+import os
+import pydoc
+import sys
+
+
+def add_appengine_module_paths():
+ """Adds paths needed to import App Engine's modules.
+
+ Locate dev_appserver.py in the execution PATH, import it and call
+ its fix_sys_path() function to add necessary paths.
+ """
+ directory = ''
+ for path in os.environ['PATH'].split(':'):
+ if os.path.exists(os.path.join(path, 'dev_appserver.py')):
+ directory = path
+ break
+ if directory:
+ sys.path.insert(0, directory)
+ dev_appserver = __import__('dev_appserver')
+ del sys.path[0]
+ dev_appserver.fix_sys_path()
+
+
+def add_gaeo_module_paths():
+ """Gets the GAEO installation path.
+
+ Derive the GAEO installation path from __file__, i.e.,
+ the full path of this script.
+ """
+ directory = ''
+ for path in os.environ['PATH'].split(':'):
+ path = os.path.expanduser(path)
+ if os.path.exists(os.path.join(path, 'gaeo.py')):
+ directory = path
+ break
+ if directory:
+ gaeo_path, bin = os.path.split(directory)
+ sys.path.append(os.path.join(gaeo_path, 'oildrum', 'lib'))
+ sys.path.append(os.path.join(gaeo_path, 'oildrum', 'sample'))
+
=======================================
--- /dev/null
+++ /lib/gaeo_path_util_test.py Sat Nov 27 08:22:53 2010
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""The unit test of gaeo_path_util.py."""
+
+__author__ = 'Ping Yeh <pi...@pingyeh.net>'
+
+# Python stdlib imports
+import os
+import StringIO
+import sys
+import unittest2
+import urllib
+
+# Import the module to be tested.
+import gaeo_path_util
+
+class TestGeeoPathUtil(unittest2.TestCase):
+
+ def test_add_appengine_module_paths(self):
+ path_backup = list(sys.path)
+ gaeo_path_util.add_appengine_module_paths()
+ self.assertGreater(len(sys.path), len(path_backup))
+ for path in sys.path:
+ if path not in path_backup:
+ self.assertTrue('google_appengine' in path)
+
+ def test_add_gaeo_module_paths(self):
+ path_backup = list(sys.path)
+ gaeo_path_util.add_gaeo_module_paths()
+ self.assertGreater(len(sys.path), len(path_backup))
+ for path in sys.path:
+ if path not in path_backup:
+ self.assertTrue('oildrum' in path)
+
+
+if __name__ == '__main__':
+ unittest2.main()
=======================================
--- /bin/gaeodoc.py Fri Nov 26 21:39:24 2010
+++ /bin/gaeodoc.py Sat Nov 27 08:22:53 2010
@@ -19,42 +19,20 @@
import pydoc
import sys

-
-def add_appengine_module_paths():
- """Add paths needed to import App Engine's modules.
-
- This function locates dev_appserver.py in the execution PATH,
- imports it and calls its fix_sys_path() function to add necessary
paths.
- """
- directory = ''
- for path in os.environ['PATH'].split(':'):
- if os.path.exists(os.path.join(path, 'dev_appserver.py')):
- directory = path
- break
- if directory:
- sys.path.insert(0, directory)
- dev_appserver = __import__('dev_appserver')
- del sys.path[0]
- dev_appserver.fix_sys_path()
-
-
-def add_gaeo_module_paths():
- """Gets the GAEO installation path.
-
- This function derives the GAEO installation path from __file__, i.e.,
- the path of this script.
- """
- binary_path, binary = os.path.split(__file__)
- gaeo_path, bin = os.path.split(binary_path)
- sys.path.append(os.path.join(gaeo_path, 'oildrum', 'lib'))
- sys.path.append(os.path.join(gaeo_path, 'oildrum', 'sample'))
-
+try:
+ import gaeo_path_util
+except ImportError:
+ directory, script_name = os.path.split(sys.argv[0])
+ lib_path = os.path.join(os.path.split(directory)[0], 'lib')
+ sys.stderr.write('Please add %s to your PYTHONPATH environment
variable.\n'
+ % lib_path)
+ sys.exit(1)

def gaeodoc():
"""Produces pydoc for files specified in command line."""
# Prepare Python PATH for pydoc module to find relevant modules to
import.
- add_appengine_module_paths()
- add_gaeo_module_paths()
+ gaeo_path_util.add_appengine_module_paths()
+ gaeo_path_util.add_gaeo_module_paths()

# Launch pydoc's command line interface.
pydoc.cli()

==============================================================================
Revision: 87102ba2b4
Author: Ping Yeh <pi...@pingyeh.net>
Date: Sat Nov 27 08:25:30 2010
Log: Add unit tests of GaeoRequest class of oildrum/lib/gaeo/app.py.
http://code.google.com/p/google-app-engine-oil/source/detail?r=87102ba2b4

Added:
/oildrum/lib/gaeo/app_test.py
Modified:
/oildrum/lib/gaeo/app.py

=======================================
--- /dev/null
+++ /oildrum/lib/gaeo/app_test.py Sat Nov 27 08:25:30 2010
@@ -0,0 +1,63 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""The unit test of app.py.
+
+To run the unit tests, """
+
+# Python stdlib imports
+import os
+import StringIO
+import sys
+import unittest2
+import urllib
+
+# Add App Engine paths
+import gaeo_path_util
+gaeo_path_util.add_appengine_module_paths()
+gaeo_path_util.add_gaeo_module_paths()
+
+# Import the module to be tested.
+import app
+
+class TestGaeoRequest(unittest2.TestCase):
+
+ def setUp(self):
+ # Construct the POST parameters.
+ self.post_param = (('hello', 'world'), ('hello', 'kitty'),
+ ('starbucks', 'coffee'), ('KFC', 'chicken'))
+ http_body = StringIO.StringIO()
+ post_data = urllib.urlencode(self.post_param)
+ http_body.write(post_data)
+ http_body.seek(0)
+ self.backup_stdin = sys.stdin
+ sys.stdin = http_body
+ # Construct the GET parameters.
+ self.get_param = (('iron man', '2'), ('harry potter', '7'),
+ ('harry potter', '8'))
+ environ = {'REQUEST_METHOD': 'POST',
+ 'CONTENT_TYPE': 'application/x-www-form-urlencoded',
+ 'QUERY_STRING': urllib.urlencode(self.get_param),
+ 'CONTENT_LENGTH': len(post_data),
+ }
+ app.make_wsgi_environment(environ)
+ # Make the request object.
+ self.request = app.GaeoRequest(environ)
+
+ def tearDown(self):
+ sys.stdin = self.backup_stdin
+
+ def test_get_all(self):
+ self.assertEqual(['2'], self.request.get_all('iron man'))
+ self.assertEqual(['world', 'kitty'], self.request.get_all('hello'))
+ self.assertEqual(['7', '8'], self.request.get_all('harry potter'))
+ self.assertEqual([], self.request.get_all('superman'))
+
+ def test_arguments(self):
+ self.assertEqual(
+ set(['hello', 'starbucks', 'KFC', 'iron man', 'harry potter']),
+ set(self.request.arguments()))
+
+
+if __name__ == '__main__':
+ unittest2.main()
=======================================
--- /oildrum/lib/gaeo/app.py Fri Nov 26 21:39:24 2010
+++ /oildrum/lib/gaeo/app.py Sat Nov 27 08:25:30 2010
@@ -358,6 +358,16 @@
return sys.stdout.write


+def make_wsgi_environment(environ):
+ environ["wsgi.input"] = sys.stdin
+ environ["wsgi.errors"] = sys.stderr
+ environ["wsgi.version"] = (1, 0)
+ environ["wsgi.run_once"] = True
+ environ["wsgi.url_scheme"] = wsgiref.util.guess_scheme(environ)
+ environ["wsgi.multithread"] = False
+ environ["wsgi.multiprocess"] = False
+
+
def start_app(app):
"""Starts the given GaeoApp instance.

@@ -373,17 +383,9 @@
app: the GaeoApp instance to be started.
"""
env = dict(os.environ)
- env["wsgi.input"] = sys.stdin
- env["wsgi.errors"] = sys.stderr
- env["wsgi.version"] = (1, 0)
- env["wsgi.run_once"] = True
- env["wsgi.url_scheme"] = wsgiref.util.guess_scheme(env)
- env["wsgi.multithread"] = False
- env["wsgi.multiprocess"] = False
-
+ make_wsgi_environment(env)
+
response_data = app(env, _start_response)
if response_data:
for data in response_data:
sys.stdout.write(data)
-
-

Reply all
Reply to author
Forward
0 new messages