def test_can_get_most_common_violations(self):
        self.db.query(Violation).delete()
        self.db.query(Key).delete()

        category = KeysCategoryFactory.create(name="SEO")
        for i in range(3):
            key = KeyFactory.create(name="some.random.fact.%s" % i, category=category)
            for j in range(i):
                ViolationFactory.create(key=key, value="value", points=10 * i + j)

        violation_definitions = {
            "some.random.fact.1": {"title": "SEO", "category": "SEO"},
            "some.random.fact.2": {"title": "SEO", "category": "SEO"},
        }

        violations = Violation.get_most_common_violations(self.db, violation_definitions)

        expect(violations).to_be_like(
            [
                {"count": 1, "key": "some.random.fact.1", "category": "SEO", "title": "SEO"},
                {"count": 2, "key": "some.random.fact.2", "category": "SEO", "title": "SEO"},
            ]
        )

        violations = Violation.get_most_common_violations(self.db, violation_definitions, sample_limit=2)

        expect(violations).to_be_like([{"count": 2, "key": "some.random.fact.2", "category": "SEO", "title": "SEO"}])
Exemple #2
0
 def get_most_common_violations(self, violation_definitions, sample_limit, callback=None):
     self.get_data(
         'most-common-violations',
         int(self.config.MOST_COMMON_VIOLATIONS_CACHE_EXPIRATION),
         lambda: Violation.get_most_common_violations(self.db, violation_definitions, sample_limit),
         callback=callback
     )