File: test_413_dxfgroups.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 (83 lines) | stat: -rw-r--r-- 2,257 bytes parent folder | download
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
# Copyright (c) 2020-2021, Manfred Moitzi
# License: MIT License

import pytest
import ezdxf
from ezdxf.audit import Auditor


@pytest.fixture(scope="module")
def doc():
    return ezdxf.new()


def test_new_group(doc):
    msp = doc.modelspace()
    group = doc.groups.new("test1")
    with group.edit_data() as g:
        g.append(msp.add_point((0, 0)))
        g.append(msp.add_line((1, 1), (2, 2)))
    assert len(group) == 2
    assert group[0].dxftype() == "POINT"
    assert group[1].dxftype() == "LINE"
    assert len(list(group.handles())) == 2
    handle = group[0].dxf.handle
    assert handle in group


def test_unique_groups(doc):
    doc.groups.new("test2")
    with pytest.raises(ValueError):
        doc.groups.new("test2")


def test_modify_group(doc):
    msp = doc.modelspace()
    group = doc.groups.new("test3")
    with group.edit_data() as g:
        g.append(msp.add_point((0, 0)))
        g.append(msp.add_line((1, 1), (2, 2)))
    assert len(group) == 2
    e = [
        msp.add_point((3, 3)),
        msp.add_point((4, 4)),
    ]
    group.extend(e)
    assert len(group) == 4


def test_can_not_add_invalid_block_entities(doc):
    group = doc.groups.new("test4")
    block = doc.blocks.new("Block4")
    point = block.add_point((0, 0))
    with pytest.raises(ezdxf.DXFStructureError):
        with group.edit_data() as g:
            g.append(point)


def test_can_not_add_invalid_table_entry(doc):
    group = doc.groups.new("test5")
    layer = doc.layers.get("0")
    with pytest.raises(ezdxf.DXFStructureError):
        with group.edit_data() as g:
            g.append(layer)


def test_audit_filters_invalid_entities(doc):
    group = doc.groups.new("test6")
    msp = doc.modelspace()
    block = doc.blocks.new("Block6")
    point1 = block.add_point((0, 0))  # invalid BLOCK entity
    point2 = msp.add_point((0, 0))  # valid model space entity ...
    point2.destroy()  # ... but destroyed
    layer = doc.layers.get("0")  # invalid table entry

    # hack, hack, hack - but since 2024-05-23 group.extend() validates the input data!
    group._data.extend([point1, point2, layer])
    auditor = Auditor(doc)
    group.audit(auditor)
    assert len(group) == 0


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