def test_get_grouping_information_year(self): stats = PropertyStatistics(columns=self.stats.columns, selector_sparql=u'wdt:P31 wd:Q41960', grouping_property=u'P577', grouping_type='year', property_threshold=10) self.mock_sparql_query.return_value.select.return_value = [ { 'grouping': '2001', 'count': '10' }, { 'grouping': '2002', 'count': '6' }, ] expected = (OrderedDict([('2001', 10), ('2002', 6)]), OrderedDict()) query = ( "\n" "SELECT ?grouping (COUNT(DISTINCT ?entity) as ?count) WHERE {\n" " ?entity wdt:P31 wd:Q41960 .\n" " ?entity wdt:P577 ?date .\n" " BIND(YEAR(?date) as ?grouping) .\n" "} GROUP BY ?grouping\n" "HAVING (?count >= 20)\n" "ORDER BY DESC(?count)\n" "LIMIT 1000\n") result = stats.get_grouping_information() self.assert_query_called(query) self.assertEqual(result, expected)
def make_stats_object_for_page(self, page): all_templates_with_params = page.templatesWithParams() if self.end_template_name not in [ template.title(with_ns=False) for (template, _) in all_templates_with_params ]: raise NoEndTemplateException("No end template '%s' provided" % self.end_template_name) start_templates_with_params = [ (template, params) for (template, params) in all_templates_with_params if template.title(with_ns=False) == self.template_name ] if len(start_templates_with_params) > 1: pywikibot.warn("More than one template on the page %s" % page.title()) (template, params) = start_templates_with_params[0] parsed_config = self.parse_config_from_params(params) config = self.parse_config(parsed_config) try: return PropertyStatistics(**config) except TypeError: raise ConfigException("The template parameters are incorrect.")
def test_get_query_for_items_for_property_positive_year_grouping(self): stats = PropertyStatistics(columns=self.stats.columns, selector_sparql=u'wdt:P31 wd:Q41960', grouping_property=u'P577', grouping_type='year', property_threshold=10) result = stats.get_query_for_items_for_property_positive('P21', 2006) expected = """ SELECT DISTINCT ?entity ?entityLabel ?value ?valueLabel WHERE { ?entity wdt:P31 wd:Q41960 . ?entity wdt:P577 ?date. BIND(YEAR(?date) as ?year). FILTER(?year = 2006). ?entity p:P21 ?prop . OPTIONAL { ?prop ps:P21 ?value } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } """ self.assertEqual(result, expected)
def make_stats_object_for_page_title(self, page_title): key = self.make_cache_key(page_title) result = self.cache.get_cache_value(key) if not result: print("No result in cache for %s, computing..." % key) page = pywikibot.Page(self.site, page_title) result = self.make_stats_object_arguments_for_page(page) try: return PropertyStatistics(**result) except TypeError: raise ConfigException("The template parameters are incorrect.")
def setUp(self): properties = [ PropertyConfig(property='P21'), PropertyConfig(property='P19'), PropertyConfig(property='P1', qualifier='P2'), PropertyConfig(property='P3', value='Q4', qualifier='P5'), ] self.stats = PropertyStatistics(properties=properties, selector_sparql=u'wdt:P31 wd:Q41960', grouping_property=u'P551', property_threshold=10)
def setUp(self): columns = [ PropertyConfig(property='P21'), PropertyConfig(property='P19'), PropertyConfig(property='P1', qualifier='P2'), PropertyConfig(property='P3', value='Q4', qualifier='P5'), LabelConfig(language='br'), DescriptionConfig(language='xy'), ] self.stats = PropertyStatistics(columns=columns, selector_sparql=u'wdt:P31 wd:Q41960', grouping_property=u'P551', property_threshold=10)
def make_stats_object_for_page(self, page): config = self.make_stats_object_arguments_for_page(page) try: return PropertyStatistics(**config) except TypeError: raise ConfigException("The template parameters are incorrect.")