summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2017-11-21 21:24:10 +0100
committermathieui <mathieui@mathieui.net>2017-11-21 22:40:43 +0100
commit057f7ac9f5073b87b42da3509d03914a9e79a08b (patch)
tree63cc7777eeee6bf14ef8d15cd9e70ce01395677b /test
parent094c62750f9b54cba43b8ff7b7c6581861b180a9 (diff)
downloadpoezio-057f7ac9f5073b87b42da3509d03914a9e79a08b.tar.gz
poezio-057f7ac9f5073b87b42da3509d03914a9e79a08b.tar.bz2
poezio-057f7ac9f5073b87b42da3509d03914a9e79a08b.tar.xz
poezio-057f7ac9f5073b87b42da3509d03914a9e79a08b.zip
Refactor logger.py to make it more testable
Also fixes: - release the message log mmap when we are done with it - Write the right character in log lines
Diffstat (limited to 'test')
-rw-r--r--test/test_logger.py24
1 files changed, 21 insertions, 3 deletions
diff --git a/test/test_logger.py b/test/test_logger.py
index b2f7c335..f1851d60 100644
--- a/test/test_logger.py
+++ b/test/test_logger.py
@@ -1,15 +1,33 @@
"""
Test the functions in the `logger` module
"""
-
-from poezio.logger import LogMessage, parse_log_line
+import datetime
+from poezio.logger import LogMessage, parse_log_line, parse_log_lines, build_log_message
+from poezio.common import get_utc_time, get_local_time
def test_parse_message():
line = 'MR 20170909T09:09:09Z 000 <nick>  body'
assert vars(parse_log_line(line)) == vars(LogMessage('2017', '09', '09', '09', '09', '09', '0', 'nick', 'body'))
line = '<>'
- assert parse_log_line(line) == None
+ assert parse_log_line(line) is None
line = 'MR 20170908T07:05:04Z 003 <nick>  '
assert vars(parse_log_line(line)) == vars(LogMessage('2017', '09', '08', '07', '05', '04', '003', 'nick', ''))
+
+
+def test_log_and_parse_messages():
+ msg1 = {'nick': 'toto', 'msg': 'coucou', 'date': datetime.datetime.now().replace(microsecond=0)}
+ msg1_utc = get_utc_time(msg1['date'])
+ built_msg1 = build_log_message(**msg1)
+ assert built_msg1 == 'MR %s 000 <toto>  coucou\n' % (msg1_utc.strftime('%Y%m%dT%H:%M:%SZ'))
+
+ msg2 = {'nick': 'toto', 'msg': 'coucou\ncoucou', 'date': datetime.datetime.now().replace(microsecond=0)}
+ built_msg2 = build_log_message(**msg2)
+ msg2_utc = get_utc_time(msg2['date'])
+ assert built_msg2 == 'MR %s 001 <toto>  coucou\n coucou\n' % (msg2_utc.strftime('%Y%m%dT%H:%M:%SZ'))
+
+ assert parse_log_lines((built_msg1 + built_msg2).split('\n')) == [
+ {'time': msg1['date'], 'history': True, 'txt': '\x195,-1}coucou', 'nickname': 'toto'},
+ {'time': msg2['date'], 'history': True, 'txt': '\x195,-1}coucou\ncoucou', 'nickname': 'toto'},
+ ]