File: test_preserve_binary_data_in_xrecords.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 (57 lines) | stat: -rw-r--r-- 1,692 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
#  Copyright (c) 2020, Manfred Moitzi
#  License: MIT License
import pytest
import os
from io import StringIO
import ezdxf
from ezdxf.lldxf.tagwriter import TagWriter

CIVIL_3D = os.path.join(
    ezdxf.EZDXF_TEST_FILES, "AutodeskProducts/Civil3D_2018.dxf"
)


@pytest.fixture(scope="module")
def doc():
    try:
        # Recover mode is not necessary:
        return ezdxf.readfile(CIVIL_3D)
    except IOError:
        pytest.skip(f'File "{CIVIL_3D}" not found.')


@pytest.mark.parametrize("handle", ["22C1", "22C4", "22C7"])
def test_civil_3d_preserves_binary_data_in_xrecords(doc, handle):
    # Civil 3D stores binary data in XRECORDS as group code 300:
    xrecord = doc.entitydb[handle]
    bin_data = xrecord.tags[1]
    assert bin_data.code == 300
    data = bin_data.value.encode("utf8", errors="surrogateescape")
    assert b"\xed\xb7\x9d" in data, "expected preserved binary data"


BASEDIR = os.path.dirname(__file__)
DATADIR = "data"


@pytest.mark.parametrize(
    ("handle", "n"), [("22C1", 0), ("22C4", 1), ("22C7", 2)]
)
def test_dxf_export_xrecords_with_binary_data(doc, handle, n):
    def expected():
        filename = os.path.join(BASEDIR, DATADIR, f"XRECORD_{n}.bin")
        if not os.path.exists(filename):
            pytest.skip(f"File {filename} not found.")
        with open(filename, "rb") as fp:
            # normalize line endings and remove spaces
            return fp.read().replace(b"\r\n", b"\n").replace(b" ", b"")

    stream = StringIO()
    writer = TagWriter(stream)

    xrecord = doc.entitydb[handle]
    xrecord.export_dxf(writer)

    # remove spaces
    data = doc.encode(stream.getvalue()).replace(b" ", b"")
    assert data == expected()