Пример #1
0
    def test_getNodeFromStrSpec(self):
        # Try all kinds of combinations of good and bad input
        # For both an original model file, and a flattened input file.
        for xmlRoot in [self.flatXMLRoot, self.inXMLRoot]:
            # Basic elements - good cases
            node = stgxml.getNodeFromStrSpec(xmlRoot, "dim")
            self.assertEqual(node.attrib['name'], "dim")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertEqual(node.text.strip(), "2")
            node = stgxml.getNodeFromStrSpec(xmlRoot, "velocityICs")
            self.assertEqual(node.attrib['name'], "velocityICs")
            self.assertEqual(stgxml.getElementType(node),
                             stgxml.STG_STRUCT_TAG)
            # Basic elements - bad cases
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "voodoo")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              ".velocityICs")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "..")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              ".[")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              ".[]")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "[]55")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "[].")
            # TODO: figure out how to handle these below - allowable on recursion,
            # but not from the root.
            #self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
            #    xmlRoot, "[]")
            #self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
            #    xmlRoot, "[0]")

            # Struct recursion - good cases
            node = stgxml.getNodeFromStrSpec(xmlRoot, "velocityICs.type")
            self.assertEqual(node.attrib['name'], "type")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertEqual(node.text.strip(), "CompositeVC")
            node = stgxml.getNodeFromStrSpec(xmlRoot, "velocityICs.vcList")
            self.assertEqual(node.attrib['name'], "vcList")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_LIST_TAG)
            # Struct recursion - bad cases
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "velocityICs.")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "velocityICs.voodoo")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "velocityICs.[")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "velocityICs.]")

            # Struct - list recursion - good cases
            node = stgxml.getNodeFromStrSpec(xmlRoot, "velocityICs.vcList[0]")
            self.assertEqual(stgxml.getElementType(node),
                             stgxml.STG_STRUCT_TAG)
            self.assertTrue('name' not in node.attrib)
            self.assertEqual(node[0].text.strip(), "AllNodesVC")
            # Struct - list recursion - bad cases
            # In this case, list is only of length 1.
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "velocityICs.vcList[1]")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "velocityICs.vcList[455]")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "velocityICs.vcList[")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "velocityICs.vcList[455")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "velocityICs.vcList[455]voodoo")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "velocityICs.vcList[455]voodoo.jabbar")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "velocityICs.vcList[.]")
            # TODO: Special case - probably need a "listAppendMode" flag
            #  to decide if this is ok or not
            #self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
            #    xmlRoot, "velocityICs.vcList[]")

            # Deep recursion - good cases
            node = stgxml.getNodeFromStrSpec(xmlRoot,
                                             "temperatureBCs.vcList[0].Shape")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertEqual(node.attrib['name'], "Shape")
            self.assertEqual(node.text.strip(), "initialConditionShape")
            node = stgxml.getNodeFromStrSpec(
                xmlRoot, "temperatureBCs.vcList[0].variables")
            self.assertEqual(node.attrib['name'], "variables")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_LIST_TAG)
            node = stgxml.getNodeFromStrSpec(
                xmlRoot, "temperatureBCs.vcList[0].variables[0]")
            self.assertEqual(stgxml.getElementType(node),
                             stgxml.STG_STRUCT_TAG)
            self.assertTrue('name' not in node.attrib)
            self.assertEqual(node[0].text.strip(), "temperature")
            node = stgxml.getNodeFromStrSpec(
                xmlRoot, "temperatureBCs.vcList[0].variables[0].value")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertEqual(node.attrib['name'], "value")
            self.assertEqual(node.text.strip(), "Temperature_CosineHill")
            # Deep recursion - bad cases
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "temperatureBCs.vcList[0].variables[0]value")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "temperatureBCs.vcList[0].variables[0].val")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec, xmlRoot,
                              "temperatureBCs.vcList[0][0].val")

            # Getting components, plugins, import (special tags, that will be
            # different in the flattened versus import.
            #import
            node = stgxml.getNodeFromStrSpec(xmlRoot, "import")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_LIST_TAG)
            self.assertEqual(node[0].text.strip(), "StgFEM")
            node = stgxml.getNodeFromStrSpec(xmlRoot, "import[0]")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertTrue('name' not in node.attrib)
            self.assertEqual(node.text.strip(), "StgFEM")
            #plugins
            node = stgxml.getNodeFromStrSpec(xmlRoot, "plugins")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_LIST_TAG)
            self.assertEqual(stgxml.getElementType(node[0]),
                             stgxml.STG_STRUCT_TAG)
            node = stgxml.getNodeFromStrSpec(xmlRoot, "plugins[0].Type")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertEqual(node.text.strip(),
                             "StgFEM_StandardConditionFunctions")
            #components
            node = stgxml.getNodeFromStrSpec(xmlRoot, "components")
            self.assertEqual(stgxml.getElementType(node),
                             stgxml.STG_STRUCT_TAG)
            self.assertEqual(node[0].attrib['name'], "context")
            node = stgxml.getNodeFromStrSpec(xmlRoot,
                                             "components.context.Type")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertEqual(node.attrib['name'], "Type")
            self.assertEqual(node.text.strip(), "FiniteElementContext")
    def test_getNodeFromStrSpec(self):
        # Try all kinds of combinations of good and bad input
        # For both an original model file, and a flattened input file.
        for xmlRoot in [self.flatXMLRoot, self.inXMLRoot]:
            # Basic elements - good cases
            node = stgxml.getNodeFromStrSpec(xmlRoot, "dim")
            self.assertEqual(node.attrib['name'], "dim")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertEqual(node.text.strip(), "2")
            node = stgxml.getNodeFromStrSpec(xmlRoot, "velocityICs")
            self.assertEqual(node.attrib['name'], "velocityICs")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_STRUCT_TAG)
            # Basic elements - bad cases
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "voodoo")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, ".velocityICs")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "..")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, ".[")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, ".[]")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "[]55")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "[].")
            # TODO: figure out how to handle these below - allowable on recursion,
            # but not from the root.
            #self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
            #    xmlRoot, "[]")
            #self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
            #    xmlRoot, "[0]")
            
            # Struct recursion - good cases
            node = stgxml.getNodeFromStrSpec(
                xmlRoot, "velocityICs.type")
            self.assertEqual(node.attrib['name'], "type")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertEqual(node.text.strip(), "CompositeVC")
            node = stgxml.getNodeFromStrSpec(xmlRoot,
                "velocityICs.vcList")
            self.assertEqual(node.attrib['name'], "vcList")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_LIST_TAG)
            # Struct recursion - bad cases
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "velocityICs.")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "velocityICs.voodoo")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "velocityICs.[")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "velocityICs.]")

            # Struct - list recursion - good cases
            node = stgxml.getNodeFromStrSpec(
                xmlRoot, "velocityICs.vcList[0]")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_STRUCT_TAG)
            self.assertTrue('name' not in node.attrib)
            self.assertEqual(node[0].text.strip(), "AllNodesVC")
            # Struct - list recursion - bad cases
            # In this case, list is only of length 1.
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "velocityICs.vcList[1]")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "velocityICs.vcList[455]")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "velocityICs.vcList[")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "velocityICs.vcList[455")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "velocityICs.vcList[455]voodoo")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "velocityICs.vcList[455]voodoo.jabbar")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "velocityICs.vcList[.]")
            # TODO: Special case - probably need a "listAppendMode" flag
            #  to decide if this is ok or not
            #self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
            #    xmlRoot, "velocityICs.vcList[]")

            # Deep recursion - good cases
            node = stgxml.getNodeFromStrSpec(
                xmlRoot, "temperatureBCs.vcList[0].Shape")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertEqual(node.attrib['name'], "Shape")
            self.assertEqual(node.text.strip(), "initialConditionShape")
            node = stgxml.getNodeFromStrSpec(
                xmlRoot, "temperatureBCs.vcList[0].variables")
            self.assertEqual(node.attrib['name'], "variables")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_LIST_TAG)
            node = stgxml.getNodeFromStrSpec(
                xmlRoot, "temperatureBCs.vcList[0].variables[0]")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_STRUCT_TAG)
            self.assertTrue('name' not in node.attrib)
            self.assertEqual(node[0].text.strip(), "temperature")
            node = stgxml.getNodeFromStrSpec(
                xmlRoot, "temperatureBCs.vcList[0].variables[0].value")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertEqual(node.attrib['name'], "value")
            self.assertEqual(node.text.strip(), "Temperature_CosineHill")
            # Deep recursion - bad cases
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "temperatureBCs.vcList[0].variables[0]value")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "temperatureBCs.vcList[0].variables[0].val")
            self.assertRaises(ValueError, stgxml.getNodeFromStrSpec,
                xmlRoot, "temperatureBCs.vcList[0][0].val")
            
            # Getting components, plugins, import (special tags, that will be 
            # different in the flattened versus import.
            #import
            node = stgxml.getNodeFromStrSpec( xmlRoot, "import")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_LIST_TAG)
            self.assertEqual(node[0].text.strip(), "StgFEM")
            node = stgxml.getNodeFromStrSpec( xmlRoot, "import[0]")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertTrue('name' not in node.attrib)
            self.assertEqual(node.text.strip(), "StgFEM")
            #plugins
            node = stgxml.getNodeFromStrSpec( xmlRoot, "plugins")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_LIST_TAG)
            self.assertEqual(stgxml.getElementType(node[0]),
                stgxml.STG_STRUCT_TAG)
            node = stgxml.getNodeFromStrSpec( xmlRoot, "plugins[0].Type")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertEqual(node.text.strip(),
                "StgFEM_StandardConditionFunctions")
            #components
            node = stgxml.getNodeFromStrSpec( xmlRoot, "components")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_STRUCT_TAG)
            self.assertEqual(node[0].attrib['name'], "context")
            node = stgxml.getNodeFromStrSpec(
                xmlRoot, "components.context.Type")
            self.assertEqual(stgxml.getElementType(node), stgxml.STG_PARAM_TAG)
            self.assertEqual(node.attrib['name'], "Type")
            self.assertEqual(node.text.strip(), "FiniteElementContext")
