Files
ent/entc/load/load_test.go
Ariel Mashraki c5b790043c entc: change the way we inject additional fields (#1560)
Summary:
Pull Request resolved: https://github.com/facebookexternal/fbc/pull/1560

Pull Request resolved: https://github.com/facebookincubator/ent/pull/96

Reviewed By: alexsn

Differential Revision: D17931147

fbshipit-source-id: 24e1d72be482cb787c557f46feeb2ed4a31dfe60
2019-10-15 12:00:46 -07:00

70 lines
2.0 KiB
Go

// 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/facebookincubator/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/facebookincubator/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/facebookincubator/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)
}