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"}])
    def test_can_get_by_key_id_group_by_domain(self):
        domains = [DomainFactory.create(name="g%d.com" % i) for i in xrange(2)]
        keys = [KeyFactory.create(name="random.fact.%s" % i) for i in xrange(3)]

        for i in range(3):
            for j in range(i + 1):
                ViolationFactory.create(key=keys[i], domain=domains[j % 2])

        violations = Violation.get_by_key_id_group_by_domain(self.db, keys[1].id)
        expect(violations).to_be_like([("g0.com", 1), ("g1.com", 1)])

        violations = Violation.get_by_key_id_group_by_domain(self.db, keys[2].id)
        expect(violations).to_be_like([("g0.com", 2), ("g1.com", 1)])
Beispiel #3
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
     )
Beispiel #4
0
 def get_top_in_category_for_domain(self, domain, key_category_id, limit, callback=None):
     self.get_data(
         '%s-top-violations-cat-%s' % (domain.name, key_category_id),
         int(self.config.TOP_CATEGORY_VIOLATIONS_EXPIRATION_IN_SECONDS),
         lambda: Violation.get_top_in_category_for_domain(self.db, domain, key_category_id, limit),
         callback=callback
     )
Beispiel #5
0
 def get_group_by_category_id_for_domain(self, domain, callback=None):
     self.get_data(
         '%s-violations-by-category' % domain.name,
         int(self.config.VIOLATIONS_BY_CATEGORY_EXPIRATION_IN_SECONDS),
         lambda: Violation.get_group_by_category_id_for_domain(self.db, domain),
         callback=callback
     )
    def test_can_get_group_by_value_for_key(self):
        self.db.query(Key).delete()
        self.db.query(Violation).delete()
        keys = [KeyFactory.create(name="random.key.%s" % i) for i in range(3)]

        for i in range(3):
            for j in range(i + 1):
                ViolationFactory.create(key=keys[i], value="random.value.%d" % i)

        violations = Violation.get_group_by_value_for_key(self.db, keys[0].name)
        expect(violations).to_be_like([("random.value.0", 1)])

        violations = Violation.get_group_by_value_for_key(self.db, keys[1].name)
        expect(violations).to_be_like([("random.value.1", 2)])

        violations = Violation.get_group_by_value_for_key(self.db, keys[2].name)
        expect(violations).to_be_like([("random.value.2", 3)])
    def test_can_get_most_common_violations_names(self):
        for i in range(3):
            key = KeyFactory.create(name='some.random.fact.%s' % i)
            for j in range(i):
                ViolationFactory.create(key=key)

        violations = Violation.get_most_common_violations_names(self.db)

        expect(violations).to_be_like([('some.random.fact.1', 1), ('some.random.fact.2', 2)])
    def test_can_get_most_common_violations_names(self):
        for i in range(3):
            key = KeyFactory.create(name='some.random.fact.%s' % i)
            for j in range(i):
                ViolationFactory.create(key=key)

        violations = Violation.get_most_common_violations_names(self.db)

        expect(violations).to_be_like([('some.random.fact.1', 1), ('some.random.fact.2', 2)])
