<commit_before>package graphql_test

import (
	"errors"
	"fmt"
	"testing"

	"github.com/graphql-go/graphql"
	"github.com/graphql-go/graphql/testutil"
)

func TestSchemaSubscribe(t *testing.T) {

	testutil.RunSubscribes(t, []*testutil.TestSubscription{
		{
			Name: "subscribe without resolver",
			Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
				Name: "Subscription",
				Fields: graphql.Fields{
					"sub_without_resolver": &graphql.Field{
						Type:      graphql.String,
						Subscribe: makeSubscribeToStringFunction([]string{"a", "b", "c"}),
						Resolve: func(p graphql.ResolveParams) (interface{}, error) { // TODO remove dummy resolver
							return p.Source, nil
						},
					},
				},
			}),
			Query: `
				subscription onHelloSaid {
					sub_without_resolver
				}
			`,
			ExpectedResults: []testutil.TestResponse{
				{Data: `{ "sub_without_resolver": "a" }`},
				{Data: `{ "sub_without_resolver": "b" }`},
				{Data: `{ "sub_without_resolver": "c" }`},
			},
		},
		{
			Name: "receive query validation error",
			Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
				Name: "Subscription",
				Fields: graphql.Fields{
					"sub_without_resolver": &graphql.Field{
						Type:      graphql.String,
						Subscribe: makeSubscribeToStringFunction([]string{"a", "b", "c"}),
					},
				},
			}),
			Query: `
				subscription onHelloSaid {
					sub_without_resolver
					xxx
				}
			`,
			ExpectedResults: []testutil.TestResponse{
				{Errors: []string{"Cannot query field \"xxx\" on type \"Subscription\"."}},
			},
		},
		{
			Name: "subscribe with resolver changes output",
			Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
				Name: "Subscription",
				Fields: graphql.Fields{
					"sub_with_resolver": &graphql.Field{
						Type:      graphql.String,
						Subscribe: makeSubscribeToStringFunction([]string{"a", "b", "c", "d"}),
						Resolve: func(p graphql.ResolveParams) (interface{}, error) {
							return fmt.Sprintf("result=%v", p.Source), nil
						},
					},
				},
			}),
			Query: `
				subscription onHelloSaid {
					sub_with_resolver
				}
			`,
			ExpectedResults: []testutil.TestResponse{
				{Data: `{ "sub_with_resolver": "result=a" }`},
				{Data: `{ "sub_with_resolver": "result=b" }`},
				{Data: `{ "sub_with_resolver": "result=c" }`},
				{Data: `{ "sub_with_resolver": "result=d" }`},
			},
		},
		{
			Name: "subscribe to a nested object",
			Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
				Name: "Subscription",
				Fields: graphql.Fields{
					"sub_with_object": &graphql.Field{
						Type: graphql.NewObject(graphql.ObjectConfig{
							Name: "Obj",
							Fields: graphql.Fields{
								"field": &graphql.Field{
									Type: graphql.String,
								},
							},
						}),
						Resolve: func(p graphql.ResolveParams) (interface{}, error) { // TODO remove dummy resolver
							return p.Source, nil
						},
						Subscribe: makeSubscribeToMapFunction([]map[string]interface{}{<commit_msg>subscription_test: added more tests for resolver behaviour<commit_after>package graphql_test

import (
	"errors"
	"fmt"
	"testing"

	"github.com/graphql-go/graphql"
	"github.com/graphql-go/graphql/testutil"
)

func TestSchemaSubscribe(t *testing.T) {

	testutil.RunSubscribes(t, []*testutil.TestSubscription{
		{
			Name: "subscribe without resolver",
			Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
				Name: "Subscription",
				Fields: graphql.Fields{
					"sub_without_resolver": &graphql.Field{
						Type: graphql.String,
						Subscribe: makeSubscribeToMapFunction([]map[string]interface{}{
							{
								"sub_without_resolver": "a",
							},
							{
								"sub_without_resolver": "b",
							},
							{
								"sub_without_resolver": "c",
							},
						}),
					},
				},
			}),
			Query: `
				subscription onHelloSaid {
					sub_without_resolver
				}
			`,
			ExpectedResults: []testutil.TestResponse{
				{Data: `{ "sub_without_resolver": "a" }`},
				{Data: `{ "sub_without_resolver": "b" }`},
				{Data: `{ "sub_without_resolver": "c" }`},
			},
		},
		{
			Name: "subscribe with resolver",
			Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
				Name: "Subscription",
				Fields: graphql.Fields{
					"sub_with_resolver": &graphql.Field{
						Type: graphql.String,
						Resolve: func(p graphql.ResolveParams) (interface{}, error) {
							return p.Source, nil
						},
						Subscribe: makeSubscribeToStringFunction([]string{"a", "b", "c"}),
					},
				},
			}),
			Query: `
				subscription onHelloSaid {
					sub_with_resolver
				}
			`,
			ExpectedResults: []testutil.TestResponse{
				{Data: `{ "sub_with_resolver": "a" }`},
				{Data: `{ "sub_with_resolver": "b" }`},
				{Data: `{ "sub_with_resolver": "c" }`},
			},
		},
		{
			Name: "receive query validation error",
			Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
				Name: "Subscription",
				Fields: graphql.Fields{
					"sub_without_resolver": &graphql.Field{
						Type:      graphql.String,
						Subscribe: makeSubscribeToStringFunction([]string{"a", "b", "c"}),
					},
				},
			}),
			Query: `
				subscription onHelloSaid {
					sub_without_resolver
					xxx
				}
			`,
			ExpectedResults: []testutil.TestResponse{
				{Errors: []string{"Cannot query field \"xxx\" on type \"Subscription\"."}},
			},
		},
		{
			Name: "subscribe with resolver changes output",
			Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
				Name: "Subscription",
				Fields: graphql.Fields{
					"sub_with_resolver": &graphql.Field{
						Type:      graphql.String,
						Subscribe: makeSubscribeToStringFunction([]string{"a", "b", "c", "d"}),
						Resolve: func(p graphql.ResolveParams) (interface{}, error) {
							return fmt.Sprintf("result=%v", p.Source), nil
						},
					},
				},
			}),
			Query: `
				subscription onHelloSaid {
					sub_with_resolver
				}
			`,
			ExpectedResults: []testutil.TestResponse{
				{Data: `{ "sub_with_resolver": "result=a" }`},
				{Data: `{ "sub_with_resolver": "result=b" }`},
				{Data: `{ "sub_with_resolver": "result=c" }`},
				{Data: `{ "sub_with_resolver": "result=d" }`},
			},
		},
		{
			Name: "subscribe to a nested object",
			Schema: makeSubscriptionSchema(t, graphql.ObjectConfig{
				Name: "Subscription",
				Fields: graphql.Fields{
					"sub_with_object": &graphql.Field{
						Type: graphql.NewObject(graphql.ObjectConfig{
							Name: "Obj",
							Fields: graphql.Fields{
								"field": &graphql.Field{
									Type: graphql.String,
								},
							},
						}),
						Resolve: func(p graphql.ResolveParams) (interface{}, error) {
							return p.Source, nil
						},
						Subscribe: makeSubscribeToMapFunction([]map[string]interface{}{
{'fraction_non_alphanumeric': 0.15583445390706707, 'fraction_numerical': 0.0, 'mean_word_length': 3.1017156862745097, 'pattern_counts': {'":': 31, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6160593', 'n_tokens_mistral': 2957, 'n_tokens_neox': 2232, 'n_words': 399}