mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 10:03:46 +01:00
32 lines
829 B
Go
32 lines
829 B
Go
package dbcopy
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestSortTables orders parents first, ties by name, and refuses a cycle
|
|
func TestSortTables(t *testing.T) {
|
|
tables := map[string]*table{
|
|
"a": {name: "a"},
|
|
"b": {name: "b", fks: []foreignKey{{column: "a_id", parent: "a"}}},
|
|
"c": {name: "c", fks: []foreignKey{{column: "b_id", parent: "b"}, {column: "a_id", parent: "a"}}},
|
|
"d": {name: "d"},
|
|
}
|
|
out, err := sortTables(tables)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var names []string
|
|
for _, tb := range out {
|
|
names = append(names, tb.name)
|
|
}
|
|
if strings.Join(names, ",") != "a,d,b,c" {
|
|
t.Fatalf("order %v", names)
|
|
}
|
|
tables["a"].fks = []foreignKey{{column: "c_id", parent: "c"}}
|
|
if _, err := sortTables(tables); err == nil || !strings.Contains(err.Error(), "cycle") {
|
|
t.Fatalf("cycle not refused: %v", err)
|
|
}
|
|
}
|