graphql-client/test/test_client_schema.rb

68 строки
1.8 KiB
Ruby
Исходник Обычный вид История

2016-11-29 15:37:10 +03:00
# frozen_string_literal: true
2016-09-14 03:35:11 +03:00
require "graphql"
require "graphql/client"
require "json"
require "minitest/autorun"
class TestClientSchema < MiniTest::Test
2019-05-14 20:30:35 +03:00
FakeConn = Class.new do
attr_reader :context
def headers(_)
{}
end
def execute(document:, operation_name: nil, variables: {}, context: {})
@context = context
end
end
2019-09-16 18:03:25 +03:00
class AwesomeQueryType < GraphQL::Schema::Object
field :version, Integer, null: false
2016-09-14 03:35:11 +03:00
end
2019-09-16 18:03:25 +03:00
class Schema < GraphQL::Schema
query(AwesomeQueryType)
end
2016-09-14 03:35:11 +03:00
def test_load_schema_identity
schema = GraphQL::Client.load_schema(Schema)
2019-09-16 18:03:25 +03:00
assert_equal "AwesomeQuery", schema.query.graphql_name
2016-09-14 03:35:11 +03:00
end
def test_load_schema_from_introspection_query_result
result = Schema.execute(GraphQL::Introspection::INTROSPECTION_QUERY)
schema = GraphQL::Client.load_schema(result)
assert_equal "AwesomeQuery", schema.query.name
end
def test_load_schema_from_json_string
json = JSON.generate(Schema.execute(GraphQL::Introspection::INTROSPECTION_QUERY))
schema = GraphQL::Client.load_schema(json)
assert_equal "AwesomeQuery", schema.query.name
end
2016-09-14 03:57:16 +03:00
def test_load_schema_ignores_missing_path
refute GraphQL::Client.load_schema("#{__dir__}/missing-schema.json")
end
2016-09-14 04:08:23 +03:00
def test_dump_schema
schema = GraphQL::Client.dump_schema(Schema)
assert_kind_of Hash, schema
assert_equal "AwesomeQuery", schema["data"]["__schema"]["queryType"]["name"]
end
def test_dump_schema_io
buffer = StringIO.new
GraphQL::Client.dump_schema(Schema, buffer)
buffer.rewind
assert_equal "{\n \"data\"", buffer.read(10)
end
2019-05-14 20:30:35 +03:00
def test_dump_schema_context
conn = FakeConn.new
GraphQL::Client.dump_schema(conn, StringIO.new, context: { user_id: 1})
assert_equal({ user_id: 1 }, conn.context)
end
2016-09-14 03:35:11 +03:00
end