linkulator2/tests/data_test.py

110 lines
3.7 KiB
Python

"""unit tests for the data module"""
import unittest
from time import time
import data
class TestDataHelperFunctions(unittest.TestCase):
"""Tests that cover helper functions, mostly handling string validation"""
def test_wash_line(self):
"""tests the data.wash_line function"""
teststrings = [
{"Test": "A line of text", "Result": "A line of text"},
{"Test": "A line of text\n", "Result": "A line of text"},
{"Test": "\033[95mPink text\033[0m", "Result": "Pink text"},
{"Test": "🚅\t\n", "Result": "🚅"},
{
"Test": "gemini://gemini.circumlunar.space",
"Result": "gemini://gemini.circumlunar.space",
},
]
for line in teststrings:
self.assertEqual(data.wash_line(line["Test"]), line["Result"])
def test_is_well_formed_line(self):
""" tests the data.is_well_formed_line function"""
teststrings = [
{"Test": "A line of text", "Result": False},
{"Test": "1 Pipe |", "Result": False},
{"Test": "4 Pipes ||||", "Result": True},
{"Test": "5 Pipes |||||", "Result": False},
{"Test": "|P|I|P|E|H|E|A|V|E|N||||||||||", "Result": False},
]
for line in teststrings:
self.assertEqual(data.is_well_formed_line(line["Test"]), line["Result"])
def test_is_valid_time(self):
"""tests the data.is_valid_time function"""
teststrings = [
{"Test": "946645140.0", "Result": True}, # 1999
{"Test": str(time() + 10), "Result": False},
]
for line in teststrings:
self.assertEqual(data.is_valid_time(line["Test"]), line["Result"])
def test_process(self):
"""tests the data.process function"""
# wash line
self.assertEqual(
data.process("\t|\033[95mPink text\033[0m|||\n", ""),
["", "", "Pink text", "", "", ""],
)
# is well formed line
with self.assertRaises(ValueError, msg="Not a well formed line"):
data.process("|||||\n", "")
# is valid date
with self.assertRaises(ValueError, msg="Invalid date"):
data.process("{}||||".format(str(time() + 10)), "")
# real data tests
teststrings_input = [
"1576123922.106229|user+1576032469.7391915|||a new reply\n",
"1576137798.4647715|user+1576032469.7391915|||Here is another new reply\n",
"575968281.7483418||tildes|gopher://tilde.town|Tilde Town\n",
"1575969313.8278663||tildes|http://tilde.team|Tilde Team\n",
]
teststrings_output = [
[
"username0",
"1576123922.106229",
"user+1576032469.7391915",
"",
"",
"a new reply",
],
[
"username1",
"1576137798.4647715",
"user+1576032469.7391915",
"",
"",
"Here is another new reply",
],
[
"username2",
"575968281.7483418",
"",
"tildes",
"gopher://tilde.town",
"Tilde Town",
],
[
"username3",
"1575969313.8278663",
"",
"tildes",
"http://tilde.team",
"Tilde Team",
],
]
for i, item in enumerate(teststrings_input):
self.assertEqual(
data.process(item, "username{}".format(i)), teststrings_output[i]
)
if __name__ == "__main__":
unittest.main()