github.com/fortexxx/gqlgen@v0.10.3-0.20191216030626-ca5ea8b21ead/plugin/modelgen/models_test.go (about)

     1  package modelgen
     2  
     3  import (
     4  	"go/parser"
     5  	"go/token"
     6  	"io/ioutil"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/99designs/gqlgen/codegen/config"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestModelGeneration(t *testing.T) {
    15  	cfg, err := config.LoadConfig("testdata/gqlgen.yml")
    16  	require.NoError(t, err)
    17  	require.NoError(t, cfg.Check())
    18  
    19  	p := Plugin{
    20  		MutateHook: mutateHook,
    21  	}
    22  	require.NoError(t, p.MutateConfig(cfg))
    23  
    24  	require.True(t, cfg.Models.UserDefined("MissingTypeNotNull"))
    25  	require.True(t, cfg.Models.UserDefined("MissingTypeNullable"))
    26  	require.True(t, cfg.Models.UserDefined("MissingEnum"))
    27  	require.True(t, cfg.Models.UserDefined("MissingUnion"))
    28  	require.True(t, cfg.Models.UserDefined("MissingInterface"))
    29  	require.True(t, cfg.Models.UserDefined("TypeWithDescription"))
    30  	require.True(t, cfg.Models.UserDefined("EnumWithDescription"))
    31  	require.True(t, cfg.Models.UserDefined("InterfaceWithDescription"))
    32  	require.True(t, cfg.Models.UserDefined("UnionWithDescription"))
    33  
    34  	t.Run("no pointer pointers", func(t *testing.T) {
    35  		generated, err := ioutil.ReadFile("./out/generated.go")
    36  		require.NoError(t, err)
    37  		require.NotContains(t, string(generated), "**")
    38  	})
    39  
    40  	t.Run("description is generated", func(t *testing.T) {
    41  		node, err := parser.ParseFile(token.NewFileSet(), "./out/generated.go", nil, parser.ParseComments)
    42  		require.NoError(t, err)
    43  		for _, commentGroup := range node.Comments {
    44  			text := commentGroup.Text()
    45  			words := strings.Split(text, " ")
    46  			require.True(t, len(words) > 1, "expected description %q to have more than one word", text)
    47  		}
    48  	})
    49  
    50  	t.Run("tags are applied", func(t *testing.T) {
    51  		file, err := ioutil.ReadFile("./out/generated.go")
    52  		require.NoError(t, err)
    53  
    54  		fileText := string(file)
    55  
    56  		expectedTags := []string{
    57  			`json:"missing2" database:"MissingTypeNotNullmissing2"`,
    58  			`json:"name" database:"MissingInputname"`,
    59  			`json:"missing2" database:"MissingTypeNullablemissing2"`,
    60  			`json:"name" database:"TypeWithDescriptionname"`,
    61  		}
    62  
    63  		for _, tag := range expectedTags {
    64  			require.True(t, strings.Contains(fileText, tag))
    65  		}
    66  	})
    67  }
    68  
    69  func mutateHook(b *ModelBuild) *ModelBuild {
    70  	for _, model := range b.Models {
    71  		for _, field := range model.Fields {
    72  			field.Tag += ` database:"` + model.Name + field.Name + `"`
    73  		}
    74  	}
    75  
    76  	return b
    77  }