TeamspeakStats/tsstats/tests/test_template.py

82 lines
2.8 KiB
Python
Raw Permalink Normal View History

2017-09-16 16:31:15 -04:00
# -*- coding: utf-8 -*-
import logging
2015-07-31 15:55:45 -04:00
import pendulum
import pytest
2017-09-16 15:59:44 -04:00
from bs4 import BeautifulSoup
from tsstats.log import parse_logs
from tsstats.template import render_servers
from tsstats.utils import filter_threshold, seconds_to_text, sort_clients
2015-09-04 17:20:35 -04:00
servers = list(parse_logs('tsstats/tests/res/test.log', online_dc=False))
servers[0] = servers[0]._replace(sid=1) # add missing sid to server object
clients = servers[0].clients
2015-07-31 15:55:45 -04:00
logger = logging.getLogger('tsstats')
2015-07-31 15:55:45 -04:00
@pytest.fixture
def soup(output):
render_servers(servers, output)
return BeautifulSoup(open(output), 'html.parser')
def test_debug(output):
logger.setLevel(logging.DEBUG)
render_servers(servers, output)
logger.setLevel(logging.INFO)
soup = BeautifulSoup(open(output), 'html.parser')
# check debug-label presence
assert soup.find('nav').find('div', id='main-nav').find('span').text \
== 'debug mode'
for client_item in soup.find('ul', id='1.onlinetime').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):
2016-11-23 15:06:11 -05:00
items = soup.find('ul', id='1.onlinetime').find_all('li')
assert len(items) == 2
for item in items:
nick, onlinetime = item.find_all('span')
nick = nick.text
onlinetime = onlinetime.text
# find corresponding client-object
client = list(filter(
2023-01-14 13:02:53 -05:00
lambda c: c.nick == nick and c.onlinetime > pendulum.duration(),
clients.values()
))
# assert existence
assert client
client = client[0]
# compare onlinetimes
client_onlinetime_text = seconds_to_text(
int(client.onlinetime.total_seconds())
)
assert onlinetime == client_onlinetime_text
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
def test_lastseen_relative(output):
render_servers(servers, output, lastseen_relative=True)
soup = BeautifulSoup(open(output), 'html.parser')
assert soup.find('ul', id='1.onlinetime')\
.select('div.hint--left')[0]['data-hint'] == \
2023-01-14 13:02:53 -05:00
pendulum.datetime(2015, 5, 18).diff_for_humans()
render_servers(servers, output, lastseen_relative=False)
soup = BeautifulSoup(open(output), 'html.parser')
assert soup.find('ul', id='1.onlinetime')\
.select('div.hint--left')[0]['data-hint'] == \
2023-01-14 13:02:53 -05:00
'05/18/15 15:54:38 UTC'