Beispiel #9
0
 def get_top_in_category_for_domain(self,
                                    domain,
                                    key_category_id,
                                    limit,
                                    callback=None):
     self.get_data(
         '%s-top-violations-cat-%s' % (domain.name, key_category_id),
         int(self.config.TOP_CATEGORY_VIOLATIONS_EXPIRATION_IN_SECONDS),
         lambda: Violation.get_top_in_category_for_domain(
             self.db, domain, key_category_id, limit),
         callback=callback)
    def test_can_get_group_by_value_for_key(self):
        self.db.query(Key).delete()
        self.db.query(Violation).delete()
        keys = [KeyFactory.create(name='random.key.%s' % i) for i in range(3)]

        for i in range(3):
            for j in range(i + 1):
                ViolationFactory.create(
                    key=keys[i],
                    value='random.value.%d' % i
                )

        violations = Violation.get_group_by_value_for_key(self.db, keys[0].name)
        expect(violations).to_be_like([('random.value.0', 1)])

        violations = Violation.get_group_by_value_for_key(self.db, keys[1].name)
        expect(violations).to_be_like([('random.value.1', 2)])

        violations = Violation.get_group_by_value_for_key(self.db, keys[2].name)
        expect(violations).to_be_like([('random.value.2', 3)])
    def test_can_get_top_in_category_for_all_domains(self):
        domains = [DomainFactory.create(name="g%d.com" % i) for i in range(2)]
        keys = [KeyFactory.create(name="random.fact.%s" % i) for i in range(3)]

        for i in range(3):
            for j in range(i + 1):
                ViolationFactory.create(key=keys[i], domain=domains[j % 2])
        violations = Violation.get_top_in_category_for_all_domains(self.db)

        expect(violations).to_length(5)
        top = ("g0.com", keys[2].category_id, str(keys[2]), 2)
        expect(violations[0]).to_be_like(top)
    def test_get_group_by_key_id_for_all_domains(self):
        domains = [DomainFactory.create(name="g%d.com" % i) for i in range(2)]
        keys = [KeyFactory.create(name="random.fact.%s" % i) for i in range(3)]

        for i in range(3):
            for j in range(i + 1):
                ViolationFactory.create(key=keys[i], domain=domains[j % 2])

        violations = Violation.get_group_by_key_id_for_all_domains(self.db)

        expect(violations).to_length(5)
        expect(violations[0]).to_be_like((keys[2].id, "g0.com", 2))
Beispiel #13
0
    def test_get_group_by_key_id_for_all_domains(self):
        domains = [DomainFactory.create(name='g%d.com' % i) for i in range(2)]
        keys = [KeyFactory.create(name='random.fact.%s' % i) for i in range(3)]

        for i in range(3):
            for j in range(i + 1):
                ViolationFactory.create(key=keys[i], domain=domains[j % 2])

        violations = Violation.get_group_by_key_id_for_all_domains(self.db)

        expect(violations).to_length(5)
        expect(violations[0]).to_be_like((keys[2].id, 'g0.com', 2))
Beispiel #14
0
    def get(self, key_name):
        violations = self.application.violation_definitions
        violation_title = violations[key_name]['title']
        key_id = violations[key_name]['key'].id

        domains = Violation.get_by_key_id_group_by_domain(self.db, key_id)

        violation = {
            'title': violation_title,
            'domains': [{'name': name, 'count': count} for (name, count) in domains],
            'total': sum(count for (name, count) in domains)
        }

        self.write_json(violation)
        self.finish()
    def test_can_get_top_in_category_for_all_domains(self):
        domains = [DomainFactory.create(name='g%d.com' % i) for i in range(2)]
        keys = [KeyFactory.create(name='random.fact.%s' % i) for i in range(3)]

        for i in range(3):
            for j in range(i + 1):
                ViolationFactory.create(
                    key=keys[i],
                    domain=domains[j % 2]
                )
        violations = Violation.get_top_in_category_for_all_domains(self.db)

        expect(violations).to_length(5)
        top = ('g0.com', keys[2].category_id, str(keys[2]), 2)
        expect(violations[0]).to_be_like(top)
Beispiel #16
0
    def _adjust_kwargs(cls, **kwargs):
        if 'page' in kwargs:
            kwargs['domain'] = kwargs['page'].domain

        if 'page' in kwargs and 'uuid' in kwargs:
            kwargs['page'].last_review_uuid = kwargs['uuid']

        if 'number_of_violations' in kwargs:
            number_of_violations = kwargs['number_of_violations']
            del kwargs['number_of_violations']

            if 'page' in kwargs:
                kwargs['page'].violations_count = number_of_violations

            violations = []
            for i in range(number_of_violations):
                key = Key.get_or_create(db, 'key.%d' % i,
                                        'category.%d' % (i % 3))
                violations.append(
                    Violation(key=key,
                              value="value %d" % i,
                              points=i,
                              domain=kwargs['page'].domain,
                              review_is_active=kwargs['is_active']))

            kwargs['violations'] = violations

        if 'number_of_facts' in kwargs:
            number_of_facts = kwargs['number_of_facts']
            del kwargs['number_of_facts']

            facts = []
            for i in range(number_of_facts):
                key = Key.get_or_create(db, 'key.%d' % i,
                                        'category.%d' % (i % 3))
                facts.append(Fact(key=key, value="value %d" % i))

            kwargs['facts'] = facts

        return kwargs