aboutsummaryrefslogtreecommitdiff
path: root/tests/models/test_utils.py
blob: 8722a02b3f0c2b3c295271ddaef630f7af088001 (plain) (blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause

import uuid
from collections.abc import Callable
from datetime import UTC, datetime

import pytest

from webmentions_ssg.models.received import ReceivedWebmention
from webmentions_ssg.models.sources import SentWebmention, SourceDocument
from webmentions_ssg.models.utils import (
    source_target_digest,
    target_digest,
    uuid7_to_datetime,
)


def test_uuid7_to_datetime() -> None:
    identifier = uuid.uuid7()
    timestamp = uuid7_to_datetime(identifier)

    assert timestamp.tzinfo is UTC
    assert timestamp.timestamp() == pytest.approx(identifier.time / 1000)


@pytest.mark.parametrize(
    "factory",
    [
        pytest.param(
            lambda identifier: ReceivedWebmention(
                uuid=identifier,
                source="https://source.example/post",
                target="https://dennisfink.me/blog/example/",
            ),
            id="received-webmention",
        ),
        pytest.param(
            lambda identifier: SourceDocument(
                uuid=identifier,
                path="example/index.html",
                url="https://dennisfink.me/blog/example/",
                content_hash="0" * 32,
                revision=1,
                last_seen_at=datetime.now(UTC),
                revised_at=datetime.now(UTC),
            ),
            id="source",
        ),
        pytest.param(
            lambda identifier: SentWebmention(
                uuid=identifier, target="https://example.com/post", desired_revision=1
            ),
            id="sent-webmention",
        ),
    ],
)
def test_created_at_comes_from_uuid7_timestamp(
    factory: Callable[
        [uuid.UUID], ReceivedWebmention | SourceDocument | SentWebmention
    ],
) -> None:
    identifier = uuid.uuid7()
    model = factory(identifier)
    assert model.created_at == uuid7_to_datetime(identifier)


def test_received_pair_fingerprint_is_unambiguous() -> None:
    assert source_target_digest("a", "bc") != source_target_digest("ab", "c")
    assert source_target_digest("a", "b") != source_target_digest("b", "a")


def test_models_hash_url_fields_when_created_or_changed() -> None:
    received = ReceivedWebmention(
        uuid=uuid.uuid7(),
        source="https://source.example/a",
        target="https://site.example/b",
    )
    assert received.source_target_digest == source_target_digest(
        received.source, received.target
    )
    received.source = "https://source.example/c"
    assert received.source_target_digest == source_target_digest(
        received.source, received.target
    )
    received.target = "https://site.example/d"
    assert received.source_target_digest == source_target_digest(
        received.source, received.target
    )

    sent = SentWebmention(target="https://site.example/a", desired_revision=1)
    assert sent.target_digest == target_digest(sent.target)
    sent.target = "https://site.example/b"
    assert sent.target_digest == target_digest(sent.target)