diff --git a/integraality/templates/queries.html b/integraality/templates/queries.html index 6347f75..651de55 100644 --- a/integraality/templates/queries.html +++ b/integraality/templates/queries.html @@ -1,16 +1,28 @@ {% extends "base.html" %} {% block content %}

From page {{ page_title }}, {{ column.format_html_snippet() | safe }}, {% if grouping == 'None' -%} without {{ formatted_predicate | safe }} grouping {%- elif grouping == 'UNKNOWN_VALUE' -%} with unknown value as {{ formatted_predicate | safe }} {%- elif grouping -%} with {{ grouping }} as {{ formatted_predicate | safe }} {%- else -%} for the totals {%- endif %}.

- All items with the {{ column.get_type_name() }} set - All items without the {{ column.get_type_name() }} set +
+ + +
{% endblock %} diff --git a/integraality/tests/test_app.py b/integraality/tests/test_app.py index 30cc97b..7f9b7ab 100644 --- a/integraality/tests/test_app.py +++ b/integraality/tests/test_app.py @@ -1,371 +1,419 @@ # -*- coding: utf-8 -*- import unittest from unittest.mock import patch import column from app import app from pages_processor import ProcessingException from sparql_utils import QueryException class AppTests(unittest.TestCase): def setUp(self): app.config["TESTING"] = True self.app = app.test_client() class BasicTests(AppTests): def test_index_page(self): response = self.app.get("/") self.assertEqual(response.status_code, 200) self.assertIn("

InteGraality

", response.get_data(as_text=True)) def test_404_page(self): response = self.app.get("/unexisting_page") self.assertEqual(response.status_code, 404) self.assertIn("This page does not exist.", response.get_data(as_text=True)) class PagesProcessorTests(AppTests): def setUp(self): super().setUp() patcher = patch("app.PagesProcessor", autospec=True) self.mock_pages_processor = patcher.start() self.addCleanup(patcher.stop) self.page_title = "Foo" self.page_url = "https://wikidata.org/wiki/%s" % self.page_title self.linked_page = '%s' % (self.page_url, self.page_title) def assertSuccessPage(self, response, message): """A custom assertion for a success page.""" self.assertEqual(response.status_code, 200) contents = response.get_data(as_text=True) self.assertIn("alert-success", contents) self.assertPresent(message, contents) def assertErrorPage(self, response, message): """A custom assertion for an error page.""" self.assertEqual(response.status_code, 200) contents = response.get_data(as_text=True) self.assertIn("alert-danger", contents) self.assertPresent(message, contents) def assertPresent(self, message, response): self.assertIn( message.replace(" ", "").replace("\t", "").replace("\n", ""), response.replace(" ", "").replace("\t", "").replace("\n", ""), ) class UpdateTests(PagesProcessorTests): def test_update_success(self): response = self.app.get( "/update?page=%s&url=%s" % (self.page_title, self.page_url) ) self.mock_pages_processor.assert_called_once_with(self.page_url) self.mock_pages_processor.return_value.process_one_page.assert_called_once_with( page_title=self.page_title ) message = "Updated page {page}".format(page=self.linked_page) self.assertSuccessPage(response, message) def test_update_error_processing_exception(self): self.mock_pages_processor.return_value.process_one_page.side_effect = ( ProcessingException ) response = self.app.get( "/update?page=%s&url=%s" % (self.page_title, self.page_url) ) self.mock_pages_processor.assert_called_once_with(self.page_url) self.mock_pages_processor.return_value.process_one_page.assert_called_once_with( page_title=self.page_title ) message = "

Something went wrong when updating page {page}. Please check your configuration.

".format( page=self.linked_page ) # noqa self.assertErrorPage(response, message) def test_update_error_unknown_exception(self): self.mock_pages_processor.return_value.process_one_page.side_effect = ValueError response = self.app.get( "/update?page=%s&url=%s" % (self.page_title, self.page_url) ) self.mock_pages_processor.assert_called_once_with(self.page_url) self.mock_pages_processor.return_value.process_one_page.assert_called_once_with( page_title=self.page_title ) message = "

Something catastrophic happened when processing page {page}.

".format( page=self.linked_page ) self.assertErrorPage(response, message) def test_update_error_query_exception(self): self.mock_pages_processor.return_value.process_one_page.side_effect = ( QueryException("Error", "SELECT X") ) response = self.app.get( "/update?page=%s&url=%s" % (self.page_title, self.page_url) ) self.mock_pages_processor.assert_called_once_with(self.page_url) self.mock_pages_processor.return_value.process_one_page.assert_called_once_with( page_title=self.page_title ) expected = ( '

Something went wrong when updating page Foo.

\n' "

The following SPARQL query timed out or returned no result:

