package batch_test import ( "os" "path/filepath" "reflect" "testing" "xyj-figures/pkg/batch" "xyj-figures/pkg/entity" "xyj-figures/pkg/relationship" "github.com/go-test/deep" ) const ( DataFolder = "../../data" ) func TestLoadFolder(t *testing.T) { tcs := []struct { name string filelist []string want struct { entities []entity.Entity relationships []relationship.Relationship errors error } wanterr bool }{ { name: "test1", filelist: []string{ "character/孙悟空.yaml", }, want: struct { entities []entity.Entity relationships []relationship.Relationship errors error }{ entities: []entity.Entity{ { Schema: "v1", Type: entity.EntityTypeCharacter, Details: entity.Character{ Name: "孙悟空", OtherNames: []string{"孙行者", "者行孙"}, Relationships: []relationship.Relationship{ { Src: "孙悟空", Dst: "唐僧", Description: "师徒", Bidirection: true, }, }, Description: "孙悟空是明朝中后期畅销小说《西游记》当中个第一主角,是从石头里向蹦出来个猢狲。", Links: []string{ "https://zh.wikipedia.org/wiki/%E5%AD%99%E6%82%9F%E7%A9%BA", }, }, }, }, relationships: []relationship.Relationship{ { Src: "孙悟空", Dst: "唐僧", Description: "师徒", Bidirection: true, }, }, }, wanterr: false, }, } for _, tc := range tcs { t.Run(tc.name, func(t *testing.T) { tmpDir := t.TempDir() for _, fn := range tc.filelist { f, err := os.ReadFile(filepath.Join(DataFolder, fn)) if err != nil { t.Fatalf("failed to copy file %s: %v", fn, err) } dstPath := filepath.Join(tmpDir, fn) if err := os.MkdirAll(filepath.Dir(dstPath), os.ModePerm); err != nil { t.Fatalf("failed to copy file %s: %v", fn, err) } if err := os.WriteFile(dstPath, f, 0644); err != nil { t.Fatalf("failed to copy file %s: %v", fn, err) } } egot, rgot, errgot := batch.LoadFolder(tmpDir) if (!tc.wanterr && errgot != nil) || (tc.wanterr && errgot == nil) { t.Errorf("error not expected, wanterr: %v got: %+v", tc.wanterr, errgot) } if !reflect.DeepEqual(egot, tc.want.entities) { t.Errorf("entities diff: %+v", deep.Equal(egot, tc.want.entities)) } if !reflect.DeepEqual(rgot, tc.want.relationships) { t.Errorf("relationship diff: %+v", deep.Equal(rgot, tc.want.relationships)) } }) } }