Skip to content

Commit

Permalink
Fix register_singular_query_field for non-page models
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Dec 8, 2020
1 parent 0968141 commit d7e2770
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion example/db.sqlite3
Git LFS file not shown
6 changes: 6 additions & 0 deletions example/home/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Advert,
Author,
Person,
SimpleModel,
)
from wagtail.core import blocks

Expand Down Expand Up @@ -138,3 +139,8 @@ class Meta:

url = factory.Sequence(lambda n: f"Person {n}")
text = fuzzy.FuzzyText()


class SimpleModelFactory(factory.django.DjangoModelFactory):
class Meta:
model = SimpleModel
27 changes: 27 additions & 0 deletions example/home/migrations/0019_simplemodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.1.3 on 2020-12-08 18:50

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("home", "0018_add_pagechooserblock"),
]

operations = [
migrations.CreateModel(
name="SimpleModel",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
],
),
]
5 changes: 5 additions & 0 deletions example/home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
from home.blocks import StreamFieldBlock


@register_singular_query_field("simpleModel")
class SimpleModel(models.Model):
pass


class HomePage(Page):
pass

Expand Down
54 changes: 54 additions & 0 deletions example/home/test/test_general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from example.tests.test_grapple import BaseGrappleTest

from home.factories import BlogPageFactory, SimpleModelFactory


class TestRegisterSingularQueryField(BaseGrappleTest):
def test_singular_blog_page_query(self):
def query():
return """
{
firstPost {
id
}
}
"""

blog_post = BlogPageFactory()
another_post = BlogPageFactory()
results = self.client.execute(query())

self.assertTrue("firstPost" in results["data"])
self.assertEqual(int(results["data"]["firstPost"]["id"]), blog_post.id)

results = self.client.execute(
"""
{
firstPost(order: "-id") {
id
}
}
"""
)

self.assertTrue("firstPost" in results["data"])
self.assertEqual(int(results["data"]["firstPost"]["id"]), another_post.id)

def test_singular_django_model_query(self):
def query():
return """
{
simpleModel {
id
}
}
"""

results = self.client.execute(query())
self.assertTrue("simpleModel" in results["data"])
self.assertIsNone(results["data"]["simpleModel"])

instance = SimpleModelFactory()
results = self.client.execute(query())

self.assertEqual(int(results["data"]["simpleModel"]["id"]), instance.id)
2 changes: 1 addition & 1 deletion grapple/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def resolve_singular(self, _, info, **kwargs):

return qs.live().public().filter(**kwargs).first()

return cls.filter(**kwargs).first()
return qs.filter(**kwargs).first()
except:
return None

Expand Down

0 comments on commit d7e2770

Please sign in to comment.