File: test_426_ezdxf_metadata.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (102 lines) | stat: -rw-r--r-- 2,879 bytes parent folder | download | duplicates (2)
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
96
97
98
99
100
101
102
#  Copyright (c) 2021, Manfred Moitzi
#  License: MIT License

import pytest
import ezdxf
from ezdxf.document import CREATED_BY_EZDXF, WRITTEN_BY_EZDXF


@pytest.fixture(params=["R12", "R2000"])
def doc(request):
    return ezdxf.new(request.param)


def test_created_by_ezdxf_metadata(doc):
    metadata = doc.ezdxf_metadata()
    assert CREATED_BY_EZDXF in metadata
    assert metadata[CREATED_BY_EZDXF].startswith(ezdxf.__version__)


def test_written_by_ezdxf_metadata(doc, tmp_path):
    doc.saveas(tmp_path / "ez.dxf")
    metadata = doc.ezdxf_metadata()
    assert WRITTEN_BY_EZDXF in metadata
    assert metadata[WRITTEN_BY_EZDXF].startswith(ezdxf.__version__)


def test_add_custom_metadata(doc):
    custom = "CUSTOM"
    metadata = doc.ezdxf_metadata()
    metadata[custom] = custom
    assert custom in metadata
    assert metadata[custom] == custom


def test_non_existing_key_raises_key_error(doc):
    metadata = doc.ezdxf_metadata()
    with pytest.raises(KeyError):
        _ = metadata["XY_UNKNOWN"]


def test_modify_metadata(doc):
    modified = "MODIFIED"
    metadata = doc.ezdxf_metadata()
    metadata[CREATED_BY_EZDXF] = modified
    assert metadata[CREATED_BY_EZDXF] == modified


def test_delete_metadata(doc):
    metadata = doc.ezdxf_metadata()
    del metadata[CREATED_BY_EZDXF]
    assert metadata.get(CREATED_BY_EZDXF) == ""
    assert CREATED_BY_EZDXF not in metadata


def test_delete_non_existing_metadata_raises_KeyError(doc):
    metadata = doc.ezdxf_metadata()
    with pytest.raises(KeyError):
        del metadata["XY_UNKNOWN"]


def test_discard_non_existing_metadata_without_exception(doc):
    metadata = doc.ezdxf_metadata()
    metadata.discard("XY_UNKNOWN")
    assert "XY_UNKNOWN" not in metadata


def test_write_and_read_metadata(doc, tmp_path):
    custom = "CUSTOM"
    metadata = doc.ezdxf_metadata()
    del metadata[CREATED_BY_EZDXF]
    metadata[custom] = custom
    doc.saveas(tmp_path / "ez.dxf")

    doc2 = ezdxf.readfile(tmp_path / "ez.dxf")
    metadata2 = doc2.ezdxf_metadata()
    assert metadata2[WRITTEN_BY_EZDXF].startswith(ezdxf.__version__)
    assert CREATED_BY_EZDXF not in metadata2, "should be deleted"
    assert metadata2[custom] == custom, "expected custom metadata"


def test_key_and_data_is_limited_to_255_chars(doc):
    custom = "CUSTOM" * 60
    metadata = doc.ezdxf_metadata()
    metadata[custom] = custom
    assert custom in metadata
    # max string length for DXF R12 without line endings
    custom255 = custom[:255]
    assert metadata[custom255] == custom255


def test_escape_line_endings_in_key_and_data(doc):
    custom = "CUSTOM\n"
    metadata = doc.ezdxf_metadata()
    metadata[custom] = custom
    assert custom in metadata
    assert (
        metadata[custom] == "CUSTOM\\P"
    ), "line ending should be replaced by \\P"


if __name__ == "__main__":
    pytest.main([__file__])