def setUp(self):
        """
        doc -- <section foo> -- <section subfoo>
                        \
                         -- <section bar> -- <section subbar>

        """
        doc = Document("author")

        foo = Section("foo", "footype")
        doc.append(foo)
        foo.append(Property("strprop", "somestring"))
        foo.append(Property("txtprop", "some\ntext"))

        subfoo = Section("subfoo", "footype")
        foo.append(subfoo)
        subfoo.append(Property("strprop", "somestring"))
        subfoo.append(Property("txtprop", "some\ntext"))

        bar = Section("bar", "bartype")
        foo.append(bar)
        bar.append(Property("strprop", "otherstring"))
        bar.append(Property("txtprop", "other\ntext"))

        subbar = Section("subbar", "bartype")
        bar.append(subbar)
        subbar.append(Property("strprop", "otherstring"))
        subbar.append(Property("txtprop", "other\ntext"))

        self.doc = doc
Beispiel #2
0
    def test_insert(self):
        doc = Document()

        sec_one = Section(name="sec_one", parent=doc)
        sec_two = Section(name="sec_two", parent=doc)
        subsec = Section(name="sec_three")

        self.assertNotEqual(doc.sections[1].name, subsec.name)
        doc.insert(1, subsec)
        self.assertEqual(len(doc.sections), 3)
        self.assertEqual(doc.sections[1].name, subsec.name)
        self.assertEqual(doc.sections[0].name, sec_one.name)
        self.assertEqual(doc.sections[2].name, sec_two.name)
        self.assertEqual(subsec.parent, doc)

        # Test invalid object
        with self.assertRaises(ValueError):
            doc.insert(1, Document())
        with self.assertRaises(ValueError):
            doc.insert(1, Property(name="prop_one"))
        with self.assertRaises(ValueError):
            doc.insert(1, "some info")
        self.assertEqual(len(doc), 3)

        # Test same name entries
        with self.assertRaises(ValueError):
            doc.insert(0, subsec)
        self.assertEqual(len(doc), 3)
    def setUp(self):
        """
        doc -- <section sec_main> -- <section sub_main>
                        \
                         -- <section sec_branch> -- <section sub_branch>
        """
        doc = Document("author")

        sec_main = Section("sec_main", "maintype")
        doc.append(sec_main)
        sec_main.append(Property("strprop", "somestring"))
        sec_main.append(Property("txtprop", "some\ntext"))

        sub_main = Section("sub_main", "maintype")
        sec_main.append(sub_main)
        sub_main.append(Property("strprop", "somestring"))
        sub_main.append(Property("txtprop", "some\ntext"))

        sec_branch = Section("sec_branch", "branchtype")
        sec_main.append(sec_branch)
        sec_branch.append(Property("strprop", "otherstring"))
        sec_branch.append(Property("txtprop", "other\ntext"))

        sub_branch = Section("sub_branch", "branchtype")
        sec_branch.append(sub_branch)
        sub_branch.append(Property("strprop", "otherstring"))
        sub_branch.append(Property("txtprop", "other\ntext"))

        self.doc = doc
Beispiel #4
0
    def test_sections_cardinality(self):
        """
        Tests the basic assignment rules for Section sections cardinality
        on init and re-assignment but does not test sections assignment or
        the actual cardinality validation.
        """
        doc = Document()

        # -- Test set cardinality on Section init
        # Test empty init
        sec_card_none = Section(name="sec_cardinality_none", type="test", parent=doc)
        self.assertIsNone(sec_card_none.sec_cardinality)

        # Test single int max init
        sec_card_max = Section(name="sec_cardinality_max", sec_cardinality=10, parent=doc)
        self.assertEqual(sec_card_max.sec_cardinality, (None, 10))

        # Test tuple init
        sec_card_min = Section(name="sec_cardinality_min", sec_cardinality=(2, None), parent=doc)
        self.assertEqual(sec_card_min.sec_cardinality, (2, None))

        # -- Test Section properties cardinality re-assignment
        sec = Section(name="sec", sec_cardinality=(None, 10), parent=doc)
        self.assertEqual(sec.sec_cardinality, (None, 10))

        # Use general method to reduce redundancy
        self._test_cardinality_re_assignment(sec, 'sec_cardinality')
