aboutsummaryrefslogtreecommitdiff
path: root/server/szurubooru/tests/func/test_snapshots_transactional_isolation.py
blob: b98cea7aeb731cf1085e674f4a55254faed60696 (plain)
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
from unittest.mock import patch

import pytest

from szurubooru import db, model
from szurubooru.func import snapshots


@pytest.fixture(autouse=True)
def session(query_logger, postgresql_db):
    """
    Override db session for this specific test section only
    """
    db.session = postgresql_db.session
    postgresql_db.create_table(*model.Base.metadata.sorted_tables)
    try:
        yield postgresql_db.session
    finally:
        postgresql_db.reset_db()


def test_modify_saves_non_empty_diffs(post_factory, user_factory):
    if "sqlite" in db.session.get_bind().driver:
        pytest.xfail(
            "SQLite doesn't support transaction isolation, "
            "which is required to retrieve original entity"
        )
    post = post_factory()
    post.notes = [model.PostNote(polygon=[(0, 0), (0, 1), (1, 1)], text="old")]
    user = user_factory()
    db.session.add_all([post, user])
    db.session.commit()
    post.source = "new source"
    post.notes = [model.PostNote(polygon=[(0, 0), (0, 1), (1, 1)], text="new")]
    db.session.flush()
    with patch("szurubooru.func.snapshots._post_to_webhooks"):
        snapshots.modify(post, user)
        db.session.flush()
        results = db.session.query(model.Snapshot).all()
        assert len(results) == 1
        assert results[0].data == {
            "type": "object change",
            "value": {
                "source": {
                    "type": "primitive change",
                    "old-value": None,
                    "new-value": "new source",
                },
                "notes": {
                    "type": "list change",
                    "removed": [
                        {"polygon": [[0, 0], [0, 1], [1, 1]], "text": "old"}
                    ],
                    "added": [
                        {"polygon": [[0, 0], [0, 1], [1, 1]], "text": "new"}
                    ],
                },
            },
        }

© 2015 - 2026 Jakob L. Kreuze