Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
catcombo committed Jun 16, 2019
2 parents 950e3e8 + 104484b commit eadd38d
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 27 deletions.
7 changes: 0 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ python:
- "3.6"
- "3.7"

services:
- mysql

before_install:
- mysql -e 'CREATE DATABASE test_db;'

env:
- DJANGO_VERSION=1.8
- DJANGO_VERSION=1.10
Expand All @@ -34,7 +28,6 @@ matrix:

install:
- pip install -q Django==$DJANGO_VERSION
- pip install mysqlclient

script:
- python manage.py test
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='django-speedinfo',
version='1.4.0',
version='1.4.1',
packages=['speedinfo', 'speedinfo.migrations', 'speedinfo.conditions'],
include_package_data=True,
license='MIT',
Expand All @@ -28,6 +28,7 @@
'Framework :: Django :: 1.11',
'Framework :: Django :: 2.0',
'Framework :: Django :: 2.1',
'Framework :: Django :: 2.2',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
Expand Down
1 change: 1 addition & 0 deletions speedinfo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

from speedinfo.profiler import Profiler

default_app_config = 'speedinfo.apps.SpeedinfoConfig'
profiler = Profiler()
4 changes: 2 additions & 2 deletions speedinfo/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def get_queryset(self, request):
qs = super(ViewProfilerAdmin, self).get_queryset(request)

for rc in speedinfo_settings.SPEEDINFO_REPORT_COLUMNS_FORMAT:
if (rc.attr_name in speedinfo_settings.SPEEDINFO_REPORT_COLUMNS) and not isinstance(rc.order_field, str):
if (rc.attr_name in speedinfo_settings.SPEEDINFO_REPORT_COLUMNS) and not isinstance(rc.expression, str):
qs = qs.annotate(**{
rc.attr_name: rc.order_field
rc.attr_name: rc.expression
})

return qs
Expand Down
78 changes: 78 additions & 0 deletions speedinfo/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# coding: utf-8

import django
from django.conf import settings
from django.core.checks import Error, Warning, register
from django.apps import AppConfig

if django.VERSION < (1, 10):
MIDDLEWARE_SETTINGS_NAME = 'MIDDLEWARE_CLASSES'
else:
MIDDLEWARE_SETTINGS_NAME = 'MIDDLEWARE'


def check_middleware(app_configs, **kwargs):
profiler_middleware_index = None
profiler_middleware_multiple = False
cache_middleware_index = None
errors = []

for i, middleware in enumerate(getattr(settings, MIDDLEWARE_SETTINGS_NAME)):
if middleware == 'speedinfo.middleware.ProfilerMiddleware':
profiler_middleware_multiple = profiler_middleware_index is not None
profiler_middleware_index = i
elif middleware == 'django.middleware.cache.FetchFromCacheMiddleware':
cache_middleware_index = i

if profiler_middleware_index is None:
errors.append(
Warning(
'speedinfo.middleware.ProfilerMiddleware is missing from %s' % MIDDLEWARE_SETTINGS_NAME,
hint='Add speedinfo.middleware.ProfilerMiddleware to %s' % MIDDLEWARE_SETTINGS_NAME,
id='speedinfo.W001',
)
)

elif (cache_middleware_index is not None) and (profiler_middleware_index > cache_middleware_index):
errors.append(
Error(
'speedinfo.middleware.ProfilerMiddleware occurs after '
'django.middleware.cache.FetchFromCacheMiddleware in %s' % MIDDLEWARE_SETTINGS_NAME,
hint='Move speedinfo.middleware.ProfilerMiddleware to before '
'django.middleware.cache.FetchFromCacheMiddleware in %s' % MIDDLEWARE_SETTINGS_NAME,
id='speedinfo.E001',
)
)

elif profiler_middleware_multiple:
errors.append(
Error(
'speedinfo.middleware.ProfilerMiddleware occurs multiple times in %s' % MIDDLEWARE_SETTINGS_NAME,
hint='Add speedinfo.middleware.ProfilerMiddleware only once in %s' % MIDDLEWARE_SETTINGS_NAME,
id='speedinfo.E002',
)
)

return errors


def check_cache_backend(app_configs, **kwargs):
for params in settings.CACHES.values():
if params.get('BACKEND') == 'speedinfo.backends.proxy_cache':
return []

