|
1 |
| - |
| 1 | +from collections import OrderedDict |
2 | 2 | from graphene import Field, Int, Interface, ObjectType
|
3 | 3 | from graphene.relay import Node, is_node
|
4 | 4 | import six
|
5 | 5 |
|
6 | 6 | from ..registry import Registry
|
7 |
| -from ..types import SQLAlchemyObjectType |
| 7 | +from ..types import SQLAlchemyObjectType, SQLAlchemyObjectTypeOptions |
8 | 8 | from .models import Article, Reporter
|
9 | 9 |
|
10 | 10 | registry = Registry()
|
@@ -116,3 +116,45 @@ def test_custom_objecttype_registered():
|
116 | 116 | 'pets',
|
117 | 117 | 'articles',
|
118 | 118 | 'favorite_article']
|
| 119 | + |
| 120 | + |
| 121 | +# Test Custom SQLAlchemyObjectType with Custom Options |
| 122 | +class CustomOptions(SQLAlchemyObjectTypeOptions): |
| 123 | + custom_option = None |
| 124 | + custom_fields = None |
| 125 | + |
| 126 | + |
| 127 | +class SQLAlchemyObjectTypeWithCustomOptions(SQLAlchemyObjectType): |
| 128 | + class Meta: |
| 129 | + abstract = True |
| 130 | + |
| 131 | + @classmethod |
| 132 | + def __init_subclass_with_meta__(cls, custom_option=None, custom_fields=None, **options): |
| 133 | + _meta = CustomOptions(cls) |
| 134 | + _meta.custom_option = custom_option |
| 135 | + _meta.fields = custom_fields |
| 136 | + super(SQLAlchemyObjectTypeWithCustomOptions, cls).__init_subclass_with_meta__(_meta=_meta, **options) |
| 137 | + |
| 138 | + |
| 139 | +class ReporterWithCustomOptions(SQLAlchemyObjectTypeWithCustomOptions): |
| 140 | + class Meta: |
| 141 | + model = Reporter |
| 142 | + custom_option = 'custom_option' |
| 143 | + custom_fields = OrderedDict([('custom_field', Field(Int()))]) |
| 144 | + |
| 145 | + |
| 146 | +def test_objecttype_with_custom_options(): |
| 147 | + assert issubclass(ReporterWithCustomOptions, ObjectType) |
| 148 | + assert ReporterWithCustomOptions._meta.model == Reporter |
| 149 | + assert list( |
| 150 | + ReporterWithCustomOptions._meta.fields.keys()) == [ |
| 151 | + 'custom_field', |
| 152 | + 'id', |
| 153 | + 'first_name', |
| 154 | + 'last_name', |
| 155 | + 'email', |
| 156 | + 'pets', |
| 157 | + 'articles', |
| 158 | + 'favorite_article'] |
| 159 | + assert ReporterWithCustomOptions._meta.custom_option == 'custom_option' |
| 160 | + assert isinstance(ReporterWithCustomOptions._meta.fields['custom_field'].type, Int) |
0 commit comments