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

Delta Between Two Patch Sets: tests/test_page_outputs.py

Issue 29912588: Issue 7019 - [CMS] Refactor `test_server.py` (Closed)
Left Patch Set: Created Oct. 16, 2018, 11:42 a.m.
Right Patch Set: Addressed docstring nit Created Oct. 29, 2018, 11 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « tests/test_dynamic_server.py ('k') | tests/utils.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import os 1 import os
2 import sys 2 import sys
3 import runpy 3 import runpy
4 from threading import Thread
5 4
6 import mock 5 import mock
7 import pytest 6 import pytest
8 import urllib2
9 7
10 from .conftest import ROOTPATH 8 from .conftest import ROOTPATH
11 from .utils import get_dir_contents, run_test_server 9 from .utils import get_dir_contents, exception_test
12 from cms.sources import FileSource 10 from cms.sources import FileSource
13 import cms.bin.test_server as test_server 11 from cms.bin.test_server import DynamicServerHandler
14
15
16 class ParametersTestServer:
17 def __init__(self):
18 pass
19 source = None
20 host = 'localhost'
21 port = 5001
22 base_url = 'http://{0}:{1}/'.format(host, port)
23 12
24 13
25 def get_expected_outputs(test_type): 14 def get_expected_outputs(test_type):
26 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') 15 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output')
27 outputs = get_dir_contents(expected_out_path) 16 outputs = get_dir_contents(expected_out_path)
28 for filename in list(outputs): 17 for filename in list(outputs):
29 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") 18 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz")
30 # 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
31 # on how they are generated; either statically or dynamically 20 # on how they are generated; either statically or dynamically
32 if filename.endswith('@' + test_type): 21 if filename.endswith('@' + test_type):
(...skipping 12 matching lines...) Expand all
45 @pytest.fixture(scope='session') 34 @pytest.fixture(scope='session')
46 def static_output(request, temp_site): 35 def static_output(request, temp_site):
47 static_out_path = os.path.join(temp_site, 'static_out') 36 static_out_path = os.path.join(temp_site, 'static_out')
48 sys.argv = ['filler', temp_site, static_out_path] 37 sys.argv = ['filler', temp_site, static_out_path]
49 with mock.patch('cms.sources.FileSource.version', 1): 38 with mock.patch('cms.sources.FileSource.version', 1):
50 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') 39 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__')
51 return static_out_path 40 return static_out_path
52 41
53 42
54 @pytest.fixture(scope='session') 43 @pytest.fixture(scope='session')
55 def dynamic_server(temp_site):
56 with run_test_server(temp_site) as ts:
57 yield ts
58
59
60 @pytest.fixture(scope='function')
61 def dynamic_server_failing_import(temp_site, monkeypatch):
Vasily Kuznetsov 2018/10/16 13:18:23 This fixture should probably just be called `dynam
Tudor Avram 2018/10/18 13:44:05 Done.
62
63 mock_params = ParametersTestServer()
64 mock_params.source = FileSource(str(temp_site))
65
66 test_server.Parameters = mock_params
67
68 run_fn = test_server.make_builtins_server()
69
70 thread = Thread(target=run_fn, args=(mock_params.host, mock_params.port,
71 test_server.handler),
72 kwargs={'use_reloader': True, 'use_debugger': True})
73 thread.daemon = True
74 thread.start()
75
76 yield mock_params.base_url
77
78 monkeypatch.setattr(test_server, 'get_data', lambda x: sys.exit())
79 with pytest.raises(urllib2.HTTPError):
80 urllib2.urlopen(mock_params.base_url + 'some_page')
Vasily Kuznetsov 2018/10/16 13:18:23 I think it would be better to join the thread here
Tudor Avram 2018/10/18 13:44:05 No longer relevant
81
82
83 @pytest.fixture(scope='session')
84 def output_pages(static_output): 44 def output_pages(static_output):
85 return get_dir_contents(static_output) 45 return get_dir_contents(static_output)
86 46
87 47
88 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) 48 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs)
89 def test_static(output_pages, filename, expected_output): 49 def test_static(output_pages, filename, expected_output):
90 if expected_output.startswith('## MISSING'): 50 if expected_output.startswith('## MISSING'):
91 assert filename not in output_pages 51 assert filename not in output_pages
92 else: 52 else:
93 assert expected_output == output_pages[filename] 53 assert expected_output == output_pages[filename]
94 54
95 55
96 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs)
97 def test_dynamic(dynamic_server, filename, expected_output):
98 response = urllib2.urlopen(dynamic_server + filename)
99 assert expected_output == response.read().strip()
100
101
102 def test_cache(output_pages): 56 def test_cache(output_pages):
103 source = FileSource(os.path.join('test_site')) 57 source = FileSource(os.path.join('test_site'))
104 assert source.get_cache_dir() == os.path.join('test_site', 'cache') 58 assert source.get_cache_dir() == os.path.join('test_site', 'cache')
105 59
106 60
107 def test_dynamic_builtins(dynamic_server_failing_import): 61 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs)
108 page, expected_output = dynamic_expected_outputs[0] 62 def test_dynamic_server_handler(filename, expected_output, temp_site):
63 def cleanup(page):
64 return page.replace(os.linesep, '').strip()
109 65
110 response = urllib2.urlopen(dynamic_server_failing_import + page) 66 handler = DynamicServerHandler('localhost', 5000, str(temp_site))
67 environ = {'PATH_INFO': filename}
111 68
112 assert expected_output in response.read().strip() 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)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld