test_twtxthash.py 3.49 KB
Newer Older
Lysander Trischler's avatar
Lysander Trischler committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
import datetime
import twtxt.models
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):
        self.assertEqual("sqwl3la", create_old_hash(twtxt.models.Tweet(
                created_at=datetime.datetime(2020, 12, 6, 20, 20, 35, tzinfo=CET),
                text="This is a test tweet for testing.",
                source=twtxt.models.Source(nick="nick", url="http://0.0.0.0:8000/user/lyse/twtxt.txt"))))

    def test_rfc3339_timestamp_with_milliseconds_precision_is_truncated_to_seconds_precision(self):
        self.assertEqual("74qtyjq", create_hash(twtxt.models.Tweet(
                created_at=datetime.datetime(2020, 12, 9, 15, 38, 42, 123, tzinfo=UTC),
                text="The twt hash now uses the RFC 3339 timestamp format.",
                source=twtxt.models.Source(nick="nick", url="https://example.com/twtxt.txt"))))

    def test_rfc3339_timestamp_with_millisecomds_precision_is_truncated_to_seconds_precision_without_rounding(self):
        self.assertEqual("74qtyjq", create_hash(twtxt.models.Tweet(
                created_at=datetime.datetime(2020, 12, 9, 15, 38, 42, 999, tzinfo=UTC),
                text="The twt hash now uses the RFC 3339 timestamp format.",
                source=twtxt.models.Source(nick="nick", url="https://example.com/twtxt.txt"))))

    def test_rfc3339_timestamp_with_seconds_precision_and_utc_plus_1_offset_is_kept_intact(self):
        self.assertEqual("64u2m5a", create_hash(twtxt.models.Tweet(
                created_at=datetime.datetime(2020, 12, 9, 16, 38, 42, tzinfo=CET),
                text="The twt hash now uses the RFC 3339 timestamp format.",
                source=twtxt.models.Source(nick="nick", url="https://example.com/twtxt.txt"))))

    def test_rfc3339_timestamp_with_minutes_precision_is_expanded_to_seconds_precision(self):
        self.assertEqual("a3c3k5q", create_hash(twtxt.models.Tweet(
                created_at=datetime.datetime(2020, 12, 9, 16, 38, 0, tzinfo=CET),
                text="The twt hash now uses the RFC 3339 timestamp format.",
                source=twtxt.models.Source(nick="nick", url="https://example.com/twtxt.txt"))))

    def test_rfc3339_timestamp_with_utc_is_rendered_as_designated_zulu_offset_rather_than_numeric_offset(self):
        self.assertEqual("74qtyjq", create_hash(twtxt.models.Tweet(
                created_at=datetime.datetime(2020, 12, 9, 15, 38, 42, tzinfo=UTC),
                text="The twt hash now uses the RFC 3339 timestamp format.",
                source=twtxt.models.Source(nick="nick", url="https://example.com/twtxt.txt"))))
        self.assertEqual("74qtyjq", create_hash(twtxt.models.Tweet(
                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.",
                source=twtxt.models.Source(nick="nick", url="https://example.com/twtxt.txt"))))

    def test_rfc3339_timestamp_without_explicit_timezone_information_is_assumed_to_be_in_utc(self):
        self.assertEqual("74qtyjq", create_hash(twtxt.models.Tweet(
                created_at=datetime.datetime(2020, 12, 9, 15, 38, 42),
                text="The twt hash now uses the RFC 3339 timestamp format.",
                source=twtxt.models.Source(nick="nick", url="https://example.com/twtxt.txt"))))


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