wcs/tests/test_block.py

55 lines
1.5 KiB
Python

import xml.etree.ElementTree as ET
import pytest
from wcs.blocks import BlockDef
from wcs.categories import BlockCategory
from wcs.qommon.misc import indent_xml as indent
from .utilities import clean_temporary_pub, create_temporary_pub
@pytest.fixture
def pub(request):
return create_temporary_pub()
def teardown_module(module):
clean_temporary_pub()
def export_to_indented_xml(block, include_id=False):
block_xml = block.export_to_xml(include_id=include_id)
indent(block_xml)
return block_xml
def assert_import_export_works(block, include_id=False):
block2 = BlockDef.import_from_xml_tree(
ET.fromstring(ET.tostring(block.export_to_xml(include_id))), include_id
)
assert ET.tostring(export_to_indented_xml(block)) == ET.tostring(export_to_indented_xml(block2))
return block2
def test_block(pub):
block = BlockDef(name='test')
assert_import_export_works(block, include_id=True)
def test_block_with_category(pub):
category = BlockCategory(name='test category')
category.store()
block = BlockDef(name='test category')
block.category_id = category.id
block.store()
block2 = assert_import_export_works(block, include_id=True)
assert block2.category_id == block.category_id
# import with non existing category
BlockCategory.wipe()
export = ET.tostring(block.export_to_xml(include_id=True))
block3 = BlockDef.import_from_xml_tree(ET.fromstring(export), include_id=True)
assert block3.category_id is None