<commit_before># -*- coding: utf-8 -*-
"""Test confirming paraminfo contains expected values."""
#
# (C) Pywikibot team, 2015-2020
#
# Distributed under the terms of the MIT license.
#
from __future__ import absolute_import, division, unicode_literals

from pywikibot.family import WikimediaFamily
from pywikibot.page import Claim, Property
from pywikibot.site import DataSite

from tests.aspects import (
    unittest,
    TestCaseBase,
    DefaultSiteTestCase,
    DefaultWikibaseClientTestCase,
    WikimediaDefaultSiteTestCase,
)


class KnownTypesTestBase(TestCaseBase):

    """Base class for paraminfo checks."""

    def _get_param_values(self, site, module, parameter):
        """Perform check that a parameter matches the expected list."""
        try:
            param = site._paraminfo.parameter(module, parameter)
        except ValueError:
            raise unittest.SkipTest(
                'Paraminfo for {0} could not be loaded'.format(module))
        if not param or 'type' not in param:
            raise unittest.SkipTest(
                'No defined values for {0}.{1}'.format(module, parameter))
        return param['type']

    def _check_param_values(self, site, module, parameter, expected):
        """Check that a parameter matches the expected list exactly."""
        values = self._get_param_values(site, module, parameter)
        self.assertCountEqual(expected, values)

    def _check_param_subset(self, site, module, parameter, expected):
        """Check that a parameter contains all entries in expected list."""
        values = self._get_param_values(site, module, parameter)
        self.assertLessEqual(set(expected), set(values))

    def _check_param_superset(self, site, module, parameter, expected):
        """Check that a parameter only contains entries in expected list."""
        values = self._get_param_values(site, module, parameter)
        exp = set(expected)
        val = set(values)
        if not exp.issuperset(val):
            diff = val - exp
            self.fail('Unexpected param{} {} in values'
                      .format('s' if len(diff) > 1 else '', diff))


