|
| 1 | +import re |
| 2 | + |
1 | 3 | from pytest import raises
|
2 | 4 |
|
3 | 5 | from ...types import Argument, Field, Int, List, NonNull, ObjectType, Schema, String
|
4 |
| -from ..connection import Connection, ConnectionField, PageInfo |
| 6 | +from ..connection import ( |
| 7 | + Connection, |
| 8 | + ConnectionField, |
| 9 | + PageInfo, |
| 10 | + ConnectionOptions, |
| 11 | + get_edge_class, |
| 12 | +) |
5 | 13 | from ..node import Node
|
6 | 14 |
|
7 | 15 |
|
@@ -51,6 +59,111 @@ class Meta:
|
51 | 59 | assert list(fields) == ["page_info", "edges", "extra"]
|
52 | 60 |
|
53 | 61 |
|
| 62 | +def test_connection_extra_abstract_fields(): |
| 63 | + class ConnectionWithNodes(Connection): |
| 64 | + class Meta: |
| 65 | + abstract = True |
| 66 | + |
| 67 | + @classmethod |
| 68 | + def __init_subclass_with_meta__(cls, node=None, name=None, **options): |
| 69 | + _meta = ConnectionOptions(cls) |
| 70 | + |
| 71 | + _meta.fields = { |
| 72 | + "nodes": Field( |
| 73 | + NonNull(List(node)), |
| 74 | + description="Contains all the nodes in this connection.", |
| 75 | + ), |
| 76 | + } |
| 77 | + |
| 78 | + return super(ConnectionWithNodes, cls).__init_subclass_with_meta__( |
| 79 | + node=node, name=name, _meta=_meta, **options |
| 80 | + ) |
| 81 | + |
| 82 | + class MyObjectConnection(ConnectionWithNodes): |
| 83 | + class Meta: |
| 84 | + node = MyObject |
| 85 | + |
| 86 | + class Edge: |
| 87 | + other = String() |
| 88 | + |
| 89 | + assert MyObjectConnection._meta.name == "MyObjectConnection" |
| 90 | + fields = MyObjectConnection._meta.fields |
| 91 | + assert list(fields) == ["nodes", "page_info", "edges"] |
| 92 | + edge_field = fields["edges"] |
| 93 | + pageinfo_field = fields["page_info"] |
| 94 | + nodes_field = fields["nodes"] |
| 95 | + |
| 96 | + assert isinstance(edge_field, Field) |
| 97 | + assert isinstance(edge_field.type, NonNull) |
| 98 | + assert isinstance(edge_field.type.of_type, List) |
| 99 | + assert edge_field.type.of_type.of_type == MyObjectConnection.Edge |
| 100 | + |
| 101 | + assert isinstance(pageinfo_field, Field) |
| 102 | + assert isinstance(pageinfo_field.type, NonNull) |
| 103 | + assert pageinfo_field.type.of_type == PageInfo |
| 104 | + |
| 105 | + assert isinstance(nodes_field, Field) |
| 106 | + assert isinstance(nodes_field.type, NonNull) |
| 107 | + assert isinstance(nodes_field.type.of_type, List) |
| 108 | + assert nodes_field.type.of_type.of_type == MyObject |
| 109 | + |
| 110 | + |
| 111 | +def test_connection_override_fields(): |
| 112 | + class ConnectionWithNodes(Connection): |
| 113 | + class Meta: |
| 114 | + abstract = True |
| 115 | + |
| 116 | + @classmethod |
| 117 | + def __init_subclass_with_meta__(cls, node=None, name=None, **options): |
| 118 | + _meta = ConnectionOptions(cls) |
| 119 | + base_name = ( |
| 120 | + re.sub("Connection$", "", name or cls.__name__) or node._meta.name |
| 121 | + ) |
| 122 | + |
| 123 | + edge_class = get_edge_class(cls, node, base_name) |
| 124 | + |
| 125 | + _meta.fields = { |
| 126 | + "page_info": Field( |
| 127 | + NonNull( |
| 128 | + PageInfo, |
| 129 | + name="pageInfo", |
| 130 | + required=True, |
| 131 | + description="Pagination data for this connection.", |
| 132 | + ) |
| 133 | + ), |
| 134 | + "edges": Field( |
| 135 | + NonNull(List(NonNull(edge_class))), |
| 136 | + description="Contains the nodes in this connection.", |
| 137 | + ), |
| 138 | + } |
| 139 | + |
| 140 | + return super(ConnectionWithNodes, cls).__init_subclass_with_meta__( |
| 141 | + node=node, name=name, _meta=_meta, **options |
| 142 | + ) |
| 143 | + |
| 144 | + class MyObjectConnection(ConnectionWithNodes): |
| 145 | + class Meta: |
| 146 | + node = MyObject |
| 147 | + |
| 148 | + assert MyObjectConnection._meta.name == "MyObjectConnection" |
| 149 | + fields = MyObjectConnection._meta.fields |
| 150 | + assert list(fields) == ["page_info", "edges"] |
| 151 | + edge_field = fields["edges"] |
| 152 | + pageinfo_field = fields["page_info"] |
| 153 | + |
| 154 | + assert isinstance(edge_field, Field) |
| 155 | + assert isinstance(edge_field.type, NonNull) |
| 156 | + assert isinstance(edge_field.type.of_type, List) |
| 157 | + assert isinstance(edge_field.type.of_type.of_type, NonNull) |
| 158 | + |
| 159 | + assert edge_field.type.of_type.of_type.of_type.__name__ == "MyObjectEdge" |
| 160 | + |
| 161 | + # This page info is NonNull |
| 162 | + assert isinstance(pageinfo_field, Field) |
| 163 | + assert isinstance(edge_field.type, NonNull) |
| 164 | + assert pageinfo_field.type.of_type == PageInfo |
| 165 | + |
| 166 | + |
54 | 167 | def test_connection_name():
|
55 | 168 | custom_name = "MyObjectCustomNameConnection"
|
56 | 169 |
|
|
0 commit comments