Index: cms/tests/test_page_outputs.py |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/cms/tests/test_page_outputs.py |
@@ -0,0 +1,80 @@ |
+import os |
+import sys |
+import shutil |
+import time |
+import runpy |
+import pytest |
+import urllib2 |
+import subprocess |
+ |
+ROOTPATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
+ |
+ |
+def get_expected_outputs(): |
+ return_data = {} |
+ expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') |
+ for (dirpath, dirnames, filenames) in os.walk(expected_out_path): |
Vasily Kuznetsov
2016/07/12 15:25:11
You actually don't need the parentheses around `di
|
+ for output_file in filenames: |
+ with open('{}/{}'.format(dirpath, output_file)) as f: |
Vasily Kuznetsov
2016/07/12 15:25:11
Here it's also better to use `os.path.join`.
|
+ return_data[output_file] = f.read() |
+ return return_data |
+ |
+expected_outputs = get_expected_outputs() |
+ |
+ |
+@pytest.fixture(scope='session') |
+def temp_site(tmpdir_factory): |
+ site_dir = tmpdir_factory.mktemp('temp_out') |
Vasily Kuznetsov
2016/07/12 15:25:11
Maybe better call this `out_dir`. Otherwise `site_
|
+ |
+ site_dir = site_dir.join('test_site').strpath |
+ shutil.copytree(os.path.join(ROOTPATH, 'tests', 'test_site'), site_dir) |
+ subprocess.check_call(['hg', 'init', site_dir]) |
+ subprocess.check_call( |
Vasily Kuznetsov
2016/07/12 15:25:10
This call seems to fit on one line, so there's no
|
+ [ |
+ 'hg', '-R', site_dir, |
+ 'commit', '-A', '-m', 'foo', |
+ ] |
+ ) |
+ return site_dir |
+ |
+ |
+@pytest.fixture(scope='session') |
+def static_output(request, temp_site): |
+ static_out_path = os.path.join(temp_site, 'static_out') |
+ sys.argv = ['filler', temp_site, static_out_path] |
+ |
+ runpy.run_module( |
Vasily Kuznetsov
2016/07/12 15:25:12
This one also fits on one line.
|
+ 'cms.bin.generate_static_pages', run_name='__main__' |
+ ) |
+ return static_out_path |
+ |
+ |
+@pytest.fixture(scope='session') |
Vasily Kuznetsov
2016/07/12 15:25:12
It seems that with current default version of pyte
Jon Sonesen
2016/07/18 14:48:27
It works but I believe I am still using the pytest
|
+def dynamic_server(temp_site): |
+ p = subprocess.Popen( |
Vasily Kuznetsov
2016/07/12 15:25:11
This also fits on one line.
|
+ ['python', 'runserver.py', temp_site] |
+ ) |
+ time.sleep(0.5) |
+ yield 'http://localhost:5000/root/' |
+ p.terminate() |
+ |
+ |
+@pytest.fixture(scope='session') |
+def output_pages(static_output): |
+ return_data = {} |
+ for (dirpath, dirnames, filenames) in os.walk(static_output): |
Vasily Kuznetsov
2016/07/12 15:25:12
This code seems to be duplicated from `get_expecte
|
+ for output_file in filenames: |
+ with open('{}/{}'.format(dirpath, output_file)) as f: |
+ return_data[output_file] = f.read() |
+ return return_data |
+ |
+ |
+@pytest.mark.parametrize('filename,expected_output', expected_outputs.items()) |
Vasily Kuznetsov
2016/07/12 15:25:10
It seems that we're not using `expected_output` as
|
+def test_static(output_pages, filename, expected_output): |
+ assert output_pages[filename] == expected_output |
+ |
+ |
+@pytest.mark.parametrize('filename,expected_output', expected_outputs.items()) |
+def test_dynamic(dynamic_server, filename, expected_output): |
+ response = urllib2.urlopen(dynamic_server + filename) |
+ assert response.read() == expected_output |