2
2
3
3
import os .path as path
4
4
5
- from nose .tools import eq_ , assert_raises
5
+ from nose .tools import eq_ , assert_raises , ok_
6
6
from webtest .app import TestRequest
7
7
from webtest import lint # NOQA
8
8
import ckan .plugins as plugins
9
9
import ckan .tests .helpers as helpers
10
10
11
11
12
12
class TestFlaskStreaming (helpers .FunctionalTestBase ):
13
-
14
13
def _get_resp (self , url ):
15
14
req = TestRequest .blank (url )
16
15
app = lint .middleware (self .app .app )
@@ -25,46 +24,52 @@ def setup(self):
25
24
plugins .load (u'example_flask_streaming' )
26
25
plugin = plugins .get_plugin (u'example_flask_streaming' )
27
26
self .app .flask_app .register_extension_blueprint (
28
- plugin .get_blueprint ())
27
+ plugin .get_blueprint ()
28
+ )
29
29
30
30
def test_accordance_of_chunks (self ):
31
- u'''Test extension sets up a unique route .'''
32
- url = str (u'/stream/string' )
31
+ u'''Test streaming of items collection .'''
32
+ url = str (u'/stream/string' ) # produces list of words
33
33
resp = self ._get_resp (url )
34
34
eq_ (
35
35
u'Hello World, this is served from an extension' .split (),
36
- list (resp .app_iter ))
36
+ list (resp .app_iter )
37
+ )
37
38
resp .app_iter .close ()
38
39
39
40
def test_template_streaming (self ):
40
- u'''Test extension sets up a unique route.'''
41
- url = str (u'/stream/template' )
42
- resp = self ._get_resp (url )
43
- eq_ (1 , len (list (resp .app_iter )))
44
-
45
- url = str (u'/stream/template/7' )
41
+ u'''Test streaming of template response.'''
42
+ bound = 7
43
+ url = str (u'/stream/template/{}' .format (bound )) # produces nums list
46
44
resp = self ._get_resp (url )
47
- eq_ (2 , len (list (resp .app_iter )))
45
+ content = u'' .join (resp .app_iter )
46
+ for i in range (bound ):
47
+ ok_ (str (i ) in content )
48
48
resp ._app_iter .close ()
49
49
50
50
def test_file_streaming (self ):
51
- u'''Test extension sets up a unique route .'''
52
- url = str (u'/stream/file' )
51
+ u'''Test streaming of existing file(10lines.txt) .'''
52
+ url = str (u'/stream/file' ) # streams file
53
53
resp = self ._get_resp (url )
54
- f_path = path .join (path .dirname (path .abspath (__file__ )), u'10lines.txt' )
54
+ f_path = path .join (
55
+ path .dirname (path .abspath (__file__ )), u'10lines.txt'
56
+ )
55
57
with open (f_path ) as test_file :
56
58
content = test_file .readlines ()
57
59
eq_ (content , list (resp .app_iter ))
58
60
resp ._app_iter .close ()
59
61
60
62
def test_render_with_context (self ):
61
- u'''Test extension sets up a unique route .'''
62
- url = str (u'/stream/context?var=10' )
63
+ u'''Test availability of context inside templates .'''
64
+ url = str (u'/stream/context?var=10' ) # produces `var` value
63
65
resp = self ._get_resp (url )
64
66
eq_ (u'10' , resp .body )
65
67
66
68
def test_render_without_context (self ):
67
- u'''Test extension sets up a unique route.'''
69
+ u'''
70
+ Test that error raised if there is an
71
+ attempt to pick variable if context is not provider.
72
+ '''
68
73
url = str (u'/stream/without_context?var=10' )
69
74
resp = self ._get_resp (url )
70
75
assert_raises (AttributeError , u'' .join , resp .app_iter )
0 commit comments