ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 02.01.2026

Просмотров: 2732

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

36. ZODB + TRAVERSAL WIKI TUTORIAL

181res = self.testapp.get(’/FrontPage’, status=200)

182res = self.testapp.get(’/logout’, status=302)

183self.assertTrue(’Logout’ not in res.body)

184

185def test_anonymous_user_cannot_edit(self):

186res = self.testapp.get(’/FrontPage/edit_page’, status=200)

187self.assertTrue(’Login’ in res.body)

188

189def test_anonymous_user_cannot_add(self):

190res = self.testapp.get(’/add_page/NewPage’, status=200)

191self.assertTrue(’Login’ in res.body)

192

193def test_viewer_user_cannot_edit(self):

194res = self.testapp.get( self.viewer_login, status=302)

195res = self.testapp.get(’/FrontPage/edit_page’, status=200)

196self.assertTrue(’Login’ in res.body)

197

198def test_viewer_user_cannot_add(self):

199res = self.testapp.get( self.viewer_login, status=302)

200res = self.testapp.get(’/add_page/NewPage’, status=200)

201self.assertTrue(’Login’ in res.body)

202

203def test_editors_member_user_can_edit(self):

204res = self.testapp.get( self.editor_login, status=302)

205res = self.testapp.get(’/FrontPage/edit_page’, status=200)

206self.assertTrue(’Editing’ in res.body)

207

208def test_editors_member_user_can_add(self):

209res = self.testapp.get( self.editor_login, status=302)

210res = self.testapp.get(’/add_page/NewPage’, status=200)

211self.assertTrue(’Editing’ in res.body)

212

213def test_editors_member_user_can_view(self):

214res = self.testapp.get( self.editor_login, status=302)

215res = self.testapp.get(’/FrontPage’, status=200)

216self.assertTrue(’FrontPage’ in res.body)

36.8.5Run the Tests

We can run these tests by using setup.py test in the same way we did in Run the Tests. However, first we must edit our setup.py to include a dependency on WebTest, which we’ve used in our tests.py. Change the requires list in setup.py to include WebTest.

436


36.8. ADDING TESTS

1

2

3

4

5

6

7

8

9

10

requires = [ ’pyramid’,

’pyramid_zodbconn’, ’pyramid_tm’, ’pyramid_debugtoolbar’, ’ZODB3’,

’waitress’, ’docutils’, ’WebTest’, # add this

]

After we’ve added a dependency on WebTest in setup.py, we need to rerun setup.py develop to get WebTest installed into our virtualenv. Assuming our shell’s current working directory is the “tutorial” distribution directory:

On UNIX:

$ ../bin/python setup.py develop

On Windows:

c:\pyramidtut\tutorial> ..\Scripts\python setup.py develop

Once that command has completed successfully, we can run the tests themselves: On UNIX:

$ ../bin/python setup.py test -q

On Windows:

c:\pyramidtut\tutorial> ..\Scripts\python setup.py test -q

The expected result looks something like:

.........

----------------------------------------------------------------------

Ran 23 tests in 1.653s

OK

437


36. ZODB + TRAVERSAL WIKI TUTORIAL

36.9 Distributing Your Application

Once your application works properly, you can create a “tarball” from it by using the setup.py sdist command. The following commands assume your current working directory is the tutorial package we’ve created and that the parent directory of the tutorial package is a virtualenv representing a Pyramid environment.

On UNIX:

$ ../bin/python setup.py sdist

On Windows:

c:\pyramidtut> ..\Scripts\python setup.py sdist

The output of such a command will be something like:

running sdist

# .. more output .. creating dist

tar -cf dist/tutorial-0.1.tar tutorial-0.1 gzip -f9 dist/tutorial-0.1.tar

removing ’tutorial-0.1’ (and everything under it)

Note that this command creates a tarball in the “dist” subdirectory named tutorial-0.1.tar.gz. You can send this file to your friends to show them your cool new application. They should be able to install it by pointing the easy_install command directly at it. Or you can upload it to PyPI and share it with the rest of the world, where it can be downloaded via easy_install remotely like any other package people download from PyPI.

438


CHAPTER

THIRTYSEVEN

SQLALCHEMY + URL DISPATCH WIKI TUTORIAL

This tutorial introduces a SQLAlchemy and url dispatch -based Pyramid application to a developer familiar with Python. When the tutorial is finished, the developer will have created a basic Wiki application with authentication.

For cut and paste purposes, the source code for all stages of this tutorial can be browsed at https://github.com/Pylons/pyramid/tree/1.3-branch/docs/tutorials/wiki2/src.

37.1 Background

This tutorial presents a Pyramid application that uses technologies which will be familiar to someone with SQL database experience. It uses SQLAlchemy as a persistence mechanism and url dispatch to map URLs to code.

To code along with this tutorial, the developer will need a UNIX machine with development tools (Mac OS X with XCode, any Linux or BSD variant, etc) or a Windows system of any kind.

latex-warning.png

This tutorial has been written for Python 2. It is unlikely to work without modification under Python 3.

Have fun!

439