Beispiel #5
0
    def test_remove(self):
        sec = Section(name="remsec")

        ssec_one = Section(name="subsec_one", parent=sec)
        ssec_two = Section(name="subsec_two", parent=sec)
        self.assertEqual(len(sec.sections), 2)
        self.assertIsNotNone(ssec_one.parent)

        sec.remove(ssec_one)
        self.assertEqual(len(sec.sections), 1)
        self.assertEqual(sec.sections[0].name, ssec_two.name)
        self.assertIsNone(ssec_one.parent)

        with self.assertRaises(ValueError):
            sec.remove(ssec_one)
        self.assertEqual(len(sec.sections), 1)

        prop_one = Property(name="prop_one", parent=sec)
        prop_two = Property(name="prop_two", parent=sec)
        self.assertEqual(len(sec.properties), 2)
        self.assertIsNotNone(prop_one.parent)

        sec.remove(prop_one)
        self.assertEqual(len(sec.properties), 1)
        self.assertEqual(sec.properties[0].name, prop_two.name)
        self.assertIsNone(prop_one.parent)

        with self.assertRaises(ValueError):
            sec.remove(prop_one)
        self.assertEqual(len(sec.properties), 1)

        with self.assertRaises(ValueError):
            sec.remove("prop_two")
    def setUp(self):
        doc = Document()
        sec = Section(name="sec1", type="test", parent=doc)
        Section(name="sec2", type="test", parent=sec)
        Property(name="prop1", values=[1.3], parent=sec)

        self.doc = doc
Beispiel #7
0
    def test_contains(self):
        sec = Section(name="root")

        subsec = Section(name="subsec", type="test")
        prop = Property(name="prop")

        # Test not contains on empty child-lists.
        self.assertIsNone(sec.contains(subsec))
        self.assertIsNone(sec.contains(prop))

        # Test contains of Section and Property
        subsec.parent = sec
        simisec = Section(name="subsec", type="test")
        self.assertEqual(sec.contains(simisec).name, subsec.name)

        prop.parent = sec
        simiprop = Property(name="prop")
        self.assertEqual(sec.contains(simiprop).name, prop.name)

        # Test not contains on mismatching Section name/type and Property name
        self.assertIsNone(sec.contains(Section(name="subsec", type="typetest")))
        self.assertIsNone(sec.contains(Section(name="typesec", type="test")))
        self.assertIsNone(sec.contains(Property(name="prop_two")))

        # Test fail on non-Section/Property objects
        with self.assertRaises(ValueError):
            sec.contains(Document())

        with self.assertRaises(ValueError):
            sec.contains("some info")
Beispiel #8
0
    def test_extend(self):
        doc = Document()
        self.assertListEqual(doc.sections, [])

        # Test extend with Section list
        doc.extend([Section(name="sec_one"), Section(name="sec_two")])
        self.assertEqual(len(doc), 2)
        self.assertEqual(len(doc.sections), 2)
        self.assertEqual(doc.sections[0].name, "sec_one")

        # Test fail on non iterable
        with self.assertRaises(TypeError):
            doc.extend(1)
        self.assertEqual(len(doc.sections), 2)

        # Test fail on non Section entry
        with self.assertRaises(ValueError):
            doc.extend([Document()])
        with self.assertRaises(ValueError):
            doc.extend([Property(name="prop")])
        with self.assertRaises(ValueError):
            doc.extend([5])
        self.assertEqual(len(doc.sections), 2)

        # Test fail on same name entities
        with self.assertRaises(KeyError):
            doc.extend([Section(name="sec_three"), Section(name="sec_one")])
        self.assertEqual(len(doc.sections), 2)
