Start a test system for the webapp itself.
authorAugie Fackler <durin42@gmail.com>
Sun, 26 Oct 2008 00:27:17 +0000
changeset 419 e9280ea935e4
parent 418 e035f31d131e
child 420 22975cc1872f
Start a test system for the webapp itself.
setup.cfg
tests/app/__init__.py
tests/app/soc/__init__.py
tests/app/soc/logic/__init__.py
tests/app/soc/logic/site/__init__.py
tests/app/soc/logic/site/test_page.py
tests/app/soc/views/__init__.py
tests/app/soc/views/simple.py
tests/app/soc/views/test_simple.py
tests/run.py
tests/test_utils.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/setup.cfg	Sun Oct 26 00:27:17 2008 +0000
@@ -0,0 +1,2 @@
+[nosetests]
+exclude=django
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/app/soc/logic/site/test_page.py	Sun Oct 26 00:27:17 2008 +0000
@@ -0,0 +1,65 @@
+#!/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.
+"""Unit tests for the Page class and its kin.
+"""
+
+__authors__ = [
+  '"Augie Fackler" <durin42@gmail.com>',
+  ]
+
+import unittest
+
+from django.core import urlresolvers
+from nose import tools
+
+from soc.logic.site import page
+
+class UrlTests(unittest.TestCase):
+  def testSimpleUrl(self):
+    # TODO(durin42) I think this is actually a bug - I gave a callable,
+    # shouldn't this require a name argument to have been provided?
+    url = page.Url(r'/', lambda r: None)
+    tools.eq_(type(url.makeDjangoUrl()), urlresolvers.RegexURLPattern)
+    url = page.Url(r'/', None)
+    tools.eq_(url.makeDjangoUrl(), None)
+
+class PageTests(unittest.TestCase):
+  def setUp(self):
+    self.home_page = page.Page(page.Url('/', lambda r: None, name='Home'),
+                               'Home!', 'Home')
+    self.child_page = page.Page(page.Url('/child', lambda r: None,
+                                         name='Child'),
+                                'Child!', 'Child', parent=self.home_page)
+    self.child_page_2 = page.Page(page.Url('/foo', None, name='None'), 'Bogus',
+                                  'Bogus', parent=self.home_page,
+                                  link_url='/bar')
+
+  def testMenuItems(self):
+    tools.eq_(list(self.home_page.getChildren()), [self.child_page,
+                                                   self.child_page_2,
+                                                  ])
+    menu = self.home_page.makeMenuItem()
+    tools.eq_(menu.name, 'Home')
+    tools.eq_([i.name for i in menu.sub_menu.items], ['Child', 'Bogus', ])
+
+  def testLinkUrl(self):
+    tools.eq_(self.home_page.makeLinkUrl(), '/')
+    tools.eq_(self.child_page.makeLinkUrl(), '/child')
+    tools.eq_(self.child_page_2.makeLinkUrl(), '/bar')
+
+
+if __name__ == '__main__':
+  print 'This is not a standalone script. Please run it using tests/run.py.'
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/app/soc/views/simple.py	Sun Oct 26 00:27:17 2008 +0000
@@ -0,0 +1,7 @@
+#!/usr/bin/env python
+
+
+
+if __name__ == '__main__':
+  print 'This is not a standalone script. Please run it using tests/run.py.'
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/app/soc/views/test_simple.py	Sun Oct 26 00:27:17 2008 +0000
@@ -0,0 +1,62 @@
+from nose import tools
+
+from google.appengine.api import users
+from soc.views import simple
+from soc.logic.site import page
+from soc.views.helper import responses
+from soc.models import user
+
+from tests import test_utils
+
+def mockRespond(request, template, context=None, response_args=None):
+  """Mock implementation of respond that passes variables through.
+  """
+  return request, template, context, response_args
+
+
+orig_respond = responses.respond
+def setup():
+  responses.respond = mockRespond
+
+
+def teardown():
+  responses.respond = orig_respond
+
+
+def test_public_without_user():
+  r = test_utils.MockRequest()
+  inbound_ctx = {'foo': 'bar', }
+  our_page = page.Page(page.Url('/', lambda r: None, name='Home'), 'Home',
+                       'Home')
+  (req, template, context,
+   respond_args, ) = simple.public(r, our_page, context=dict(inbound_ctx),
+                                   link_name='testuser')
+  tools.eq_(req, r)
+  tools.eq_(inbound_ctx['foo'], context['foo'])
+  tools.assert_not_equal(inbound_ctx, context)
+  tools.eq_(context['page'], our_page)
+  tools.eq_(context['error_status'], 404)
+  tools.eq_(context['error_message'], ('There is no user with a "link name" '
+                                       'of "testuser".'))
+
+
+def test_public_with_user():
+  r = test_utils.MockRequest()
+  inbound_ctx = {'foo': 'bar', }
+  our_page = page.Page(page.Url('/', lambda r: None, name='Home'), 'Home',
+                       'Home')
+  u = user.User(link_name='testuser',
+                nick_name='Test User!',
+                id=users.User('foo@bar.com'))
+  u.save()
+  (req, template, context,
+   respond_args, ) = simple.public(r, our_page, context=dict(inbound_ctx),
+                                   link_name='testuser')
+  tools.eq_(req, r)
+  tools.eq_(inbound_ctx['foo'], context['foo'])
+  tools.assert_not_equal(inbound_ctx, context)
+  tools.eq_(context['page'], our_page)
+  assert 'error_status' not in context
+  assert 'error_message' not in context
+  tools.eq_(context['link_name'], 'testuser')
+  tools.eq_(context['link_name_user'].id, u.id)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/run.py	Sun Oct 26 00:27:17 2008 +0000
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+import sys
+import os
+
+HERE = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),
+                                     '..'))
+appengine_location = os.path.join(HERE, 'thirdparty', 'google_appengine')
+extra_paths = [HERE,
+               os.path.join(appengine_location, 'lib', 'django'),
+               os.path.join(appengine_location, 'lib', 'webob'),
+               os.path.join(appengine_location, 'lib', 'yaml', 'lib'),
+               appengine_location,
+               os.path.join(HERE, 'app'),
+              ]
+
+import nose
+import nosegae
+from nose import config
+from nose.plugins import manager
+
+def main():
+  sys.path = extra_paths + sys.path
+  os.environ['SERVER_SOFTWARE'] = 'Development via nose'
+  os.environ['SERVER_NAME'] = 'Foo'
+  os.environ['SERVER_PORT'] = '8080'
+  os.environ['APPLICATION_ID'] = 'test-app-run'
+  import main as app_main
+  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 user_service_stub
+  from google.appengine.api import urlfetch_stub
+  apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
+  apiproxy_stub_map.apiproxy.RegisterStub('urlfetch',
+                                          urlfetch_stub.URLFetchServiceStub())
+  apiproxy_stub_map.apiproxy.RegisterStub('user',
+                                          user_service_stub.UserServiceStub())
+  apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3',
+    datastore_file_stub.DatastoreFileStub('your_app_id', '/dev/null',
+                                          '/dev/null'))
+  apiproxy_stub_map.apiproxy.RegisterStub('mail', mail_stub.MailServiceStub())
+  nose.main(config=config.Config(files=config.all_config_files()))
+
+
+if __name__ == '__main__':
+  main()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_utils.py	Sun Oct 26 00:27:17 2008 +0000
@@ -0,0 +1,28 @@
+#!/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.
+"""Common testing utilities.
+"""
+
+__authors__ = [
+  '"Augie Fackler" <durin42@gmail.com>',
+  ]
+
+class MockRequest(object):
+  """Shared dummy request object to mock common aspects of a request.
+  """
+  def __init__(self):
+    self.REQUEST = self.GET = self.POST = {}
+