Exemple #1
0
    def test_update_bad(self):
        media = self.db().create('media', {'url': self.bad_url, 'name': 'a'})
        client = self.get_test_client()
        tasks.update(self.db(), media['media_id'], client)

        result = self.db().find_by_id('similarweb_media_metrics', media['media_id'])
        assert result['monthly_audience'] == 0
Exemple #2
0
    def test_update_good(self):
        media = self.db().create('media', {'url': self.good_url, 'name': 'a'})
        client = self.get_test_client()
        tasks.update(self.db(), media['media_id'], client)
        result = self.db().find_by_id('similarweb_media_metrics', media['media_id'])
        visits = self.good_json['visits']
        expected_visits = int(sum(j['visits'] for j in visits) / len(visits))

        assert result['monthly_audience'] == expected_visits
Exemple #3
0
    def test_multiple_update(self):
        media = self.db().create('media', {'url': self.bad_url, 'name': 'a'})
        client = self.get_test_client()
        tasks.update(self.db(), media['media_id'], client)

        result = self.db().find_by_id('similarweb_media_metrics', media['media_id'])
        assert result['monthly_audience'] == 0

        responses.replace(responses.GET, client.prepare_similarweb_url(self.bad_url),
                          json=self.good_json, status=200)
        tasks.update(self.db(), media['media_id'], client)
        result = self.db().find_by_id('similarweb_media_metrics', media['media_id'])
        assert result['monthly_audience'] > 0
Exemple #4
0
    def run_job(cls, media_id: int) -> None:
        if isinstance(media_id, bytes):
            media_id = decode_object_from_bytes_if_needed(media_id)

        media_id = int(media_id)

        db = connect_to_db()
        similarweb_client = get_similarweb_client()

        log.info(
            "Collecting audience data for media ID {}...".format(media_id))
        update(db, media_id, similarweb_client)

        log.info("Finished collecting audience data for media ID {}".format(
            media_id))
Exemple #5
0
 def test_bad_id(self):
     # Should fail without touching the similarweb client
     with pytest.raises(ValueError):
         tasks.update(self.db(), b'-1', None)