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

36.8. ADDING TESTS

Visiting http://localhost:6543/add_page/SomePageName in a browser 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 show the edit page form being displayed.

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.

36.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 some changes are made in the future.

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

36.8.1 Test the Models

We write tests for the model classes and the appmaker. Changing tests.py, we’ll write a separate test class for each model class, and we’ll write a test class for the appmaker.

To do so, we’ll retain the tutorial.tests.ViewTests class provided as a result of the zodb project generator. We’ll add three test classes: one for the Page model named PageModelTests, one for the Wiki model named WikiModelTests, and one for the appmaker named AppmakerTests.

36.8.2 Test 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 test in the file, 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.

36.8.3 Functional tests

We 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.

431

36. ZODB + TRAVERSAL WIKI TUTORIAL

36.8.4 View the results of all our edits to tests.py

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

1 import unittest

2

3 from pyramid import testing

4

5 class PageModelTests(unittest.TestCase):

6

7def _getTargetClass(self):

8 from .models import Page

9return Page

10

11def _makeOne(self, data=u’some data’):

12return self._getTargetClass()(data=data)

13

14def test_constructor(self):

15instance = self._makeOne()

16self.assertEqual(instance.data, u’some data’)

17

18 class WikiModelTests(unittest.TestCase):

19

20def _getTargetClass(self):

21from .models import Wiki

22return Wiki

23

24def _makeOne(self):

25return self._getTargetClass()()

26

27def test_it(self):

28wiki = self._makeOne()

29self.assertEqual(wiki.__parent__, None)

30self.assertEqual(wiki.__name__, None)

31

32class AppmakerTests(unittest.TestCase):

33def _callFUT(self, zodb_root):

34from .models import appmaker

35return appmaker(zodb_root)

36

37def test_it(self):

38root = {}

39self._callFUT(root)

40self.assertEqual(root[’app_root’][’FrontPage’].data,

41

’This is the front page’)

42

 

432

36.8. ADDING TESTS

43class ViewWikiTests(unittest.TestCase):

44def test_it(self):

45from .views import view_wiki

46context = testing.DummyResource()

47request = testing.DummyRequest()

48response = view_wiki(context, request)

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

50

51class ViewPageTests(unittest.TestCase):

52def _callFUT(self, context, request):

53from .views import view_page

54return view_page(context, request)

55

56def test_it(self):

57wiki = testing.DummyResource()

58wiki[’IDoExist’] = testing.DummyResource()

59context = testing.DummyResource(data=’Hello CruelWorld IDoExist’)

60context.__parent__ = wiki

61context.__name__ = ’thepage’

62request = testing.DummyRequest()

63info = self._callFUT(context, request)

64self.assertEqual(info[’page’], context)

65self.assertEqual(

66

info[’content’],

67

’<div class="document">\n

68

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

69

’CruelWorld</a> ’

70

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

71

’IDoExist</a>’

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

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

74

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

75

 

76

 

77class AddPageTests(unittest.TestCase):

78def _callFUT(self, context, request):

79from .views import add_page

80return add_page(context, request)

81

82def test_it_notsubmitted(self):

83context = testing.DummyResource()

84request = testing.DummyRequest()

85request.subpath = [’AnotherPage’]

86info = self._callFUT(context, request)

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

88self.assertEqual(

433

36. ZODB + TRAVERSAL WIKI TUTORIAL

89

info[’save_url’],

90

request.resource_url(context, ’add_page’, ’AnotherPage’))

91

92def test_it_submitted(self):

93context = testing.DummyResource()

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

95

’body’:’Hello yo!’})

96request.subpath = [’AnotherPage’]

97self._callFUT(context, request)

98page = context[’AnotherPage’]

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

100self.assertEqual(page.__name__, ’AnotherPage’)

101self.assertEqual(page.__parent__, context)

102

103class EditPageTests(unittest.TestCase):

104def _callFUT(self, context, request):

105from .views import edit_page

106return edit_page(context, request)

107

108def test_it_notsubmitted(self):

109context = testing.DummyResource()

110request = testing.DummyRequest()

111info = self._callFUT(context, request)

112self.assertEqual(info[’page’], context)

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

114

request.resource_url(context, ’edit_page’))

115

116def test_it_submitted(self):

117context = testing.DummyResource()

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

119

’body’:’Hello yo!’})

120response = self._callFUT(context, request)

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

122self.assertEqual(context.data, ’Hello yo!’)

123

124 class FunctionalTests(unittest.TestCase):

125

 

126

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

127

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

128

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

129

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

130

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

131

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

132

133def setUp(self):

134import tempfile

434

36.8. ADDING TESTS

135import os.path

136from . import main

137self.tmpdir = tempfile.mkdtemp()

138

139dbpath = os.path.join( self.tmpdir, ’test.db’)

140uri = ’file://’ + dbpath

141settings = { ’zodbconn.uri’ : uri ,

142

’pyramid.includes’: [’pyramid_zodbconn’, ’pyramid_tm’] }

143

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

145self.db = app.registry.zodb_database

146from webtest import TestApp

147self.testapp = TestApp(app)

148

149def tearDown(self):

150import shutil

151self.db.close()

152shutil.rmtree( self.tmpdir )

153

154def test_root(self):

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

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

157

158def test_FrontPage(self):

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

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

161

162def test_unexisting_page(self):

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

164self.assertTrue(’Not Found’ in res.body)

165

166def test_successful_log_in(self):

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

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

169

170def test_failed_log_in(self):

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

172self.assertTrue(’login’ in res.body)

173

174def test_logout_link_present_when_logged_in(self):

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

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

177self.assertTrue(’Logout’ in res.body)

178

179def test_logout_link_not_present_after_logged_out(self):

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

435

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