github.com/senomas/gqlgen@v0.17.11-0.20220626120754-9aee61b0716a/plugin/resolvergen/resolver_test.go (about) 1 package resolvergen 2 3 import ( 4 "fmt" 5 "os" 6 "syscall" 7 "testing" 8 9 "github.com/99designs/gqlgen/codegen" 10 "github.com/99designs/gqlgen/codegen/config" 11 "github.com/stretchr/testify/require" 12 "golang.org/x/tools/go/packages" 13 ) 14 15 func TestLayoutSingleFile(t *testing.T) { 16 _ = syscall.Unlink("testdata/singlefile/out/resolver.go") 17 18 cfg, err := config.LoadConfig("testdata/singlefile/gqlgen.yml") 19 require.NoError(t, err) 20 p := Plugin{} 21 22 require.NoError(t, cfg.Init()) 23 24 data, err := codegen.BuildData(cfg) 25 if err != nil { 26 panic(err) 27 } 28 29 require.NoError(t, p.GenerateCode(data)) 30 assertNoErrors(t, "github.com/99designs/gqlgen/plugin/resolvergen/testdata/singlefile/out") 31 } 32 33 func TestLayoutFollowSchema(t *testing.T) { 34 testFollowSchemaPersistence(t, "testdata/followschema") 35 36 b, err := os.ReadFile("testdata/followschema/out/schema.resolvers.go") 37 require.NoError(t, err) 38 source := string(b) 39 40 require.Contains(t, source, "// CustomerResolverType.Resolver implementation") 41 require.Contains(t, source, "// CustomerResolverType.Name implementation") 42 require.Contains(t, source, "// AUserHelperFunction implementation") 43 } 44 45 func TestLayoutFollowSchemaWithCustomFilename(t *testing.T) { 46 testFollowSchemaPersistence(t, "testdata/filetemplate") 47 48 b, err := os.ReadFile("testdata/filetemplate/out/schema.custom.go") 49 require.NoError(t, err) 50 source := string(b) 51 52 require.Contains(t, source, "// CustomerResolverType.Resolver implementation") 53 require.Contains(t, source, "// CustomerResolverType.Name implementation") 54 require.Contains(t, source, "// AUserHelperFunction implementation") 55 } 56 57 func TestLayoutInvalidModelPath(t *testing.T) { 58 cfg, err := config.LoadConfig("testdata/invalid_model_path/gqlgen.yml") 59 require.NoError(t, err) 60 61 require.NoError(t, cfg.Init()) 62 63 _, err = codegen.BuildData(cfg) 64 require.Error(t, err) 65 } 66 67 func testFollowSchemaPersistence(t *testing.T, dir string) { 68 _ = syscall.Unlink(dir + "/out/resolver.go") 69 70 cfg, err := config.LoadConfig(dir + "/gqlgen.yml") 71 require.NoError(t, err) 72 p := Plugin{} 73 74 require.NoError(t, cfg.Init()) 75 76 data, err := codegen.BuildData(cfg) 77 if err != nil { 78 panic(err) 79 } 80 81 require.NoError(t, p.GenerateCode(data)) 82 assertNoErrors(t, "github.com/99designs/gqlgen/plugin/resolvergen/"+dir+"/out") 83 } 84 85 func assertNoErrors(t *testing.T, pkg string) { 86 pkgs, err := packages.Load(&packages.Config{ 87 Mode: packages.NeedName | 88 packages.NeedFiles | 89 packages.NeedCompiledGoFiles | 90 packages.NeedImports | 91 packages.NeedTypes | 92 packages.NeedTypesSizes, 93 }, pkg) 94 if err != nil { 95 panic(err) 96 } 97 98 hasErrors := false 99 for _, pkg := range pkgs { 100 for _, err := range pkg.Errors { 101 hasErrors = true 102 fmt.Println(err.Pos + ":" + err.Msg) 103 } 104 } 105 if hasErrors { 106 t.Fatal("see compilation errors above") 107 } 108 }