\n" "
SELECT X
\n" ) self.assertErrorPage(response, expected) buttons = ( 'Try it in Wikidata Query Service' 'P495' ) self.mock_grouping_configuration.property = "P495" patcher1 = patch("pages_processor.PropertyStatistics", autospec=True) self.mock_property_statistics = patcher1.start() self.mock_property_statistics.grouping_configuration = ( self.mock_grouping_configuration ) self.mock_property_statistics.columns = { "P1": self.column_P1, "Lbr": self.column_Lbr, "Dbr": self.column_Dbr, } self.addCleanup(patcher1.stop) def test_queries_success(self): self.mock_pages_processor.return_value.make_stats_object_for_page_title.return_value = self.mock_property_statistics # noqa self.mock_property_statistics.get_query_for_items_for_property_positive.return_value = "X" self.mock_property_statistics.get_query_for_items_for_property_negative.return_value = "Z" self.mock_property_statistics.GROUP_MAPPING.side_effect = ValueError self.mock_property_statistics.GROUP_MAPPING.__members__.get.return_value = "Q2" response = self.app.get( "/queries?page=%s&url=%s&column=P1&grouping=Q2" % (self.page_title, self.page_url) ) self.mock_pages_processor.assert_called_once_with(self.page_url) self.mock_pages_processor.return_value.make_stats_object_for_page_title.assert_called_once_with( page_title=self.page_title ) # noqa self.mock_property_statistics.get_query_for_items_for_property_positive.assert_called_once_with( self.column_P1, "Q2" ) self.mock_property_statistics.get_query_for_items_for_property_negative.assert_called_once_with( self.column_P1, "Q2" ) - expected = ( + self.assertEqual(response.status_code, 200) + content = response.get_data(as_text=True) + expected_body = ( '

From page Foo, ' 'P1, ' 'with Q2 as P495.

\n\t' - 'All items with the property set\n\t' # noqa - 'All items without the property set' # noqa ) - self.assertEqual(response.status_code, 200) - self.assertPresent(expected, response.get_data(as_text=True)) + self.assertPresent(expected_body, content) + expected_wdqs = ( + 'WDQS: All items with the property set' + 'WDQS: All items without the property set' + ) + self.assertPresent(expected_wdqs, content) + expected_qlever = ( + 'From page Foo, ' 'P1, ' 'without P495 grouping.

\n\t' - 'All items with the property set\n\t' # noqa - 'All items without the property set' # noqa ) - self.assertEqual(response.status_code, 200) - self.assertPresent(expected, response.get_data(as_text=True)) + self.assertPresent(expected_body, content) + expected_wdqs = ( + 'WDQS: All items with the property set' + 'WDQS: All items without the property set' + ) + self.assertPresent(expected_wdqs, content) + expected_qlever = ( + 'From page Foo, ' "br label, " 'with Q2 as P495.

\n\t' - 'All items with the label set\n\t' # noqa - 'All items without the label set' # noqa ) - self.assertEqual(response.status_code, 200) - self.assertPresent(expected, response.get_data(as_text=True)) + self.assertPresent(expected_body, content) + expected_wdqs = ( + 'WDQS: All items with the label set' + 'WDQS: All items without the label set' + ) + self.assertPresent(expected_wdqs, content) + expected_qlever = ( + 'From page Foo, ' "br description, " 'with Q2 as P495.

\n\t' - 'All items with the description set\n\t' # noqa - 'All items without the description set' # noqa ) - self.assertEqual(response.status_code, 200) - self.assertPresent(expected, response.get_data(as_text=True)) + self.assertPresent(expected_body, content) + expected_wdqs = ( + 'WDQS: All items with the description set' + 'WDQS: All items without the description set' + ) + self.assertPresent(expected_wdqs, content) + expected_qlever = ( + 'From page Foo, ' 'P1, ' "for the totals.

\n\t" - 'All items with the property set\n\t' # noqa - 'All items without the property set' # noqa ) - self.assertEqual(response.status_code, 200) - self.assertPresent(expected, response.get_data(as_text=True)) + self.assertPresent(expected_body, content) + expected_wdqs = ( + 'WDQS: All items with the property set' + 'WDQS: All items without the property set' + ) + self.assertPresent(expected_wdqs, content) + expected_qlever = ( + 'From page Foo, ' 'P1, ' 'with unknown value as P495.

\n\t' - 'All items with the property set\n\t' # noqa - 'All items without the property set' # noqa ) - self.assertEqual(response.status_code, 200) - self.assertPresent(expected, response.get_data(as_text=True)) + self.assertPresent(expected_body, content) + expected_wdqs = ( + 'WDQS: All items with the property set' + 'WDQS: All items without the property set' + ) + self.assertPresent(expected_wdqs, content) + expected_qlever = ( + '