Пример #3
0
    def test_getElementType(self):
        # Regular params syntax
        testNode = etree.Element(stgxml.STG_PARAM_TAG, name="dim")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_PARAM_TAG)
        testNode = etree.Element(stgxml.STG_LIST_TAG, name="test")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml.STG_STRUCT_TAG, name="test")
        self.assertEqual(stgxml.getElementType(testNode),
                         stgxml.STG_STRUCT_TAG)
        # bad case
        testNode = etree.Element("sillyTag", name="test")
        self.assertRaises(ValueError, stgxml.getElementType, testNode)

        # Element params syntax
        testNode = etree.Element(stgxml.STG_ELEMENT_TAG,
                                 type=stgxml.STG_PARAM_TAG,
                                 name="dim")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_PARAM_TAG)
        testNode = etree.Element(stgxml.STG_ELEMENT_TAG,
                                 type=stgxml.STG_LIST_TAG,
                                 name="test")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml.STG_ELEMENT_TAG,
                                 type=stgxml.STG_STRUCT_TAG,
                                 name="test")
        self.assertEqual(stgxml.getElementType(testNode),
                         stgxml.STG_STRUCT_TAG)
        # Element params syntax - with LXML NS included
        testNode = etree.Element(stgxml._STG_NS_LXML + stgxml.STG_ELEMENT_TAG,
                                 type=stgxml.STG_PARAM_TAG,
                                 name="dim")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_PARAM_TAG)
        testNode = etree.Element(stgxml._STG_NS_LXML + stgxml.STG_ELEMENT_TAG,
                                 type=stgxml.STG_LIST_TAG,
                                 name="test")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml._STG_NS_LXML + stgxml.STG_ELEMENT_TAG,
                                 type=stgxml.STG_STRUCT_TAG,
                                 name="test")
        self.assertEqual(stgxml.getElementType(testNode),
                         stgxml.STG_STRUCT_TAG)
        # bad cases
        testNode = etree.Element(stgxml.STG_ELEMENT_TAG, name="test")
        self.assertRaises(ValueError, stgxml.getElementType, testNode)
        testNode = etree.Element(stgxml.STG_ELEMENT_TAG,
                                 type="sillyType",
                                 name="test")
        self.assertRaises(ValueError, stgxml.getElementType, testNode)

        # Special params - should be recognised
        testNode = etree.Element(stgxml.STG_IMPORT_TAG)
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml.STG_PLUGINS_TAG)
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml.STG_COMPONENTS_TAG)
        self.assertEqual(stgxml.getElementType(testNode),
                         stgxml.STG_STRUCT_TAG)
        # Special params - NS prefixed
        testNode = etree.Element(stgxml._STG_NS_LXML + stgxml.STG_IMPORT_TAG)
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml._STG_NS_LXML + stgxml.STG_PLUGINS_TAG)
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml._STG_NS_LXML +
                                 stgxml.STG_COMPONENTS_TAG)
        self.assertEqual(stgxml.getElementType(testNode),
                         stgxml.STG_STRUCT_TAG)
    def test_getElementType(self):
        # Regular params syntax
        testNode = etree.Element(stgxml.STG_PARAM_TAG, name="dim")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_PARAM_TAG)
        testNode = etree.Element(stgxml.STG_LIST_TAG, name="test")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml.STG_STRUCT_TAG, name="test")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_STRUCT_TAG)
        # bad case
        testNode = etree.Element("sillyTag", name="test")
        self.assertRaises(ValueError, stgxml.getElementType, testNode)

        # Element params syntax
        testNode = etree.Element(stgxml.STG_ELEMENT_TAG,
            type=stgxml.STG_PARAM_TAG, name="dim")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_PARAM_TAG)
        testNode = etree.Element(stgxml.STG_ELEMENT_TAG,
            type=stgxml.STG_LIST_TAG, name="test")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml.STG_ELEMENT_TAG,
            type=stgxml.STG_STRUCT_TAG, name="test")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_STRUCT_TAG)
        # Element params syntax - with LXML NS included
        testNode = etree.Element(stgxml._STG_NS_LXML+stgxml.STG_ELEMENT_TAG,
            type=stgxml.STG_PARAM_TAG, name="dim")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_PARAM_TAG)
        testNode = etree.Element(stgxml._STG_NS_LXML+stgxml.STG_ELEMENT_TAG,
            type=stgxml.STG_LIST_TAG, name="test")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml._STG_NS_LXML+stgxml.STG_ELEMENT_TAG,
            type=stgxml.STG_STRUCT_TAG, name="test")
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_STRUCT_TAG)
        # bad cases
        testNode = etree.Element(stgxml.STG_ELEMENT_TAG, name="test")
        self.assertRaises(ValueError, stgxml.getElementType, testNode)
        testNode = etree.Element(stgxml.STG_ELEMENT_TAG,
            type="sillyType", name="test")
        self.assertRaises(ValueError, stgxml.getElementType, testNode)

        # Special params - should be recognised
        testNode = etree.Element(stgxml.STG_IMPORT_TAG)
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml.STG_PLUGINS_TAG)
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml.STG_COMPONENTS_TAG)
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_STRUCT_TAG)
        # Special params - NS prefixed
        testNode = etree.Element(stgxml._STG_NS_LXML+stgxml.STG_IMPORT_TAG)
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml._STG_NS_LXML+stgxml.STG_PLUGINS_TAG)
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_LIST_TAG)
        testNode = etree.Element(stgxml._STG_NS_LXML+stgxml.STG_COMPONENTS_TAG)
        self.assertEqual(stgxml.getElementType(testNode), stgxml.STG_STRUCT_TAG)