Beispiel #9
0
    def test_get_merged_equivalent(self):
        sec = Section(name="parent")
        mersec = Section(name="merged_section")
        merprop_other = Property(name="other_prop", value="other")
        merprop = Property(name="prop", value=[1, 2, 3])

        # Check None on unset parent.
        prop = Property(name="prop")
        self.assertIsNone(prop.get_merged_equivalent())

        # Check None on parent without merged Section.
        prop.parent = sec
        self.assertIsNone(prop.get_merged_equivalent())

        # Check None on parent with merged Section but no attached Property.
        sec.merge(mersec)
        self.assertIsNone(prop.get_merged_equivalent())

        # Check None on parent with merged Section and unequal Property.
        merprop_other.parent = mersec
        self.assertIsNone(prop.get_merged_equivalent())

        # Check receiving merged equivalent Property.
        merprop.parent = mersec
        self.assertIsNotNone(prop.get_merged_equivalent())
        self.assertEqual(prop.get_merged_equivalent(), merprop)
    def setUp(self):
        """
        Set up local temporary terminology files in a temporary folder
        """
        tmp_dir = create_test_dir(__file__)
        tmp_name = os.path.basename(tmp_dir)

        main_name = "%s_main.xml" % tmp_name
        main_file_path = os.path.join(tmp_dir, main_name)
        main_url = "file://%s" % pathname2url(main_file_path)

        include_name = "%s_include.xml" % tmp_name
        include_file_path = os.path.join(tmp_dir, include_name)
        include_url = "file://%s" % pathname2url(include_file_path)

        include_doc = Document()
        _ = Section(name="include_sec", type="test", parent=include_doc)
        save(include_doc, include_file_path)

        main_doc = Document()
        _ = Section(name="main_sec", type="test", include=include_url, parent=main_doc)
        save(main_doc, main_file_path)

        self.main_terminology_url = main_url
        self.temp_dir_base = tmp_name
Beispiel #11
0
    def test_id(self):
        s = Section(name="S")
        self.assertIsNotNone(s.id)

        s = Section("S", oid="79b613eb-a256-46bf-84f6-207df465b8f7")
        self.assertEqual(s.id, "79b613eb-a256-46bf-84f6-207df465b8f7")

        s = Section("S", oid="id")
        self.assertNotEqual(s.id, "id")

        # Make sure id cannot be reset programmatically.
        with self.assertRaises(AttributeError):
            s.id = "someId"
Beispiel #12
0
def handle_sub_container(helper, node, sec, sub_sec_type):
    # We might need to handle the case, when a container holds
    # only the content of one xml element and does not contain
    # the content and attributes of this xml element as a sole
    # list element but as many elements within an OrderedDict.
    if isinstance(node[helper.section_name], list):
        for (idx, title_node) in enumerate(node[helper.section_name]):
            sec_name = "%s_%d" % (helper.section_name, idx + 1)
            sub_sec = Section(name=sec_name, type=sub_sec_type, parent=sec)
            helper.item_func(helper, title_node, sub_sec)
    else:
        sub_sec_name = "%s_1" % helper.section_name
        sub_sec = Section(name=sub_sec_name, type=sub_sec_type, parent=sec)
        helper.item_func(helper, node[helper.section_name], sub_sec)
Beispiel #13
0
def parse_datacite_dict(doc):
    """
    :param doc: python dict containing datacite conform data to
                be parsed.
    """
    if not doc or "resource" not in doc:
        msg = "Could not find root. "
        msg += "Please escape any XML namespace using the '-n' command line option."
        raise ParserException(msg)

    datacite_root = doc["resource"]
    if "identifier" not in datacite_root:
        raise ParserException("Could not find identifier (DOI) node")

    odml_doc = Document()
    odml_doc.repository = "https://terminologies.g-node.org/v1.1/terminologies.xml"
    odml_doc.date = date.today().isoformat()

    root_sec = Section(name="DataCite", type="data_reference", parent=odml_doc)

    supported_tags = setup_supported_tags()
    for node_tag in datacite_root:
        if node_tag in supported_tags:
            helper = supported_tags[node_tag]
            helper.func(helper, datacite_root[node_tag], root_sec)
        else:
            print("[Warning] Ignoring unsupported root node '%s'" % node_tag)

    return odml_doc
