def test_report_views(self, mock_reportbuild_run): with self.app.test_request_context(): # test main page and assert we have the button for the ZE 2012 report: app = self.app.test_client() res = app.get("/") m = re.compile(r">ZE_2012\s*</a>", re.DOTALL | re.MULTILINE).search(res.data) assert m with self.app.test_request_context(): app = self.app.test_client() # If we add routes with the slash at the end, we should # add follow_redirect=True to app.get. See # http://flask.pocoo.org/docs/0.11/quickstart/#routing res = app.get("/ZE_2012", follow_redirects=True) # few stupid asserts, the main test is not raising # we should ahve an html page: assert "<html lang=\"en\">" in res.data assert "ng-app=\"MyApp\" ng-controller=\"MyController\"" in res.data j =9 # now test the page content with self.app.test_request_context(): app = self.app.test_client() # If we add routes with the slash at the end, we should # add follow_redirect=True to app.get. See # http://flask.pocoo.org/docs/0.11/quickstart/#routing res = app.get("/ZE_2012/html", follow_redirects=True) # few stupid asserts, the main test is not raising # we should ahve an html page: assert mock_reportbuild_run.call_count == 1 logfile = os.path.join(os.getcwd(), 'build', 'ZE_2012', 'html', get_logfilename()) with open(logfile, 'r') as opn: logfilecontent = opn.read() assert len(logfilecontent) > 0 and "Build successful" in logfilecontent # now put a non-found image: mock_reportbuild_run.reset_mock() self.modifyrst("", ".. figure:: whatever_data_not_found_blabla.png") res = app.get("/ZE_2012/html", follow_redirects=True) # few stupid asserts, the main test is not raising # we should ahve an html page: assert mock_reportbuild_run.call_count == 1 logfile = os.path.join(os.getcwd(), 'build', 'ZE_2012', 'html', get_logfilename()) with open(logfile, 'r') as opn: logfilecontent = opn.read() assert len(logfilecontent) > 0 and ": WARNING: image" in logfilecontent mock_reportbuild_run.reset_mock() # now test the page content is not re-built: with self.app.test_request_context(): app = self.app.test_client() # If we add routes with the slash at the end, we should # add follow_redirect=True to app.get. See # http://flask.pocoo.org/docs/0.11/quickstart/#routing res = app.get("/ZE_2012/html") # few stupid asserts, the main test is not raising # we should ahve an html page: assert mock_reportbuild_run.call_count == 0
def test_report_views_build_failed_sphinxerr(self): '''test when sphinx_build raises die to rst syntax error in the source''' # mock a syntax error. Apparently, misplacing alignment does so: source_rst = os.path.join(self.source, 'source', 'ZE_2012', 'report.rst') with open(source_rst, 'r') as opn: rst_text = opn.read() _tmp_rst_text = rst_text.replace(" :align: center", "\n:align: center") assert _tmp_rst_text != rst_text # write to file with open(os.path.join(source_rst), "w") as _opn: _opn.write(_tmp_rst_text) # test the page pdf. We are unauthorized, so this should give us error: with self.app.test_request_context(): app = self.app.test_client() # login: # now try to login: # with a registered email and good write permission res = app.post("/ZE_2012/login", data={'email' :'*****@*****.**'}) assert res.status_code == 200 # thus, we DO access the pdf creation: res = app.get("/ZE_2012/pdf", follow_redirects=True) # status code is still 200, as we redirect to the error page assert res.status_code == 200 # raw check: the title of the error page. If we change it in the future, fix this test accordingly: assert "Build failed" in res.data # if we try to get the log: logfile = os.path.join(self.get_buildir('pdf'), get_logfilename()) # in case of sphinx exception, we do not have a build dir created. # Still to FIXME: is that a problem for the web app? assert os.path.isfile(logfile) with open(logfile) as opn: content = opn.read() # this has to be changhed if we provide some way to normalize the message.For the # moment: assert "Exception occurred" in content # check logs: res = app.post("/ZE_2012/get_logs", data=json.dumps({'buildtype': 'pdf'}), content_type='application/json') assert res.status_code == 200 _data = json.loads(res.data) # _data has two elements: the standard error (plus some titles added, such as # 'Sphinx build') and the standard error showing only relevant errors/warnings: these # are lines recognizable by a special format (using regexps) and that are more # informative for the web user than the all standard error, which might be too verbose. # (The the standard error showing only relevant errors/warnings can be shown by means # of a checkobx on the GUI) assert len(_data) == 2 assert "Exception occurred" in _data[0] # as the sphinx exceptions are not formatted as sphinx errors, we should have this # in the short message: assert "No compilation error found" in _data[1]
def test_report_views_build_failed_sphinxerr2(self, mock_sphinxbuild): '''test when sphinx_build raises. This should never be the case as sphinx prints exception to stderr, but we can check other stuff, e.g. that get_logs returns 'file not found' string''' # test the page pdf. We are unauthorized, so this should give us error: with self.app.test_request_context(): app = self.app.test_client() # login: # now try to login: # with a registered email and good write permission res = app.post("/ZE_2012/login", data={'email': '*****@*****.**'}) assert res.status_code == 200 # thus, we DO access the pdf creation: res = app.get("/ZE_2012/pdf", follow_redirects=True) # status code is still 200, as we redirect to the error page assert res.status_code == 200 # raw check: the title of the error page. If we change it in the future, fix this test accordingly: assert "Build failed" in res.data # if we try to get the log: logfile = os.path.join(self.get_buildir('pdf'), get_logfilename()) # in case of sphinx exception, we do not have a build dir created. # Still to FIXME: is that a problem for the web app? assert not os.path.isfile(logfile) # check logs: res = app.post("/ZE_2012/get_logs", data=json.dumps({'buildtype': 'pdf'}), content_type='application/json') assert res.status_code == 200 _data = json.loads(res.data) # _data has two elements: the standard error (plus some titles added, such as # 'Sphinx build') and the standard error showing only relevant errors/warnings: these # are lines recognizable by a special format (using regexps) and that are more # informative for the web user than the all standard error, which might be too verbose. # (The the standard error showing only relevant errors/warnings can be shown by means # of a checkobx on the GUI) assert len(_data) == 2 assert "Log file not found" in _data[0] assert "Log file not found" in _data[1]
def test_report_views_build_failed_sphinxerr2(self, mock_sphinxbuild): '''test when sphinx_build raises. This should never be the case as sphinx prints exception to stderr, but we can check other stuff, e.g. that get_logs returns 'file not found' string''' # test the page pdf. We are unauthorized, so this should give us error: with self.app.test_request_context(): app = self.app.test_client() # login: # now try to login: # with a registered email and good write permission res = app.post("/ZE_2012/login", data={'email' :'*****@*****.**'}) assert res.status_code == 200 # thus, we DO access the pdf creation: res = app.get("/ZE_2012/pdf", follow_redirects=True) # status code is still 200, as we redirect to the error page assert res.status_code == 200 # raw check: the title of the error page. If we change it in the future, fix this test accordingly: assert "Build failed" in res.data # if we try to get the log: logfile = os.path.join(self.get_buildir('pdf'), get_logfilename()) # in case of sphinx exception, we do not have a build dir created. # Still to FIXME: is that a problem for the web app? assert not os.path.isfile(logfile) # check logs: res = app.post("/ZE_2012/get_logs", data=json.dumps({'buildtype': 'pdf'}), content_type='application/json') assert res.status_code == 200 _data = json.loads(res.data) # _data has two elements: the standard error (plus some titles added, such as # 'Sphinx build') and the standard error showing only relevant errors/warnings: these # are lines recognizable by a special format (using regexps) and that are more # informative for the web user than the all standard error, which might be too verbose. # (The the standard error showing only relevant errors/warnings can be shown by means # of a checkobx on the GUI) assert len(_data) == 2 assert "Log file not found" in _data[0] assert "Log file not found" in _data[1]
def test_report_views_build_failed_pdflatex(self, mock_pdflatex, mock_reportbuild_run): # test the page pdf. We are unauthorized, so this should give us error: with self.app.test_request_context(): app = self.app.test_client() # login: # now try to login: # with a registered email and good write permission res = app.post("/ZE_2012/login", data={'email': '*****@*****.**'}) assert res.status_code == 200 # thus, we DO access the pdf creation: # but w need to setup urlread for the arcgis image, because we mocked it # (FIXME: we mocked in gfzreport.templates.network.core.utils.urllib2.urlopen, # why is it mocked in map module?!!!) # The signature is: # _get_urlopen_sideeffect(geofon_retval=None, others_retval=None): # Thus, we setup others_retval=URLError, which means that if 'geofon' is not in url # (which is the case for arcgis query) and URLException is raised. # This way, the map is generated with drawcostallines # and the pdf is created. Keep in mind that pdflatex will raise in any case self.mock_urlopen.side_effect = _get_urlopen_sideeffect( None, URLError('wat')) res = app.get("/ZE_2012/pdf", follow_redirects=True) # status code is still 200, as we redirect to the error page assert res.status_code == 200 # raw check: the title of the error page. If we change it in the future, fix this test accordingly: assert "Build failed" in res.data # if we try to get the log: logfile = os.path.join(self.get_buildir('pdf'), get_logfilename()) with open(logfile) as fopen: logcontent = fopen.read() assert "!wow!" in logcontent # the exception message (see mock above) # but if we get the commits, we registered that pdflatex was wrong: res = app.post("/ZE_2012/get_commits", content_type='application/json') assert res.status_code == 200 commitz = json.loads(res.data) commit = commitz[0] assert commit['email'] == '*****@*****.**' assert ("pdf: %s" % exitstatus2str(2)) in commit['notes'] res1 = app.post("/ZE_2012/last_build_exitcode", data=json.dumps({'buildtype': "html"}), content_type='application/json') res2 = app.post("/ZE_2012/last_build_exitcode", data=json.dumps({'buildtype': "pdf"}), content_type='application/json') assert res1.status_code == 200 and res2.status_code == 200 assert int(res1.data) == -1 assert int(res2.data) == 2 # assert we do not have a html file: htmlfile = os.path.join(self.get_buildir('html'), 'report.html') assert not os.path.isfile(htmlfile) # compile html res = app.get("/ZE_2012/html", follow_redirects=True) # status code is still 200, as we redirect to the error page assert res.status_code == 200 # re-query the exit code: res1 = app.post("/ZE_2012/last_build_exitcode", data=json.dumps({'buildtype': "html"}), content_type='application/json') assert res.status_code == 200 assert int(res1.data) == 0 assert os.path.isfile(htmlfile)
def test_report_views_build_failed_sphinxerr(self): '''test when sphinx_build raises die to rst syntax error in the source''' # mock a syntax error. Apparently, misplacing alignment does so: source_rst = os.path.join(self.source, 'source', 'ZE_2012', 'report.rst') with open(source_rst, 'r') as opn: rst_text = opn.read() _tmp_rst_text = rst_text.replace(" :align: center", "\n:align: center") assert _tmp_rst_text != rst_text # write to file with open(os.path.join(source_rst), "w") as _opn: _opn.write(_tmp_rst_text) # test the page pdf. We are unauthorized, so this should give us error: with self.app.test_request_context(): app = self.app.test_client() # login: # now try to login: # with a registered email and good write permission res = app.post("/ZE_2012/login", data={'email': '*****@*****.**'}) assert res.status_code == 200 # thus, we DO access the pdf creation: res = app.get("/ZE_2012/pdf", follow_redirects=True) # status code is still 200, as we redirect to the error page assert res.status_code == 200 # raw check: the title of the error page. If we change it in the future, fix this test accordingly: assert "Build failed" in res.data # if we try to get the log: logfile = os.path.join(self.get_buildir('pdf'), get_logfilename()) # in case of sphinx exception, we do not have a build dir created. # Still to FIXME: is that a problem for the web app? assert os.path.isfile(logfile) with open(logfile) as opn: content = opn.read() # this has to be changhed if we provide some way to normalize the message.For the # moment: assert "Exception occurred" in content # check logs: res = app.post("/ZE_2012/get_logs", data=json.dumps({'buildtype': 'pdf'}), content_type='application/json') assert res.status_code == 200 _data = json.loads(res.data) # _data has two elements: the standard error (plus some titles added, such as # 'Sphinx build') and the standard error showing only relevant errors/warnings: these # are lines recognizable by a special format (using regexps) and that are more # informative for the web user than the all standard error, which might be too verbose. # (The the standard error showing only relevant errors/warnings can be shown by means # of a checkobx on the GUI) assert len(_data) == 2 assert "Exception occurred" in _data[0] # as the sphinx exceptions are not formatted as sphinx errors, we should have this # in the short message: assert "No compilation error found" in _data[1]
def test_report_views(self, mock_reportbuild_run): with self.app.test_request_context(): # test main page and assert we have the button for the ZE 2012 report: app = self.app.test_client() res = app.get("/") m = re.compile(r">ZE_2012\s*</a>", re.DOTALL | re.MULTILINE).search(res.data) assert m with self.app.test_request_context(): app = self.app.test_client() # If we add routes with the slash at the end, we should # add follow_redirect=True to app.get. See # http://flask.pocoo.org/docs/0.11/quickstart/#routing res = app.get("/ZE_2012", follow_redirects=True) # few stupid asserts, the main test is not raising # we should ahve an html page: assert "<html lang=\"en\">" in res.data assert "ng-app=\"MyApp\" ng-controller=\"MyController\"" in res.data j = 9 # now test the page content with self.app.test_request_context(): app = self.app.test_client() # If we add routes with the slash at the end, we should # add follow_redirect=True to app.get. See # http://flask.pocoo.org/docs/0.11/quickstart/#routing res = app.get("/ZE_2012/html", follow_redirects=True) # few stupid asserts, the main test is not raising # we should ahve an html page: assert mock_reportbuild_run.call_count == 1 logfile = os.path.join(os.getcwd(), 'build', 'ZE_2012', 'html', get_logfilename()) with open(logfile, 'r') as opn: logfilecontent = opn.read() assert len( logfilecontent) > 0 and "Build successful" in logfilecontent # now put a non-found image: mock_reportbuild_run.reset_mock() self.modifyrst("", ".. figure:: whatever_data_not_found_blabla.png") res = app.get("/ZE_2012/html", follow_redirects=True) # few stupid asserts, the main test is not raising # we should ahve an html page: assert mock_reportbuild_run.call_count == 1 logfile = os.path.join(os.getcwd(), 'build', 'ZE_2012', 'html', get_logfilename()) with open(logfile, 'r') as opn: logfilecontent = opn.read() assert len( logfilecontent) > 0 and ": WARNING: image" in logfilecontent mock_reportbuild_run.reset_mock() # now test the page content is not re-built: with self.app.test_request_context(): app = self.app.test_client() # If we add routes with the slash at the end, we should # add follow_redirect=True to app.get. See # http://flask.pocoo.org/docs/0.11/quickstart/#routing res = app.get("/ZE_2012/html") # few stupid asserts, the main test is not raising # we should ahve an html page: assert mock_reportbuild_run.call_count == 0
def test_report_views_build_failed_pdflatex(self, mock_pdflatex, mock_reportbuild_run): # test the page pdf. We are unauthorized, so this should give us error: with self.app.test_request_context(): app = self.app.test_client() # login: # now try to login: # with a registered email and good write permission res = app.post("/ZE_2012/login", data={'email' :'*****@*****.**'}) assert res.status_code == 200 # thus, we DO access the pdf creation: # but w need to setup urlread for the arcgis image, because we mocked it # (FIXME: we mocked in gfzreport.templates.network.core.utils.urllib2.urlopen, # why is it mocked in map module?!!!) # The signature is: # _get_urlopen_sideeffect(geofon_retval=None, others_retval=None): # Thus, we setup others_retval=URLError, which means that if 'geofon' is not in url # (which is the case for arcgis query) and URLException is raised. # This way, the map is generated with drawcostallines # and the pdf is created. Keep in mind that pdflatex will raise in any case self.mock_urlopen.side_effect = _get_urlopen_sideeffect(None, URLError('wat')) res = app.get("/ZE_2012/pdf", follow_redirects=True) # status code is still 200, as we redirect to the error page assert res.status_code == 200 # raw check: the title of the error page. If we change it in the future, fix this test accordingly: assert "Build failed" in res.data # if we try to get the log: logfile = os.path.join(self.get_buildir('pdf'), get_logfilename()) with open(logfile) as fopen: logcontent = fopen.read() assert "!wow!" in logcontent # the exception message (see mock above) # but if we get the commits, we registered that pdflatex was wrong: res = app.post("/ZE_2012/get_commits", content_type='application/json') assert res.status_code == 200 commitz = json.loads(res.data) commit = commitz[0] assert commit['email'] == '*****@*****.**' assert ("pdf: %s" % exitstatus2str(2)) in commit['notes'] res1 = app.post("/ZE_2012/last_build_exitcode", data=json.dumps({'buildtype': "html"}), content_type='application/json') res2 = app.post("/ZE_2012/last_build_exitcode", data=json.dumps({'buildtype': "pdf"}), content_type='application/json') assert res1.status_code == 200 and res2.status_code == 200 assert int(res1.data) == -1 assert int(res2.data) == 2 # assert we do not have a html file: htmlfile = os.path.join(self.get_buildir('html'), 'report.html') assert not os.path.isfile(htmlfile) # compile html res = app.get("/ZE_2012/html", follow_redirects=True) # status code is still 200, as we redirect to the error page assert res.status_code == 200 # re-query the exit code: res1 = app.post("/ZE_2012/last_build_exitcode", data=json.dumps({'buildtype': "html"}), content_type='application/json') assert res.status_code == 200 assert int(res1.data) == 0 assert os.path.isfile(htmlfile)
def get_logfile(app, reportdirname, buildtype): return os.path.join(get_builddir(app, reportdirname, buildtype), get_logfilename())
def test_netgen_ok_sphinxbuild_retval(mock_urlopen, mock_get_dcs): # set args, with wildcards # mock urllib returns our testdata files setupurlread(mock_urlopen) args = ['-n', 'ZE', '-s', '2012', "--noprompt", "-i", "inst_uptimes/*", "-p", "noise_pdf/sta1*"] with invoke(*args) as _: result, outpath, args = _ # Now re-set our mock library to return an exception (the mock_url # is intended to distinguish if 'geofon' is in the url or not, provide # an exception for both cases to be sure) # Our map module will handle silently the error by returning a map # with coastal lines drawn setupurlread(mock_urlopen, URLError('wat?'), URLError('wat?')) # and re-run: runner = CliRunner() RSTDIR = os.path.join(outpath, 'ZE_2012') # modify the rst with an error, and see the return exit_status: with open(os.path.join(RSTDIR, "report.rst"), "r") as _opn: rst_text = _opn.read() # TRY TO TYPO DIRECTIVE: NO ERROR # change a directive to something it does not exist (mock typo) _tmp_rst_text = rst_text.replace(".. gridfigure::", ".. bridfigure::") assert _tmp_rst_text != rst_text # write to file with open(os.path.join(RSTDIR, "report.rst"), "w") as _opn: _opn.write(_tmp_rst_text) # run sphinx and see the output code: # with runner.isolated_filesystem(): btype = 'html' printl("Testing rst typo", btype) args_ = [RSTDIR, os.path.join(outpath, "build"), "-b", btype] result = runner.invoke(gfzreport_main, BUILD + args_, catch_exceptions=False) with open(os.path.join(args_[1], get_logfilename()), "r") as _opn: _log_out = _opn.read() # SPHINX IS OK WITH UNKNOWN DIRECTIVE TYPES, SHIT! assert 'Unknown directive type "bridfigure"' in _log_out assert result.exit_code == 1 print(result.output) # TRY TO MISALIGN A INDENTATION # change a directive to something it does not exist (mock typo) _tmp_rst_text = rst_text.replace(" :align: center", "\n:align: center") assert _tmp_rst_text != rst_text # write to file with open(os.path.join(RSTDIR, "report.rst"), "w") as _opn: _opn.write(_tmp_rst_text) # run sphinx and see the output code: # with runner.isolated_filesystem(): btype = 'html' printl("Testing mis-alignement of :align:", btype) args_ = [RSTDIR, os.path.join(outpath, "build"), "-b", btype] result = runner.invoke(gfzreport_main, BUILD + args_, catch_exceptions=False) with open(os.path.join(args_[1], get_logfilename()), "r") as _opn: _log_out = _opn.read() assert result.exit_code == 2 assert 'Exception occurred:' in _log_out print(result.output) # check if we deleted the temop dir: assert not os.path.isdir(outpath)