table_dependency_service_test.go 1.6 KB
package domainService

import (
	"fmt"
	"github.com/stretchr/testify/assert"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"sort"
	"testing"
)

func TestTableDependTree(t *testing.T) {
	/*
					5                      8
			       / \                    / \
			      1   3                  9  10
		 	     /   / \                / \
			    4   6   2              11 12
			            \
			             7
	*/
	tables := []*domain.Table{
		newTable(5, 1, 3),
		newTable(1, 4),
		newTable(4),
		newTable(3, 6, 2),
		newTable(6),
		newTable(2, 7),
		newTable(7),

		newTable(8, 9, 10),
		newTable(10),
		newTable(9, 11, 12),
		newTable(11),
		newTable(12, 9),
	}
	tableDependencyService := &TableDependencyService{DebugLog: func(s string) {
		//t.Log(s)
	}}

	inputs := []struct {
		BindTableId int
		Trees       []int
		Circle      bool
	}{
		{
			3,
			[]int{1, 2, 3, 4, 5, 6, 7},
			false,
		},
		{
			4,
			[]int{1, 2, 3, 4, 5, 6, 7},
			false,
		},

		{
			12,
			[]int{8, 9, 10, 11, 12},
			true,
		},
		{
			8,
			[]int{8, 9, 10, 11, 12},
			true,
		},
		{
			10,
			[]int{8, 9, 10, 11, 12},
			true,
		},
	}
	for _, input := range inputs {
		tree := tableDependencyService.TableDependTree(tables, input.BindTableId)
		got := tree.Tree
		sort.Ints(got)
		assert.Equal(t, got, input.Trees)

		assert.Equal(t, tableDependencyService.Detect(nil, tree.EdgesArray()), input.Circle)
	}
}

func newTable(id int, dependTables ...int) *domain.Table {
	return &domain.Table{
		TableId: id,
		Name:    fmt.Sprintf("t%d", id),
		TableInfo: &domain.TableInfo{
			DependencyTables: dependTables,
		},
	}
}