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: Created Oct. 16, 2018, 11:42 a.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 from threading import Thread
4 5
5 import mock 6 import mock
6 import pytest 7 import pytest
7 import urllib2 8 import urllib2
8 9
9 from .conftest import ROOTPATH 10 from .conftest import ROOTPATH
10 from .utils import get_dir_contents, run_test_server 11 from .utils import get_dir_contents, run_test_server
11 from cms.sources import FileSource 12 from cms.sources import FileSource
13 import cms.bin.test_server as test_server
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)
12 23
13 24
14 def get_expected_outputs(test_type): 25 def get_expected_outputs(test_type):
15 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') 26 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output')
16 outputs = get_dir_contents(expected_out_path) 27 outputs = get_dir_contents(expected_out_path)
17 for filename in list(outputs): 28 for filename in list(outputs):
18 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") 29 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz")
19 # There are cases where we need to test outputs which differ depending 30 # There are cases where we need to test outputs which differ depending
20 # on how they are generated; either statically or dynamically 31 # on how they are generated; either statically or dynamically
21 if filename.endswith('@' + test_type): 32 if filename.endswith('@' + test_type):
(...skipping 11 matching lines...) Expand all
33 44
34 @pytest.fixture(scope='session') 45 @pytest.fixture(scope='session')
35 def static_output(request, temp_site): 46 def static_output(request, temp_site):
36 static_out_path = os.path.join(temp_site, 'static_out') 47 static_out_path = os.path.join(temp_site, 'static_out')
37 sys.argv = ['filler', temp_site, static_out_path] 48 sys.argv = ['filler', temp_site, static_out_path]
38 with mock.patch('cms.sources.FileSource.version', 1): 49 with mock.patch('cms.sources.FileSource.version', 1):
39 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') 50 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__')
40 return static_out_path 51 return static_out_path
41 52
42 53
43 @pytest.fixture(scope='module') 54 @pytest.fixture(scope='session')
44 def dynamic_server(temp_site): 55 def dynamic_server(temp_site):
45 with run_test_server(temp_site) as ts: 56 with run_test_server(temp_site) as ts:
46 yield ts 57 yield ts
47 58
48 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
49 @pytest.fixture(scope='session') 83 @pytest.fixture(scope='session')
50 def output_pages(static_output): 84 def output_pages(static_output):
51 return get_dir_contents(static_output) 85 return get_dir_contents(static_output)
52 86
53 87
54 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) 88 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs)
55 def test_static(output_pages, filename, expected_output): 89 def test_static(output_pages, filename, expected_output):
56 if expected_output.startswith('## MISSING'): 90 if expected_output.startswith('## MISSING'):
57 assert filename not in output_pages 91 assert filename not in output_pages
58 else: 92 else:
59 assert expected_output == output_pages[filename] 93 assert expected_output == output_pages[filename]
60 94
61 95
62 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) 96 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs)
63 def test_dynamic(dynamic_server, filename, expected_output): 97 def test_dynamic(dynamic_server, filename, expected_output):
64 response = urllib2.urlopen(dynamic_server + filename) 98 response = urllib2.urlopen(dynamic_server + filename)
65 assert expected_output == response.read().strip() 99 assert expected_output == response.read().strip()
66 100
67 101
68 def test_cache(output_pages): 102 def test_cache(output_pages):
69 source = FileSource(os.path.join('test_site')) 103 source = FileSource(os.path.join('test_site'))
70 assert source.get_cache_dir() == os.path.join('test_site', 'cache') 104 assert source.get_cache_dir() == os.path.join('test_site', 'cache')
105
106
107 def test_dynamic_builtins(dynamic_server_failing_import):
108 page, expected_output = dynamic_expected_outputs[0]
109
110 response = urllib2.urlopen(dynamic_server_failing_import + page)
111
112 assert expected_output in response.read().strip()
OLDNEW

Powered by Google App Engine
This is Rietveld