Skip to content

Commit

Permalink
Add test and new model to test in example app
Browse files Browse the repository at this point in the history
  • Loading branch information
SebCorbin committed Nov 19, 2022
1 parent 7937aea commit cb964c3
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 12 deletions.
3 changes: 2 additions & 1 deletion project/example_app/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib import admin
from django.urls import reverse

from .models import Blind
from .models import Blind, Category


class BlindAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -33,3 +33,4 @@ def desc(self, obj):


admin.site.register(Blind, BlindAdmin)
admin.site.register(Category, admin.ModelAdmin)
39 changes: 39 additions & 0 deletions project/example_app/migrations/0004_category_blind_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by Django 4.1.3 on 2022-11-19 17:10

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("example_app", "0003_blind_unique_name_if_provided"),
]

operations = [
migrations.CreateModel(
name="Category",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=50)),
],
),
migrations.AddField(
model_name="blind",
name="category",
field=models.ForeignKey(
null=True,
blank=True,
on_delete=django.db.models.deletion.SET_NULL,
to="example_app.category",
),
),
]
19 changes: 14 additions & 5 deletions project/example_app/models.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

# Create your models here.
from django.db.models import BooleanField, ImageField, TextField

class Category(models.Model):
name = models.CharField(max_length=50)

def __str__(self):
return self.name

class Meta:
verbose_name_plural = _("Categories")


class Product(models.Model):
photo = ImageField(upload_to='products')
photo = models.ImageField(upload_to='products')

class Meta:
abstract = True


class Blind(Product):
name = TextField()
child_safe = BooleanField(default=False)
name = models.TextField()
child_safe = models.BooleanField(default=False)
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True)

def __str__(self):
return self.name
Expand Down
2 changes: 2 additions & 0 deletions project/example_app/templates/example_app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ <h1>Example App</h1>
<th>Photo</th>
<th>Name</th>
<th>Child safe?</th>
<th>Category</th>
</tr>
{% for blind in blinds %}
<tr>
<td>{% if blind.photo %}<img class="blind" src="{{ blind.photo.url }}">{% endif %}</td>
<td>{{ blind.name }}</td>
<td>{% if blind.child_safe %}Yes{% else %}No{% endif %}</td>
<td>{{ blind.category }}</td>
</tr>
{% endfor %}
</table>
Expand Down
2 changes: 1 addition & 1 deletion project/example_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ def do_something_long():

class ExampleCreateView(CreateView):
model = models.Blind
fields = ['name']
fields = ['name', 'category']
success_url = reverse_lazy('example_app:index')
11 changes: 9 additions & 2 deletions project/tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import factory
import factory.fuzzy
from example_app.models import Blind
from example_app.models import Blind, Category

from silk.models import Request, Response, SQLQuery

Expand Down Expand Up @@ -34,10 +33,18 @@ class Meta:
model = Response


class CategoryFactory(factory.django.DjangoModelFactory):
name = factory.Faker('pystr', min_chars=5, max_chars=10)

class Meta:
model = Category


class BlindFactory(factory.django.DjangoModelFactory):
name = factory.Faker('pystr', min_chars=5, max_chars=10)
child_safe = factory.Faker('pybool')
photo = factory.django.ImageField()
category = factory.SubFactory(CategoryFactory)

class Meta:
model = Blind
4 changes: 3 additions & 1 deletion project/tests/test_lib/mock_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ def mock_sql_queries(self, request=None, profile=None, n=1, as_dict=False):
queries = []
for _ in range(0, n):
tb = ''.join(reversed(traceback.format_stack()))
random_query = self._random_query()
d = {
'query': self._random_query(),
'query': random_query,
'query_structure': random_query,
'start_time': start_time,
'end_time': end_time,
'request': request,
Expand Down
28 changes: 28 additions & 0 deletions project/tests/test_view_sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.test import TestCase

from silk.config import SilkyConfig
from silk.middleware import silky_reverse

from .test_lib.mock_suite import MockSuite


class TestViewSQL(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
SilkyConfig().SILKY_AUTHENTICATION = False
SilkyConfig().SILKY_AUTHORISATION = False

def test_duplicates_should_show(self):
"""Generate a lot of duplicates and test that they are visible on the page"""
request = MockSuite().mock_request()
request.queries.all().delete()
# Ensure we have a amount of queries with the same structure
query = MockSuite().mock_sql_queries(request=request, n=1)[0]
for _ in range(0, 4):
query.id = None
query.save()
url = silky_reverse('request_sql', kwargs={'request_id': request.id})
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertContains(response, '<td class="right-aligned">4</td>')
5 changes: 3 additions & 2 deletions silk/views/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ def get(self, request, *_, **kwargs):
duplicate_queries[q.query_structure] += 1
structures = list(duplicate_queries.keys())
for q in query_set:
q.num_duplicates = duplicate_queries[q.query_structure]
q.duplicate_id = structures.index(q.query_structure)
if q.query_structure:
q.num_duplicates = duplicate_queries[q.query_structure]
q.duplicate_id = structures.index(q.query_structure)
page = _page(request, query_set)
context['silk_request'] = silk_request
if profile_id:
Expand Down

0 comments on commit cb964c3

Please sign in to comment.