mirror of
https://github.com/django-components/django-components.git
synced 2025-11-23 07:45:21 +00:00
Deployed 06d08a7c to 0.143.0 with MkDocs 1.6.1 and mike 2.1.3
This commit is contained in:
parent
720aceb939
commit
14ae96e591
460 changed files with 47230 additions and 3 deletions
71
0.143.0/examples/analytics/test_example_analytics.py
Normal file
71
0.143.0/examples/analytics/test_example_analytics.py
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import pytest
|
||||
from django.template import Context, Template
|
||||
|
||||
from django_components import registry, types
|
||||
from django_components.testing import djc_test
|
||||
|
||||
|
||||
# Imported lazily, so we import components only once settings are set
|
||||
def _create_components():
|
||||
from docs.examples.analytics.component import ( # noqa: PLC0415
|
||||
ApiWidget,
|
||||
SentryErrorTracker,
|
||||
SuccessRateTracker,
|
||||
analytics_events,
|
||||
error_rate,
|
||||
)
|
||||
|
||||
registry.register("api_widget", ApiWidget)
|
||||
registry.register("sentry_error_tracker", SentryErrorTracker)
|
||||
registry.register("success_rate_tracker", SuccessRateTracker)
|
||||
analytics_events.clear()
|
||||
error_rate["error"] = 0
|
||||
error_rate["success"] = 0
|
||||
return analytics_events, error_rate
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@djc_test
|
||||
class TestAnalytics:
|
||||
def test_sentry_tracker_logs_only_errors(self):
|
||||
analytics_events, error_rate = _create_components()
|
||||
template_str: types.django_html = """
|
||||
{% load component_tags %}
|
||||
{% component "error_fallback" %}
|
||||
{% component "sentry_error_tracker" %}
|
||||
{% component "api_widget" simulate_error=True / %}
|
||||
{% endcomponent %}
|
||||
{% endcomponent %}
|
||||
{% component "sentry_error_tracker" %}
|
||||
{% component "api_widget" simulate_error=False / %}
|
||||
{% endcomponent %}
|
||||
"""
|
||||
template = Template(template_str)
|
||||
template.render(Context({}))
|
||||
|
||||
assert error_rate["error"] == 0
|
||||
assert error_rate["success"] == 0
|
||||
assert len(analytics_events) == 1
|
||||
assert analytics_events[0]["type"] == "error"
|
||||
assert analytics_events[0]["component"] == "sentry_error_tracker"
|
||||
assert analytics_events[0]["error"] is not None
|
||||
|
||||
def test_success_rate_tracker_logs_all(self):
|
||||
analytics_events, error_rate = _create_components()
|
||||
template_str: types.django_html = """
|
||||
{% load component_tags %}
|
||||
{% component "error_fallback" %}
|
||||
{% component "success_rate_tracker" %}
|
||||
{% component "api_widget" simulate_error=True / %}
|
||||
{% endcomponent %}
|
||||
{% endcomponent %}
|
||||
{% component "success_rate_tracker" %}
|
||||
{% component "api_widget" simulate_error=False / %}
|
||||
{% endcomponent %}
|
||||
"""
|
||||
template = Template(template_str)
|
||||
template.render(Context({}))
|
||||
|
||||
assert len(analytics_events) == 0
|
||||
assert error_rate["error"] == 1
|
||||
assert error_rate["success"] == 1
|
||||
Loading…
Add table
Add a link
Reference in a new issue