<commit_before>import logging
from datetime import timedelta
from os import remove

import pytest
from bs4 import BeautifulSoup

from tsstats.log import _parse_details, Server
from tsstats.template import render_servers
from tsstats.utils import filter_threshold, seconds_to_text, sort_clients

output_path = 'tsstats/tests/res/output.html'
clients = _parse_details('tsstats/tests/res/test.log', online_dc=False)
servers = [Server(1, clients)]

logger = logging.getLogger('tsstats')


@pytest.fixture
def output(request):
    def clean():
        remove(output_path)
    request.addfinalizer(clean)


@pytest.fixture
def soup(output):
    render_servers(servers, output_path)
    return BeautifulSoup(open(output_path), 'html.parser')


def test_debug(output):
    logger.setLevel(logging.DEBUG)
    render_servers(servers, output_path)
    logger.setLevel(logging.INFO)
    soup = BeautifulSoup(open(output_path), 'html.parser')
    # check debug-label presence
    assert soup.find_all(style='color: red; padding-right: 10px;')
    for client_item in soup.find_all('li'):
        nick = client_item.find('span').text
        # check for right identifier
        nick, encl_identifier = nick.split()
        identifier = encl_identifier.replace('(', '').replace(')', '')
        assert clients[identifier].nick == nick


def test_onlinetime(soup):
    # move this into a (parameterized) fixture or function
    items = soup.find('ul', id='onlinetime').find_all('li')
    nick_data = {}
    for item in items:
        nick, data = item.find_all('span')
        nick_data[nick.text] = data.text
    # seperate between uuid and id-clients or merge them some way
    # => assert len(items) == len(clients.id)
    assert len(items) == 2
    for client in clients:
        if client.nick in nick_data and client.onlinetime > timedelta(0):
            # remove this clause after splitting cients
            # (uuid-clients will never have a online-time, because
            #  they're only used for bans and kicks)
            assert nick_data[client.nick] == \
                seconds_to_text(int(client.onlinetime.total_seconds()))


def test_filter_threshold():<commit_msg>Use new, sid-prefixed section-id<commit_after>import logging
from datetime import timedelta
from os import remove

import pytest
from bs4 import BeautifulSoup

from tsstats.log import _parse_details, Server
from tsstats.template import render_servers
from tsstats.utils import filter_threshold, seconds_to_text, sort_clients

output_path = 'tsstats/tests/res/output.html'
clients = _parse_details('tsstats/tests/res/test.log', online_dc=False)
servers = [Server(1, clients)]

logger = logging.getLogger('tsstats')


@pytest.fixture
def output(request):
    def clean():
        remove(output_path)
    request.addfinalizer(clean)


@pytest.fixture
def soup(output):
    render_servers(servers, output_path)
    return BeautifulSoup(open(output_path), 'html.parser')


def test_debug(output):
    logger.setLevel(logging.DEBUG)
    render_servers(servers, output_path)
    logger.setLevel(logging.INFO)
    soup = BeautifulSoup(open(output_path), 'html.parser')
    # check debug-label presence
    assert soup.find_all(style='color: red; padding-right: 10px;')
    for client_item in soup.find_all('li'):
        nick = client_item.find('span').text
        # check for right identifier
        nick, encl_identifier = nick.split()
        identifier = encl_identifier.replace('(', '').replace(')', '')
        assert clients[identifier].nick == nick


def test_onlinetime(soup):
    # move this into a (parameterized) fixture or function
    items = soup.find('ul', id='1.onlinetime').find_all('li')
    nick_data = {}
    for item in items:
        nick, data = item.find_all('span')
        nick_data[nick.text] = data.text
    # seperate between uuid and id-clients or merge them some way
    # => assert len(items) == len(clients.id)
    assert len(items) == 2
    for client in clients:
        if client.nick in nick_data and client.onlinetime > timedelta(0):
            # remove this clause after splitting cients
            # (uuid-clients will never have a online-time, because
            #  they're only used for bans and kicks)
            assert nick_data[client.nick] == \
                seconds_to_text(int(client.onlinetime.total_seconds()))


def test_filter_threshold():
{'fraction_non_alphanumeric': 0.10772404308961724, 'fraction_numerical': 0.002979601191840477, 'mean_word_length': 3.6277836691410394, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1661257', 'n_tokens_mistral': 1324, 'n_tokens_neox': 1309, 'n_words': 336}