test_twtxthash.py 4.24 KB
Newer Older
Lysander Trischler's avatar
Lysander Trischler committed
1
import datetime
2
import twtxtmodels
Lysander Trischler's avatar
Lysander Trischler committed
3 4 5 6 7 8 9 10 11
import unittest
from twtxthash import create_hash, create_old_hash

UTC = datetime.timezone.utc
CET = datetime.timezone(datetime.timedelta(hours=1))

class CreateHashTest(unittest.TestCase):

    def test_old_timestamp_format(self):
12
        self.assertEqual("sqwl3la", create_old_hash(
Lysander Trischler's avatar
Lysander Trischler committed
13 14
                created_at=datetime.datetime(2020, 12, 6, 20, 20, 35, tzinfo=CET),
                text="This is a test tweet for testing.",
15
                source=twtxtmodels.Source(nick="nick", url="http://0.0.0.0:8000/user/lyse/twtxt.txt")))
Lysander Trischler's avatar
Lysander Trischler committed
16 17

    def test_rfc3339_timestamp_with_milliseconds_precision_is_truncated_to_seconds_precision(self):
18
        self.assertEqual("74qtyjq", create_hash(
Lysander Trischler's avatar
Lysander Trischler committed
19 20
                created_at=datetime.datetime(2020, 12, 9, 15, 38, 42, 123, tzinfo=UTC),
                text="The twt hash now uses the RFC 3339 timestamp format.",
21
                source=twtxtmodels.Source(nick="nick", url="https://example.com/twtxt.txt")))
Lysander Trischler's avatar
Lysander Trischler committed
22 23

    def test_rfc3339_timestamp_with_millisecomds_precision_is_truncated_to_seconds_precision_without_rounding(self):
24
        self.assertEqual("74qtyjq", create_hash(
Lysander Trischler's avatar
Lysander Trischler committed
25 26
                created_at=datetime.datetime(2020, 12, 9, 15, 38, 42, 999, tzinfo=UTC),
                text="The twt hash now uses the RFC 3339 timestamp format.",
27
                source=twtxtmodels.Source(nick="nick", url="https://example.com/twtxt.txt")))
Lysander Trischler's avatar
Lysander Trischler committed
28 29

    def test_rfc3339_timestamp_with_seconds_precision_and_utc_plus_1_offset_is_kept_intact(self):
30
        self.assertEqual("64u2m5a", create_hash(
Lysander Trischler's avatar
Lysander Trischler committed
31 32
                created_at=datetime.datetime(2020, 12, 9, 16, 38, 42, tzinfo=CET),
                text="The twt hash now uses the RFC 3339 timestamp format.",
33
                source=twtxtmodels.Source(nick="nick", url="https://example.com/twtxt.txt")))
Lysander Trischler's avatar
Lysander Trischler committed
34 35

    def test_rfc3339_timestamp_with_minutes_precision_is_expanded_to_seconds_precision(self):
36 37
        self.assertEqual("a3c3k5q", create_hash(
                created_at=datetime.datetime(2020, 12, 9, 16, 38, tzinfo=CET),
Lysander Trischler's avatar
Lysander Trischler committed
38
                text="The twt hash now uses the RFC 3339 timestamp format.",
39
                source=twtxtmodels.Source(nick="nick", url="https://example.com/twtxt.txt")))
Lysander Trischler's avatar
Lysander Trischler committed
40 41

    def test_rfc3339_timestamp_with_utc_is_rendered_as_designated_zulu_offset_rather_than_numeric_offset(self):
42
        self.assertEqual("74qtyjq", create_hash(
Lysander Trischler's avatar
Lysander Trischler committed
43 44
                created_at=datetime.datetime(2020, 12, 9, 15, 38, 42, tzinfo=UTC),
                text="The twt hash now uses the RFC 3339 timestamp format.",
45
                source=twtxtmodels.Source(nick="nick", url="https://example.com/twtxt.txt")))
46
        self.assertEqual("74qtyjq", create_hash(
Lysander Trischler's avatar
Lysander Trischler committed
47 48
                created_at=datetime.datetime(2020, 12, 9, 15, 38, 42, tzinfo=datetime.timezone(datetime.timedelta(hours=-0))),
                text="The twt hash now uses the RFC 3339 timestamp format.",
49
                source=twtxtmodels.Source(nick="nick", url="https://example.com/twtxt.txt")))
Lysander Trischler's avatar
Lysander Trischler committed
50 51

    def test_rfc3339_timestamp_without_explicit_timezone_information_is_assumed_to_be_in_utc(self):
52
        self.assertEqual("74qtyjq", create_hash(
Lysander Trischler's avatar
Lysander Trischler committed
53 54
                created_at=datetime.datetime(2020, 12, 9, 15, 38, 42),
                text="The twt hash now uses the RFC 3339 timestamp format.",
55 56 57 58 59 60 61 62 63 64 65 66 67 68
                source=twtxtmodels.Source(nick="nick", url="https://example.com/twtxt.txt")))

    def test_first_comment_meta_data_url_must_be_used_for_hashing_when_exists(self):
        self.assertEqual("64u2m5a", create_hash(
                created_at=datetime.datetime(2020, 12, 9, 16, 38, 42, tzinfo=CET),
                text="The twt hash now uses the RFC 3339 timestamp format.",
                source=twtxtmodels.Source(nick="nick", url="https://example.org/",
                                          parsed_urls=["https://example.com/twtxt.txt"])))
        self.assertEqual("64u2m5a", create_hash(
                created_at=datetime.datetime(2020, 12, 9, 16, 38, 42, tzinfo=CET),
                text="The twt hash now uses the RFC 3339 timestamp format.",
                source=twtxtmodels.Source(nick="nick", url="https://example.org/",
                                          parsed_urls=["https://example.com/twtxt.txt",
                                                       "https://example.net/twtxt.txt"])))
Lysander Trischler's avatar
Lysander Trischler committed
69 70 71 72 73


if __name__ == "__main__":
    unittest.main()