Add to_many relationships as a first class citizen
- to_many relationships are now cached on the table data structure - to_many relationships now know if any columns involved are nullable
This commit is contained in:
parent
bac9bb30ce
commit
fb802ad687
5 changed files with 319 additions and 62 deletions
|
@ -11,32 +11,38 @@ func (t testInterface) TableNames() ([]string, error) {
|
|||
return []string{"table1", "table2"}, nil
|
||||
}
|
||||
|
||||
func (t testInterface) Columns(tableName string) ([]Column, error) {
|
||||
return []Column{
|
||||
Column{Name: "col1", Type: "character varying"},
|
||||
Column{Name: "col2", Type: "character varying", Nullable: true},
|
||||
}, nil
|
||||
var testCols = []Column{
|
||||
Column{Name: "col1", Type: "character varying"},
|
||||
Column{Name: "col2", Type: "character varying", Nullable: true},
|
||||
}
|
||||
|
||||
func (t testInterface) Columns(tableName string) ([]Column, error) {
|
||||
return testCols, nil
|
||||
}
|
||||
|
||||
var testPkey = &PrimaryKey{Name: "pkey1", Columns: []string{"col1", "col2"}}
|
||||
|
||||
func (t testInterface) PrimaryKeyInfo(tableName string) (*PrimaryKey, error) {
|
||||
return &PrimaryKey{Name: "pkey1", Columns: []string{"col1", "col2"}}, nil
|
||||
return testPkey, nil
|
||||
}
|
||||
|
||||
var testFkeys = []ForeignKey{
|
||||
{
|
||||
Name: "fkey1",
|
||||
Column: "col1",
|
||||
ForeignTable: "table2",
|
||||
ForeignColumn: "col2",
|
||||
},
|
||||
{
|
||||
Name: "fkey2",
|
||||
Column: "col2",
|
||||
ForeignTable: "table1",
|
||||
ForeignColumn: "col1",
|
||||
},
|
||||
}
|
||||
|
||||
func (t testInterface) ForeignKeyInfo(tableName string) ([]ForeignKey, error) {
|
||||
return []ForeignKey{
|
||||
{
|
||||
Name: "fkey1",
|
||||
Column: "col1",
|
||||
ForeignTable: "table3",
|
||||
ForeignColumn: "col3",
|
||||
},
|
||||
{
|
||||
Name: "fkey2",
|
||||
Column: "col2",
|
||||
ForeignTable: "table3",
|
||||
ForeignColumn: "col3",
|
||||
},
|
||||
}, nil
|
||||
return testFkeys, nil
|
||||
}
|
||||
|
||||
func (t testInterface) TranslateColumnType(column Column) Column {
|
||||
|
@ -62,42 +68,27 @@ func TestTables(t *testing.T) {
|
|||
t.Errorf("Expected len 2, got: %d\n", len(tables))
|
||||
}
|
||||
|
||||
expectCols := []Column{
|
||||
Column{Name: "col1", Type: "string"},
|
||||
Column{Name: "col2", Type: "string", Nullable: true},
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(tables[0].Columns, expectCols) {
|
||||
t.Errorf("Did not get expected columns, got:\n%#v\n%#v", tables[0].Columns, expectCols)
|
||||
if !reflect.DeepEqual(tables[0].Columns, testCols) {
|
||||
t.Errorf("Did not get expected columns, got:\n%#v\n%#v", tables[0].Columns, testCols)
|
||||
}
|
||||
|
||||
if !tables[0].IsJoinTable || !tables[1].IsJoinTable {
|
||||
t.Errorf("Expected IsJoinTable to be true")
|
||||
}
|
||||
|
||||
expectPkey := &PrimaryKey{Name: "pkey1", Columns: []string{"col1", "col2"}}
|
||||
expectFkey := []ForeignKey{
|
||||
{
|
||||
Name: "fkey1",
|
||||
Column: "col1",
|
||||
ForeignTable: "table3",
|
||||
ForeignColumn: "col3",
|
||||
},
|
||||
{
|
||||
Name: "fkey2",
|
||||
Column: "col2",
|
||||
ForeignTable: "table3",
|
||||
ForeignColumn: "col3",
|
||||
Nullable: true,
|
||||
},
|
||||
if !reflect.DeepEqual(tables[0].PKey, testPkey) {
|
||||
t.Errorf("Did not get expected PKey, got:\n#%v\n%#v", tables[0].PKey, testPkey)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(tables[0].FKeys, expectFkey) {
|
||||
t.Errorf("Did not get expected Fkey, got:\n%#v\n%#v", tables[0].FKeys, expectFkey)
|
||||
if !reflect.DeepEqual(tables[0].FKeys, testFkeys) {
|
||||
t.Errorf("Did not get expected Fkey, got:\n%#v\n%#v", tables[0].FKeys, testFkeys)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(tables[0].PKey, expectPkey) {
|
||||
t.Errorf("Did not get expected PKey, got:\n#%v\n%#v", tables[0].PKey, expectPkey)
|
||||
if len(tables[0].ToManyRelationships) != 1 {
|
||||
t.Error("wanted a to many relationship")
|
||||
}
|
||||
if len(tables[1].ToManyRelationships) != 1 {
|
||||
t.Error("wanted a to many relationship")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -161,3 +152,47 @@ func TestSetForeignKeyNullability(t *testing.T) {
|
|||
t.Error("should be nullable")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetRelationships(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables := []Table{
|
||||
Table{
|
||||
Name: "one",
|
||||
Columns: []Column{
|
||||
Column{Name: "id", Type: "string"},
|
||||
},
|
||||
},
|
||||
Table{
|
||||
Name: "other",
|
||||
Columns: []Column{
|
||||
Column{Name: "other_id", Type: "string"},
|
||||
},
|
||||
FKeys: []ForeignKey{{Column: "other_id", ForeignTable: "one", ForeignColumn: "id", Nullable: true}},
|
||||
},
|
||||
}
|
||||
|
||||
setRelationships(&tables[0], tables)
|
||||
setRelationships(&tables[1], tables)
|
||||
|
||||
if got := len(tables[0].ToManyRelationships); got != 1 {
|
||||
t.Error("should have a relationship:", got)
|
||||
}
|
||||
if got := len(tables[1].ToManyRelationships); got != 0 {
|
||||
t.Error("should have no to many relationships:", got)
|
||||
}
|
||||
|
||||
rel := tables[0].ToManyRelationships[0]
|
||||
if rel.Column != "id" {
|
||||
t.Error("wrong column:", rel.Column)
|
||||
}
|
||||
if rel.ForeignTable != "other" {
|
||||
t.Error("wrong table:", rel.ForeignTable)
|
||||
}
|
||||
if rel.ForeignColumn != "other_id" {
|
||||
t.Error("wrong column:", rel.ForeignColumn)
|
||||
}
|
||||
if rel.ToJoinTable {
|
||||
t.Error("should not be a join table")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue