Created
July 16, 2020 08:11
-
-
Save kharandziuk/c9050e044b552f97cf1d4a04c61db657 to your computer and use it in GitHub Desktop.
django aggregation query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import factory | |
from broschures import models | |
class BroschureFactory(factory.django.DjangoModelFactory): | |
class Meta: | |
model = models.Broschure | |
class BroschureClickFactory(factory.django.DjangoModelFactory): | |
class Meta: | |
model = models.BroschureClick |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
import datetime | |
from django.utils.timezone import make_aware | |
class BroschureManager(models.Manager): | |
def with_clicks(self, number, year, month): | |
start = make_aware(datetime.datetime(year, month, 1, 0, 0, 0)) | |
end = make_aware( | |
datetime.datetime(year, month + 1, 1, 0, 0, 0) - datetime.timedelta(days=1) | |
) | |
return self.annotate(num_clicks=models.Sum( | |
models.Case( | |
models.When( | |
clicks__created__range=( | |
start, end | |
), | |
then=1, | |
), | |
default=0, | |
output_field=models.IntegerField(), | |
) | |
) | |
).filter(num_clicks__gt=number) | |
class Broschure(models.Model): | |
name = models.CharField(max_length=255) | |
objects = BroschureManager() | |
class BroschureClick(models.Model): | |
broschure = models.ForeignKey( | |
'broschures.Broschure', related_name='clicks', on_delete=models.CASCADE | |
) | |
created = models.DateTimeField(auto_now_add=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from django.urls import reverse | |
from django.utils.http import urlencode | |
import pytest | |
from broschures import models, factories | |
import pytz | |
from unittest import mock | |
import datetime | |
from django.utils.timezone import make_aware | |
pytestmark = pytest.mark.django_db | |
def test__with_a_match(client): | |
broschure = factories.BroschureFactory() | |
mocked = make_aware(datetime.datetime(2019, 10, 10, 0, 0, 0)) | |
with mock.patch('django.utils.timezone.now', mock.Mock(return_value=mocked)): | |
for x in range(30): | |
factories.BroschureClickFactory(broschure=broschure) | |
assert models.Broschure.objects.with_clicks(29, 2019, 10).count() == 1 | |
def test__with_no_match(client): | |
broschure = factories.BroschureFactory() | |
mocked = make_aware(datetime.datetime(2019, 8, 10, 0, 0, 0)) | |
with mock.patch('django.utils.timezone.now', mock.Mock(return_value=mocked)): | |
for x in range(3): | |
factories.BroschureClickFactory(broschure=broschure) | |
assert models.Broschure.objects.with_clicks(1, 2019, 10).count() == 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It will generate query below: