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: Added test_and_wait to test server fixture Created Oct. 25, 2018, 12:22 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
8 7
9 from .conftest import ROOTPATH 8 from .conftest import ROOTPATH
10 from .utils import get_dir_contents, run_test_server 9 from .utils import get_dir_contents, exception_test
11 from cms.sources import FileSource 10 from cms.sources import FileSource
11 from cms.bin.test_server import DynamicServerHandler
12 12
13 13
14 def get_expected_outputs(test_type): 14 def get_expected_outputs(test_type):
15 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') 15 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output')
16 outputs = get_dir_contents(expected_out_path) 16 outputs = get_dir_contents(expected_out_path)
17 for filename in list(outputs): 17 for filename in list(outputs):
18 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") 18 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz")
19 # There are cases where we need to test outputs which differ depending 19 # There are cases where we need to test outputs which differ depending
20 # on how they are generated; either statically or dynamically 20 # on how they are generated; either statically or dynamically
21 if filename.endswith('@' + test_type): 21 if filename.endswith('@' + test_type):
(...skipping 11 matching lines...) Expand all
33 33
34 @pytest.fixture(scope='session') 34 @pytest.fixture(scope='session')
35 def static_output(request, temp_site): 35 def static_output(request, temp_site):
36 static_out_path = os.path.join(temp_site, 'static_out') 36 static_out_path = os.path.join(temp_site, 'static_out')
37 sys.argv = ['filler', temp_site, static_out_path] 37 sys.argv = ['filler', temp_site, static_out_path]
38 with mock.patch('cms.sources.FileSource.version', 1): 38 with mock.patch('cms.sources.FileSource.version', 1):
39 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') 39 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__')
40 return static_out_path 40 return static_out_path
41 41
42 42
43 @pytest.fixture(scope='module')
44 def dynamic_server(temp_site):
45 with run_test_server(temp_site) as ts:
46 yield ts
47
48
49 @pytest.fixture(scope='session') 43 @pytest.fixture(scope='session')
50 def output_pages(static_output): 44 def output_pages(static_output):
51 return get_dir_contents(static_output) 45 return get_dir_contents(static_output)
52 46
53 47
54 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) 48 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs)
55 def test_static(output_pages, filename, expected_output): 49 def test_static(output_pages, filename, expected_output):
56 if expected_output.startswith('## MISSING'): 50 if expected_output.startswith('## MISSING'):
57 assert filename not in output_pages 51 assert filename not in output_pages
58 else: 52 else:
59 assert expected_output == output_pages[filename] 53 assert expected_output == output_pages[filename]
60 54
61 55
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): 56 def test_cache(output_pages):
69 source = FileSource(os.path.join('test_site')) 57 source = FileSource(os.path.join('test_site'))
70 assert source.get_cache_dir() == os.path.join('test_site', 'cache') 58 assert source.get_cache_dir() == os.path.join('test_site', 'cache')
59
60
61 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs)
62 def test_dynamic_server_handler(filename, expected_output, temp_site):
63 def cleanup(page):
64 return page.replace(os.linesep, '').strip()
65
66 handler = DynamicServerHandler('localhost', 5000, str(temp_site))
67 environ = {'PATH_INFO': filename}
68
69 generated_page = handler(environ, lambda x, y: None)
70
71 assert cleanup(expected_output) == cleanup(generated_page[0])
72
73
74 @pytest.mark.parametrize('page', ['en/translate', '/en/translate'])
75 def test_dynamic_server_handler_with_conflicts(page, temp_site_with_conflicts):
76 handler = DynamicServerHandler('localhost', 5000,
77 str(temp_site_with_conflicts))
78 environ = {'PATH_INFO': page}
79 exp_msg = 'The requested page conflicts with another page.'
80
81 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None)
OLDNEW
« no previous file with comments | « tests/test_dynamic_server.py ('k') | tests/utils.py » ('j') | tests/utils.py » ('J')

Powered by Google App Engine
This is Rietveld