class MediaWikiKnownTypesTestCase(KnownTypesTestBase,
                                  DefaultSiteTestCase):

    """Verify MediaWiki types using paraminfo."""

    def test_api_format(self):
        """Test api format."""
        known = ['json', 'xml']
        self._check_param_subset(self.site, 'main', 'format', known)

    def test_assert_user(self):
        """Test assert type."""
        known = ['user', 'bot']
        self._check_param_subset(self.site, 'main', 'assert', known)

    def test_feed_format(self):
        """Test feed format."""
        known = ['rss', 'atom']

        if self.site.has_extension('GoogleNewsSitemap'):
            known.append('sitemap')

        self._check_param_values(
            self.site, 'feedwatchlist', 'feedformat', known)

    def test_watchlist_show_flags(self):
        """Test watchlist show flags."""
        types = ['minor', 'bot', 'anon', 'patrolled']
        if self.site.mw_version >= '1.24':
            types.append('unread')

        known = types + ['!{}'.format(item) for item in types]

        self._check_param_subset(self.site, 'query+watchlist', 'show', known)

    def test_watchlist_type(self):
        """Test watchlist type."""
        known = ['edit', 'new', 'log']

        mw_ver = self.site.mw_version

        if mw_ver >= '1.20':
            known.append('external')
        if mw_ver.version >= (1, 27):
            if mw_ver >= '1.27.0-wmf.4' or mw_ver.suffix == 'alpha':
                known.append('categorize')

        self._check_param_values(self.site, 'query+watchlist', 'type', known)

    def test_watchlist_modification_flag(self):
        """Test watchlist modification flag."""
        known = ['watch', 'unwatch', 'preferences', 'nochange']

        self._check_param_values(self.site, 'edit', 'watchlist', known)
        self._check_param_values(self.site, 'delete', 'watchlist', known)
        self._check_param_values(self.site, 'move', 'watchlist', known)
        self._check_param_values(self.site, 'protect', 'watchlist', known)
        self._check_param_values(self.site, 'rollback', 'watchlist', known)
        self._check_param_values(self.site, 'undelete', 'watchlist', known)

        known = ['watch', 'preferences', 'nochange']
        self._check_param_values(self.site, 'upload', 'watchlist', known)

    def test_content_format(self):
        """Test content format."""
        base = [
            'text/x-wiki',
            'text/javascript',
            'text/css',
            'text/plain',
        ]
        if self.site.mw_version >= '1.24':
            base.append('application/json')
        if isinstance(self.site, DataSite):
            # It is not clear when this format has been added, see T129281.
            base.append('application/vnd.php.serialized')
        extensions = {e['name'] for e in self.site.siteinfo['extensions']}
        if 'CollaborationKit' in extensions:
            base.append('text/x-collabkit')

        self._check_param_values(self.site, 'edit', 'contentformat', base)
        self._check_param_values(self.site, 'parse', 'contentformat', base)

    def test_content_model(self):
        """Test content model."""
        base = [
            'wikitext',
            'javascript',
            'css',<commit_msg>[bugfix] Ignore experimental content format at test wiki<commit_after># -*- coding: utf-8 -*-
"""Test confirming paraminfo contains expected values."""
#
# (C) Pywikibot team, 2015-2020
#
# Distributed under the terms of the MIT license.
#
from pywikibot.family import WikimediaFamily
from pywikibot.page import Claim, Property
from pywikibot.site import DataSite

from tests.aspects import (
    unittest,
    TestCaseBase,
    DefaultSiteTestCase,
    DefaultWikibaseClientTestCase,
    WikimediaDefaultSiteTestCase,
)


class KnownTypesTestBase(TestCaseBase):

    """Base class for paraminfo checks."""

    def _get_param_values(self, site, module, parameter):
        """Perform check that a parameter matches the expected list."""
        try:
            param = site._paraminfo.parameter(module, parameter)
        except ValueError:
            raise unittest.SkipTest(
                'Paraminfo for {0} could not be loaded'.format(module))
        if not param or 'type' not in param:
            raise unittest.SkipTest(
                'No defined values for {0}.{1}'.format(module, parameter))
        return param['type']

    def _check_param_values(self, site, module, parameter, expected):
        """Check that a parameter matches the expected list exactly."""
        values = self._get_param_values(site, module, parameter)
        self.assertCountEqual(expected, values)

    def _check_param_subset(self, site, module, parameter, expected):
        """Check that a parameter contains all entries in expected list."""
        values = self._get_param_values(site, module, parameter)
        self.assertLessEqual(set(expected), set(values))

    def _check_param_superset(self, site, module, parameter, expected):
        """Check that a parameter only contains entries in expected list."""
        values = self._get_param_values(site, module, parameter)
        exp = set(expected)
        val = set(values)
        if not exp.issuperset(val):
            diff = val - exp
            self.fail('Unexpected param{} {} in values'
                      .format('s' if len(diff) > 1 else '', diff))


class MediaWikiKnownTypesTestCase(KnownTypesTestBase,
                                  DefaultSiteTestCase):

    """Verify MediaWiki types using paraminfo."""

    def test_api_format(self):
        """Test api format."""
        known = ['json', 'xml']
        self._check_param_subset(self.site, 'main', 'format', known)

    def test_assert_user(self):
        """Test assert type."""
        known = ['user', 'bot']
        self._check_param_subset(self.site, 'main', 'assert', known)

    def test_feed_format(self):
        """Test feed format."""
        known = ['rss', 'atom']

        if self.site.has_extension('GoogleNewsSitemap'):
            known.append('sitemap')

        self._check_param_values(
            self.site, 'feedwatchlist', 'feedformat', known)

    def test_watchlist_show_flags(self):
        """Test watchlist show flags."""
        types = ['minor', 'bot', 'anon', 'patrolled']
        if self.site.mw_version >= '1.24':
            types.append('unread')

        known = types + ['!{}'.format(item) for item in types]

        self._check_param_subset(self.site, 'query+watchlist', 'show', known)

    def test_watchlist_type(self):
        """Test watchlist type."""
        known = ['edit', 'new', 'log']

        mw_ver = self.site.mw_version

        if mw_ver >= '1.20':
            known.append('external')
        if mw_ver.version >= (1, 27):
            if mw_ver >= '1.27.0-wmf.4' or mw_ver.suffix == 'alpha':
                known.append('categorize')

        self._check_param_values(self.site, 'query+watchlist', 'type', known)

    def test_watchlist_modification_flag(self):
        """Test watchlist modification flag."""
        known = ['watch', 'unwatch', 'preferences', 'nochange']

        self._check_param_values(self.site, 'edit', 'watchlist', known)
        self._check_param_values(self.site, 'delete', 'watchlist', known)
        self._check_param_values(self.site, 'move', 'watchlist', known)
        self._check_param_values(self.site, 'protect', 'watchlist', known)
        self._check_param_values(self.site, 'rollback', 'watchlist', known)
        self._check_param_values(self.site, 'undelete', 'watchlist', known)

        known = ['watch', 'preferences', 'nochange']
        self._check_param_values(self.site, 'upload', 'watchlist', known)

    def test_content_format(self):
        """Test content format."""
        base = [
            'text/x-wiki',
            'text/javascript',
            'text/css',
            'text/plain',
        ]
        if self.site.mw_version >= '1.24':
            base.append('application/json')
        if isinstance(self.site, DataSite):
            # It is not clear when this format has been added, see T129281.
            base.append('application/vnd.php.serialized')
        extensions = {e['name'] for e in self.site.siteinfo['extensions']}
        if 'CollaborationKit' in extensions:
            base.append('text/x-collabkit')

        for module in ('edit', 'parse'):
            args = self.site, module, 'contentformat', base
            with self.subTest(module=module):
                if self.site.code == 'test':  # T259100: ignore experimentals
                    self._check_param_subset(*args)
                else:
                    self._check_param_values(*args)

    def test_content_model(self):
        """Test content model."""
        base = [
            'wikitext',
            'javascript',
            'css',
{'fraction_non_alphanumeric': 0.1272876263316034, 'fraction_numerical': 0.0071018847309478285, 'mean_word_length': 2.6084099868593955, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6926595', 'n_tokens_mistral': 3204, 'n_tokens_neox': 3072, 'n_words': 793}