def test_field_less_than_criteria(): """with_field with less_than is translated as expected for date and non-date types """ publish_date = datetime.datetime(2019, 8, 27, 0, 0, 0) c1 = Criteria.with_field("num_field", Matcher.less_than(5)) c2 = Criteria.with_field("date_field", Matcher.less_than(publish_date)) assert filters_for_criteria(c1) == {"num_field": {"$lt": 5}} assert filters_for_criteria(c2) == { "date_field": {"$lt": {"$date": "2019-08-27T00:00:00Z"}} }
def test_search_mapped_field_less_than(): controller = FakeController() dist1 = Distributor( id="yum_distributor", type_id="yum_distributor", repo_id="repo1", last_publish=datetime.datetime(2019, 8, 23, 2, 5, 0, tzinfo=None), ) dist2 = Distributor( id="cdn_distributor", type_id="rpm_rsync_distributor", repo_id="repo1", last_publish=datetime.datetime(2019, 8, 27, 2, 5, 0, tzinfo=None), ) repo1 = Repository(id="repo1", distributors=(dist1, dist2)) controller.insert_repository(repo1) client = controller.client crit = Criteria.with_field( "last_publish", Matcher.less_than(datetime.datetime(2019, 8, 24, 0, 0, 0))) found = client.search_distributor(crit).result().data assert found == [dist1]
def clean_all_rpm_content(self): # Clear out old all-rpm-content LOG.info("Start old all-rpm-content deletion") arc_threshold = self.args.arc_threshold criteria = Criteria.and_( Criteria.with_unit_type(RpmUnit), Criteria.with_field( "cdn_published", Matcher.less_than(datetime.utcnow() - timedelta(days=arc_threshold)), ), ) clean_repos = list( self.pulp_client.search_repository( Criteria.with_field("id", "all-rpm-content"))) if not clean_repos: LOG.info("No repos found for cleaning.") return arc_repo = clean_repos[0] deleted_arc = list(arc_repo.remove_content(criteria=criteria)) deleted_content = [] for task in deleted_arc: if task.repo_id == "all-rpm-content": for unit in task.units: LOG.info("Old all-rpm-content deleted: %s", unit.name) deleted_content.append(unit) if not deleted_content: LOG.info("No all-rpm-content found older than %s", arc_threshold)
def test_dict_matcher_value(): """criteria using a dict as matcher value""" crit = Criteria.with_field( "created", Matcher.less_than({"created_date": datetime.datetime(2019, 9, 4, 0, 0, 0)}), ) assert filters_for_criteria(crit) == { "created": {"$lt": {"created_date": {"$date": "2019-09-04T00:00:00Z"}}} }
def test_stringify_complex_criteria(): crit = Criteria.and_( Criteria.with_field("must-exist", Matcher.exists()), Criteria.with_field("foo", Matcher.equals("bar")), Criteria.true(), Criteria.or_( Criteria.with_field("foo", Matcher.regex("quux")), Criteria.with_field("other", Matcher.in_(["x", "y", "z"])), Criteria.with_field("num", Matcher.less_than(9000)), ), Criteria.with_unit_type(FileUnit), ) assert (str(crit) == "((must-exist EXISTS) AND foo=='bar' AND TRUE " "AND (foo=~/quux/ OR (other IN ['x', 'y', 'z']) OR num<9000) " "AND (content_type_id IN ['iso']))")
def _filtered_repo_distributors(self): published_before = self.args.published_before url_regex = self.args.repo_url_regex # define the criteria on available filters crit = [Criteria.true()] if published_before: crit.append( Criteria.with_field("last_publish", Matcher.less_than(published_before)) ) if url_regex: crit.append( Criteria.with_field("relative_url", Matcher.regex(url_regex.pattern)) ) crit = Criteria.and_(*crit) return self.pulp_client.search_distributor(crit)