def test_table_creation(self):
        test_data = [('average_doc.doc', {
            'rating': [5.0, 4.4],
            'reviewed': [True],
            'review_date': [parse_date('2016')]
        }), ('poor_table.csv', {
            'rating': [2.0]
        }), ('great_image.jpg', {
            'rating': [10.0]
        }), ('mixed_image.jpg', {
            'rating': [1.5, 11.5]
        }), ('unrated_image.jpg', {
            'author': ['me']
        })]
        index_main = medinx.MetadataIndex(test_data)

        table_model = mdx_ui.MdataTableModel(index_main)

        indexed_fns = index_main.get_files()
        indexed_attrs = index_main.get_attributes(
        )  # To implement and test in TopLevelAPI

        self.assertEqual(table_model.rowCount(), len(indexed_fns))
        self.assertEqual(table_model.columnCount(), len(indexed_attrs))

        for ifile, (file_fn, mdata) in enumerate(test_data):  # row
            for iattr, attr in enumerate(indexed_attrs):  # column
                qt_index = table_model.index(ifile, iattr)
                self.assertEqual(
                    table_model.data(qt_index),
                    medinx.format_values(test_data[ifile][1].get(attr, [])))
Exemple #2
0
    def test_filter_equality(self):
        test_data = [('doc1.doc', {
            'author': ['me'],
            'reviewed': [True],
            'rating': [1.2],
            'review_date': [parse_date('2016-02-01')]
        }),
                     ('table.csv', {
                         'author': ['somebody'],
                         'rating': [5.0],
                         'reviewed': [False],
                         'review_date': [parse_date('2016-02-01T12:12')]
                     })]
        index_main = medinx.MetadataIndex(test_data)

        # Check string
        test_author = 'me'
        selection = index_main.filter('author=%s' % test_author)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if test_author in md.get('author', [])))

        # Check boolean
        reviewed = True
        selection = index_main.filter('reviewed=%s' % reviewed)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if 'reviewed' in md and md['reviewed'][0] == reviewed))
        reviewed = False
        selection = index_main.filter('reviewed=%s' % reviewed)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if 'reviewed' in md and md['reviewed'][0] == reviewed))

        # Check number
        rating = 5.5
        selection = index_main.filter('rating=%s' % rating)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if 'rating' in md and md['rating'][0] == rating))

        rating = 5
        selection = index_main.filter('rating=%s' % rating)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if 'rating' in md and md['rating'][0] == rating))

        # Check date
        rev_date = '#2016-02-01'
        selection = index_main.filter('review_date=%s' % rev_date)
        self.assertEqual(set(selection.get_files()),
                         set(fn for fn, md in test_data
                             if 'review_date' in md and \
                             md['review_date'][0] == parse_date(rev_date[1:])))
Exemple #3
0
    def test_filter_comparison_date(self):
        test_data = [('very_old.doc', {
            'rev_date': [parse_date('1949-05-23'),
                         parse_date('2016-04-01')],
            'location': ['paris', 'new-york']
        }),
                     ('early_doc.doc', {
                         'rev_date':
                         [parse_date('2017-03-31'),
                          parse_date('2016-04')],
                         'location': ['london']
                     }), ('unrelated.doc', {
                         'rating': [5]
                     })]
        index_main = medinx.MetadataIndex(test_data)

        ref_sdate = '2016-04'
        ref_date = parse_date(ref_sdate)

        min_date = parse_date('1900')
        max_date = parse_date('2100')

        selection = index_main.filter('rev_date>%s' % ref_sdate)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any([v > ref_date
                        for v in md.get('rev_date', [min_date])])))

        selection = index_main.filter('rev_date>=%s' % ref_sdate)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any([v >= ref_date
                        for v in md.get('rev_date', [min_date])])))

        selection = index_main.filter('rev_date<%s' % ref_sdate)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any([v < ref_date
                        for v in md.get('rev_date', [max_date])])))

        selection = index_main.filter('rev_date<=%s' % ref_sdate)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any([v <= ref_date
                        for v in md.get('rev_date', [max_date])])))
Exemple #4
0
    def test_get_attributes(self):
        test_data = [('doc1.doc', {
            'author': ['me'],
            'reviewed': [True],
            'rating': [1.2],
            'review_date': [parse_date('2016-02-01')]
        }),
                     ('table.csv', {
                         'author': ['somebody'],
                         'rating': [5.0],
                         'reviewed': [False],
                         'review_date': [parse_date('2016-02-01T12:12')]
                     })]
        index_main = medinx.MetadataIndex(test_data)

        self.assertEqual(
            index_main.get_attributes(),
            sorted(list(set(chain(*[md.keys() for fn, md in test_data])))))
Exemple #5
0
    def test_filter_value(self):
        test_data = [('report.doc', {
            'tag': ['nice', 'specification', 'data']
        }), ('summary.doc', {
            'keyword': ['data', 'specification']
        }),
                     ('mixed_feelings.doc', {
                         'qualifier': ['specification', 'algorithm']
                     }), ('unrelated.doc', {
                         'rating': [5]
                     })]

        index_main = medinx.MetadataIndex(test_data)

        term = 'specification'
        selection = index_main.filter('%s' % term)
        self.assertEqual(set(selection.get_files()),
                         set(fn for fn, md in test_data
                             if any([term in vals for vals in md.values() \
                                     if isinstance(vals[0], str)])))
