Esempio n. 1
0
def test_basic_operation_query(mock_urlopen):
    'Test if query with type sgqlc.operation.Operation() works'

    configure_mock_urlopen(mock_urlopen, graphql_response_ok)

    schema = Schema()

    # MyType and Query may be declared if doctests were processed by nose
    if 'MyType' in schema:
        schema -= schema.MyType

    if 'Query' in schema:
        schema -= schema.Query

    class MyType(Type):
        __schema__ = schema
        i = int

    class Query(Type):
        __schema__ = schema
        my_type = MyType

    op = Operation(Query)
    op.my_type.i()

    endpoint = HTTPEndpoint(test_url)
    data = endpoint(op)
    eq_(data, json.loads(graphql_response_ok))
    check_mock_urlopen(mock_urlopen, query=bytes(op))
Esempio n. 2
0
def test_operation_query(mock_websocket):
    'Test if query with type sgqlc.operation.Operation() or raw bytes works'

    schema = Schema()

    # MyType and Query may be declared if doctests were processed by nose
    if 'MyType' in schema:
        schema -= schema.MyType

    if 'Query' in schema:
        schema -= schema.Query

    class MyType(Type):
        __schema__ = schema
        i = int

    class Query(Type):
        __schema__ = schema
        my_type = MyType

    op = Operation(Query)
    op.my_type.i()

    mock_connection = Mock()
    mock_websocket.create_connection.return_value = mock_connection
    return_values = [
        """
        {
            "type": "connection_ack",
            "id": "123"
        }
        """,
        """
        {
            "type": "data",
            "id": "123",
            "payload": {
                "data": {"test": ["1", "2"]}
            }
        }
        """,
        """
        {
            "type": "complete",
            "id": "123"
        }
        """,
    ]
    # query twice so double ret values twice
    return_values.extend(return_values)
    mock_connection.recv.side_effect = return_values
    eq_(list(endpoint(op)), [{'data': {'test': ['1', '2']}}])
    eq_(list(endpoint(bytes(op))), [{'data': {'test': ['1', '2']}}])
    eq_(
        bytes(json.loads(
            mock_connection.send.call_args_list[1][0][0])['payload']['query'],
              encoding='utf-8'), bytes(op))
Esempio n. 3
0
def test_input_parameter_query():
    schema = Schema()

    if 'Query' in schema:
        schema -= schema.Query

    if 'NestedThing' in schema:
        schema -= schema.NestedThing

    if 'InputThing' in schema:
        schema -= schema.InputThing

    class TestEnum(Enum):
        __choices__ = ['def', 'ghi']

    class NestedThing(Input):
        __schema__ = schema
        nested_value = Field(String)
        nested_enum = Field(TestEnum)

        @staticmethod
        def create(nested_value, nested_enum):
            return NestedThing(json_data={k: v for k, v in {
                'nested_value': nested_value.__to_internal_json_value__()
                if getattr(nested_value, '__to_internal_json_value__', None)
                else nested_value,
                'nested_enum': nested_enum.__to_internal_json_value__()
                if getattr(nested_enum, '__to_internal_json_value__', None)
                else nested_enum,
            }.items() if k and v})

    class InputThing(Input):
        __schema__ = schema
        nest = Field(NestedThing)

        @staticmethod
        def create(nest):
            return InputThing(json_data={k: v for k, v in {
                'nest': nest.__to_internal_json_value__()
                if getattr(nest, '__to_internal_json_value__', None) else nest,
            }.items() if k and v})

    class Query(Type):
        __schema__ = schema
        thing = Field(String,
                      args=ArgDict((('param_value', Arg(InputThing)),)))

    op = Operation(Query)
    op.thing(param_value=InputThing.create(nest=NestedThing.create(
        nested_value="abc",
        nested_enum="def")))
    assert 'nestedValue: "abc"' in str(op)
    assert 'nestedEnum: def' in str(op)