2016-05-10 16:50:34 -04:00
|
|
|
import logging
|
2016-06-12 12:39:56 -04:00
|
|
|
from datetime import timedelta
|
2015-07-31 15:55:45 -04:00
|
|
|
from os import remove
|
|
|
|
|
2016-05-09 14:09:16 -04:00
|
|
|
import pytest
|
2015-09-04 17:20:35 -04:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
2016-11-23 15:00:35 -05:00
|
|
|
from tsstats.log import _parse_details, Server
|
|
|
|
from tsstats.template import render_servers
|
2016-11-11 12:41:51 -05:00
|
|
|
from tsstats.utils import filter_threshold, seconds_to_text, sort_clients
|
2015-09-04 17:20:35 -04:00
|
|
|
|
2016-05-08 15:32:37 -04:00
|
|
|
output_path = 'tsstats/tests/res/output.html'
|
2016-11-11 12:41:51 -05:00
|
|
|
clients = _parse_details('tsstats/tests/res/test.log', online_dc=False)
|
2016-11-23 15:00:35 -05:00
|
|
|
servers = [Server(1, clients)]
|
2015-07-31 15:55:45 -04:00
|
|
|
|
2016-05-10 16:50:34 -04:00
|
|
|
logger = logging.getLogger('tsstats')
|
|
|
|
|
2015-07-31 15:55:45 -04:00
|
|
|
|
2016-05-09 14:09:16 -04:00
|
|
|
@pytest.fixture
|
|
|
|
def output(request):
|
|
|
|
def clean():
|
2016-06-12 12:39:56 -04:00
|
|
|
remove(output_path)
|
2016-05-09 14:09:16 -04:00
|
|
|
request.addfinalizer(clean)
|
|
|
|
|
|
|
|
|
2016-06-10 09:36:32 -04:00
|
|
|
@pytest.fixture
|
|
|
|
def soup(output):
|
2016-11-23 15:00:35 -05:00
|
|
|
render_servers(servers, output_path)
|
2016-06-10 09:36:32 -04:00
|
|
|
return BeautifulSoup(open(output_path), 'html.parser')
|
|
|
|
|
|
|
|
|
2016-05-09 14:09:16 -04:00
|
|
|
def test_debug(output):
|
2016-05-10 16:50:34 -04:00
|
|
|
logger.setLevel(logging.DEBUG)
|
2016-11-23 15:00:35 -05:00
|
|
|
render_servers(servers, output_path)
|
2016-05-10 16:50:34 -04:00
|
|
|
logger.setLevel(logging.INFO)
|
2016-05-09 14:09:16 -04:00
|
|
|
soup = BeautifulSoup(open(output_path), 'html.parser')
|
2016-06-10 10:22:52 -04:00
|
|
|
# check debug-label presence
|
2016-11-23 15:04:51 -05:00
|
|
|
assert soup.find_all(style='color: red; padding-right: 10px;')
|
2016-11-23 15:06:55 -05:00
|
|
|
for client_item in soup.find('ul', id='1.onlinetime').find_all('li'):
|
2016-06-10 10:22:52 -04:00
|
|
|
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
|
2016-05-09 14:09:16 -04:00
|
|
|
|
|
|
|
|
2016-06-10 09:36:32 -04:00
|
|
|
def test_onlinetime(soup):
|
2016-06-10 10:45:41 -04:00
|
|
|
# move this into a (parameterized) fixture or function
|
2016-11-23 15:06:11 -05:00
|
|
|
items = soup.find('ul', id='1.onlinetime').find_all('li')
|
2016-06-10 10:45:41 -04:00
|
|
|
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:
|
2016-06-12 12:36:09 -04:00
|
|
|
if client.nick in nick_data and client.onlinetime > timedelta(0):
|
2016-06-10 10:45:41 -04:00
|
|
|
# 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] == \
|
2016-06-12 12:36:09 -04:00
|
|
|
seconds_to_text(int(client.onlinetime.total_seconds()))
|
2016-11-11 12:41:51 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_filter_threshold():
|
|
|
|
sorted_clients = sort_clients(
|
|
|
|
clients, lambda c: c.onlinetime.total_seconds())
|
|
|
|
assert len(filter_threshold(sorted_clients, -1)) == len(sorted_clients)
|
|
|
|
assert len(filter_threshold(sorted_clients, 20)) == 1
|
|
|
|
assert len(filter_threshold(sorted_clients, 500)) == 0
|