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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
// Copyright 2021-present The Atlas Authors. 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 specutil
import (
"testing"
"ariga.io/atlas/schemahcl"
"ariga.io/atlas/sql/schema"
"ariga.io/atlas/sql/sqlspec"
"github.com/stretchr/testify/require"
)
func TestFromSpec_SchemaName(t *testing.T) {
sc := &schema.Schema{
Name: "schema",
Tables: []*schema.Table{
{},
},
}
sc.Tables[0].Schema = sc
s, ta, err := FromSchema(sc, func(table *schema.Table) (*sqlspec.Table, error) {
return &sqlspec.Table{}, nil
})
require.NoError(t, err)
require.Equal(t, sc.Name, s.Name)
require.Equal(t, "$schema."+sc.Name, ta[0].Schema.V)
}
func TestFromForeignKey(t *testing.T) {
tbl := &schema.Table{
Name: "users",
Columns: []*schema.Column{
{
Name: "id",
Type: &schema.ColumnType{
Type: &schema.IntegerType{
T: "int",
},
},
},
{
Name: "parent_id",
Type: &schema.ColumnType{
Type: &schema.IntegerType{
T: "int",
},
},
},
},
}
fk := &schema.ForeignKey{
Symbol: "fk",
Table: tbl,
Columns: tbl.Columns[1:],
RefTable: tbl,
RefColumns: tbl.Columns[:1],
OnUpdate: schema.NoAction,
OnDelete: schema.Cascade,
}
key, err := FromForeignKey(fk)
require.NoError(t, err)
require.EqualValues(t, &sqlspec.ForeignKey{
Symbol: "fk",
Columns: []*schemahcl.Ref{
{V: "$column.parent_id"},
},
RefColumns: []*schemahcl.Ref{
{V: "$column.id"},
},
OnUpdate: &schemahcl.Ref{V: "NO_ACTION"},
OnDelete: &schemahcl.Ref{V: "CASCADE"},
}, key)
fk.OnDelete = ""
fk.OnUpdate = ""
key, err = FromForeignKey(fk)
require.NoError(t, err)
require.EqualValues(t, &sqlspec.ForeignKey{
Symbol: "fk",
Columns: []*schemahcl.Ref{
{V: "$column.parent_id"},
},
RefColumns: []*schemahcl.Ref{
{V: "$column.id"},
},
}, key)
}
|