def test_get_by_url_explicit_re(db_connection): logo1 = create_logo('1', link='https://abc.example.com', link_re=r'abc\.example\.com|moo\.example\.com') logo2 = create_logo('2', link='https://xyz.example.com', link_re=r'xyz\.example\.com|utm_medium') assert Logo.get_by_url('https://abc.example.com/something/') == logo1 assert Logo.get_by_url('https://moo.example.com/everything/?v=123') == logo1 assert Logo.get_by_url('https://xyz.example.com/moo/') == logo2 assert Logo.get_by_url('https://example.com/moo/?utm_medium=foo') == logo2
def main(): google_analytics_metrics = fetch_from_google_analytics() mailchimp_metrics = fetch_from_mailchimp() with db: Metric.drop_table() Metric.create_table() google_analytics_metric_names = [ 'avg_monthly_users', 'avg_monthly_pageviews', 'avg_monthly_handbook_users', 'avg_monthly_handbook_pageviews', 'avg_monthly_handbook_logo_clicks', 'avg_monthly_jobs_users', ] for name in google_analytics_metric_names: Metric.create(name=name, value=google_analytics_metrics[name]) Metric.create(name='subscribers', value=mailchimp_metrics['subscribers']) JobMetric.drop_table() JobMetric.create_table() for url, value in google_analytics_metrics['users_per_job'].items(): try: job = Job.get_by_url(url) JobMetric.create(job=job, name='users', value=value) except Job.DoesNotExist: pass for url, value in google_analytics_metrics['pageviews_per_job'].items( ): try: job = Job.get_by_url(url) JobMetric.create(job=job, name='pageviews', value=value) except Job.DoesNotExist: pass for url, value in google_analytics_metrics[ 'applications_per_job'].items(): try: job = Job.get_by_url(url) JobMetric.create(job=job, name='applications', value=value) except Job.DoesNotExist: pass users_per_external_url = merge_metric_dicts( google_analytics_metrics['users_per_external_job'], mailchimp_metrics['users_per_external_url']) for url, value in users_per_external_url.items(): try: job = Job.get_by_link(url) JobMetric.create(job=job, name='users', value=value) except Job.DoesNotExist: pass pageviews_per_external_url = merge_metric_dicts( google_analytics_metrics['pageviews_per_external_job'], mailchimp_metrics['pageviews_per_external_url']) for url, value in pageviews_per_external_url.items(): try: job = Job.get_by_link(url) JobMetric.create(job=job, name='pageviews', value=value) except Job.DoesNotExist: pass LogoMetric.drop_table() LogoMetric.create_table() for url, value in google_analytics_metrics['clicks_per_logo'].items(): try: logo = Logo.get_by_url(url) LogoMetric.create(logo=logo, name='clicks', value=value) except Logo.DoesNotExist: pass for logo in Logo.listing(): values = google_analytics_metrics['handbook_users_per_date'] metric = LogoMetric.from_values_per_date(logo, 'users', values) metric.save() values = google_analytics_metrics['handbook_pageviews_per_date'] metric = LogoMetric.from_values_per_date(logo, 'pageviews', values) metric.save()
def test_get_by_url_multiple_match(db_connection): create_logo('1', link='https://example.com', link_re=r'example\.com') create_logo('2', link='https://example.com/something/', link_re=r'example\.com') with pytest.raises(Logo.AmbiguousMatch): Logo.get_by_url('https://example.com/moo/')
def test_get_by_url_no_match(db_connection): create_logo('1', link='https://abc.example.com') create_logo('2', link='https://xyz.example.com') with pytest.raises(Logo.DoesNotExist): Logo.get_by_url('https://example.com/moo/?utm_medium=foo')
def test_get_by_url(db_connection): logo1 = create_logo('1', link='https://abc.example.com') logo2 = create_logo('2', link='https://xyz.example.com') assert Logo.get_by_url('https://abc.example.com/something/') == logo1 assert Logo.get_by_url('https://xyz.example.com/moo/?utm_medium=foo') == logo2