def test_picklable():
    d = DefaultOrderedDict(list, [('a', 1), ('b', 2)])
    d_copied = pickle.loads(pickle.dumps(d))

    assert d_copied == d
    assert d.default_factory == d_copied.default_factory

    d = DefaultOrderedDict(None, [('a', 1), ('b', 2)])
    d_copied = pickle.loads(pickle.dumps(d))

    assert d_copied == d
    assert d.default_factory == d_copied.default_factory
def get_field_asts_from_execution_context(exe_context):
    fields = collect_fields(exe_context, type,
                            exe_context.operation.selection_set,
                            DefaultOrderedDict(list), set())
    # field_asts = next(iter(fields.values()))
    field_asts = tuple(fields.values())[0]
    return field_asts
def test_will_act_list_default_dict_if_no_factory_defined():
    d = DefaultOrderedDict()

    with raises(KeyError) as excinfo:
        assert d['test']

    assert str(excinfo.value) == "'test'"
def test_preserves_input_order():
    d = DefaultOrderedDict(list)
    d['a'].append(1)
    d['b'].append(2)
    d['c'].append(3)
    d['a'].append(4)

    assert list(d.keys()) == ['a', 'b', 'c']
    assert list(d.values()) == [[1, 4], [2], [3]]
Ejemplo n.º 5
0
def test_preserves_input_order():
    d = DefaultOrderedDict(list)
    d["a"].append(1)
    d["b"].append(2)
    d["c"].append(3)
    d["a"].append(4)

    assert list(d.keys()) == ["a", "b", "c"]
    assert list(d.values()) == [[1, 4], [2], [3]]
def test_deep_copy():
    d = DefaultOrderedDict(list, [('a', [1, 2]), ('b', [3, 4])])
    d_copied = copy.deepcopy(d)

    assert d_copied == d
    assert d.default_factory == d_copied.default_factory
    assert d_copied['a'] == d['a']
    assert d_copied['a'] is not d['a']
    assert d_copied['b'] == d['b']
    assert d_copied['b'] is not d['b']
Ejemplo n.º 7
0
def test_deep_copy():
    d = DefaultOrderedDict(list, [("a", [1, 2]), ("b", [3, 4])])
    d_copied = copy.deepcopy(d)

    assert d_copied == d
    assert d.default_factory == d_copied.default_factory
    assert d_copied["a"] == d["a"]
    assert d_copied["a"] is not d["a"]
    assert d_copied["b"] == d["b"]
    assert d_copied["b"] is not d["b"]
Ejemplo n.º 8
0
def get_endpoint(the_schema,
                 document_ast,
                 root_value=None,
                 context_value=None,
                 variable_values=None,
                 operation_name=None,
                 executor=None,
                 middleware=None):
    context = ExecutionContext(the_schema, document_ast, root_value,
                               context_value, variable_values, operation_name,
                               executor, middleware)

    the_type = get_operation_root_type(context.schema, context.operation)
    fields = collect_fields(context.schema, the_type,
                            context.operation.selection_set,
                            DefaultOrderedDict(list), set())
    return [str(field) for field in fields]
def test_will_missing_will_set_value_from_factory():
    d = DefaultOrderedDict(list)
    f = d['foo']
    assert isinstance(f, list)
    assert d['foo'] is f
def test_requires_callable_default_factory():
    with raises(TypeError) as excinfo:
        DefaultOrderedDict('not callable')

    assert str(excinfo.value) == 'first argument must be callable'
def test_will_repr_properly():
    d = DefaultOrderedDict(list, [('a', 1), ('b', 2)])
    assert repr(d) == "DefaultOrderedDict({}, [('a', 1), ('b', 2)])".format(
        list)
def resolve_prefetch(
        exe_context,  # type: ExecutionContext
        operation,  # type: OperationDefinition
        root_type=None,  # type: Any
        type_=None,
        path=None,
        selection_set=None,  # type: Optional[SelectionSet]
        action_id=None):
    if path is None:
        path = []
    if type_ is None:
        type_ = get_operation_root_type(exe_context.schema, operation)
    if isinstance(type_, GraphQLList):
        type_ = type_.of_type
    if root_type is None:
        root_type = type_.graphene_type
    if selection_set is None:
        selection_set = operation.selection_set
    fields = collect_fields(exe_context, type_, selection_set,
                            DefaultOrderedDict(list), set())
    try:
        prefetch_fn = getattr(type_.graphene_type, "prefetch_fn")
    except AttributeError:
        prefetch_fn = None
    for response_name, field_asts in fields.items():
        # todo: 这里可能有bug
        field_ast = field_asts[0]
        field_name = field_ast.name.value
        try:
            field_def = get_field_def(exe_context.schema, type_, field_name)
        except:
            field_def = None
        path_name = SKIP
        if prefetch_fn:
            try:
                # 在 prefetch_fn 中进行类似于select_related prefetch_related等操作
                # 告诉orm应该加载关系
                # 返回的东西会被拼凑到path里面,如果返回SKIP,那么不会拼路径,如果不返回,那么字段名将拼凑到路径中
                new_action_id = action_id

                def set_action_id(x):
                    nonlocal new_action_id
                    new_action_id = x

                def add_prefetch_info(x):
                    exe_context.context_value["prefetch"][action_id].append(x)

                path_name = prefetch_fn(action_id=action_id,
                                        prefetch_name=field_name,
                                        prefetch_field_def=field_def,
                                        path=path,
                                        add_prefetch_info=add_prefetch_info,
                                        set_action_id=set_action_id)
            except:
                traceback.print_exc()
        if field_def and field_ast.selection_set:
            if path_name == SKIP:
                new_path = path
            else:
                new_path = path + [path_name]
            resolve_prefetch(exe_context, operation, root_type, field_def.type,
                             new_path, field_ast.selection_set, new_action_id)