1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package load
import (
"testing"
"github.com/facebook/ent/schema/field"
"github.com/stretchr/testify/require"
)
func TestLoad(t *testing.T) {
cfg := &Config{Path: "./testdata/valid"}
spec, err := cfg.Load()
require.NoError(t, err)
require.Len(t, spec.Schemas, 3)
require.Equal(t, "github.com/facebook/ent/entc/load/testdata/valid", spec.PkgPath)
require.Equal(t, "Group", spec.Schemas[0].Name, "ordered alphabetically")
require.Equal(t, "Tag", spec.Schemas[1].Name)
require.Equal(t, "User", spec.Schemas[2].Name)
}
func TestLoadWrongPath(t *testing.T) {
cfg := &Config{Path: "./boring"}
plg, err := cfg.Load()
require.Error(t, err)
require.Nil(t, plg)
}
func TestLoadSpecific(t *testing.T) {
cfg := &Config{Path: "./testdata/valid", Names: []string{"User"}}
spec, err := cfg.Load()
require.NoError(t, err)
require.Len(t, spec.Schemas, 1)
require.Equal(t, "User", spec.Schemas[0].Name)
require.Equal(t, "github.com/facebook/ent/entc/load/testdata/valid", spec.PkgPath)
}
func TestLoadNoSchema(t *testing.T) {
cfg := &Config{Path: "./testdata/invalid"}
schemas, err := cfg.Load()
require.Error(t, err)
require.Empty(t, schemas)
}
func TestLoadSchemaFailure(t *testing.T) {
cfg := &Config{Path: "./testdata/failure"}
spec, err := cfg.Load()
require.Error(t, err)
require.Nil(t, spec)
}
func TestLoadBaseSchema(t *testing.T) {
cfg := &Config{Path: "./testdata/base"}
spec, err := cfg.Load()
require.NoError(t, err)
require.Len(t, spec.Schemas, 1)
require.Len(t, spec.Schemas[0].Fields, 2, "embedded base schema")
f1 := spec.Schemas[0].Fields[0]
require.Equal(t, "base_field", f1.Name)
require.Equal(t, field.TypeInt, f1.Info.Type)
f2 := spec.Schemas[0].Fields[1]
require.Equal(t, "user_field", f2.Name)
require.Equal(t, field.TypeString, f2.Info.Type)
}
|