<commit_before># encoding: utf-8

import time

from web.core.context import Context
from web.ext.analytics import AnalyticsExtension


def test_analytics_extension():
	ctx = Context(response=Context(headers=dict()))
	ext = AnalyticsExtension()
	
	assert not hasattr(ctx, '_start_time')
	ext.prepare(ctx)
	
	assert hasattr(ctx, '_start_time')
	ext.before(ctx)
	time.sleep(0.1)
	
	ext.after(ctx)
	assert 0.1 <= float(ctx.response.headers['X-Generation-Time']) <= 0.2
<commit_msg>Add test for full processing pipeline.<commit_after># encoding: utf-8

import time
import pytest

from webob import Request
from web.core import Application
from web.core.context import Context
from web.ext.analytics import AnalyticsExtension


def endpoint(context):
	time.sleep(0.1)
	return "Hi."


sample = Application(endpoint, extensions=[AnalyticsExtension()])


def test_analytics_extension():
	ctx = Context(response=Context(headers=dict()))
	ext = AnalyticsExtension()
	
	assert not hasattr(ctx, '_start_time')
	ext.prepare(ctx)
	
	assert hasattr(ctx, '_start_time')
	ext.before(ctx)
	time.sleep(0.1)
	
	ext.after(ctx)
	assert 0.1 <= float(ctx.response.headers['X-Generation-Time']) <= 0.2


def test_analytics_extension_in_context():
	try:
		__import__('web.dispatch.object')
	except ImportError:
		pytest.skip("web.dispatch.object not installed")
	
	resp = Request.blank('/').get_response(sample)
	assert 0.1 <= float(resp.headers['X-Generation-Time']) <= 0.2

{'fraction_non_alphanumeric': 0.13259668508287292, 'fraction_numerical': 0.013812154696132596, 'mean_word_length': 5.44, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2478206', 'n_tokens_mistral': 495, 'n_tokens_neox': 465, 'n_words': 106}