1# Created: 03.04.2011
2# Copyright (c) 2011-2020, Manfred Moitzi
3# License: MIT License
4import pytest
5import ezdxf
6
7
8@pytest.fixture
9def doc():
10    return ezdxf.new()
11
12
13@pytest.fixture
14def block(doc):
15    block = doc.blocks.new('TEST')
16    block.add_attdef('TAG1', (0, 0))
17    block.add_attdef('TAG2', (0, 5))
18    return block
19
20
21def test_auto_blockref(doc, block):
22    values = {'TAG1': 'text1', 'TAG2': 'text2'}
23    msp = doc.modelspace()
24    blockref = msp.add_auto_blockref('TEST', (0, 0), values)
25    autoblock = doc.blocks[blockref.dxf.name]
26    entities = list(autoblock)
27    insert = entities[0]
28    assert insert.dxftype() == 'INSERT'
29
30    attrib1, attrib2 = insert.attribs
31    assert attrib1.dxftype() == 'ATTRIB'
32    assert attrib1.dxf.tag == 'TAG1'
33    assert attrib1.dxf.text == 'text1'
34    assert attrib2.dxftype() == 'ATTRIB'
35    assert attrib2.dxf.tag == 'TAG2'
36    assert attrib2.dxf.text == 'text2'
37
38
39def test_blockref_with_attribs(doc, block):
40    values = {'TAG1': 'text1', 'TAG2': 'text2'}
41    msp = doc.modelspace()
42    blockref = msp.add_blockref('TEST', (0, 0)).add_auto_attribs(values)
43    assert blockref.dxftype() == 'INSERT'
44
45    attrib1, attrib2 = blockref.attribs
46    assert attrib1.dxftype() == 'ATTRIB'
47    assert attrib1.dxf.tag == 'TAG1'
48    assert attrib1.dxf.text == 'text1'
49    assert attrib2.dxftype() == 'ATTRIB'
50    assert attrib2.dxf.tag == 'TAG2'
51    assert attrib2.dxf.text == 'text2'
52
53
54def test_has_attdef(block):
55    assert block.has_attdef('TAG1') is True
56    assert block.has_attdef('TAG_Z') is False
57
58
59def test_get_attdef(block):
60    attdef = block.get_attdef('TAG1')
61    assert attdef.dxf.tag == 'TAG1'
62    assert block.get_attdef('TAG1_Z') is None
63
64
65def test_get_attdef_text(block):
66    block.add_attdef('TAGX', insert=(0, 0), text='PRESET_TEXT')
67    text = block.get_attdef_text('TAGX')
68    assert text == 'PRESET_TEXT'
69
70
71def test_attdef_getter_properties(block):
72    attrib = block.get_attdef('TAG1')
73
74    assert attrib.is_const is False
75    assert attrib.is_invisible is False
76    assert attrib.is_verify is False
77    assert attrib.is_preset is False
78
79
80def test_attdef_setter_properties(block):
81    attrib = block.get_attdef('TAG1')
82
83    assert attrib.is_const is False
84    attrib.is_const = True
85    assert attrib.is_const is True
86
87    assert attrib.is_invisible is False
88    attrib.is_invisible = True
89    assert attrib.is_invisible is True
90
91    assert attrib.is_verify is False
92    attrib.is_verify = True
93    assert attrib.is_verify is True
94
95    assert attrib.is_preset is False
96    attrib.is_preset = True
97    assert attrib.is_preset is True
98