Best Syzkaller code snippet using ast.TestParse
variables_test.go
Source:variables_test.go
...172 "fieldWithObjectInput": `{"a":"foo","b":["bar"],"c":"baz"}`,173 },174 }175 // parse query176 ast := testutil.TestParse(t, doc)177 // execute178 ep := graphql.ExecuteParams{179 Schema: variablesTestSchema,180 AST: ast,181 }182 result := testutil.TestExecute(t, ep)183 if len(result.Errors) > 0 {184 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)185 }186 if !reflect.DeepEqual(expected, result) {187 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))188 }189}190func TestVariables_ObjectsAndNullability_UsingInlineStructs_ProperlyParsesSingleValueToList(t *testing.T) {191 doc := `192 {193 fieldWithObjectInput(input: {a: "foo", b: "bar", c: "baz"})194 }195 `196 expected := &graphql.Result{197 Data: map[string]interface{}{198 "fieldWithObjectInput": `{"a":"foo","b":["bar"],"c":"baz"}`,199 },200 }201 // parse query202 ast := testutil.TestParse(t, doc)203 // execute204 ep := graphql.ExecuteParams{205 Schema: variablesTestSchema,206 AST: ast,207 }208 result := testutil.TestExecute(t, ep)209 if len(result.Errors) > 0 {210 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)211 }212 if !reflect.DeepEqual(expected, result) {213 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))214 }215}216func TestVariables_ObjectsAndNullability_UsingInlineStructs_DoesNotUseIncorrectValue(t *testing.T) {217 doc := `218 {219 fieldWithObjectInput(input: ["foo", "bar", "baz"])220 }221 `222 expected := &graphql.Result{223 Data: map[string]interface{}{224 "fieldWithObjectInput": nil,225 },226 }227 // parse query228 ast := testutil.TestParse(t, doc)229 // execute230 ep := graphql.ExecuteParams{231 Schema: variablesTestSchema,232 AST: ast,233 }234 result := testutil.TestExecute(t, ep)235 if len(result.Errors) > 0 {236 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)237 }238 if !reflect.DeepEqual(expected, result) {239 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))240 }241}242func TestVariables_ObjectsAndNullability_UsingInlineStructs_ProperlyRunsParseLiteralOnComplexScalarTypes(t *testing.T) {243 doc := `244 {245 fieldWithObjectInput(input: {a: "foo", d: "SerializedValue"})246 }247 `248 expected := &graphql.Result{249 Data: map[string]interface{}{250 "fieldWithObjectInput": `{"a":"foo","d":"DeserializedValue"}`,251 },252 }253 // parse query254 ast := testutil.TestParse(t, doc)255 // execute256 ep := graphql.ExecuteParams{257 Schema: variablesTestSchema,258 AST: ast,259 }260 result := testutil.TestExecute(t, ep)261 if len(result.Errors) > 0 {262 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)263 }264 if !reflect.DeepEqual(expected, result) {265 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))266 }267}268func testVariables_ObjectsAndNullability_UsingVariables_GetAST(t *testing.T) *ast.Document {269 doc := `270 query q($input: TestInputObject) {271 fieldWithObjectInput(input: $input)272 }273 `274 return testutil.TestParse(t, doc)275}276func TestVariables_ObjectsAndNullability_UsingVariables_ExecutesWithComplexInput(t *testing.T) {277 params := map[string]interface{}{278 "input": map[string]interface{}{279 "a": "foo",280 "b": []interface{}{"bar"},281 "c": "baz",282 },283 }284 expected := &graphql.Result{285 Data: map[string]interface{}{286 "fieldWithObjectInput": `{"a":"foo","b":["bar"],"c":"baz"}`,287 },288 }289 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t)290 // execute291 ep := graphql.ExecuteParams{292 Schema: variablesTestSchema,293 AST: ast,294 Args: params,295 }296 result := testutil.TestExecute(t, ep)297 if len(result.Errors) > 0 {298 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)299 }300 if !reflect.DeepEqual(expected, result) {301 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))302 }303}304func TestVariables_ObjectsAndNullability_UsingVariables_UsesDefaultValueWhenNotProvided(t *testing.T) {305 doc := `306 query q($input: TestInputObject = {a: "foo", b: ["bar"], c: "baz"}) {307 fieldWithObjectInput(input: $input)308 }309 `310 expected := &graphql.Result{311 Data: map[string]interface{}{312 "fieldWithObjectInput": `{"a":"foo","b":["bar"],"c":"baz"}`,313 },314 }315 withDefaultsAST := testutil.TestParse(t, doc)316 // execute317 ep := graphql.ExecuteParams{318 Schema: variablesTestSchema,319 AST: withDefaultsAST,320 }321 result := testutil.TestExecute(t, ep)322 if len(result.Errors) > 0 {323 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)324 }325 if !reflect.DeepEqual(expected, result) {326 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))327 }328}329func TestVariables_ObjectsAndNullability_UsingVariables_ProperlyParsesSingleValueToList(t *testing.T) {330 params := map[string]interface{}{331 "input": map[string]interface{}{332 "a": "foo",333 "b": "bar",334 "c": "baz",335 },336 }337 expected := &graphql.Result{338 Data: map[string]interface{}{339 "fieldWithObjectInput": `{"a":"foo","b":["bar"],"c":"baz"}`,340 },341 }342 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t)343 // execute344 ep := graphql.ExecuteParams{345 Schema: variablesTestSchema,346 AST: ast,347 Args: params,348 }349 result := testutil.TestExecute(t, ep)350 if len(result.Errors) > 0 {351 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)352 }353 if !reflect.DeepEqual(expected, result) {354 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))355 }356}357func TestVariables_ObjectsAndNullability_UsingVariables_ExecutesWithComplexScalarInput(t *testing.T) {358 params := map[string]interface{}{359 "input": map[string]interface{}{360 "c": "foo",361 "d": "SerializedValue",362 },363 }364 expected := &graphql.Result{365 Data: map[string]interface{}{366 "fieldWithObjectInput": `{"c":"foo","d":"DeserializedValue"}`,367 },368 }369 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t)370 // execute371 ep := graphql.ExecuteParams{372 Schema: variablesTestSchema,373 AST: ast,374 Args: params,375 }376 result := testutil.TestExecute(t, ep)377 if len(result.Errors) > 0 {378 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)379 }380 if !reflect.DeepEqual(expected, result) {381 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))382 }383}384func TestVariables_ObjectsAndNullability_UsingVariables_ErrorsOnNullForNestedNonNull(t *testing.T) {385 params := map[string]interface{}{386 "input": map[string]interface{}{387 "a": "foo",388 "b": "bar",389 "c": nil,390 },391 }392 expected := &graphql.Result{393 Data: nil,394 Errors: []gqlerrors.FormattedError{395 {396 Message: `Variable "$input" got invalid value {"a":"foo","b":"bar","c":null}.` +397 "\nIn field \"c\": Expected \"String!\", found null.",398 Locations: []location.SourceLocation{399 {400 Line: 2, Column: 17,401 },402 },403 },404 },405 }406 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t)407 // execute408 ep := graphql.ExecuteParams{409 Schema: variablesTestSchema,410 AST: ast,411 Args: params,412 }413 result := testutil.TestExecute(t, ep)414 if !testutil.EqualResults(expected, result) {415 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))416 }417}418func TestVariables_ObjectsAndNullability_UsingVariables_ErrorsOnIncorrectType(t *testing.T) {419 params := map[string]interface{}{420 "input": "foo bar",421 }422 expected := &graphql.Result{423 Data: nil,424 Errors: []gqlerrors.FormattedError{425 {426 Message: "Variable \"$input\" got invalid value \"foo bar\".\nExpected \"TestInputObject\", found not an object.",427 Locations: []location.SourceLocation{428 {429 Line: 2, Column: 17,430 },431 },432 },433 },434 }435 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t)436 // execute437 ep := graphql.ExecuteParams{438 Schema: variablesTestSchema,439 AST: ast,440 Args: params,441 }442 result := testutil.TestExecute(t, ep)443 if !testutil.EqualResults(expected, result) {444 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))445 }446}447func TestVariables_ObjectsAndNullability_UsingVariables_ErrorsOnOmissionOfNestedNonNull(t *testing.T) {448 params := map[string]interface{}{449 "input": map[string]interface{}{450 "a": "foo",451 "b": "bar",452 },453 }454 expected := &graphql.Result{455 Data: nil,456 Errors: []gqlerrors.FormattedError{457 {458 Message: `Variable "$input" got invalid value {"a":"foo","b":"bar"}.` +459 "\nIn field \"c\": Expected \"String!\", found null.",460 Locations: []location.SourceLocation{461 {462 Line: 2, Column: 17,463 },464 },465 },466 },467 }468 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t)469 // execute470 ep := graphql.ExecuteParams{471 Schema: variablesTestSchema,472 AST: ast,473 Args: params,474 }475 result := testutil.TestExecute(t, ep)476 if !testutil.EqualResults(expected, result) {477 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))478 }479}480func TestVariables_ObjectsAndNullability_UsingVariables_ErrorsOnDeepNestedErrorsAndWithManyErrors(t *testing.T) {481 params := map[string]interface{}{482 "input": map[string]interface{}{483 "na": map[string]interface{}{484 "a": "foo",485 },486 },487 }488 expected := &graphql.Result{489 Data: nil,490 Errors: []gqlerrors.FormattedError{491 {492 Message: `Variable "$input" got invalid value {"na":{"a":"foo"}}.` +493 "\nIn field \"na\": In field \"c\": Expected \"String!\", found null." +494 "\nIn field \"nb\": Expected \"String!\", found null.",495 Locations: []location.SourceLocation{496 {497 Line: 2, Column: 19,498 },499 },500 },501 },502 }503 doc := `504 query q($input: TestNestedInputObject) {505 fieldWithNestedObjectInput(input: $input)506 }507 `508 nestedAST := testutil.TestParse(t, doc)509 // execute510 ep := graphql.ExecuteParams{511 Schema: variablesTestSchema,512 AST: nestedAST,513 Args: params,514 }515 result := testutil.TestExecute(t, ep)516 if !testutil.EqualResults(expected, result) {517 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))518 }519}520func TestVariables_ObjectsAndNullability_UsingVariables_ErrorsOnAdditionOfUnknownInputField(t *testing.T) {521 params := map[string]interface{}{522 "input": map[string]interface{}{523 "a": "foo",524 "b": "bar",525 "c": "baz",526 "extra": "dog",527 },528 }529 expected := &graphql.Result{530 Data: nil,531 Errors: []gqlerrors.FormattedError{532 {533 Message: `Variable "$input" got invalid value {"a":"foo","b":"bar","c":"baz","extra":"dog"}.` +534 "\nIn field \"extra\": Unknown field.",535 Locations: []location.SourceLocation{536 {537 Line: 2, Column: 17,538 },539 },540 },541 },542 }543 ast := testVariables_ObjectsAndNullability_UsingVariables_GetAST(t)544 // execute545 ep := graphql.ExecuteParams{546 Schema: variablesTestSchema,547 AST: ast,548 Args: params,549 }550 result := testutil.TestExecute(t, ep)551 if !testutil.EqualResults(expected, result) {552 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))553 }554}555func TestVariables_NullableScalars_AllowsNullableInputsToBeOmitted(t *testing.T) {556 doc := `557 {558 fieldWithNullableStringInput559 }560 `561 expected := &graphql.Result{562 Data: map[string]interface{}{563 "fieldWithNullableStringInput": nil,564 },565 }566 ast := testutil.TestParse(t, doc)567 // execute568 ep := graphql.ExecuteParams{569 Schema: variablesTestSchema,570 AST: ast,571 }572 result := testutil.TestExecute(t, ep)573 if len(result.Errors) > 0 {574 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)575 }576 if !reflect.DeepEqual(expected, result) {577 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))578 }579}580func TestVariables_NullableScalars_AllowsNullableInputsToBeOmittedInAVariable(t *testing.T) {581 doc := `582 query SetsNullable($value: String) {583 fieldWithNullableStringInput(input: $value)584 }585 `586 expected := &graphql.Result{587 Data: map[string]interface{}{588 "fieldWithNullableStringInput": nil,589 },590 }591 ast := testutil.TestParse(t, doc)592 // execute593 ep := graphql.ExecuteParams{594 Schema: variablesTestSchema,595 AST: ast,596 }597 result := testutil.TestExecute(t, ep)598 if len(result.Errors) > 0 {599 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)600 }601 if !reflect.DeepEqual(expected, result) {602 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))603 }604}605func TestVariables_NullableScalars_AllowsNullableInputsToBeOmittedInAnUnlistedVariable(t *testing.T) {606 doc := `607 query SetsNullable {608 fieldWithNullableStringInput(input: $value)609 }610 `611 expected := &graphql.Result{612 Data: map[string]interface{}{613 "fieldWithNullableStringInput": nil,614 },615 }616 ast := testutil.TestParse(t, doc)617 // execute618 ep := graphql.ExecuteParams{619 Schema: variablesTestSchema,620 AST: ast,621 }622 result := testutil.TestExecute(t, ep)623 if len(result.Errors) > 0 {624 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)625 }626 if !reflect.DeepEqual(expected, result) {627 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))628 }629}630func TestVariables_NullableScalars_AllowsNullableInputsToBeSetToNullInAVariable(t *testing.T) {631 doc := `632 query SetsNullable($value: String) {633 fieldWithNullableStringInput(input: $value)634 }635 `636 params := map[string]interface{}{637 "value": nil,638 }639 expected := &graphql.Result{640 Data: map[string]interface{}{641 "fieldWithNullableStringInput": nil,642 },643 }644 ast := testutil.TestParse(t, doc)645 // execute646 ep := graphql.ExecuteParams{647 Schema: variablesTestSchema,648 AST: ast,649 Args: params,650 }651 result := testutil.TestExecute(t, ep)652 if len(result.Errors) > 0 {653 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)654 }655 if !reflect.DeepEqual(expected, result) {656 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))657 }658}659func TestVariables_NullableScalars_AllowsNullableInputsToBeSetToAValueInAVariable(t *testing.T) {660 doc := `661 query SetsNullable($value: String) {662 fieldWithNullableStringInput(input: $value)663 }664 `665 params := map[string]interface{}{666 "value": "a",667 }668 expected := &graphql.Result{669 Data: map[string]interface{}{670 "fieldWithNullableStringInput": `"a"`,671 },672 }673 ast := testutil.TestParse(t, doc)674 // execute675 ep := graphql.ExecuteParams{676 Schema: variablesTestSchema,677 AST: ast,678 Args: params,679 }680 result := testutil.TestExecute(t, ep)681 if len(result.Errors) > 0 {682 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)683 }684 if !reflect.DeepEqual(expected, result) {685 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))686 }687}688func TestVariables_NullableScalars_AllowsNullableInputsToBeSetToAValueDirectly(t *testing.T) {689 doc := `690 {691 fieldWithNullableStringInput(input: "a")692 }693 `694 expected := &graphql.Result{695 Data: map[string]interface{}{696 "fieldWithNullableStringInput": `"a"`,697 },698 }699 ast := testutil.TestParse(t, doc)700 // execute701 ep := graphql.ExecuteParams{702 Schema: variablesTestSchema,703 AST: ast,704 }705 result := testutil.TestExecute(t, ep)706 if len(result.Errors) > 0 {707 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)708 }709 if !reflect.DeepEqual(expected, result) {710 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))711 }712}713func TestVariables_NonNullableScalars_DoesNotAllowNonNullableInputsToBeOmittedInAVariable(t *testing.T) {714 doc := `715 query SetsNonNullable($value: String!) {716 fieldWithNonNullableStringInput(input: $value)717 }718 `719 expected := &graphql.Result{720 Data: nil,721 Errors: []gqlerrors.FormattedError{722 {723 Message: `Variable "$value" of required type "String!" was not provided.`,724 Locations: []location.SourceLocation{725 {726 Line: 2, Column: 31,727 },728 },729 },730 },731 }732 ast := testutil.TestParse(t, doc)733 // execute734 ep := graphql.ExecuteParams{735 Schema: variablesTestSchema,736 AST: ast,737 }738 result := testutil.TestExecute(t, ep)739 if !testutil.EqualResults(expected, result) {740 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))741 }742}743func TestVariables_NonNullableScalars_DoesNotAllowNonNullableInputsToBeSetToNullInAVariable(t *testing.T) {744 doc := `745 query SetsNonNullable($value: String!) {746 fieldWithNonNullableStringInput(input: $value)747 }748 `749 params := map[string]interface{}{750 "value": nil,751 }752 expected := &graphql.Result{753 Data: nil,754 Errors: []gqlerrors.FormattedError{755 {756 Message: `Variable "$value" of required type "String!" was not provided.`,757 Locations: []location.SourceLocation{758 {759 Line: 2, Column: 31,760 },761 },762 },763 },764 }765 ast := testutil.TestParse(t, doc)766 // execute767 ep := graphql.ExecuteParams{768 Schema: variablesTestSchema,769 AST: ast,770 Args: params,771 }772 result := testutil.TestExecute(t, ep)773 if !testutil.EqualResults(expected, result) {774 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))775 }776}777func TestVariables_NonNullableScalars_AllowsNonNullableInputsToBeSetToAValueInAVariable(t *testing.T) {778 doc := `779 query SetsNonNullable($value: String!) {780 fieldWithNonNullableStringInput(input: $value)781 }782 `783 params := map[string]interface{}{784 "value": "a",785 }786 expected := &graphql.Result{787 Data: map[string]interface{}{788 "fieldWithNonNullableStringInput": `"a"`,789 },790 }791 ast := testutil.TestParse(t, doc)792 // execute793 ep := graphql.ExecuteParams{794 Schema: variablesTestSchema,795 AST: ast,796 Args: params,797 }798 result := testutil.TestExecute(t, ep)799 if len(result.Errors) > 0 {800 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)801 }802 if !reflect.DeepEqual(expected, result) {803 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))804 }805}806func TestVariables_NonNullableScalars_AllowsNonNullableInputsToBeSetToAValueDirectly(t *testing.T) {807 doc := `808 {809 fieldWithNonNullableStringInput(input: "a")810 }811 `812 params := map[string]interface{}{813 "value": "a",814 }815 expected := &graphql.Result{816 Data: map[string]interface{}{817 "fieldWithNonNullableStringInput": `"a"`,818 },819 }820 ast := testutil.TestParse(t, doc)821 // execute822 ep := graphql.ExecuteParams{823 Schema: variablesTestSchema,824 AST: ast,825 Args: params,826 }827 result := testutil.TestExecute(t, ep)828 if len(result.Errors) > 0 {829 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)830 }831 if !reflect.DeepEqual(expected, result) {832 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))833 }834}835func TestVariables_NonNullableScalars_PassesAlongNullForNonNullableInputsIfExplicitlySetInTheQuery(t *testing.T) {836 doc := `837 {838 fieldWithNonNullableStringInput839 }840 `841 params := map[string]interface{}{842 "value": "a",843 }844 expected := &graphql.Result{845 Data: map[string]interface{}{846 "fieldWithNonNullableStringInput": nil,847 },848 }849 ast := testutil.TestParse(t, doc)850 // execute851 ep := graphql.ExecuteParams{852 Schema: variablesTestSchema,853 AST: ast,854 Args: params,855 }856 result := testutil.TestExecute(t, ep)857 if len(result.Errors) > 0 {858 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)859 }860 if !reflect.DeepEqual(expected, result) {861 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))862 }863}864func TestVariables_ListsAndNullability_AllowsListsToBeNull(t *testing.T) {865 doc := `866 query q($input: [String]) {867 list(input: $input)868 }869 `870 params := map[string]interface{}{871 "input": nil,872 }873 expected := &graphql.Result{874 Data: map[string]interface{}{875 "list": nil,876 },877 }878 ast := testutil.TestParse(t, doc)879 // execute880 ep := graphql.ExecuteParams{881 Schema: variablesTestSchema,882 AST: ast,883 Args: params,884 }885 result := testutil.TestExecute(t, ep)886 if len(result.Errors) > 0 {887 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)888 }889 if !reflect.DeepEqual(expected, result) {890 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))891 }892}893func TestVariables_ListsAndNullability_AllowsListsToContainValues(t *testing.T) {894 doc := `895 query q($input: [String]) {896 list(input: $input)897 }898 `899 params := map[string]interface{}{900 "input": []interface{}{"A"},901 }902 expected := &graphql.Result{903 Data: map[string]interface{}{904 "list": `["A"]`,905 },906 }907 ast := testutil.TestParse(t, doc)908 // execute909 ep := graphql.ExecuteParams{910 Schema: variablesTestSchema,911 AST: ast,912 Args: params,913 }914 result := testutil.TestExecute(t, ep)915 if len(result.Errors) > 0 {916 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)917 }918 if !reflect.DeepEqual(expected, result) {919 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))920 }921}922func TestVariables_ListsAndNullability_AllowsListsToContainNull(t *testing.T) {923 doc := `924 query q($input: [String]) {925 list(input: $input)926 }927 `928 params := map[string]interface{}{929 "input": []interface{}{"A", nil, "B"},930 }931 expected := &graphql.Result{932 Data: map[string]interface{}{933 "list": `["A",null,"B"]`,934 },935 }936 ast := testutil.TestParse(t, doc)937 // execute938 ep := graphql.ExecuteParams{939 Schema: variablesTestSchema,940 AST: ast,941 Args: params,942 }943 result := testutil.TestExecute(t, ep)944 if len(result.Errors) > 0 {945 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)946 }947 if !reflect.DeepEqual(expected, result) {948 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))949 }950}951func TestVariables_ListsAndNullability_DoesNotAllowNonNullListsToBeNull(t *testing.T) {952 doc := `953 query q($input: [String]!) {954 nnList(input: $input)955 }956 `957 expected := &graphql.Result{958 Data: nil,959 Errors: []gqlerrors.FormattedError{960 {961 Message: `Variable "$input" of required type "[String]!" was not provided.`,962 Locations: []location.SourceLocation{963 {964 Line: 2, Column: 17,965 },966 },967 },968 },969 }970 ast := testutil.TestParse(t, doc)971 // execute972 ep := graphql.ExecuteParams{973 Schema: variablesTestSchema,974 AST: ast,975 }976 result := testutil.TestExecute(t, ep)977 if !testutil.EqualResults(expected, result) {978 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))979 }980}981func TestVariables_ListsAndNullability_AllowsNonNullListsToContainValues(t *testing.T) {982 doc := `983 query q($input: [String]!) {984 nnList(input: $input)985 }986 `987 params := map[string]interface{}{988 "input": []interface{}{"A"},989 }990 expected := &graphql.Result{991 Data: map[string]interface{}{992 "nnList": `["A"]`,993 },994 }995 ast := testutil.TestParse(t, doc)996 // execute997 ep := graphql.ExecuteParams{998 Schema: variablesTestSchema,999 AST: ast,1000 Args: params,1001 }1002 result := testutil.TestExecute(t, ep)1003 if len(result.Errors) > 0 {1004 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)1005 }1006 if !reflect.DeepEqual(expected, result) {1007 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))1008 }1009}1010func TestVariables_ListsAndNullability_AllowsNonNullListsToContainNull(t *testing.T) {1011 doc := `1012 query q($input: [String]!) {1013 nnList(input: $input)1014 }1015 `1016 params := map[string]interface{}{1017 "input": []interface{}{"A", nil, "B"},1018 }1019 expected := &graphql.Result{1020 Data: map[string]interface{}{1021 "nnList": `["A",null,"B"]`,1022 },1023 }1024 ast := testutil.TestParse(t, doc)1025 // execute1026 ep := graphql.ExecuteParams{1027 Schema: variablesTestSchema,1028 AST: ast,1029 Args: params,1030 }1031 result := testutil.TestExecute(t, ep)1032 if len(result.Errors) > 0 {1033 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)1034 }1035 if !reflect.DeepEqual(expected, result) {1036 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))1037 }1038}1039func TestVariables_ListsAndNullability_AllowsListsOfNonNullsToBeNull(t *testing.T) {1040 doc := `1041 query q($input: [String!]) {1042 listNN(input: $input)1043 }1044 `1045 params := map[string]interface{}{1046 "input": nil,1047 }1048 expected := &graphql.Result{1049 Data: map[string]interface{}{1050 "listNN": nil,1051 },1052 }1053 ast := testutil.TestParse(t, doc)1054 // execute1055 ep := graphql.ExecuteParams{1056 Schema: variablesTestSchema,1057 AST: ast,1058 Args: params,1059 }1060 result := testutil.TestExecute(t, ep)1061 if len(result.Errors) > 0 {1062 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)1063 }1064 if !reflect.DeepEqual(expected, result) {1065 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))1066 }1067}1068func TestVariables_ListsAndNullability_AllowsListsOfNonNullsToContainValues(t *testing.T) {1069 doc := `1070 query q($input: [String!]) {1071 listNN(input: $input)1072 }1073 `1074 params := map[string]interface{}{1075 "input": []interface{}{"A"},1076 }1077 expected := &graphql.Result{1078 Data: map[string]interface{}{1079 "listNN": `["A"]`,1080 },1081 }1082 ast := testutil.TestParse(t, doc)1083 // execute1084 ep := graphql.ExecuteParams{1085 Schema: variablesTestSchema,1086 AST: ast,1087 Args: params,1088 }1089 result := testutil.TestExecute(t, ep)1090 if len(result.Errors) > 0 {1091 t.Fatalf("wrong result, unexpected errors: %v", result.Errors)1092 }1093 if !reflect.DeepEqual(expected, result) {1094 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))1095 }1096}1097func TestVariables_ListsAndNullability_DoesNotAllowListOfNonNullsToContainNull(t *testing.T) {1098 doc := `1099 query q($input: [String!]) {1100 listNN(input: $input)1101 }1102 `1103 params := map[string]interface{}{1104 "input": []interface{}{"A", nil, "B"},1105 }1106 expected := &graphql.Result{1107 Data: nil,1108 Errors: []gqlerrors.FormattedError{1109 {1110 Message: `Variable "$input" got invalid value ` +1111 `["A",null,"B"].` +1112 "\nIn element #1: Expected \"String!\", found null.",1113 Locations: []location.SourceLocation{1114 {1115 Line: 2, Column: 17,1116 },1117 },1118 },1119 },1120 }1121 ast := testutil.TestParse(t, doc)1122 // execute1123 ep := graphql.ExecuteParams{1124 Schema: variablesTestSchema,1125 AST: ast,1126 Args: params,1127 }1128 result := testutil.TestExecute(t, ep)1129 if !testutil.EqualResults(expected, result) {1130 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))1131 }1132}1133func TestVariables_ListsAndNullability_DoesNotAllowNonNullListOfNonNullsToBeNull(t *testing.T) {1134 doc := `1135 query q($input: [String!]!) {1136 nnListNN(input: $input)1137 }1138 `1139 params := map[string]interface{}{1140 "input": nil,1141 }1142 expected := &graphql.Result{1143 Data: nil,1144 Errors: []gqlerrors.FormattedError{1145 {1146 Message: `Variable "$input" of required type "[String!]!" was not provided.`,1147 Locations: []location.SourceLocation{1148 {1149 Line: 2, Column: 17,1150 },1151 },1152 },1153 },1154 }1155 ast := testutil.TestParse(t, doc)1156 // execute1157 ep := graphql.ExecuteParams{1158 Schema: variablesTestSchema,1159 AST: ast,1160 Args: params,1161 }1162 result := testutil.TestExecute(t, ep)1163 if !testutil.EqualResults(expected, result) {1164 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))1165 }1166}1167func TestVariables_ListsAndNullability_AllowsNonNullListsOfNonNulsToContainValues(t *testing.T) {1168 doc := `1169 query q($input: [String!]!) {1170 nnListNN(input: $input)1171 }1172 `1173 params := map[string]interface{}{1174 "input": []interface{}{"A"},1175 }1176 expected := &graphql.Result{1177 Data: map[string]interface{}{1178 "nnListNN": `["A"]`,1179 },1180 }1181 ast := testutil.TestParse(t, doc)1182 // execute1183 ep := graphql.ExecuteParams{1184 Schema: variablesTestSchema,1185 AST: ast,1186 Args: params,1187 }1188 result := testutil.TestExecute(t, ep)1189 if len(result.Errors) != len(expected.Errors) {1190 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))1191 }1192 if !reflect.DeepEqual(expected, result) {1193 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))1194 }1195}1196func TestVariables_ListsAndNullability_DoesNotAllowNonNullListOfNonNullsToContainNull(t *testing.T) {1197 doc := `1198 query q($input: [String!]!) {1199 nnListNN(input: $input)1200 }1201 `1202 params := map[string]interface{}{1203 "input": []interface{}{"A", nil, "B"},1204 }1205 expected := &graphql.Result{1206 Data: nil,1207 Errors: []gqlerrors.FormattedError{1208 {1209 Message: `Variable "$input" got invalid value ` +1210 `["A",null,"B"].` +1211 "\nIn element #1: Expected \"String!\", found null.",1212 Locations: []location.SourceLocation{1213 {1214 Line: 2, Column: 17,1215 },1216 },1217 },1218 },1219 }1220 ast := testutil.TestParse(t, doc)1221 // execute1222 ep := graphql.ExecuteParams{1223 Schema: variablesTestSchema,1224 AST: ast,1225 Args: params,1226 }1227 result := testutil.TestExecute(t, ep)1228 if !testutil.EqualResults(expected, result) {1229 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))1230 }1231}1232func TestVariables_ListsAndNullability_DoesNotAllowInvalidTypesToBeUsedAsValues(t *testing.T) {1233 doc := `1234 query q($input: TestType!) {1235 fieldWithObjectInput(input: $input)1236 }1237 `1238 params := map[string]interface{}{1239 "input": map[string]interface{}{1240 "list": []interface{}{"A", "B"},1241 },1242 }1243 expected := &graphql.Result{1244 Data: nil,1245 Errors: []gqlerrors.FormattedError{1246 {1247 Message: `Variable "$input" expected value of type "TestType!" which cannot be used as an input type.`,1248 Locations: []location.SourceLocation{1249 {1250 Line: 2, Column: 17,1251 },1252 },1253 },1254 },1255 }1256 ast := testutil.TestParse(t, doc)1257 // execute1258 ep := graphql.ExecuteParams{1259 Schema: variablesTestSchema,1260 AST: ast,1261 Args: params,1262 }1263 result := testutil.TestExecute(t, ep)1264 if len(result.Errors) != len(expected.Errors) {1265 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))1266 }1267 if !testutil.EqualResults(expected, result) {1268 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))1269 }1270}1271func TestVariables_ListsAndNullability_DoesNotAllowUnknownTypesToBeUsedAsValues(t *testing.T) {1272 doc := `1273 query q($input: UnknownType!) {1274 fieldWithObjectInput(input: $input)1275 }1276 `1277 params := map[string]interface{}{1278 "input": "whoknows",1279 }1280 expected := &graphql.Result{1281 Data: nil,1282 Errors: []gqlerrors.FormattedError{1283 {1284 Message: `Variable "$input" expected value of type "UnknownType!" which cannot be used as an input type.`,1285 Locations: []location.SourceLocation{1286 {1287 Line: 2, Column: 17,1288 },1289 },1290 },1291 },1292 }1293 ast := testutil.TestParse(t, doc)1294 // execute1295 ep := graphql.ExecuteParams{1296 Schema: variablesTestSchema,1297 AST: ast,1298 Args: params,1299 }1300 result := testutil.TestExecute(t, ep)1301 if !testutil.EqualResults(expected, result) {1302 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))1303 }1304}1305func TestVariables_UsesArgumentDefaultValues_WhenNoArgumentProvided(t *testing.T) {1306 doc := `1307 {1308 fieldWithDefaultArgumentValue1309 }1310 `1311 expected := &graphql.Result{1312 Data: map[string]interface{}{1313 "fieldWithDefaultArgumentValue": `"Hello World"`,1314 },1315 }1316 ast := testutil.TestParse(t, doc)1317 // execute1318 ep := graphql.ExecuteParams{1319 Schema: variablesTestSchema,1320 AST: ast,1321 }1322 result := testutil.TestExecute(t, ep)1323 if len(result.Errors) != len(expected.Errors) {1324 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))1325 }1326 if !reflect.DeepEqual(expected, result) {1327 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))1328 }1329}1330func TestVariables_UsesArgumentDefaultValues_WhenNullableVariableProvided(t *testing.T) {1331 doc := `1332 query optionalVariable($optional: String) {1333 fieldWithDefaultArgumentValue(input: $optional)1334 }1335 `1336 expected := &graphql.Result{1337 Data: map[string]interface{}{1338 "fieldWithDefaultArgumentValue": `"Hello World"`,1339 },1340 }1341 ast := testutil.TestParse(t, doc)1342 // execute1343 ep := graphql.ExecuteParams{1344 Schema: variablesTestSchema,1345 AST: ast,1346 }1347 result := testutil.TestExecute(t, ep)1348 if len(result.Errors) != len(expected.Errors) {1349 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))1350 }1351 if !reflect.DeepEqual(expected, result) {1352 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))1353 }1354}1355func TestVariables_UsesArgumentDefaultValues_WhenArgumentProvidedCannotBeParsed(t *testing.T) {1356 doc := `1357 {1358 fieldWithDefaultArgumentValue(input: WRONG_TYPE)1359 }1360 `1361 expected := &graphql.Result{1362 Data: map[string]interface{}{1363 "fieldWithDefaultArgumentValue": `"Hello World"`,1364 },1365 }1366 ast := testutil.TestParse(t, doc)1367 // execute1368 ep := graphql.ExecuteParams{1369 Schema: variablesTestSchema,1370 AST: ast,1371 }1372 result := testutil.TestExecute(t, ep)1373 if len(result.Errors) != len(expected.Errors) {1374 t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))1375 }1376 if !reflect.DeepEqual(expected, result) {1377 t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))1378 }1379}...
parser_dml_test.go
Source:parser_dml_test.go
1package parser2import (3 "fmt"4 "reflect"5 "testing"6)7func fmtimport() {8 fmt.Println()9}10func matchType(t *testing.T, st IStatement, ref interface{}) {11 if reflect.TypeOf(st) != reflect.TypeOf(ref) {12 t.Fatalf("expect type[%v] not match[%v]", reflect.TypeOf(ref), reflect.TypeOf(st))13 }14}15func matchSchemas(t *testing.T, st IStatement, tables ...string) {16 var ts []string17 switch ast := st.(type) {18 case *Select:19 ts = ast.GetSchemas()20 case *Union:21 ts = ast.GetSchemas()22 case *Insert:23 ts = ast.GetSchemas()24 case *Delete:25 ts = ast.GetSchemas()26 case *Update:27 ts = ast.GetSchemas()28 case *Replace:29 ts = ast.GetSchemas()30 case *AlterView:31 ts = ast.GetSchemas()32 case IDDLSchemas:33 ts = ast.GetSchemas()34 case *Lock:35 ts = ast.GetSchemas()36 case *DescribeTable:37 ts = ast.GetSchemas()38 case *DescribeStmt:39 ts = ast.GetSchemas()40 case ITableMtStmt:41 ts = ast.GetSchemas()42 case *CacheIndex:43 ts = ast.GetSchemas()44 case *LoadIndex:45 ts = ast.GetSchemas()46 case *FlushTables:47 ts = ast.GetSchemas()48 case IShowSchemas:49 ts = ast.GetSchemas()50 default:51 t.Fatalf("unknow statement type: %T", ast)52 }53 if len(tables) == 0 && len(ts) == 0 {54 return55 } else if len(tables) != len(ts) {56 t.Fatalf("expect table number[%d] not match return[%d]", len(tables), len(ts))57 }58 for k, v := range ts {59 if v != tables[k] {60 t.Fatalf("expect table[%s] not match return[%s]", tables[k], v)61 }62 }63}64func TestSelect(t *testing.T) {65 st := testParse("SELECT * FROM table1;", t, false)66 matchSchemas(t, st)67 st = testParse("SELECT t1.* FROM (select * from db1.table1) as t1;", t, false)68 matchSchemas(t, st, "db1")69 st = testParse("SELECT sb1,sb2,sb3 \n FROM (SELECT s1 AS sb1, s2 AS sb2, s3*2 AS sb3 FROM db1.t1) AS sb \n WHERE sb1 > 1;", t, false)70 matchSchemas(t, st, "db1")71 st = testParse("SELECT AVG(SUM(column1)) FROM t1 GROUP BY column1;", t, false)72 matchSchemas(t, st)73 st = testParse("SELECT REPEAT('a',1) UNION SELECT REPEAT('b',10);", t, false)74 matchSchemas(t, st)75 st = testParse(`(SELECT a FROM db1.t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)76 UNION77 (SELECT a FROM db2.t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);`, t, false)78 matchSchemas(t, st, "db1", "db2")79 st = testParse(`SELECT funcs(s)80 FROM db1.table181 LEFT OUTER JOIN db2.table282 ON db1.table1.column_name=db2.table2.column_name;`, t, false)83 matchSchemas(t, st, "db1", "db2")84 st = testParse("SELECT * FROM db1.table1 LEFT JOIN db2.table2 ON table1.id=table2.id LEFT JOIN db3.table3 ON table2.id = table3.id for update", t, false)85 matchSchemas(t, st, "db1", "db2", "db3")86 if st.(*Select).LockType != LockType_ForUpdate {87 t.Fatalf("lock type is not For Update")88 }89 st = testParse(`select last_insert_id() as a`, t, false)90 st = testParse(`SELECT substr('''a''bc',0,3) FROM dual`, t, false)91 testParse(`SELECT /*mark for picman*/ * FROM filterd limit 1;`, t, false)92 testParse(`SELECT ?,?,? from t1;`, t, false)93}94func TestInsert(t *testing.T) {95 st := testParse(`INSERT INTO db1.tbl_temp2 (fld_id)96 SELECT tempdb.tbl_temp1.fld_order_id97 FROM tempdb.tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;`, t, false)98 matchSchemas(t, st, "db1", "tempdb")99}100func TestUpdate(t *testing.T) {101 st := testParse(`UPDATE t1 SET col1 = col1 + 1, col2 = col1;`, t, false)102 matchSchemas(t, st)103 st = testParse("UPDATE `Table A`,`Table B` SET `Table A`.`text`=concat_ws('',`Table A`.`text`,`Table B`.`B-num`,\" from \",`Table B`.`date`,'/') WHERE `Table A`.`A-num` = `Table B`.`A-num`", t, false)104 matchSchemas(t, st)105 st = testParse(`UPDATE db1.items,db2.month SET items.price=month.price106 WHERE items.id=month.id;`, t, false)107 matchSchemas(t, st, "db1", "db2")108}109func TestDelete(t *testing.T) {110 st := testParse(`DELETE FROM db.somelog WHERE user = 'jcole'111 ORDER BY timestamp_column LIMIT 1;`, t, false)112 matchSchemas(t, st, "db")113 st = testParse(`DELETE FROM db1.t1, db2.t2 USING t1 INNER JOIN t2 INNER JOIN db3.t3114 WHERE t1.id=t2.id AND t2.id=t3.id;`, t, false)115 matchSchemas(t, st, "db1", "db2", "db3")116 st = testParse(`DELETE FROM a1, a2 USING db1.t1 AS a1 INNER JOIN t2 AS a2117 WHERE a1.id=a2.id;`, t, false)118 matchSchemas(t, st, "db1")119}120func TestReplace(t *testing.T) {121 st := testParse(`REPLACE INTO test2 VALUES (1, 'Old', '2014-08-20 18:47:00');`, t, false)122 matchSchemas(t, st)123 st = testParse(`REPLACE INTO dbname2.test2 VALUES (1, 'Old', '2014-08-20 18:47:00');`, t, false)124 matchSchemas(t, st, "dbname2")125}...
TestParse
Using AI Code Generation
1import (2func main() {3 f, err := parser.ParseFile(fset, "1.go", nil, parser.ImportsOnly)4 if err != nil {5 fmt.Println(err)6 }7 for _, s := range f.Imports {8 fmt.Println(s.Path.Value)9 }10}11import (12func main() {13 f, err := parser.ParseFile(fset, "1.go", nil, parser.ImportsOnly)14 if err != nil {15 fmt.Println(err)16 }17 for _, s := range f.Imports {18 fmt.Println(s.Path.Value)19 }20}21import (22func main() {23 f, err := parser.ParseFile(fset, "1.go", nil, parser.ImportsOnly)24 if err != nil {25 fmt.Println(err)26 }27 for _, s := range f.Imports {28 fmt.Println(s.Path.Value)29 }30}31import (
TestParse
Using AI Code Generation
1import (2func main() {3 fset := token.NewFileSet()4 node, err := parser.ParseFile(fset, "1.go", nil, 0)5 if err != nil {6 fmt.Println(err)7 }8 ast.Print(fset, node)9}10import (11func main() {12 fset := token.NewFileSet()13 node, err := parser.ParseFile(fset, "1.go", nil, 0)14 if err != nil {15 fmt.Println(err)16 }17 ast.Print(fset, node)18}19import (20func main() {21 fset := token.NewFileSet()22 node, err := parser.ParseFile(fset, "1.go", nil, 0)23 if err != nil {24 fmt.Println(err)25 }26 ast.Print(fset, node)27}28import (29func main() {30 fset := token.NewFileSet()31 node, err := parser.ParseFile(fset, "1.go", nil, 0)32 if err != nil {33 fmt.Println(err)34 }35 ast.Print(fset, node)36}37import (38func main() {39 fset := token.NewFileSet()40 node, err := parser.ParseFile(fset, "1.go", nil, 0)41 if err != nil {42 fmt.Println(err)43 }
TestParse
Using AI Code Generation
1import (2func main() {3import "fmt"4func main() {5 fmt.Println("Hello, World!")6}7 fset := token.NewFileSet()8 f, err := parser.ParseFile(fset, "", src, 0)9 if err != nil {10 fmt.Println(err)11 }12 ast.Print(fset, f)13}14import "fmt"15func main() {16 fmt.Println("Hello, World!")17}18func ParseFile(fset *token.FileSet, filename string, src []byte, mode Mode) (f *ast.File, err error)19func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, mode Mode) (pkgs map[string]*ast.Package, first error)
TestParse
Using AI Code Generation
1import (2func main() {3import "fmt"4func main() {5 fmt.Println("Hello, world!")6}7 fset := token.NewFileSet()8 f, err := parser.ParseFile(fset, "src.go", input, 0)9 if err != nil {10 fmt.Println(err)11 }12 ast.Print(fset, f)13}14import "fmt"15func main() {16 fmt.Println("Hello, world!")17}18import (19func main() {20import "fmt"21func main() {22 fmt.Println("Hello, world!")23}24 fset := token.NewFileSet()25 f, err := parser.ParseFile(fset, "src.go", input, 0)26 if err != nil {27 fmt.Println(err)28 }29 ast.Print(fset, f)30 ast.Print(fset, f)31}32import "fmt"33func main() {34 fmt.Println("Hello, world!")35}36import "fmt"37func main() {38 fmt.Println("Hello, world!")39}40import (41func main() {42import "fmt"43func main() {44 fmt.Println("Hello, world!")45}46 fset := token.NewFileSet()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!