Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test according to @warmwaffles's example #116

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions tests/fake_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ def define_fake_partitioned_model(
return model


def get_fake_partitioned_model(
fields=None, partitioning_options={}, meta_options={}
):
"""Defines a fake partitioned model and creates it in the database."""

model = define_fake_partitioned_model(
fields, partitioning_options, meta_options
)

with connection.schema_editor() as schema_editor:
schema_editor.create_model(model)

return model


def get_fake_model(fields=None, model_base=PostgresModel, meta_options={}):
"""Defines a fake model and creates it in the database."""

Expand Down
73 changes: 71 additions & 2 deletions tests/test_query.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import uuid

from datetime import datetime

import freezegun

from dateutil.relativedelta import relativedelta
from django.db import models
from django.db.models import F
from django.db.models import F, Sum

from psqlextra.expressions import HStoreRef
from psqlextra.fields import HStoreField
from psqlextra.types import PostgresPartitioningMethod

from .fake_model import get_fake_model
from .fake_model import get_fake_model, get_fake_partitioned_model


def test_query_annotate_hstore_key_ref():
Expand Down Expand Up @@ -114,3 +122,64 @@ def test_query_hstore_value_update_escape():

inst = model.objects.all().first()
assert inst.title.get("en") == "console.log('test')"


@freezegun.freeze_time("2020-10-23")
def test_query_annotate_rename_fail_for_warmwaffles():
Account = get_fake_model(
{"id": models.UUIDField(primary_key=True, default=uuid.uuid4)}
)

AmsCampaignReport = get_fake_partitioned_model(
{
"id": models.UUIDField(primary_key=True, default=uuid.uuid4),
"account": models.ForeignKey(
to=Account, on_delete=models.DO_NOTHING
),
"report_date": models.DateField(),
"metric": models.IntegerField(),
},
{"method": PostgresPartitioningMethod.RANGE, "key": ["report_date"]},
)

account1 = Account.objects.create()
account2 = Account.objects.create()
AmsCampaignReport.objects.create(
account=account1, report_date=datetime.now().date(), metric=2
)
AmsCampaignReport.objects.create(
account=account1, report_date=datetime.now().date(), metric=4
)
AmsCampaignReport.objects.create(
account=account2, report_date=datetime.now().date(), metric=7
)
AmsCampaignReport.objects.create(
account=account2, report_date=datetime.now().date(), metric=3
)

result = list(
AmsCampaignReport.objects.filter(
account__in=[account1, account2],
report_date__range=(
datetime.now() - relativedelta(days=1),
datetime.now(),
),
)
.values("account_id", date=F("report_date"))
.annotate(total_metric=Sum(F("metric")))
.order_by("total_metric")
.all()
)

assert result == [
{
"account_id": account1.id,
"date": datetime.now().date(),
"total_metric": 6,
},
{
"account_id": account2.id,
"date": datetime.now().date(),
"total_metric": 10,
},
]