1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
import pytest
from django.db import connection
from django.test.utils import CaptureQueriesContext
from strawberry.relay.utils import to_base64
from tests import utils
from tests.projects.faker import (
FavoriteFactory,
IssueFactory,
MilestoneFactory,
ProjectFactory,
QuizFactory,
UserFactory,
)
from tests.projects.models import Favorite, Milestone, Project, Quiz
@pytest.mark.django_db(transaction=True)
def test_required(gql_client: utils.GraphQLTestClient):
# Query a required field, and a nested required field with no ordering
# We expect the queries to **not** have an `ORDER BY` clause
query = """
query testRequired($id: ID!) {
projectMandatory(id: $id) {
name
firstMilestoneRequired {
name
}
}
}
"""
# Sanity check the models
assert Project._meta.ordering == []
assert Milestone._meta.ordering == []
# Create a project and a milestone
project = ProjectFactory()
milestone = MilestoneFactory(project=project)
# Run the query
# Capture the SQL queries that are executed
with CaptureQueriesContext(connection) as ctx:
result = gql_client.query(
query,
variables={"id": to_base64("ProjectType", project.pk)},
)
# Sanity check the results
assert not result.errors
assert result.data == {
"projectMandatory": {
"name": project.name,
"firstMilestoneRequired": {"name": milestone.name},
}
}
# Assert that the queries do **not** have an `ORDER BY` clause
for query in ctx.captured_queries:
assert "ORDER BY" not in query["sql"]
@pytest.mark.django_db(transaction=True)
def test_optional(gql_client: utils.GraphQLTestClient):
# Query an optional field, and a nested optional field with no ordering
# We expect the queries to have an `ORDER BY` clause
query = """
query testOptional($id: ID!) {
project(id: $id) {
name
firstMilestone {
name
}
}
}
"""
# Sanity check the models
assert Project._meta.ordering == []
assert Milestone._meta.ordering == []
# Create a project and a milestone
project = ProjectFactory()
milestone = MilestoneFactory(project=project)
# Run the query
# Capture the SQL queries that are executed
with CaptureQueriesContext(connection) as ctx:
result = gql_client.query(
query,
variables={"id": to_base64("ProjectType", project.pk)},
)
# Sanity check the results
assert not result.errors
assert result.data == {
"project": {
"name": project.name,
"firstMilestone": {"name": milestone.name},
}
}
# Assert that the queries do have an `ORDER BY` clause
for query in ctx.captured_queries:
assert "ORDER BY" in query["sql"]
@pytest.mark.django_db(transaction=True)
def test_list(gql_client: utils.GraphQLTestClient):
# Query a list field, and a nested list field with no ordering
# We expect the queries to have an `ORDER BY` clause
query = """
query testList{
projectList {
name
milestones {
name
}
}
}
"""
# Sanity check the models
assert Project._meta.ordering == []
assert Milestone._meta.ordering == []
# Create some projects and milestones
projects = ProjectFactory.create_batch(3)
milestones = []
for project in projects:
milestones.extend(MilestoneFactory.create_batch(3, project=project))
# Run the query
# Capture the SQL queries that are executed
with CaptureQueriesContext(connection) as ctx:
result = gql_client.query(query)
# Sanity check the results
assert not result.errors
assert result.data == {
"projectList": [
{
"name": project.name,
"milestones": [
{"name": milestone.name}
for milestone in project.milestones.order_by("pk")
],
}
for project in Project.objects.order_by("pk")
]
}
# Assert that the queries do have an `ORDER BY` clause
for query in ctx.captured_queries:
assert "ORDER BY" in query["sql"]
@pytest.mark.django_db(transaction=True)
def test_connection(gql_client: utils.GraphQLTestClient):
# Query a connection field, and a nested connection field with no ordering
# We expect the queries to have an `ORDER BY` clause
query = """
query testConnection{
projectConn {
edges {
node {
name
milestoneConn {
edges {
node {
name
}
}
}
}
}
}
}
"""
# Sanity check the models
assert Project._meta.ordering == []
assert Milestone._meta.ordering == []
# Create some projects and milestones
projects = ProjectFactory.create_batch(3)
milestones = []
for project in projects:
milestones.extend(MilestoneFactory.create_batch(3, project=project))
# Run the query
# Capture the SQL queries that are executed
with CaptureQueriesContext(connection) as ctx:
result = gql_client.query(query)
# Sanity check the results
assert not result.errors
assert result.data == {
"projectConn": {
"edges": [
{
"node": {
"name": project.name,
"milestoneConn": {
"edges": [
{"node": {"name": milestone.name}}
for milestone in project.milestones.order_by("pk")
]
},
}
}
for project in Project.objects.order_by("pk")
]
}
}
# Assert that the queries do have an `ORDER BY` clause
for query in ctx.captured_queries:
assert "ORDER BY" in query["sql"]
@pytest.mark.django_db(transaction=True)
def test_paginated(gql_client: utils.GraphQLTestClient):
# Query a paginated field, and a nested paginated field with no ordering
# We expect the queries to have an `ORDER BY` clause
query = """
query testPaginated{
projectsPaginated {
results {
name
milestonesPaginated {
results {
name
}
}
}
}
}
"""
# Sanity check the models
assert Project._meta.ordering == []
assert Milestone._meta.ordering == []
# Create some projects and milestones
projects = ProjectFactory.create_batch(3)
milestones = []
for project in projects:
milestones.extend(MilestoneFactory.create_batch(3, project=project))
# Run the query
# Capture the SQL queries that are executed
with CaptureQueriesContext(connection) as ctx:
result = gql_client.query(query)
# Sanity check the results
assert not result.errors
assert result.data == {
"projectsPaginated": {
"results": [
{
"name": project.name,
"milestonesPaginated": {
"results": [
{"name": milestone.name}
for milestone in project.milestones.order_by("pk")
]
},
}
for project in Project.objects.order_by("pk")
]
}
}
# Assert that the queries do have an `ORDER BY` clause
for query in ctx.captured_queries:
assert "ORDER BY" in query["sql"]
@pytest.mark.django_db(transaction=True)
def test_default_ordering(gql_client: utils.GraphQLTestClient):
# Query a field for a model with default ordering
# We expect the default ordering to be respected
query = """
query testDefaultOrdering{
favoriteConn {
edges {
node {
name
}
}
}
}
"""
# Sanity check the model
assert Favorite._meta.ordering == ("name",)
# Create some favorites
# Ensure the names are in reverse order to the primary keys
user = UserFactory()
issue = IssueFactory()
favorites = [
FavoriteFactory(name=name, user=user, issue=issue) for name in ["c", "b", "a"]
]
# Run the query
# Note that we need to login to access the favorites
with gql_client.login(user):
result = gql_client.query(query)
# Sanity check the results
# We expect the favorites to be ordered by name
assert not result.errors
assert result.data == {
"favoriteConn": {
"edges": [
{"node": {"name": favorite.name}} for favorite in reversed(favorites)
]
}
}
@pytest.mark.django_db(transaction=True)
def test_get_queryset_ordering(gql_client: utils.GraphQLTestClient):
# Query a field for a type with a `get_queryset` method that applies ordering
# We expect the ordering to be respected
query = """
query testGetQuerySetOrdering{
quizList {
title
}
}
"""
# Sanity check the model
assert Quiz._meta.ordering == []
# Create some quizzes
# Ensure the titles are in reverse order to the primary keys
quizzes = [QuizFactory(title=title) for title in ["c", "b", "a"]]
# Run the query
result = gql_client.query(query)
# Sanity check the results
# We expect the quizzes to be ordered by title
assert not result.errors
assert result.data == {
"quizList": [{"title": quiz.title} for quiz in reversed(quizzes)]
}
@pytest.mark.django_db(transaction=True)
def test_graphql_ordering(gql_client: utils.GraphQLTestClient):
# Query a field for a type that allows ordering via GraphQL
# We expect the ordering to be respected
query = """
query testGraphQLOrdering{
milestoneList(order: { name: ASC }) {
name
}
}
"""
# Sanity check the model
assert Milestone._meta.ordering == []
# Create some milestones
# Ensure the names are in reverse order to the primary keys
milestones = [MilestoneFactory(name=name) for name in ["c", "b", "a"]]
# Run the query
result = gql_client.query(query)
# Sanity check the results
# We expect the milestones to be ordered by name
assert not result.errors
assert result.data == {
"milestoneList": [
{"name": milestone.name} for milestone in reversed(milestones)
]
}
|