Exemple #6
0
    def test_filter_comparison_float(self):
        test_data = [('average_doc.doc', {
            'rating': [5.0, 4.4],
            'reviewed': [True],
            'review_date': [parse_date('2016')]
        }), ('poor_table.csv', {
            'rating': [2.0]
        }), ('great_image.jpg', {
            'rating': [10.0]
        }), ('mixed_image.jpg', {
            'rating': [1.5, 11.5]
        }), ('unrated_image.jpg', {
            'author': ['me']
        })]
        index_main = medinx.MetadataIndex(test_data)

        threshold = 5.0
        selection = index_main.filter('rating<%f' % threshold)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any(v < threshold for v in md.get('rating', [100]))))

        selection = index_main.filter('rating>%f' % threshold)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any(v > threshold for v in md.get('rating', [0]))))

        selection = index_main.filter('rating>=%f' % threshold)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any(v >= threshold for v in md.get('rating', [0]))))

        selection = index_main.filter('rating<=%f' % threshold)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any(v <= threshold for v in md.get('rating', [100]))))
Exemple #7
0
    def test_filter_comparison_bool(self):
        test_data = [('validated.doc', {
            'approved': [True],
            'reviewed': [True]
        }), ('rejected.doc', {
            'approved': [False],
            'location': ['london']
        }), ('mixed_feelings.doc', {
            'approved': [False, True]
        }), ('unrelated.doc', {
            'rating': [5]
        })]

        index_main = medinx.MetadataIndex(test_data)

        ref_val = True
        selection = index_main.filter('approved>%s' % ref_val)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any([v > ref_val for v in md.get('approved', [-1])])))

        selection = index_main.filter('approved>=%s' % ref_val)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any([v >= ref_val for v in md.get('approved', [-1])])))

        selection = index_main.filter('approved<%s' % ref_val)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any([v < ref_val for v in md.get('approved', [2])])))

        selection = index_main.filter('approved<=%s' % ref_val)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any([v <= ref_val for v in md.get('approved', [2])])))
Exemple #8
0
    def test_filter_comparison_str(self):
        test_data = [('repport.doc', {
            'author': ['me', 'myself'],
            'location': ['paris', 'new-york']
        }), ('spec.doc', {
            'author': ['group'],
            'location': ['london']
        }), ('unrelated.doc', {
            'rating': [5]
        })]
        index_main = medinx.MetadataIndex(test_data)

        ref_str = 'm'
        selection = index_main.filter('author<=%s' % ref_str)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any([v <= ref_str for v in md.get('author', ['zzz'])])))

        ref_str = 'me'
        selection = index_main.filter('author<%s' % ref_str)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any([v < ref_str for v in md.get('author', ['zzz'])])))

        ref_str = 'm'
        selection = index_main.filter('author>=%s' % ref_str)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any([v >= ref_str for v in md.get('author', [''])])))

        ref_str = 'me'
        selection = index_main.filter('author>%s' % ref_str)
        self.assertEqual(
            set(selection.get_files()),
            set(fn for fn, md in test_data
                if any([v > ref_str for v in md.get('author', [''])])))
    def test_table_edition(self):
        test_data = [('average_doc.doc', {
            'rating': [5.0, 4.4],
            'reviewed': [True],
            'review_date': [parse_date('2016')]
        }), ('poor_table.csv', {
            'rating': [2.0]
        }), ('great_image.jpg', {
            'rating': [10.0]
        }), ('mixed_image.jpg', {
            'rating': [1.5, 11.5]
        }), ('unrated_image.jpg', {
            'author': ['me']
        })]
        index_main = medinx.MetadataIndex(test_data)

        table_model = mdx_ui.MdataTableModel(index_main)

        attr_to_edit = 'rating'
        new_values = [4.1]
        attributes = index_main.get_attributes()
        iattr = attributes.index(attr_to_edit)
        qt_index = table_model.index(0, iattr)
        table_model.setData(qt_index, medinx.format_values(new_values))

        self.assertEqual(
            index_main.get_metadata(test_data[0][0])[attr_to_edit], new_values)

        new_values = []
        table_model.setData(qt_index, medinx.format_values(new_values))
        self.assertEqual(
            index_main.get_metadata(test_data[0][0])[attr_to_edit], new_values)

        attr_to_edit = 'author'
        new_values = ['nobody', 'them']
        attributes = index_main.get_attributes()
        iattr = attributes.index(attr_to_edit)
        ifile = 4
        qt_index = table_model.index(ifile, iattr)
        table_model.setData(qt_index, medinx.format_values(new_values))

        self.assertEqual(
            index_main.get_metadata(test_data[ifile][0])[attr_to_edit],
            new_values)

        attr_to_edit = 'review_date'
        new_values = [parse_date('2020-01-02')]
        attributes = index_main.get_attributes()
        iattr = attributes.index(attr_to_edit)
        ifile = 4
        qt_index = table_model.index(ifile, iattr)
        table_model.setData(qt_index, medinx.format_values(new_values))

        self.assertEqual(
            index_main.get_metadata(test_data[ifile][0])[attr_to_edit],
            new_values)

        attr_to_edit = 'reviewed'
        new_values = [False]
        attributes = index_main.get_attributes()
        iattr = attributes.index(attr_to_edit)
        ifile = 0
        qt_index = table_model.index(ifile, iattr)
        table_model.setData(qt_index, medinx.format_values(new_values))

        self.assertEqual(
            index_main.get_metadata(test_data[ifile][0])[attr_to_edit],
            new_values)