Ejemplo n.º 1
0
    def test_merge_sections(self):
        # set up 2 odmls with partially overlapping sections
        doc1 = odml.Document(author='Me')
        doc2 = odml.Document(author='You')

        doc1.extend([odml.Section('MySection'), odml.Section('OurSection')])
        doc2.extend([odml.Section('YourSection'), odml.Section('OurSection')])

        # adding properties to sections, because odml is omitting sections without properties
        for sec in doc1.sections + doc2.sections:
            sec.append(odml.Property('prop'))

        table1 = OdmlTable(load_from=doc1)
        table2 = OdmlTable(load_from=doc2)

        table1.merge(table2, strict=False)

        result = table1.convert2odml()

        expected = ['MySection', 'OurSection', 'YourSection']
        self.assertListEqual([s.name for s in result.sections], expected)
Ejemplo n.º 2
0
class TestFilter(unittest.TestCase):
    """
    class to test the other functions of the OdmlTable-class
    """
    def setUp(self):
        self.test_table = OdmlTable()
        self.test_table.load_from_odmldoc(create_compare_test(levels=2))

    def test_filter_errors(self):
        """
        test filter function for exceptions
        """

        with self.assertRaises(ValueError):
            self.test_table.filter()

        with self.assertRaises(ValueError):
            self.test_table.filter(mode='wrongmode', Property='Property')

    def test_filter_mode_and(self):
        """
        testing mode='and' setting of filter function
        """

        self.test_table.filter(mode='and',
                               invert=False,
                               SectionName='Section2',
                               PropertyName='Property2')
        num_props_new = len(self.test_table._odmldict)

        self.assertEqual(4, num_props_new)

    def test_filter_mode_or(self):
        """
        testing mode='or' setting of filter function
        """

        self.test_table.filter(mode='or',
                               invert=False,
                               SectionName='Section2',
                               PropertyName='Property2')
        num_props_new = len(self.test_table._odmldict)

        self.assertEqual(17, num_props_new)

    def test_filter_invert(self):
        """
        testing invert setting of filter function
        """

        num_props_original = len(self.test_table._odmldict)
        self.test_table.filter(mode='or',
                               invert=True,
                               SectionName='Section2',
                               PropertyName='Property2')
        num_props_new = len(self.test_table._odmldict)

        self.assertEqual(num_props_original - 17, num_props_new)

    def test_filter_recursive(self):
        """
        testing recursive setting of filter function
        """

        # total_number of properties
        doc = self.test_table.convert2odml()
        tot_props = len(list(doc.iterproperties()))
        sec2s = list(
            doc.itersections(filter_func=lambda x: x.name == 'Section2'))
        sec2_props = sum([len(list(sec.properties)) for sec in sec2s])

        # removing all sections with name 'Section2' independent of location in odml tree
        self.test_table.filter(mode='and',
                               recursive=True,
                               invert=True,
                               SectionName='Section2')
        num_props_new = len(self.test_table._odmldict)

        self.assertEqual(tot_props - sec2_props, num_props_new)

    def test_filter_comparison_func_false(self):
        """
        keeping/removing all properties by providing True/False as comparison function
        """

        num_props_original = len(self.test_table._odmldict)
        self.test_table.filter(comparison_func=lambda x, y: True,
                               PropertyName='')
        self.assertEqual(len(self.test_table._odmldict), num_props_original)

        self.test_table.filter(comparison_func=lambda x, y: False,
                               PropertyName='')
        self.assertEqual(len(self.test_table._odmldict), 0)