Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: tests/test_page_outputs.py

Issue 29912588: Issue 7019 - [CMS] Refactor `test_server.py` (Closed)
Patch Set: Updated exception tests. Removed duplicates from ignores Created Oct. 18, 2018, 4:09 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 import os 1 import os
2 import sys 2 import sys
3 import runpy 3 import runpy
4 4
5 import mock 5 import mock
6 import pytest 6 import pytest
7 import urllib2 7 import urllib2
8 8
9 from .conftest import ROOTPATH 9 from .conftest import ROOTPATH
10 from .utils import get_dir_contents, run_test_server 10 from .utils import get_dir_contents, run_test_server, exception_test
11 from cms.sources import FileSource 11 from cms.sources import FileSource
12 from cms.bin.test_server import DynamicServerHandler
12 13
13 14
14 def get_expected_outputs(test_type): 15 def get_expected_outputs(test_type):
15 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') 16 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output')
16 outputs = get_dir_contents(expected_out_path) 17 outputs = get_dir_contents(expected_out_path)
17 for filename in list(outputs): 18 for filename in list(outputs):
18 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") 19 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz")
19 # There are cases where we need to test outputs which differ depending 20 # There are cases where we need to test outputs which differ depending
20 # on how they are generated; either statically or dynamically 21 # on how they are generated; either statically or dynamically
21 if filename.endswith('@' + test_type): 22 if filename.endswith('@' + test_type):
(...skipping 11 matching lines...) Expand all
33 34
34 @pytest.fixture(scope='session') 35 @pytest.fixture(scope='session')
35 def static_output(request, temp_site): 36 def static_output(request, temp_site):
36 static_out_path = os.path.join(temp_site, 'static_out') 37 static_out_path = os.path.join(temp_site, 'static_out')
37 sys.argv = ['filler', temp_site, static_out_path] 38 sys.argv = ['filler', temp_site, static_out_path]
38 with mock.patch('cms.sources.FileSource.version', 1): 39 with mock.patch('cms.sources.FileSource.version', 1):
39 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') 40 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__')
40 return static_out_path 41 return static_out_path
41 42
42 43
43 @pytest.fixture(scope='module') 44 @pytest.fixture(scope='function')
44 def dynamic_server(temp_site): 45 def dynamic_server_werkzeug(temp_site):
45 with run_test_server(temp_site) as ts: 46 with run_test_server(str(temp_site)) as ts:
46 yield ts 47 yield ts
47 48
48 49
50 @pytest.fixture(scope='function')
51 def dynamic_server_builtins(temp_site, tmpdir):
52 werkzeug_dir = tmpdir.mkdir('werkzeug')
Vasily Kuznetsov 2018/10/22 14:36:48 Perhaps we can add a comment here explaining what
Tudor Avram 2018/10/23 16:44:35 Done.
53 werkzeug_dir.join('__init__.py').write('raise ImportError')
54 werkzeug_dir.join('serving.py').write('raise ImportError')
Vasily Kuznetsov 2018/10/22 14:36:47 It seems that this line is actually not necessary.
Tudor Avram 2018/10/23 16:44:35 Done.
55
56 new_env = dict(os.environ)
57 new_env['PYTHONPATH'] = os.pathsep.join([str(tmpdir),
58 os.getenv('PYTHONPATH', '')])
59
60 with run_test_server(str(temp_site), new_env) as ts:
61 yield ts
62
63
49 @pytest.fixture(scope='session') 64 @pytest.fixture(scope='session')
50 def output_pages(static_output): 65 def output_pages(static_output):
51 return get_dir_contents(static_output) 66 return get_dir_contents(static_output)
52 67
53 68
54 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) 69 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs)
55 def test_static(output_pages, filename, expected_output): 70 def test_static(output_pages, filename, expected_output):
56 if expected_output.startswith('## MISSING'): 71 if expected_output.startswith('## MISSING'):
57 assert filename not in output_pages 72 assert filename not in output_pages
58 else: 73 else:
59 assert expected_output == output_pages[filename] 74 assert expected_output == output_pages[filename]
60 75
61 76
62 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs)
63 def test_dynamic(dynamic_server, filename, expected_output):
64 response = urllib2.urlopen(dynamic_server + filename)
65 assert expected_output == response.read().strip()
66
67
68 def test_cache(output_pages): 77 def test_cache(output_pages):
69 source = FileSource(os.path.join('test_site')) 78 source = FileSource(os.path.join('test_site'))
70 assert source.get_cache_dir() == os.path.join('test_site', 'cache') 79 assert source.get_cache_dir() == os.path.join('test_site', 'cache')
80
81
82 @pytest.mark.slowtest
Vasily Kuznetsov 2018/10/22 14:36:48 It seems that now these four slowtests with relate
Tudor Avram 2018/10/23 16:44:34 Done.
83 def test_dynamic_werkzeug_good_page(dynamic_server_werkzeug):
84 filename, expected_output = dynamic_expected_outputs[0]
85 response = urllib2.urlopen(dynamic_server_werkzeug + filename)
86
87 assert expected_output in response.read().strip()
88
89
90 @pytest.mark.slowtest
91 def test_dynamic_werkzeug_not_found(dynamic_server_werkzeug):
92 filename = 'en/no-page-here'
93 exp_msg = 'Not Found'
94
95 exception_test(urllib2.urlopen, urllib2.HTTPError, exp_msg,
96 dynamic_server_werkzeug + filename)
97
98
99 @pytest.mark.slowtest
100 def test_dynamic_builtins_good_page(dynamic_server_builtins):
101 filename, expected_output = dynamic_expected_outputs[0]
102 response = urllib2.urlopen(dynamic_server_builtins + filename)
103
104 assert expected_output in response.read().strip()
105
106
107 @pytest.mark.slowtest
108 def test_dynamic_builtins_not_found(dynamic_server_builtins):
109 filename = 'en/no-page-here'
110 exp_msg = 'Not Found'
111
112 exception_test(urllib2.urlopen, urllib2.HTTPError, exp_msg,
113 dynamic_server_builtins + filename)
114
115
116 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs)
117 def test_dynamic_server_handler(filename, expected_output, temp_site):
118 def cleanup(page):
119 return page.replace(os.linesep, '').strip()
120
121 handler = DynamicServerHandler('localhost', 5000, str(temp_site))
122 environ = {'PATH_INFO': filename}
123
124 generated_page = handler(environ, lambda x, y: None)
125
126 assert cleanup(expected_output) == cleanup(generated_page[0])
127
128
129 @pytest.mark.parametrize('page', ['en/translate', '/en/translate'])
130 def test_dynamic_server_handler_with_conflicts(page, temp_site_with_conflicts):
131 handler = DynamicServerHandler('localhost', 5000,
132 str(temp_site_with_conflicts))
133 environ = {'PATH_INFO': page}
134 exp_msg = 'The requested page conflicts with another page.'
135
136 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None)
OLDNEW
« tests/conftest.py ('K') | « tests/conftest.py ('k') | tests/utils.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld