<commit_before># -*- coding: utf-8  -*-
"""Test logentries module."""
#
# (C) Pywikibot team, 2015
#
# Distributed under the terms of the MIT license.
#
from __future__ import absolute_import, unicode_literals

__version__ = '$Id$'

import datetime

import pywikibot

from pywikibot.logentries import LogEntryFactory
from pywikibot.tools import (
    MediaWikiVersion,
    UnicodeType as unicode,
)

from tests.aspects import (
    unittest, MetaTestCaseClass, TestCase, DeprecationTestCase
)
from tests.utils import add_metaclass


class TestLogentriesBase(TestCase):

    """
    Base class for log entry tests.

    It uses the German Wikipedia for a current representation of the log entries
    and the test Wikipedia for the future representation. It also tests on a
    wiki with MW 1.19 or older to check that it can still read the older format.
    It currently uses lyricwiki which as of this commit uses 1.19.24.
    """

    sites = {
        'tewp': {
            'family': 'wikipedia',
            'code': 'test',
            'target': 'Main Page on wheels',
        },
        'dewp': {
            'family': 'wikipedia',
            'code': 'de',
            'target': 'Hauptseite',
        },
        'old': {
            'family': 'lyricwiki',
            'code': 'en',
            'target': None,
        }
    }

    def _get_logentry(self, logtype):
        """Retrieve a single log entry."""
        if self.site_key == 'old':
            # This is an assertion as the tests don't make sense with newer
            # MW versions and otherwise it might not be visible that the test
            # isn't run on an older wiki.
            self.assertLess(MediaWikiVersion(self.site.version()),
                            MediaWikiVersion('1.20'))
        return next(iter(self.site.logevents(logtype=logtype, total=1)))

    def _test_logevent(self, logtype):
        """Test a single logtype entry."""
        logentry = self._get_logentry(logtype)
        if logtype in LogEntryFactory._logtypes:
            self.assertEqual(logentry._expectedType, logtype)
        else:
            self.assertIsNone(logentry._expectedType)
        if self.site_key == 'old':
            self.assertNotIn('params', logentry.data)
        else:
            self.assertNotIn(logentry.type(), logentry.data)
        self.assertIsInstance(logentry.action(), unicode)
        self.assertIsInstance(logentry.comment(), unicode)
        self.assertIsInstance(logentry.logid(), int)
        self.assertIsInstance(logentry.ns(), int)
        self.assertIsInstance(logentry.pageid(), int)
        self.assertIsInstance(logentry.timestamp(), pywikibot.Timestamp)
        if 'title' in logentry.data:  # title may be missing
            self.assertIsInstance(logentry.page(), pywikibot.Page)
        else:
            self.assertRaises(KeyError, logentry.page)
        self.assertEqual(logentry.type(), logtype)
        self.assertIsInstance(logentry.user(), unicode)
        self.assertGreaterEqual(logentry.logid(), 0)
        self.assertGreaterEqual(logentry.ns(), -2)
        self.assertGreaterEqual(logentry.pageid(), 0)


class TestLogentriesMeta(MetaTestCaseClass):

    """Test meta class for TestLogentries."""
<commit_msg>[bugfix] logentry.page() may return an int value<commit_after># -*- coding: utf-8  -*-
"""Test logentries module."""
#
# (C) Pywikibot team, 2015
#
# Distributed under the terms of the MIT license.
#
from __future__ import absolute_import, unicode_literals

__version__ = '$Id$'

import datetime

import pywikibot

from pywikibot.logentries import LogEntryFactory
from pywikibot.tools import (
    MediaWikiVersion,
    UnicodeType as unicode,
)

from tests.aspects import (
    unittest, MetaTestCaseClass, TestCase, DeprecationTestCase
)
from tests.utils import add_metaclass


class TestLogentriesBase(TestCase):

    """
    Base class for log entry tests.

    It uses the German Wikipedia for a current representation of the log entries
    and the test Wikipedia for the future representation. It also tests on a
    wiki with MW 1.19 or older to check that it can still read the older format.
    It currently uses lyricwiki which as of this commit uses 1.19.24.
    """

    sites = {
        'tewp': {
            'family': 'wikipedia',
            'code': 'test',
            'target': 'Main Page on wheels',
        },
        'dewp': {
            'family': 'wikipedia',
            'code': 'de',
            'target': 'Hauptseite',
        },
        'old': {
            'family': 'lyricwiki',
            'code': 'en',
            'target': None,
        }
    }

    def _get_logentry(self, logtype):
        """Retrieve a single log entry."""
        if self.site_key == 'old':
            # This is an assertion as the tests don't make sense with newer
            # MW versions and otherwise it might not be visible that the test
            # isn't run on an older wiki.
            self.assertLess(MediaWikiVersion(self.site.version()),
                            MediaWikiVersion('1.20'))
        return next(iter(self.site.logevents(logtype=logtype, total=1)))

    def _test_logevent(self, logtype):
        """Test a single logtype entry."""
        logentry = self._get_logentry(logtype)
        if logtype in LogEntryFactory._logtypes:
            self.assertEqual(logentry._expectedType, logtype)
        else:
            self.assertIsNone(logentry._expectedType)
        if self.site_key == 'old':
            self.assertNotIn('params', logentry.data)
        else:
            self.assertNotIn(logentry.type(), logentry.data)
        self.assertIsInstance(logentry.action(), unicode)
        self.assertIsInstance(logentry.comment(), unicode)
        self.assertIsInstance(logentry.logid(), int)
        self.assertIsInstance(logentry.ns(), int)
        self.assertIsInstance(logentry.pageid(), int)
        self.assertIsInstance(logentry.timestamp(), pywikibot.Timestamp)
        if 'title' in logentry.data:  # title may be missing
            if logtype == 'block' and logentry.isAutoblockRemoval:
                self.assertIsInstance(logentry.page(), int)
            else:
                self.assertIsInstance(logentry.page(), pywikibot.Page)
        else:
            self.assertRaises(KeyError, logentry.page)
        self.assertEqual(logentry.type(), logtype)
        self.assertIsInstance(logentry.user(), unicode)
        self.assertGreaterEqual(logentry.logid(), 0)
        self.assertGreaterEqual(logentry.ns(), -2)
        self.assertGreaterEqual(logentry.pageid(), 0)


class TestLogentriesMeta(MetaTestCaseClass):

    """Test meta class for TestLogentries."""

{'fraction_non_alphanumeric': 0.10560409761976498, 'fraction_numerical': 0.006025911419102139, 'mean_word_length': 2.5276301806588735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7213826', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1865, 'n_words': 516}