Beispiel #14
0
    def test_create_section(self):
        root = Section("root")
        self.assertEqual(len(root.sections), 0)

        name = "subsec"
        type = "subtype"
        oid = "79b613eb-a256-46bf-84f6-207df465b8f7"
        subsec = root.create_section(name, type, oid)

        self.assertEqual(len(root.sections), 1)
        self.assertEqual(subsec.parent, root)
        self.assertEqual(root.sections[name], subsec)
        self.assertEqual(root.sections[name].type, type)
        self.assertEqual(root.sections[name].oid, oid)

        name = "othersec"
        subsec = root.create_section(name)
        self.assertEqual(len(root.sections), 2)
        self.assertEqual(subsec.parent, root)
        self.assertEqual(root.sections[name], subsec)
        self.assertEqual(root.sections[name].type, "undefined")

        name = "subsubsec"
        subsec = root.sections[0].create_section(name)
        self.assertEqual(len(root.sections), 2)
        self.assertEqual(subsec.parent, root.sections[0])
        self.assertEqual(len(root.sections[0].sections), 1)
        self.assertEqual(root.sections[0].sections[0].name, name)
Beispiel #15
0
    def test_simple_attributes(self):
        sec_name = "sec name"
        sec_type = "sec type"
        sec_def = "an odml test section"
        sec_ref = "from over there"

        sec = Section(name=sec_name, type=sec_type, definition=sec_def, reference=sec_ref)

        self.assertEqual(sec.name, sec_name)
        self.assertEqual(sec.type, sec_type)
        self.assertEqual(sec.definition, sec_def)
        self.assertEqual(sec.reference, sec_ref)

        # Test setting attributes
        sec.name = "%s_edit" % sec_name
        self.assertEqual(sec.name, "%s_edit" % sec_name)
        sec.type = "%s_edit" % sec_type
        self.assertEqual(sec.type, "%s_edit" % sec_type)
        sec.definition = "%s_edit" % sec_def
        self.assertEqual(sec.definition, "%s_edit" % sec_def)
        sec.reference = "%s_edit" % sec_ref
        self.assertEqual(sec.reference, "%s_edit" % sec_ref)

        # Test setting attributes to None when '' is passed.
        sec.reference = ""
        self.assertIsNone(sec.reference)
        sec.definition = ""
        self.assertIsNone(sec.definition)
Beispiel #16
0
    def test_create_property(self):
        root = Section("root")
        self.assertEqual(len(root.properties), 0)

        name = "prop"
        oid = "79b613eb-a256-46bf-84f6-207df465b8f7"
        prop = root.create_property(name, oid=oid)
        self.assertEqual(len(root.properties), 1)
        self.assertEqual(prop.parent, root)
        self.assertEqual(root.properties[name].oid, oid)

        name = "test_values"
        values = ["a", "b"]
        prop = root.create_property(name, value=values)
        self.assertEqual(len(root.properties), 2)
        self.assertEqual(root.properties[name].values, values)

        name = "test_dtype"
        dtype = "str"
        prop = root.create_property(name, dtype=dtype)
        self.assertEqual(len(root.properties), 3)
        self.assertEqual(root.properties[name].dtype, dtype)

        name = "test_dtype_fail"
        dtype = "I do not exist"
        prop = root.create_property(name, dtype=dtype)
        self.assertIsNone(prop.dtype)
Beispiel #17
0
    def test_reorder(self):
        sec = Section()
        prop_zero = Property(name="prop_zero", parent=sec)
        prop_one = Property(name="prop_one", parent=sec)
        prop_two = Property(name="prop_two", parent=sec)
        prop_three = Property(name="prop_three", parent=sec)

        self.assertEqual(sec.properties[0].name, prop_zero.name)
        self.assertEqual(sec.properties[2].name, prop_two.name)
        prop_two.reorder(0)

        self.assertEqual(sec.properties[0].name, prop_two.name)
        self.assertEqual(sec.properties[1].name, prop_zero.name)
        self.assertEqual(sec.properties[2].name, prop_one.name)
        self.assertEqual(sec.properties[3].name, prop_three.name)

        prop_two.reorder(2)

        self.assertEqual(sec.properties[0].name, prop_zero.name)
        self.assertEqual(sec.properties[1].name, prop_one.name)
        self.assertEqual(sec.properties[2].name, prop_two.name)
        self.assertEqual(sec.properties[3].name, prop_three.name)

        # Test Exception on unconnected property
        prop = Property(name="main")
        with self.assertRaises(ValueError):
            prop.reorder(0)
    def test_property(self):
        """
        Test if a property and its attributes get converted correctly from rdf to odml.
        """
        doc = Document()
        sec1 = Section(name="sec1", type="test", parent=doc)
        prop2 = Property(name="numbers",
                         definition="any number",
                         dtype="float",
                         parent=sec1,
                         values=[1, 3.4, 67.8, -12],
                         unit="meter",
                         uncertainty=0.8,
                         value_origin="force",
                         reference="Experiment 1")

        rdf_writer = RDFWriter(doc).get_rdf_str()
        rdf_reader = RDFReader().from_string(rdf_writer, "turtle")

        prop = rdf_reader[0].sections[0].properties["numbers"]

        self.assertEqual(prop.name, "numbers")
        self.assertEqual(prop.dtype, "float")
        self.assertEqual(prop.id, prop2.id)
        self.assertEqual(prop.parent, rdf_reader[0].sections[0])
        self.assertEqual(len(prop.values), 4)
        self.assertEqual(prop.values, [1, 3.4, 67.8, -12])
        self.assertEqual(prop.definition, "any number")
        self.assertEqual(prop.unit, "meter")
        self.assertEqual(prop.uncertainty, "0.8")
        self.assertEqual(prop.value_origin, "force")
        self.assertEqual(prop.reference, "Experiment 1")
Beispiel #19
0
    def test_name(self):
        # Test id is used when name is not provided
        prop = Property()
        self.assertIsNotNone(prop.name)
        self.assertEqual(prop.name, prop.id)

        # Test name is properly set on init
        name = "rumpelstilzchen"
        prop = Property(name)
        self.assertEqual(prop.name, name)

        # Test name can be properly set on single and connected Properties
        prop = Property()
        self.assertNotEqual(prop.name, "prop")
        prop.name = "prop"
        self.assertEqual(prop.name, "prop")

        sec = Section()
        prop_a = Property(parent=sec)
        self.assertNotEqual(prop_a.name, "prop_a")
        prop_a.name = "prop_a"
        self.assertEqual(prop_a.name, "prop_a")

        # Test property name can be changed with siblings
        prop_b = Property(name="prop_b", parent=sec)
        self.assertEqual(prop_b.name, "prop_b")
        prop_b.name = "prop"
        self.assertEqual(prop_b.name, "prop")

        # Test property name set will fail on existing sibling with same name
        with self.assertRaises(KeyError):
            prop_b.name = "prop_a"
        self.assertEqual(prop_b.name, "prop")
Beispiel #20
0
def handle_sec(helper, node, root_sec):
    if not node:
        return

    sec_type = "datacite/%s" % camel_to_snake(helper.section_name)
    sec = Section(name=helper.section_name, type=sec_type, parent=root_sec)

    handle_props(helper, node, sec)
Beispiel #21
0
    def test_reorder(self):
        # Test reorder of document sections
        doc = Document()
        sec_one = Section(name="sec_one", parent=doc)
        sec_two = Section(name="sec_two", parent=doc)
        sec_three = Section(name="sec_three", parent=doc)

        self.assertEqual(doc.sections[0].name, sec_one.name)
        self.assertEqual(doc.sections[2].name, sec_three.name)
        sec_three.reorder(0)

        self.assertEqual(doc.sections[0].name, sec_three.name)
        self.assertEqual(doc.sections[2].name, sec_two.name)

        # Test reorder of document sections
        sec = Section(name="main")
        sec_one = Section(name="sec_one", parent=sec)
        sec_two = Section(name="sec_two", parent=sec)
        sec_three = Section(name="sec_three", parent=sec)

        self.assertEqual(sec.sections[0].name, sec_one.name)
        self.assertEqual(sec.sections[2].name, sec_three.name)
        sec_three.reorder(0)

        self.assertEqual(sec.sections[0].name, sec_three.name)
        self.assertEqual(sec.sections[2].name, sec_two.name)

        # Test Exception on unconnected section
        with self.assertRaises(ValueError):
            sec.reorder(0)
Beispiel #22
0
    def test_read_write(self):
        doc = Document("author")
        sec = Section("name", "type")
        doc.append(sec)
        sec.append(Property("strprop", "somestring"))
        sec.append(Property("txtprop", "some\ntext"))
        sec.append(Property("intprop", 200))
        sec.append(Property("floatprop", 2.00))
        sec.append(Property("datetimeprop", dt.now()))
        sec.append(Property("dateprop", dt.now().date()))
        sec.append(Property("timeprop", dt.now().time()))
        sec.append(Property("boolprop", True))
        if sys.version_info < (3, 0):
            str_doc = unicode(XMLWriter(doc))
        else:
            str_doc = str(XMLWriter(doc))
        new_doc = XMLReader().fromString(str_doc)
        new_sec = new_doc.sections[0]

        p = new_sec.properties["strprop"]
        assert (p.dtype == "string")
        if sys.version_info < (3, 0):
            assert (type(p.value[0]) == unicode)
        else:
            assert (type(p.value[0]) == str)

        p = new_sec.properties["txtprop"]
        assert (p.dtype == "text")
        if sys.version_info < (3, 0):
            assert (type(p.value[0]) == unicode)
        else:
            assert (type(p.value[0]) == str)

        p = new_sec.properties["intprop"]
        assert (p.dtype == "int")
        assert (type(p.value[0]) == int)

        p = new_sec.properties["floatprop"]
        assert (p.dtype == "float")
        assert (type(p.value[0]) == float)

        p = new_sec.properties["datetimeprop"]
        assert (p.dtype == "datetime")
        assert (type(p.value[0]) == dt)

        p = new_sec.properties["dateprop"]
        assert (p.dtype == "date")
        assert (type(p.value[0]) == date)

        p = new_sec.properties["timeprop"]
        assert (p.dtype == "time")
        assert (type(p.value[0]) == time)

        p = new_sec.properties["boolprop"]
        assert (p.dtype == "boolean")
        assert (type(p.value[0]) == bool)
Beispiel #23
0
def handle_container(helper, node, root_sec):
    if not node or helper.section_name not in node:
        return

    sec_cont_type = "datacite/%s" % camel_to_snake(helper.container_name)
    sub_sec_type = "datacite/%s" % camel_to_snake(helper.section_name)

    sec = Section(name=helper.container_name, type=sec_cont_type, parent=root_sec)

    handle_sub_container(helper, node, sec, sub_sec_type)
Beispiel #24
0
    def test_path(self):
        sec = Section(name="center")
        self.assertEqual(sec.get_path(), "/")

        subsec = Section(name="leaf", parent=sec)
        self.assertEqual(subsec.get_path(), "/leaf")

        doc = Document()
        sec.parent = doc
        self.assertEqual(sec.get_path(), "/center")
        self.assertEqual(subsec.get_path(), "/center/leaf")

        top = Section(name="top", parent=doc)
        sec.parent = top
        self.assertEqual(sec.get_path(), "/top/center")
        self.assertEqual(subsec.get_path(), "/top/center/leaf")

        subsec.parent = None
        self.assertEqual(subsec.get_path(), "/")
Beispiel #25
0
def handle_geo_entry(helper_list, node, sec, sub_sec_name, sub_sec_type):
    sub_sec = Section(name=sub_sec_name, type=sub_sec_type, parent=sec)

    for entry in node:
        if entry in helper_list:
            try:
                Property(name=entry, dtype=DType.float,
                         values=node[entry], parent=sub_sec)
            except ValueError:
                print("[Warning] Skipping invalid '%s' value '%s'" %
                      (entry, node[entry]))
Beispiel #26
0
    def test_get_path(self):
        doc = Document()
        sec = Section(name="parent", parent=doc)

        # Check root path for a detached Property.
        prop = Property(name="prop")
        self.assertEqual("/", prop.get_path())

        # Check absolute path of Property in a Document.
        prop.parent = sec
        self.assertEqual("/%s:%s" % (sec.name, prop.name), prop.get_path())
Beispiel #27
0
    def test_parent(self):
        p = Property("property_section", parent=Section("S"))
        self.assertIsInstance(p.parent, BaseSection)
        self.assertEqual(len(p.parent._props), 1)
        """ Test if child is removed from _props of a parent after assigning
            a new parent to the child """
        prop_parent = p.parent
        p.parent = Section("S1")
        self.assertEqual(len(prop_parent._props), 0)
        self.assertIsInstance(p.parent, BaseSection)
        self.assertIsInstance(p.parent._props[0], BaseProperty)

        prop_parent = p.parent
        p.parent = None
        self.assertIsNone(p.parent)
        self.assertEqual(len(prop_parent._props), 0)

        with self.assertRaises(ValueError):
            Property("property_prop", parent=Property("P"))
        with self.assertRaises(ValueError):
            Property("property_doc", parent=Document())