return [
Error(
'speedinfo.backends.proxy_cache is missing from CACHES',
hint='Add speedinfo.backends.proxy_cache to CACHES',
id='speedinfo.E003',
)
]


class SpeedinfoConfig(AppConfig):
name = 'speedinfo'

def ready(self):
register()(check_middleware)
register()(check_cache_backend)
2 changes: 1 addition & 1 deletion speedinfo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.conf import settings
from django.db.models import ExpressionWrapper, F, FloatField, IntegerField

ReportColumnFormat = namedtuple('ReportColumnFormat', ['name', 'format', 'attr_name', 'order_field'])
ReportColumnFormat = namedtuple('ReportColumnFormat', ['name', 'format', 'attr_name', 'expression'])

DEFAULTS = {
'SPEEDINFO_TESTS': False,
Expand Down
10 changes: 4 additions & 6 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = 'secret-key'
SECRET_KEY = 'dummy'
DEBUG = False
ALLOWED_HOSTS = ['*']

INSTALLED_APPS = [
'django.contrib.admin',
Expand Down Expand Up @@ -54,11 +55,8 @@

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test_db',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'db.sqlite',
}
}

Expand Down
73 changes: 63 additions & 10 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import django

from time import sleep

from django.contrib.auth.models import User
from django.core.cache import cache
from django.core.checks import run_checks
from django.test import TestCase, override_settings

from speedinfo import profiler
Expand All @@ -16,6 +15,64 @@
except ImportError:
from django.core.urlresolvers import reverse

if django.VERSION < (1, 10):
MIDDLEWARE_SETTINGS_NAME = 'MIDDLEWARE_CLASSES'
else:
MIDDLEWARE_SETTINGS_NAME = 'MIDDLEWARE'


class SystemChecksTestCase(TestCase):
def test_valid_middleware_config(self):
messages = run_checks()
self.assertEqual(messages, [])

def test_missing_middleware(self):
with self.settings(**{MIDDLEWARE_SETTINGS_NAME: [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
]}):
messages = run_checks()
self.assertTrue(len(messages) > 0)
self.assertEqual(messages[0].id, 'speedinfo.W001')

def test_cache_middleware_position(self):
with self.settings(**{MIDDLEWARE_SETTINGS_NAME: [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'speedinfo.middleware.ProfilerMiddleware',
]}):
messages = run_checks()
self.assertTrue(len(messages) > 0)
self.assertEqual(messages[0].id, 'speedinfo.E001')

def test_multiple_middlewares(self):
with self.settings(**{MIDDLEWARE_SETTINGS_NAME: [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'speedinfo.middleware.ProfilerMiddleware',
'speedinfo.middleware.ProfilerMiddleware',
]}):
messages = run_checks()
self.assertTrue(len(messages) > 0)
self.assertEqual(messages[0].id, 'speedinfo.E002')

def test_missing_cache_backend(self):
with self.settings(CACHES={
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
}
}):
messages = run_checks()
self.assertTrue(len(messages) > 0)
self.assertEqual(messages[0].id, 'speedinfo.E003')


@override_settings(SPEEDINFO_EXCLUDE_URLS=[], SPEEDINFO_TESTS=True)
class ProfilerTest(TestCase):
Expand Down Expand Up @@ -115,19 +172,15 @@ def test_per_view_cache(self):
data.refresh_from_db()
self.assertEqual(data.cache_hits, 2)

# Wait for the cache timeout
sleep(3)
# Clear cache
cache.clear()

self.client.get(self.cached_func_view_url)
data.refresh_from_db()
self.assertEqual(data.cache_hits, 2)

def test_per_site_cache(self):
if django.VERSION < (1, 10):
middleware_settings_name = 'MIDDLEWARE_CLASSES'
else:
middleware_settings_name = 'MIDDLEWARE'

with self.modify_settings(**{middleware_settings_name: {
with self.modify_settings(**{MIDDLEWARE_SETTINGS_NAME: {
'append': 'django.middleware.cache.FetchFromCacheMiddleware',
'prepend': 'django.middleware.cache.UpdateCacheMiddleware',
}}):
Expand Down

0 comments on commit eadd38d

Please sign in to comment.