kubelatte-ce
Форк от sbertech/kubelatte-ce
583 строки · 15.3 Кб
1package location2
3import (4"reflect"5"testing"6)
7
8func TestCompilePathToLocation(t *testing.T) {9t.Parallel()10type args struct {11path string12}13tests := []struct {14name string15args args
16want *Node17wantErr bool18}{19{20name: "ok",21args: args{22path: "test.generic[].name[@.name].value[@.name == `value`]",23},24want: &Node{25nodeType: SimplePath,26path: "test",27filter: nil,28child: &Node{29nodeType: GenericFilter,30path: "generic",31filter: nil,32child: &Node{33nodeType: NameFilter,34path: "name",35filter: &filter{36name: "name",37value: "",38},39child: &Node{40nodeType: ValueFilter,41path: "value",42filter: &filter{43name: "name",44value: "value",45},46child: &Node{},47},48},49},50},51wantErr: false,52},53}54for _, tt := range tests {55t.Run(tt.name, func(t *testing.T) {56got, err := CompilePathToLocation(tt.args.path)57if (err != nil) != tt.wantErr {58t.Errorf("CompilePathToLocation() error = %v, wantErr %v", err, tt.wantErr)59return60}61if !reflect.DeepEqual(got, tt.want) {62t.Errorf("CompilePathToLocation() got = %v, want %v", got, tt.want)63}64})65}66}
67
68func TestLocationNode_ReplaceByLocation(t *testing.T) {69t.Parallel()70type fields struct {71nodeType NodeType
72path string73filter *filter74child *Node75}76type args struct {77target map[string]interface{}78replacement map[string]interface{}79location *Node80}81var tests = []struct {82name string83fields fields
84args args
85want map[string]interface{}86wantErr bool87}{88{89name: "simple path exists",90fields: fields{91nodeType: 0,92path: "test",93filter: nil,94child: &Node{},95},96args: args{97target: map[string]interface{}{"test": map[string]interface{}{}},98replacement: map[string]interface{}{"res": nil},99location: nil,100},101want: map[string]interface{}{"test": map[string]interface{}{"res": nil}},102wantErr: false,103},104{105name: "simple path doesnt exist",106fields: fields{107nodeType: SimplePath,108path: "dif",109filter: nil,110child: &Node{},111},112args: args{113target: map[string]interface{}{"test": map[string]interface{}{}},114replacement: map[string]interface{}{"res": nil},115location: nil,116},117want: map[string]interface{}{"dif": map[string]interface{}{"res": nil}, "test": map[string]interface{}{}},118wantErr: false,119},120{121name: "simple path is not a map",122fields: fields{123nodeType: SimplePath,124path: "dif",125filter: nil,126child: &Node{},127},128args: args{129target: map[string]interface{}{"dif": "string"},130replacement: map[string]interface{}{"res": nil},131location: nil,132},133want: map[string]interface{}{"dif": map[string]interface{}{"res": nil}},134wantErr: false,135},136{137name: "generic filter",138fields: fields{139nodeType: GenericFilter,140path: "test",141child: &Node{},142},143args: args{144target: map[string]interface{}{"test": []interface{}{map[string]interface{}{}}},145replacement: map[string]interface{}{"test": "value"},146location: nil,147},148want: map[string]interface{}{"test": []interface{}{map[string]interface{}{"test": "value"}}},149wantErr: false,150},151{152name: "generic filter, yaml error",153fields: fields{154nodeType: GenericFilter,155path: "test",156child: &Node{},157},158args: args{159target: map[string]interface{}{"test": []interface{}{nil}},160replacement: map[string]interface{}{"test": "value"},161location: nil,162},163want: map[string]interface{}{"test": []interface{}{map[string]interface{}{"test": "value"}}},164wantErr: false,165},166{167name: "generic filter not a list",168fields: fields{169nodeType: GenericFilter,170path: "test",171child: &Node{},172},173args: args{174target: map[string]interface{}{"test": map[string]interface{}{"test1": "value"}},175replacement: map[string]interface{}{"test": "value"},176},177want: map[string]interface{}{"test": map[string]interface{}{"test1": map[string]interface{}{"test": "value"}}},178wantErr: false,179},180{181name: "name filter",182fields: fields{183nodeType: NameFilter,184path: "test",185child: &Node{},186filter: &filter{name: "name"},187},188args: args{189target: map[string]interface{}{"test": []interface{}{map[string]interface{}{"name": "val"}, map[string]interface{}{"name2": "val2"}}},190replacement: map[string]interface{}{"test": "value"},191location: nil,192},193want: map[string]interface{}{"test": []interface{}{map[string]interface{}{"test": "value"}, map[string]interface{}{"name2": "val2"}}},194wantErr: false,195},196{197name: "name filter not a list",198fields: fields{199nodeType: NameFilter,200path: "test",201child: &Node{},202filter: &filter{name: "name"},203},204args: args{205target: map[string]interface{}{"test": map[string]interface{}{"d": "e"}},206replacement: map[string]interface{}{"test": "value"},207},208want: nil,209wantErr: true,210},211{212name: "name filter elements are not maps",213fields: fields{214nodeType: NameFilter,215path: "test",216child: &Node{},217filter: &filter{name: "name"},218},219args: args{220target: map[string]interface{}{"test": []interface{}{"not", "map"}},221replacement: map[string]interface{}{"test": "value"},222location: nil,223},224want: nil,225wantErr: true,226},227{228name: "value filter",229fields: fields{230nodeType: ValueFilter,231path: "test",232child: &Node{},233filter: &filter{name: "name", value: "value"},234},235args: args{236target: map[string]interface{}{"test": []interface{}{map[string]interface{}{"name": "value"}, map[string]interface{}{"name2": "val2"}}},237replacement: map[string]interface{}{"test": "value"},238location: nil,239},240want: map[string]interface{}{"test": []interface{}{map[string]interface{}{"test": "value"}, map[string]interface{}{"name2": "val2"}}},241wantErr: false,242},243{244name: "value filter elements are not a map",245fields: fields{246nodeType: ValueFilter,247path: "test",248child: &Node{},249filter: &filter{name: "name", value: "value"},250},251args: args{252target: map[string]interface{}{"test": []interface{}{"sdf", "sdf"}},253replacement: map[string]interface{}{"test": "value"},254location: nil,255},256want: nil,257wantErr: true,258},259{260name: "value filter not list",261fields: fields{262nodeType: ValueFilter,263path: "test",264child: &Node{},265filter: &filter{name: "name", value: "value"},266},267args: args{268target: map[string]interface{}{"test": "string"},269replacement: map[string]interface{}{"test": "value"},270location: nil,271},272want: nil,273wantErr: true,274},275}276for _, tt := range tests {277t.Run(tt.name, func(t *testing.T) {278l := &Node{279nodeType: tt.fields.nodeType,280path: tt.fields.path,281filter: tt.fields.filter,282child: tt.fields.child,283}284got, err := l.ReplaceByLocation(tt.args.target, tt.args.replacement, l)285if (err != nil) != tt.wantErr {286t.Errorf("ReplaceByLocation() error = %v, wantErr %v", err, tt.wantErr)287return288}289if !reflect.DeepEqual(got, tt.want) {290t.Errorf("ReplaceByLocation() got = %v, want %v", got, tt.want)291}292})293}294}
295
296func TestLocationNode_GetLastItem(t *testing.T) {297t.Parallel()298type fields struct {299nodeType NodeType
300path string301filter *filter302child *Node303}304tests := []struct {305name string306fields fields
307want string308}{309{310name: "ok",311fields: fields{312nodeType: 0,313path: "test",314filter: nil,315child: &Node{path: "last", child: &Node{}},316},317want: "last",318},319}320for _, tt := range tests {321t.Run(tt.name, func(t *testing.T) {322l := &Node{323nodeType: tt.fields.nodeType,324path: tt.fields.path,325filter: tt.fields.filter,326child: tt.fields.child,327}328if got := l.GetLastItem(); got != tt.want {329t.Errorf("GetLastItem() = %v, want %v", got, tt.want)330}331})332}333}
334
335func TestLocationNode_MergeByLocation(t *testing.T) {336t.Parallel()337type fields struct {338nodeType NodeType
339path string340filter *filter341child *Node342}343type args struct {344reference interface{}345replacement map[string]interface{}346location *Node347}348tests := []struct {349name string350fields fields
351args args
352want map[string]interface{}353wantErr bool354}{355{356name: "simple path exists",357fields: fields{358nodeType: 0,359path: "test",360filter: nil,361child: &Node{},362},363args: args{364reference: map[string]interface{}{"test": map[string]interface{}{}},365replacement: map[string]interface{}{"res": nil},366},367want: map[string]interface{}{"test": map[string]interface{}{"res": nil}},368wantErr: false,369},370{371name: "generic filter",372fields: fields{373nodeType: GenericFilter,374path: "test",375child: &Node{},376},377args: args{378reference: map[string]interface{}{"test": []interface{}{map[string]interface{}{}}},379replacement: map[string]interface{}{"test": "value"},380},381want: map[string]interface{}{"test": []interface{}{map[string]interface{}{"test": "value"}}},382wantErr: false,383},384{385name: "generic filter, yaml error",386fields: fields{387nodeType: GenericFilter,388path: "test",389child: &Node{},390},391args: args{392reference: map[string]interface{}{"test": []interface{}{nil}},393replacement: map[string]interface{}{"test": "value"},394},395want: nil,396wantErr: true,397},398{399name: "generic filter not a list",400fields: fields{401nodeType: GenericFilter,402path: "test",403child: &Node{},404},405args: args{406reference: map[string]interface{}{"test": map[string]interface{}{"test1": "value"}},407replacement: map[string]interface{}{"test": "value"},408},409want: map[string]interface{}{"test": map[string]interface{}{"test1": map[string]interface{}{"test": "value"}}},410wantErr: false,411},412{413name: "name filter",414fields: fields{415nodeType: NameFilter,416path: "test",417child: &Node{},418filter: &filter{name: "name"},419},420args: args{421reference: map[string]interface{}{"test": []interface{}{map[string]interface{}{"name": "val"}, map[string]interface{}{"name2": "val2"}}},422replacement: map[string]interface{}{"test": "value"},423},424want: map[string]interface{}{"test": []interface{}{map[string]interface{}{"name": "val", "test": "value"}}},425wantErr: false,426},427{428name: "name filter not a list",429fields: fields{430nodeType: NameFilter,431path: "test",432child: &Node{},433filter: &filter{name: "name"},434},435args: args{436reference: map[string]interface{}{"test": map[string]interface{}{"d": "e"}},437replacement: map[string]interface{}{"test": "value"},438},439want: nil,440wantErr: true,441},442{443name: "name filter elements are not maps",444fields: fields{445nodeType: NameFilter,446path: "test",447child: &Node{},448filter: &filter{name: "name"},449},450args: args{451reference: map[string]interface{}{"test": []interface{}{"not", "map"}},452replacement: map[string]interface{}{"test": "value"},453},454want: nil,455wantErr: true,456},457{458name: "value filter",459fields: fields{460nodeType: ValueFilter,461path: "test",462child: &Node{},463filter: &filter{name: "name", value: "value"},464},465args: args{466reference: map[string]interface{}{"test": []interface{}{map[string]interface{}{"name": "value"}, map[string]interface{}{"name2": "val2"}}},467replacement: map[string]interface{}{"test": "value"},468},469want: map[string]interface{}{"test": []interface{}{map[string]interface{}{"name": "value", "test": "value"}}},470wantErr: false,471},472{473name: "value filter elements are not a map",474fields: fields{475nodeType: ValueFilter,476path: "test",477child: &Node{},478filter: &filter{name: "name", value: "value"},479},480args: args{481reference: map[string]interface{}{"test": []interface{}{"sdf", "sdf"}},482replacement: map[string]interface{}{"test": "value"},483},484want: nil,485wantErr: true,486},487{488name: "value filter not list",489fields: fields{490nodeType: ValueFilter,491path: "test",492child: &Node{},493filter: &filter{name: "name", value: "value"},494},495args: args{496reference: map[string]interface{}{"test": "string"},497replacement: map[string]interface{}{"test": "value"},498},499want: nil,500wantErr: true,501},502
503{504name: "merge no matches",505fields: fields{506nodeType: ValueFilter,507path: "test",508filter: &filter{name: "name", value: "value2"},509child: &Node{},510},511args: args{512reference: map[string]interface{}{"test": []interface{}{map[string]interface{}{"name": "value"}, map[string]interface{}{"name2": "val2"}}},513replacement: map[string]interface{}{"test": "value"},514location: nil,515},516want: map[string]interface{}{"test": []interface{}{}},517wantErr: false,518},519{520name: "merge no matches",521fields: fields{522nodeType: ValueFilter,523path: "test",524filter: &filter{name: "name", value: "value2"},525child: &Node{},526},527args: args{528reference: map[string]interface{}{"test": []interface{}{map[string]interface{}{"name": "value"}, map[string]interface{}{"name": "value2"}}},529replacement: map[string]interface{}{"test": "new_val"},530},531want: map[string]interface{}{"test": []interface{}{map[string]interface{}{"name": "value2", "test": "new_val"}}},532wantErr: false,533},534{535name: "merge name filter",536fields: fields{537nodeType: NameFilter,538path: "test",539filter: &filter{name: "name"},540child: &Node{},541},542args: args{543reference: map[string]interface{}{"test": []interface{}{map[string]interface{}{"wrong": "value"}, map[string]interface{}{"name": "value2"}}},544replacement: map[string]interface{}{"test": "new_val"},545},546want: map[string]interface{}{"test": []interface{}{map[string]interface{}{"name": "value2", "test": "new_val"}}},547wantErr: false,548},549{550name: "test iterate",551fields: fields{552nodeType: GenericFilter,553path: "test",554filter: &filter{},555child: &Node{},556},557args: args{558reference: map[string]interface{}{"not_here": nil, "test": map[string]interface{}{"wrong": "value", "name": "value2"}},559replacement: map[string]interface{}{"ez": "value"},560},561want: map[string]interface{}{"test": map[string]interface{}{"wrong": map[string]interface{}{"ez": "value"}, "name": map[string]interface{}{"ez": "value"}}},562wantErr: false,563},564}565for _, tt := range tests {566t.Run(tt.name, func(t *testing.T) {567l := &Node{568nodeType: tt.fields.nodeType,569path: tt.fields.path,570filter: tt.fields.filter,571child: tt.fields.child,572}573got, err := l.MergeByLocation(tt.args.reference, tt.args.replacement, l)574if (err != nil) != tt.wantErr {575t.Errorf("MergeByLocation() error = %v, wantErr %v", err, tt.wantErr)576return577}578if !reflect.DeepEqual(got, tt.want) {579t.Errorf("MergeByLocation() got = %v, want %v", got, tt.want)580}581})582}583}
584