Commit 31ca1d7f authored by John Tourtellott's avatar John Tourtellott Committed by Haocheng LIU
Browse files

Extend test to include component (attribute) and check resource loading

parent b86dd4eb
......@@ -42,7 +42,7 @@ namespace py = pybind11;
PySharedPtrClass< smtk::attribute::Attribute > pybind11_init_smtk_attribute_Attribute(py::module &m)
{
PySharedPtrClass< smtk::attribute::Attribute > instance(m, "Attribute");
PySharedPtrClass< smtk::attribute::Attribute, smtk::resource::Component > instance(m, "Attribute");
instance
.def(py::init<::smtk::attribute::Attribute const &>())
.def("deepcopy", (smtk::attribute::Attribute & (smtk::attribute::Attribute::*)(::smtk::attribute::Attribute const &)) &smtk::attribute::Attribute::operator=)
......
......@@ -12,6 +12,7 @@
#define pybind_smtk_attribute_ReferenceItemDefinition_h
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "smtk/attribute/ReferenceItemDefinition.h"
#include "smtk/attribute/ItemDefinition.h"
......
......@@ -44,7 +44,7 @@ if (SMTK_DATA_DIR)
# New-style tests that use smtk.testing.TestCase
foreach (test ${smtkAttributePythonNewDataTests})
smtk_add_test_python(${test}Py ${test}.py
-D ${SMTK_DATA_DIR})
--data-dir ${SMTK_DATA_DIR} --temp-dir ${CMAKE_BINARY_DIR}/Testing/Temporary)
set_tests_properties(${test}Py PROPERTIES SKIP_RETURN_CODE 125)
endforeach()
endif()
......@@ -20,6 +20,7 @@ import smtk
import smtk.attribute
import smtk.io
import smtk.operation
import smtk.resource
import smtk.testing
SIMATTS_FILENAME = 'simatts.sbi.smtk'
......@@ -56,6 +57,23 @@ class TestReadReferenceItem(unittest.TestCase):
self.assertFalse(err, logger.convertToString())
return att_resource
def read_json(self, filename):
input_path = os.path.join(smtk.testing.TEMP_DIR, filename)
read_op = self.op_manager.createOperation('smtk::operation::ReadResource')
read_params = read_op.parameters()
file_item = read_params.findFile('filename')
file_item.setValue(input_path)
result = read_op.operate()
outcome = result.findInt('outcome')
self.assertEqual(outcome.value(), OPERATION_SUCCEEDED)
resource_item = result.findResource("resource");
resource = resource_item.value()
self.assertIsNotNone(resource)
return resource
def write_json(self, resource, filename):
output_path = os.path.join(smtk.testing.TEMP_DIR, filename)
......@@ -69,6 +87,7 @@ class TestReadReferenceItem(unittest.TestCase):
result = write_op.operate()
outcome = result.findInt('outcome')
self.assertEqual(outcome.value(), OPERATION_SUCCEEDED)
print('Wrote', output_path)
def load_simatts(self, assign=True, write=True):
""""""
......@@ -78,6 +97,13 @@ class TestReadReferenceItem(unittest.TestCase):
self.sim_atts = self.read_xml(sim_template)
self.res_manager.add(self.sim_atts)
if assign:
# Create material attribute
defn = self.sim_atts.findDefinition('Material')
self.assertIsNotNone(defn)
att = self.sim_atts.createAttribute('a material', defn)
self.assertIsNotNone(att)
if write:
self.write_json(self.sim_atts, SIMATTS_FILENAME)
......@@ -87,11 +113,19 @@ class TestReadReferenceItem(unittest.TestCase):
<Definitions>
<AttDef Type="exp_att">
<ItemDefinitions>
<Resource Name="attributes">
<Resource Name="attribute-resource">
<Accepts>
<Resource Name="smtk::attribute::Resource"/>
<Resource Name="smtk::attribute::Resource" />
</Accepts>
</Resource>
<String Name="note">
<DefaultValue>xyzzy</DefaultValue>
</String>
<Component Name="attribute-component">
<Accepts>
<Resource Name="smtk::attribute::Resource" filter="*" />
</Accepts>
</Component>
</ItemDefinitions>
</AttDef>
</Definitions>
......@@ -104,13 +138,22 @@ class TestReadReferenceItem(unittest.TestCase):
self.assertFalse(err, logger.convertToString())
self.res_manager.add(self.sim_atts)
# Create attribute and assign "attributes" item
# Create attribute and assign reference items
defn = self.exp_atts.findDefinition('exp_att')
att = self.exp_atts.createAttribute('exp_att', defn)
if assign:
res_item = att.findResource('attributes')
# Assign attribute-resource
res_item = att.findResource('attribute-resource')
ok = res_item.setValue(self.sim_atts)
self.assertTrue(ok)
# Assign attribute component
comp_item = att.findComponent('attribute-component')
mat = self.sim_atts.findAttribute('a material')
self.assertIsNotNone(mat)
ok = comp_item.setValue(mat)
self.assertTrue(ok)
self.res_manager.add(self.exp_atts)
if write:
......@@ -120,23 +163,37 @@ class TestReadReferenceItem(unittest.TestCase):
att = exp_atts.findAttribute('exp_att')
self.assertIsNotNone(att)
res_item = att.findResource('attributes')
res_item = att.findResource('attribute-resource')
self.assertIsNotNone(res_item)
self.assertEqual(res_item.numberOfValues(), 1)
# Resource should be gone
# The simatts resource should be loaded
res = res_item.value()
self.assertIsNone(res)
self.assertIsNotNone(res)
# Check the component reference
comp_item = att.findComponent('attribute-component')
self.assertIsNotNone(comp_item)
self.assertEqual(comp_item.numberOfValues(), 1)
comp = comp_item.value()
self.assertIsNotNone(comp)
def remove_temp_files(self, filenames):
# for filename in filenames:
# path = os.path.join(smtk.testing.TEMP_DIR, filename)
# if os.path.exists(path):
# print('Deleting {}'.format(path))
# shutil.rm(path)
return
def test_readXML(self):
debug_mode = True
if debug_mode:
print('NOT removing output files (debug_mode):', filenames)
return
# (else)
for filename in filenames:
path = os.path.join(smtk.testing.TEMP_DIR, filename)
if os.path.exists(path):
print('Deleting {}'.format(path))
shutil.rm(path)
@unittest.skip('ok')
def test_xmlIO(self):
"""Sanity check xml code"""
expatts_xml_filename = 'expatts.sbi'
try:
self.load_simatts(assign=True, write=False)
......@@ -156,13 +213,15 @@ class TestReadReferenceItem(unittest.TestCase):
# Read back exp_atts file
exp_atts = self.read_xml(path)
self.check_expatts(exp_atts)
is_loaded = False
self.check_expatts(exp_atts, is_loaded)
except:
raise
finally:
self.remove_temp_files([expatts_xml_filename])
def test_readJSON(self):
def test_jsonIO(self):
"""Test reference items with json I/O"""
try:
self.load_simatts()
self.init_expatts()
......@@ -174,18 +233,13 @@ class TestReadReferenceItem(unittest.TestCase):
self.exp_atts = None
# Read back exp_atts file
input_path = os.path.join(smtk.testing.TEMP_DIR, EXPATTS_FILENAME)
read_op = self.op_manager.createOperation('smtk::operation::ReadResource')
read_params = read_op.parameters()
file_item = read_params.findFile('filename')
file_item.setValue(input_path)
result = read_op.operate()
outcome = result.findInt('outcome')
self.assertEqual(outcome.value(), OPERATION_SUCCEEDED)
exp_atts = result.findResource('resource').value()
self.assertIsNotNone(exp_atts)
exp_atts = self.read_json(EXPATTS_FILENAME)
# For test/debug only
check_path = os.path.join(smtk.testing.TEMP_DIR, 'check_expatts.smtk')
self.write_json(exp_atts, check_path)
self.res_manager.add(exp_atts)
self.check_expatts(exp_atts)
except:
raise
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment