Example #1
0
    def test_comparison(self):
        sec_name = "sec name"
        sec_type = "sec type"
        sec_def = "an odml test section"
        sec_ref = "from over there"

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

        self.assertEqual(sec_a, sec_b)

        sec_b.name = "newSecName"
        self.assertNotEqual(sec_a, sec_b)

        sec_b.name = sec_name

        # Test section equality with subsections

        # Test equality with subsection of different entities
        # with same content and same children order
        subsec_aa = Section(name="subsecA", type=sec_type,
                            definition=sec_def, reference=sec_ref)
        subsec_ab = Section(name="subsecB", type=sec_type,
                            definition=sec_def, reference=sec_ref)
        subsec_ba = Section(name="subsecA", type=sec_type,
                            definition=sec_def, reference=sec_ref)
        subsec_bb = Section(name="subsecB", type=sec_type,
                            definition=sec_def, reference=sec_ref)

        sec_a.extend([subsec_aa, subsec_ab])
        sec_b.extend([subsec_ba, subsec_bb])

        self.assertEqual(sec_a, sec_b)
        self.assertEqual(sec_a.sections, sec_b.sections)

        sec_b.sections[0].name = "newSubsecA"
        self.assertNotEqual(sec_a, sec_b)
        self.assertNotEqual(sec_a.sections, sec_b.sections)

        sec_b.sections[0].name = "subsecA"

        # Test inequality with different number of children
        sec_b.remove(sec_b.sections[1])
        self.assertNotEqual(sec_a, sec_b)
        self.assertNotEqual(sec_a.sections, sec_b.sections)

        # Test equality with subsection of different entities
        # with same content and different children order
        sec_b.remove(sec_b.sections[0])
        sec_b.extend([subsec_bb, subsec_ba])

        self.assertEqual(sec_a, sec_b)
        self.assertEqual(sec_a.sections, sec_b.sections)

        sec_b.sections[0].name = "newSubsecB"
        self.assertNotEqual(sec_a, sec_b)
        self.assertNotEqual(sec_a.sections, sec_b.sections)

        sec_b.sections[0].name = "subsecB"

        # Test section equality with properties

        # Test equality with properties of different entities
        # with same content and same children order
        prop_aa = Property(name="propA", definition="propDef")
        prop_ab = Property(name="propB", definition="propDef")
        prop_ba = Property(name="propA", definition="propDef")
        prop_bb = Property(name="propB", definition="propDef")

        sec_a.extend([prop_aa, prop_ab])
        sec_b.extend([prop_ba, prop_bb])

        self.assertEqual(sec_a, sec_b)
        self.assertEqual(sec_a.properties, sec_b.properties)

        sec_b.properties[0].name = "newPropA"
        self.assertNotEqual(sec_a, sec_b)
        self.assertNotEqual(sec_a.properties, sec_b.properties)

        sec_b.properties[0].name = "propA"

        # Test inequality with different number of children
        sec_b.remove(sec_b.properties[1])
        self.assertNotEqual(sec_a, sec_b)
        self.assertNotEqual(sec_a.properties, sec_b.properties)

        # Test equality with properties of different entities
        # with same content and different children order
        sec_b.remove(sec_b.properties[0])
        sec_b.extend([prop_bb, prop_ba])

        self.assertEqual(sec_a, sec_b)
        self.assertEqual(sec_a.properties, sec_b.properties)

        sec_b.properties[0].name = "newPropB"
        self.assertNotEqual(sec_a, sec_b)
        self.assertNotEqual(sec_a.properties, sec_b.properties)
Example #2
0
    def test_extend(self):
        sec = Section(name="main")
        self.assertListEqual(sec.sections, [])
        self.assertListEqual(sec.properties, [])

        # Test extend with Section list
        sec.extend([Section(name="sec1"), Section(name="sec2")])
        self.assertEqual(len(sec), 2)
        self.assertEqual(len(sec.sections), 2)
        self.assertEqual(sec.sections[0].name, "sec1")

        # Test extend with Property list
        sec.extend((Property(name="prop1"), Property(name="prop2")))
        self.assertEqual(len(sec), 4)
        self.assertEqual(len(sec.properties), 2)
        self.assertEqual(sec.properties[0].name, "prop1")

        # Test extend with mixed list
        sec.extend([Section(name="sec3"), Property(name="prop3")])
        self.assertEqual(len(sec), 6)
        self.assertEqual(len(sec.sections), 3)
        self.assertEqual(len(sec.properties), 3)

        # Test fail on non iterable
        with self.assertRaises(TypeError):
            sec.extend(1)

        # Test fail on non Section/Property list entry
        with self.assertRaises(ValueError):
            sec.extend([Property(name="prop4"), 5])

        # Test fail on same name entities
        with self.assertRaises(KeyError):
            sec.extend([Property(name="new"), Property(name="prop3")])
        self.assertEqual(len(sec.properties), 3)

        with self.assertRaises(KeyError):
            sec.extend([Section(name="new"), Section(name="sec3")])
        self.assertEqual(len(sec.sections), 3)