From 234f671e84b504a45dcfbc0641da35b5b09e4250 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Mon, 13 Jul 2015 21:35:05 +0200 Subject: [PATCH] add tests --- tests/res/test.log | 11 ++++++++++ tests/test_config.py | 47 +++++++++++++++++++++++++++++++++++++++++++ tests/test_general.py | 36 +++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 tests/res/test.log create mode 100644 tests/test_config.py create mode 100644 tests/test_general.py diff --git a/tests/res/test.log b/tests/res/test.log new file mode 100644 index 0000000..d16d0ed --- /dev/null +++ b/tests/res/test.log @@ -0,0 +1,11 @@ +2015-05-18 15:52:52.685612|INFO |VirtualServerBase| 3| client connected 'Client1'(id:1) from 1.2.3.4:1234 +2015-05-18 15:54:13.966536|INFO |VirtualServerBase| 3| client connected 'Client2'(id:2) from 5.6.7.8:5678 +2015-05-18 15:54:25.055304|INFO |VirtualServerBase| 3| client disconnected 'Client2'(id:2) reason 'invokerid=1 invokername=Client1 invokeruid=UIDClient1 reasonmsg' +2015-05-18 15:54:30.053231|INFO |VirtualServerBase| 3| client connected 'Client2'(id:2) from 5.6.7.8:5678 +2015-05-18 15:54:38.723364|INFO |VirtualServer | 3| ban added reason='' cluid='UIDClient2' bantime=0 by client 'Client1'(id:1) +2015-05-18 15:54:38.724012|INFO |VirtualServer | 3| ban added reason='' ip='5.6.7.8:5678' bantime=0 by client 'Client1'(id:1) +2015-05-18 15:54:38.724107|INFO |VirtualServerBase| 3| client disconnected 'Client2'(id:2) reason 'invokerid=1 invokername=Client1 invokeruid=UIDClient1 reasonmsg bantime=0' +2015-05-18 15:54:43.340308|INFO |VirtualServerBase| 3| client disconnected 'Client1'(id:1) reason 'reasonmsg=ByeBye!' +2015-05-18 15:55:23.456679|INFO |VirtualServerBase| 3| client connected 'Client1'(id:1) from 1.2.3.4:1234 +2015-05-18 16:00:14.951191|INFO |VirtualServerBase| 3| client disconnected 'Client1'(id:1) reason 'reasonmsg=ByeBye!' +2015-05-18 16:00:38.802524|INFO |VirtualServerBase| 3| client connected 'Client1'(id:1) from 1.2.3.4:1234 diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..6780046 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,47 @@ +import configparser +from os import remove +from os.path import exists +import tsstats +from nose.tools import raises, with_setup + + +def create_config(values, key='General'): + config = configparser.ConfigParser() + config[key] = values + with open('config.ini', 'w') as configfile: + config.write(configfile) + + +def clean_config(): + if exists('config.ini'): + remove('config.ini') + + +def clean_result(): + if exists('output.html'): + remove('output.html') + + +@with_setup(clean_config, clean_config) +@raises(Exception) +def test_invalid_config(): + config = configparser.ConfigParser() + create_config({ + 'logfile': 'tests/res/test.log', + 'outputfile': '', + 'deebug': 'false', + }) + _, _, _, _ = parse_config(config_path) + + +@with_setup(clean_config, clean_config) +@raises(Exception) +def test_debug_without_debugfile(): + config = configparser.ConfigParser() + create_config({ + 'logfile': 'tests/res/test.log', + 'debug': 'true', + 'debugfile': 'false', + }) + _, _, _, _ = parse_config(config_path) + open('debug.txt', 'r') diff --git a/tests/test_general.py b/tests/test_general.py new file mode 100644 index 0000000..db32b95 --- /dev/null +++ b/tests/test_general.py @@ -0,0 +1,36 @@ +from tsstats import parse_logs +from sys import stderr + +clients = parse_logs('tests/res/test.log') + + +def test_length(): + assert len(clients.clients_by_id) == 2 + assert len(clients.clients_by_uid) == 1 + + +def test_getter(): + # check getter not raise + assert clients['UIDClient2'].onlinetime == 0 + + +def test_parse_onlinetime(): + # check different dicts + assert clients['1'].onlinetime == 402 + assert clients['2'].onlinetime == 20 + + +def test_parse_kicks(): + assert clients['UIDClient1'].kicks == 1 + + +def test_parse_pkicks(): + assert clients['2'].pkicks == 1 + + +def test_parse_bans(): + assert clients['UIDClient1'].bans == 1 + + +def test_parse_pbans(): + assert clients['2'].pbans == 1