Fix for [#46], test updates by jjjordanmsft · Pull Request #48 · microsoft/ApplicationInsights-Python · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion applicationinsights/django/middleware.py
6 changes: 3 additions & 3 deletions django_tests/all_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ BASEDIR=$(pwd)
# Django/python compatibility matrix...
if $PYTHON -c "import sys; sys.exit(0 if sys.version_info < (3, 0) else 1)"; then
# Django2.0 won't support Python2
DJANGO_VERSIONS='1.7.11 1.8.18 1.9.13 1.10.7 1.11'
DJANGO_VERSIONS='1.7.11 1.8.18 1.9.13 1.10.7 1.11.4'
elif $PYTHON -c "import sys; sys.exit(0 if sys.version_info < (3, 5) else 1)"; then
DJANGO_VERSIONS='1.7.11 1.8.18 1.9.13 1.10.7 1.11'
DJANGO_VERSIONS='1.7.11 1.8.18 1.9.13 1.10.7 1.11.4'
else
# python3.5 dropped html.parser.HtmlParserError versions older than Django1.8 won't work
DJANGO_VERSIONS='1.8.18 1.9.13 1.10.7 1.11'
DJANGO_VERSIONS='1.8.18 1.9.13 1.10.7 1.11.4'
fi

# For each Django version...
Expand Down
1 change: 1 addition & 0 deletions django_tests/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ cd aitest
cp $SRCDIR/views.py aitest/views.py
cp $SRCDIR/tests.py aitest/tests.py
cp $SRCDIR/urls.py aitest/urls.py
cp $SRCDIR/template.html aitest/template.html

./manage.py test
exit $?
1 change: 1 addition & 0 deletions django_tests/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test django template: {{ context }}
22 changes: 21 additions & 1 deletion django_tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import logging

import django
Expand All @@ -15,6 +16,7 @@
TEST_IKEY = '12345678-1234-5678-9012-123456789abc'
TEST_ENDPOINT = 'https://test.endpoint/v2/track'
DEFAULT_ENDPOINT = AsynchronousSender().service_endpoint_uri
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

class AITestCase(TestCase):
def plug_sender(self):
Expand All @@ -37,7 +39,13 @@ def get_events(self, count):
return self.events

@modify_settings(**{MIDDLEWARE_NAME: {'append': 'applicationinsights.django.ApplicationInsightsMiddleware'}})
@override_settings(APPLICATION_INSIGHTS={'ikey': TEST_IKEY})
@override_settings(
APPLICATION_INSIGHTS={'ikey': TEST_IKEY},
# Templates for 1.7
TEMPLATE_DIRS=(PROJECT_ROOT,),
TEMPLATE_LOADERS=('django.template.loaders.filesystem.Loader',),
# Templates for 1.8 and up
TEMPLATES=[{'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [PROJECT_ROOT]}])
class MiddlewareTests(AITestCase):
def setUp(self):
self.plug_sender()
Expand Down Expand Up @@ -133,6 +141,18 @@ def test_error(self):
self.assertEqual(data['success'], False, "Success value")
self.assertEqual(data['url'], 'http://testserver/errorer', "Request url")

def test_template(self):
"""Tests that views using templates operate correctly and that template data is logged"""
response = self.client.get("/templater/ctx")
self.assertEqual(response.status_code, 200)

event = self.get_events(1)
data = event['data']['baseData']
self.assertEqual(event['name'], 'Microsoft.ApplicationInsights.Request', "Event type")
self.assertEqual(data['success'], True, "Success value")
self.assertEqual(data['responseCode'], 200, "Status code")
self.assertEqual(data['properties']['template_name'], 'template.html', "Template name")

def test_no_view_arguments(self):
"""Tests that view id logging is off by default"""
self.plug_sender()
Expand Down
1 change: 1 addition & 0 deletions django_tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
url(r'^errorer$', views.errorer, name='errorer'),
url(r'^getid/([0-9]+)$', views.getid, name='getid'),
url(r'^returncode/([0-9]+)$', views.returncode, name='returncode'),
url(r'^templater/([^/]*)$', views.templater, name='templater'),
]
4 changes: 4 additions & 0 deletions django_tests/views.py