tagtimepy/tests/test_taglog.py

68 lines
2.3 KiB
Python

import os
import unittest
import taglog
class TestTagLog(unittest.TestCase):
TEST_LOG_1 = "tests/user1.log"
TEST_LOG_2 = "tests/user2.log"
TEST_LOG_3 = "tests/user3.log"
TEST_LOG_1_LOCK = TEST_LOG_1 + ".lock"
TEST_LOG_2_LOCK = TEST_LOG_2 + ".lock"
def test_tag_log_locking(self):
tl1 = taglog.TagLog(self.TEST_LOG_1)
tl2 = taglog.TagLog(self.TEST_LOG_2)
# Make sure that lock files are created.
self.assertTrue(os.path.isfile(self.TEST_LOG_1_LOCK))
self.assertTrue(os.path.isfile(self.TEST_LOG_2_LOCK))
# Make sure that lock files are deleted on object destruction.
del tl2
self.assertFalse(os.path.isfile(self.TEST_LOG_2_LOCK))
del tl1
self.assertFalse(os.path.isfile(self.TEST_LOG_1_LOCK))
def test_tag_log_locking_conflict(self):
tl1 = taglog.TagLog(self.TEST_LOG_1)
self.assertTrue(os.path.isfile(self.TEST_LOG_1_LOCK))
# Creating another TagLog object for the same log caues a system exit.
with self.assertRaises(SystemExit):
tl2 = taglog.TagLog(self.TEST_LOG_1)
# Create another TagLog after destroying the first one.
del tl1
tl2 = taglog.TagLog(self.TEST_LOG_1)
self.assertTrue(os.path.isfile(self.TEST_LOG_1_LOCK))
del tl2
def test_exists(self):
tl = taglog.TagLog(self.TEST_LOG_1)
self.assertTrue(tl.exists())
tl = taglog.TagLog(self.TEST_LOG_3)
self.assertFalse(tl.exists())
def test_last_ping(self):
Ping = taglog.Ping
p1 = taglog.TagLog(self.TEST_LOG_1).last_ping()
line = "1600808609 (lol) afk bar [lulz]\n"
p2 = Ping(1600808609, ["afk", "bar"], ["lol", "lulz"], line)
self.assertEqual(p1, p2)
def test_last_ping_time(self):
time = taglog.TagLog(self.TEST_LOG_1).last_ping_time()
self.assertEqual(time, 1600808609)
time = taglog.TagLog(self.TEST_LOG_2).last_ping_time()
self.assertEqual(time, 1601543391)
def test_get_tags(self):
tags = taglog.TagLog(self.TEST_LOG_1).get_tags()
expected_tags = [
(6, "afk"),
(5, "off"),
(3, "bar"),
(1, "quz"),
(1, "foo")]
self.assertEqual(tags, expected_tags)