Beispiel #28
0
    def test_append(self):
        doc = Document()
        self.assertListEqual(doc.sections, [])

        # Test append Section
        sec = Section(name="sec_one")
        doc.append(sec)
        self.assertEqual(len(doc.sections), 1)
        self.assertEqual(sec.parent, doc)

        # Test fail on Section list or tuple append
        with self.assertRaises(ValueError):
            doc.append([Section(name="sec_two"), Section(name="sec_three")])
        with self.assertRaises(ValueError):
            doc.append((Section(name="sec_two"), Section(name="sec_three")))
        self.assertEqual(len(doc.sections), 1)

        # Test fail on unsupported value
        with self.assertRaises(ValueError):
            doc.append(Document())
        with self.assertRaises(ValueError):
            doc.append("Section")
        with self.assertRaises(ValueError):
            doc.append(Property(name="prop"))

        # Test fail on same name entities
        with self.assertRaises(KeyError):
            doc.append(Section(name="sec_one"))
        self.assertEqual(len(doc.sections), 1)
Beispiel #29
0
    def test_parent(self):
        s = Section("Section")
        self.assertIsNone(s.parent)
        s.parent = None
        self.assertIsNone(s.parent)
        s.parent = Document()
        self.assertIsInstance(s.parent, BaseDocument)
        self.assertIsInstance(s.parent._sections[0], BaseSection)

        """ Test if child is removed from _sections of a parent after assigning
            a new parent to the child """
        p = s.parent
        s.parent = Section("S")
        self.assertEqual(len(p._sections), 0)

        s = Section("section_doc", parent=Document())
        self.assertIsInstance(s.parent, BaseDocument)
        self.assertEqual(len(s.parent._sections), 1)
        p = s.parent
        s.parent = None
        self.assertEqual(len(p._sections), 0)
        self.assertEqual(s.parent, None)

        s = Section("section_sec", parent=Section("S"))
        self.assertIsInstance(s.parent, BaseSection)
        self.assertEqual(len(s.parent._sections), 1)
        p = s.parent
        s.parent = None
        self.assertEqual(len(p._sections), 0)
        self.assertEqual(s.parent, None)

        with self.assertRaises(ValueError):
            Section("section_property", parent=Property("P"))
Beispiel #30
0
    def test_clone(self):
        # Check parent removal in clone.
        psec = Section(name="parent")
        sec = Section(name="original", parent=psec)
        clone_sec = sec.clone()
        self.assertEqual(sec.parent, psec)
        self.assertIsNone(clone_sec.parent)

        # Check new id in clone.
        sec = Section(name="original")
        clone_sec = sec.clone()
        self.assertEqual(sec, clone_sec)
        self.assertNotEqual(sec.id, clone_sec.id)

        # Check child Sections and Properties are cloned and have new ids.
        Section(name="sec_one", parent=sec)
        Section(name="sec_two", parent=sec)
        Property(name="prop_one", parent=sec)
        Property(name="prop_two", parent=sec)

        clone_sec = sec.clone()

        # Check sections
        self.assertListEqual(sec.sections, clone_sec.sections)
        self.assertEqual(sec.sections["sec_one"], clone_sec.sections["sec_one"])
        self.assertNotEqual(sec.sections["sec_one"].id, clone_sec.sections["sec_one"].id)

        # Check properties
        self.assertListEqual(sec.properties, clone_sec.properties)
        self.assertEqual(sec.properties["prop_one"], clone_sec.properties["prop_one"])
        self.assertNotEqual(sec.properties["prop_one"].id,
                            clone_sec.properties["prop_one"].id)

        # Check child Sections and Properties are not cloned.
        clone_sec = sec.clone(children=False)

        self.assertListEqual(clone_sec.sections, [])
        self.assertListEqual(clone_sec.properties, [])