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

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

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

Добавлен: 02.01.2026

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

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

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

37. SQLALCHEMY + URL DISPATCH WIKI TUTORIAL

<div id="middle">

<div class="middle align-right">

<div id="left" class="app-welcome align-left"> Viewing <b><span tal:replace="page.name">Page Name

Goes Here</span></b><br/>

You can return to the

<a href="${request.application_url}">FrontPage</a>.<br/>

</div>

<div id="right" class="app-welcome align-right"> <span tal:condition="logged_in">

<a href="${request.application_url}/logout">Logout</a>

</span>

</div>

</div>

</div>

<div id="bottom">

<div class="bottom">

<div tal:replace="structure content"> Page text goes here.

</div>

<p>

<a tal:attributes="href edit_url" href=""> Edit this page

</a>

</p>

</div>

</div>

</div>

<div id="footer"> <div class="footer"

>© Copyright 2008-2011, Agendaless Consulting.</div>

</div>

</body>

</html>

(Only the highlighted lines need to be added.)

37.7.9 Viewing the Application in a Browser

We can finally examine our application in a browser (See Starting the Application). Launch a browser and visit each of the following URLs, check that the result is as expected:

http://localhost:6543/ invokes the view_wiki view. This always redirects to the view_page view of the FrontPage page object. It is executable by any user.

486



37.8. ADDING TESTS

http://localhost:6543/FrontPage invokes the view_page view of the FrontPage page object.

http://localhost:6543/FrontPage/edit_page invokes the edit view for the FrontPage object. It is executable by only the editor user. If a different user (or the anonymous user) invokes it, a login form will be displayed. Supplying the credentials with the username editor, password editor will display the edit page form.

http://localhost:6543/add_page/SomePageName invokes the add view for a page. It is executable by only the editor user. If a different user (or the anonymous user) invokes it, a login form will be displayed. Supplying the credentials with the username editor, password editor will display the edit page form.

After logging in (as a result of hitting an edit or add page and submitting the login form with the editor credentials), we’ll see a Logout link in the upper right hand corner. When we click it, we’re logged out, and redirected back to the front page.

37.8 Adding Tests

We will now add tests for the models and the views and a few functional tests in the tests.py. Tests ensure that an application works, and that it continues to work after changes are made in the future.

The source code for this tutorial stage can be browsed at http://github.com/Pylons/pyramid/tree/1.3- branch/docs/tutorials/wiki2/src/tests/.

37.8.1 Testing the Models

To test the model class Page we’ll add a new PageModelTests class to our tests.py file that was generated as part of the alchemy scaffold.

37.8.2 Testing the Views

We’ll modify our tests.py file, adding tests for each view function we added above. As a result, we’ll delete the ViewTests class that the alchemy scaffold provided, and add four other test classes: ViewWikiTests, ViewPageTests, AddPageTests, and EditPageTests. These test the view_wiki, view_page, add_page, and edit_page views respectively.

487

37. SQLALCHEMY + URL DISPATCH WIKI TUTORIAL

37.8.3 Functional tests

We’ll test the whole application, covering security aspects that are not tested in the unit tests, like logging in, logging out, checking that the viewer user cannot add or edit pages, but the editor user can, and so on.

37.8.4 Viewing the results of all our edits to tests.py

Once we’re done with the tests.py module, it will look a lot like:

1

import unittest

2

import transaction

3

from pyramid import testing

4

 

5

def _initTestingDB():

6

from sqlalchemy import create_engine

7from tutorial.models import (

8DBSession,

9Page,

10Base

11)

12engine = create_engine(’sqlite://’)

13Base.metadata.create_all(engine)

14DBSession.configure(bind=engine)

15with transaction.manager:

16model = Page(’FrontPage’, ’This is the front page’)

17DBSession.add(model)

18return DBSession

19

20def _registerRoutes(config):

21config.add_route(’view_page’, ’{pagename}’)

22config.add_route(’edit_page’, ’{pagename}/edit_page’)

23config.add_route(’add_page’, ’add_page/{pagename}’)

24

25

26 class PageModelTests(unittest.TestCase):

27

28def setUp(self):

29self.session = _initTestingDB()

30

31def tearDown(self):

32self.session.remove()

33

34 def _getTargetClass(self):

488


37.8. ADDING TESTS

35from tutorial.models import Page

36return Page

37

38def _makeOne(self, name=’SomeName’, data=’some data’):

39return self._getTargetClass()(name, data)

40

41def test_constructor(self):

42instance = self._makeOne()

43self.assertEqual(instance.name, ’SomeName’)

44self.assertEqual(instance.data, ’some data’)

45

46class ViewWikiTests(unittest.TestCase):

47def setUp(self):

48self.config = testing.setUp()

49

50def tearDown(self):

51testing.tearDown()

52

53def _callFUT(self, request):

54from tutorial.views import view_wiki

55return view_wiki(request)

56

57def test_it(self):

58_registerRoutes(self.config)

59request = testing.DummyRequest()

60response = self._callFUT(request)

61self.assertEqual(response.location, ’http://example.com/FrontPage’)

62

63class ViewPageTests(unittest.TestCase):

64def setUp(self):

65self.session = _initTestingDB()

66self.config = testing.setUp()

67

68def tearDown(self):

69self.session.remove()

70testing.tearDown()

71

72def _callFUT(self, request):

73from tutorial.views import view_page

74return view_page(request)

75

76def test_it(self):

77from tutorial.models import Page

78request = testing.DummyRequest()

79request.matchdict[’pagename’] = ’IDoExist’

80page = Page(’IDoExist’, ’Hello CruelWorld IDoExist’)

489


37. SQLALCHEMY + URL DISPATCH WIKI TUTORIAL

81self.session.add(page)

82_registerRoutes(self.config)

83info = self._callFUT(request)

84self.assertEqual(info[’page’], page)

85self.assertEqual(

86

info[’content’],

87

’<div class="document">\n

88

’<p>Hello <a href="http://example.com/add_page/CruelWorld">’

89

’CruelWorld</a> ’

90

’<a href="http://example.com/IDoExist">’

91

’IDoExist</a>’

92’</p>\n</div>\n)

93self.assertEqual(info[’edit_url’],

94

’http://example.com/IDoExist/edit_page’)

95

 

96class AddPageTests(unittest.TestCase):

97def setUp(self):

98self.session = _initTestingDB()

99self.config = testing.setUp()

100

101def tearDown(self):

102self.session.remove()

103testing.tearDown()

104

105def _callFUT(self, request):

106from tutorial.views import add_page

107return add_page(request)

108

109def test_it_notsubmitted(self):

110_registerRoutes(self.config)

111request = testing.DummyRequest()

112request.matchdict = {’pagename’:’AnotherPage’}

113info = self._callFUT(request)

114self.assertEqual(info[’page’].data,’’)

115self.assertEqual(info[’save_url’],

116

’http://example.com/add_page/AnotherPage’)

117

 

118def test_it_submitted(self):

119from tutorial.models import Page

120_registerRoutes(self.config)

121request = testing.DummyRequest({’form.submitted’:True,

122

’body’:’Hello yo!’})

123request.matchdict = {’pagename’:’AnotherPage’}

124self._callFUT(request)

125page = self.session.query(Page).filter_by(name=’AnotherPage’).one()

126self.assertEqual(page.data, ’Hello yo!’)

490