Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
10
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

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

37.8. ADDING TESTS

127

128class EditPageTests(unittest.TestCase):

129def setUp(self):

130self.session = _initTestingDB()

131self.config = testing.setUp()

132

133def tearDown(self):

134self.session.remove()

135testing.tearDown()

136

137def _callFUT(self, request):

138from tutorial.views import edit_page

139return edit_page(request)

140

141def test_it_notsubmitted(self):

142from tutorial.models import Page

143_registerRoutes(self.config)

144request = testing.DummyRequest()

145request.matchdict = {’pagename’:’abc’}

146page = Page(’abc’, ’hello’)

147self.session.add(page)

148info = self._callFUT(request)

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

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

151

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

152

 

153def test_it_submitted(self):

154from tutorial.models import Page

155_registerRoutes(self.config)

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

157’body’:’Hello yo!’})

158request.matchdict = {’pagename’:’abc’}

159page = Page(’abc’, ’hello’)

160self.session.add(page)

161response = self._callFUT(request)

162self.assertEqual(response.location, ’http://example.com/abc’)

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

164

165 class FunctionalTests(unittest.TestCase):

166

 

167

viewer_login = ’/login?login=viewer&password=viewer’ \

168

’&came_from=FrontPage&form.submitted=Login’

169

viewer_wrong_login = ’/login?login=viewer&password=incorrect’ \

170

’&came_from=FrontPage&form.submitted=Login’

171

editor_login = ’/login?login=editor&password=editor’ \

172

’&came_from=FrontPage&form.submitted=Login’

491

37. SQLALCHEMY + URL DISPATCH WIKI TUTORIAL

173

174def setUp(self):

175from tutorial import main

176settings = { ’sqlalchemy.url’: ’sqlite://’}

177app = main({}, **settings)

178from webtest import TestApp

179self.testapp = TestApp(app)

180_initTestingDB()

181

182def tearDown(self):

183del self.testapp

184from tutorial.models import DBSession

185DBSession.remove()

186

187def test_root(self):

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

189self.assertEqual(res.location, ’http://localhost/FrontPage’)

190

191def test_FrontPage(self):

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

193self.assertTrue(b’FrontPage’ in res.body)

194

195def test_unexisting_page(self):

196self.testapp.get(’/SomePage’, status=404)

197

198def test_successful_log_in(self):

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

200self.assertEqual(res.location, ’http://localhost/FrontPage’)

201

202def test_failed_log_in(self):

203res = self.testapp.get(self.viewer_wrong_login, status=200)

204self.assertTrue(b’login’ in res.body)

205

206def test_logout_link_present_when_logged_in(self):

207self.testapp.get(self.viewer_login, status=302)

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

209self.assertTrue(b’Logout’ in res.body)

210

211def test_logout_link_not_present_after_logged_out(self):

212self.testapp.get(self.viewer_login, status=302)

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

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

215self.assertTrue(b’Logout’ not in res.body)

216

217def test_anonymous_user_cannot_edit(self):

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

492

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]