tagtimepy/tests/test_ping.py

122 lines
4.0 KiB
Python

import unittest
from ping import Ping
class TestLineToPing(unittest.TestCase):
def test_line_to_ping(self):
l = "1600790846 afk off RETRO [2020.09.22 12:07:26 TUE]"
p1 = Ping.line_to_ping(l)
p2 = Ping(1600790846,
["afk", "off", "RETRO"],
["2020.09.22 12:07:26 TUE"],
l)
self.assertEqual(p1, p2)
l = " 1600790846 [2020.09.22 12:07:26 TUE] foo bar (lol)"
p1 = Ping.line_to_ping(l)
p2 = Ping(1600790846,
["foo", "bar"],
["lol", "2020.09.22 12:07:26 TUE"],
l)
self.assertEqual(p1, p2)
l = "1600790846 12weird #tags :should work"
p1 = Ping.line_to_ping(l)
p2 = Ping(1600790846,
["12weird", "#tags", ":should", "work"],
[],
l)
self.assertEqual(p1, p2)
def test_line_to_ping_no_timestamp(self):
l = " 846 [2020.09.22 12:07:26 TUE] foo bar (lol)"
with self.assertRaises(AttributeError):
Ping.line_to_ping(l)
l = "foo bar (lol)"
with self.assertRaises(AttributeError):
Ping.line_to_ping(l)
def test_line_to_ping_no_tags(self):
l = "1600790846 [2020.09.22 12:07:26 TUE] (foo) [baar]"
p = Ping.line_to_ping(l)
self.assertEqual([], p.tags)
class TestGetTags(unittest.TestCase):
def test_get_tags(self):
tags = Ping.get_tags("123456789 foo #bar quz [sd ab)")
expected = ["foo", "#bar", "quz", "[sd", "ab)"]
self.assertEqual(tags, expected)
class TestPingToLine(unittest.TestCase):
def test_ping_to_line(self):
p = Ping(1600790846,
["afk", "off", "RETRO"],
["qul", "2020.09.22 12:07:26 TUE"])
line = Ping.ping_to_line(p)
expected = "1600790846 afk off RETRO [qul] [2020.09.22 12:07:26 TUE]"
self.assertEqual(line, expected)
def test_ping_to_line_with_annotation(self):
p = Ping(1600790846,
["afk", "off", "RETRO"],
["qul"])
line = Ping.ping_to_line(p, True, 57)
expected = "1600790846 afk off RETRO [qul] [2020.09.22 12:07:26 Tue]"
self.assertEqual(line, expected)
class TestAddTimeAnnotation(unittest.TestCase):
def test_add_time_annotation_24(self):
time = 1600790846
line = str(time)
new_line = Ping.add_time_annotation(time, line, 40)
exp_line = line + " [2020.09.22 12:07:26 Tue]"
self.assertEqual(new_line, exp_line)
def test_add_time_annotation_18(self):
time = 1600790846
line = str(time)
new_line = Ping.add_time_annotation(time, line, 29)
exp_line = line + " [09.22 12:07:26 Tue]"
self.assertEqual(new_line, exp_line)
def test_add_time_annotation_15(self):
time = 1600790846
line = str(time)
new_line = Ping.add_time_annotation(time, line, 26)
exp_line = line + " [22 12:07:26 Tue]"
self.assertEqual(new_line, exp_line)
def test_add_time_annotation_12(self):
time = 1600790846
line = str(time)
new_line = Ping.add_time_annotation(time, line, 23)
exp_line = line + " [12:07:26 Tue]"
self.assertEqual(new_line, exp_line)
def test_add_time_annotation_9(self):
time = 1600790846
line = str(time)
new_line = Ping.add_time_annotation(time, line, 20)
exp_line = line + " [12:07 Tue]"
self.assertEqual(new_line, exp_line)
def test_add_time_annotation_5(self):
time = 1600790846
line = str(time)
new_line = Ping.add_time_annotation(time, line, 16)
exp_line = line + " [12:07]"
self.assertEqual(new_line, exp_line)
def test_add_time_annotation_else(self):
time = 1600790846
line = str(time)
new_line = Ping.add_time_annotation(time, line, 10)
exp_line = line + " [07]"
self.assertEqual(new_line, exp_line)