Exemple #1
0
 def test_raises_value_error_on_invalid_relation_exclusion_filter_mapping(self):
     """
     Test that ValueError is raised if relation_exclusion_filter_mapping contains invalid keys.
     """
     relation_exclusion_filter_mapping = {
         Mock(): Mock(),
     }
     with pytest.raises(ValueError):
         get_unreferenced_objects_query(
             Person,
             relation_exclusion_filter_mapping=relation_exclusion_filter_mapping,
         )
 def _handle(self, *args, **options):
     """
     Get unreferenced sectors and store in variable for use in _process_row.
     This avoids having to call the same query for each row in the csv.
     """
     self.unreferenced_sectors = get_unreferenced_objects_query(
         Sector).all()
     return super()._handle(*args, **options)
Exemple #3
0
    def _get_query(self, model):
        config = self.CONFIGS[model._meta.label]

        return get_unreferenced_objects_query(model).filter(
            **{
                f'{config.date_field}__lt':
                today(tzinfo=utc) - config.age_threshold
            }, ).order_by(f'-{config.date_field}', )
Exemple #4
0
    def test_relation_filter_mapping(self):
        """Test that relation_exclusion_filter_mapping excludes related objects as expected."""
        book_1 = BookFactory(name='book 1')
        BookFactory(name='book 2')
        # Proofreaders for book 1 are not considered as referenced, and hence should appear in
        # the query results
        queryset = get_unreferenced_objects_query(
            Person,
            relation_exclusion_filter_mapping={
                Person._meta.get_field('proofread_books'): Q(name=book_1.name),
            },
        )

        assert list(queryset) == [book_1.proofreader]
Exemple #5
0
    def _get_query(self, model):
        config = self.CONFIGS[model._meta.label]
        relation_filter_mapping = config.relation_filter_mapping or {}

        relation_filter_kwargs = {
            field: _join_cleanup_filters(filters)
            for field, filters in relation_filter_mapping.items()
        }

        return get_unreferenced_objects_query(
            model,
            excluded_relations=config.excluded_relations,
            relation_exclusion_filter_mapping=relation_filter_kwargs,
        ).filter(_join_cleanup_filters(config.filters), ).order_by(
            f'-{config.filters[0].date_field}', )
Exemple #6
0
 def test_only_excludes_referenced_objects(self):
     """Test that only referenced objects are excluded."""
     unreferenced_person = PersonFactory()
     BookFactory()
     queryset = get_unreferenced_objects_query(Person)
     assert list(queryset) == [unreferenced_person]