From 1d13dd2ec33ed514c14b0d9a6c418f5d5c4428c9 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Wed, 6 May 2020 16:25:47 +0300 Subject: [PATCH] Updated unit tests --- config/cdrecfg_test.go | 7 ++ config/config_test.go | 18 ++++ config/erscfg_test.go | 12 ++- config/fctemplate_test.go | 14 +++ config/httpagntcfg_test.go | 36 ++++--- config/loaderscfg_test.go | 2 + utils/dataprovider_test.go | 26 +++-- utils/orderednavigablemap.go | 9 +- utils/orderednavigablemap_test.go | 171 +++++++++++++++++++++--------- 9 files changed, 208 insertions(+), 87 deletions(-) diff --git a/config/cdrecfg_test.go b/config/cdrecfg_test.go index 57ca650de..4759af5cd 100644 --- a/config/cdrecfg_test.go +++ b/config/cdrecfg_test.go @@ -37,6 +37,9 @@ func TestCdreCfgClone(t *testing.T) { Path: "runid", Value: runIDRsrs}, } + for _, v := range initContentFlds { + v.ComputePath() + } initCdreCfg := &CdreCfg{ ExportFormat: utils.MetaFileCSV, ExportPath: "/var/spool/cgrates/cdre", @@ -55,6 +58,9 @@ func TestCdreCfgClone(t *testing.T) { Path: "runid", Value: runIDRsrs}, } + for _, v := range eClnContentFlds { + v.ComputePath() + } eClnCdreCfg := &CdreCfg{ ExportFormat: utils.MetaFileCSV, ExportPath: "/var/spool/cgrates/cdre", @@ -125,6 +131,7 @@ func TestCdreCfgloadFromJsonCfg(t *testing.T) { Value: val, }}, } + expected.Fields[0].ComputePath() if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { t.Error(err) } else if jsnCdreCfg, err := jsnCfg.CdreJsonCfgs(); err != nil { diff --git a/config/config_test.go b/config/config_test.go index 21d6f9659..8668db265 100755 --- a/config/config_test.go +++ b/config/config_test.go @@ -568,6 +568,9 @@ func TestCgrCfgJSONDefaultsCdreProfiles(t *testing.T) { RoundingDecimals: 4, }, } + for _, v := range eContentFlds { + v.ComputePath() + } eCdreCfg := map[string]*CdreCfg{ utils.MetaDefault: { ExportFormat: utils.MetaFileCSV, @@ -1475,6 +1478,13 @@ func TestCgrLoaderCfgITDefaults(t *testing.T) { }, }, } + for _, profile := range eCfg { + for _, fields := range profile.Data { + for _, v := range fields.Fields { + v.ComputePath() + } + } + } if !reflect.DeepEqual(eCfg, cgrCfg.loaderCfg) { t.Errorf("received: %+v, \n expecting: %+v", utils.ToJSON(eCfg), utils.ToJSON(cgrCfg.loaderCfg)) @@ -1705,6 +1715,11 @@ func TestCgrCdfEventReader(t *testing.T) { }, }, } + for _, profile := range eCfg.Readers { + for _, v := range profile.Fields { + v.ComputePath() + } + } if !reflect.DeepEqual(cgrCfg.ersCfg, eCfg) { t.Errorf("received: %+v,\n expecting: %+v", utils.ToJSON(cgrCfg.ersCfg), utils.ToJSON(eCfg)) } @@ -1750,6 +1765,9 @@ func TestCgrCfgEventReaderDefault(t *testing.T) { }, CacheDumpFields: make([]*FCTemplate, 0), } + for _, v := range eCfg.Fields { + v.ComputePath() + } if !reflect.DeepEqual(cgrCfg.dfltEvRdr, eCfg) { t.Errorf("received: %+v,\n expecting: %+v", utils.ToJSON(cgrCfg.dfltEvRdr), utils.ToJSON(eCfg)) } diff --git a/config/erscfg_test.go b/config/erscfg_test.go index 6f7d523a4..29e0df052 100644 --- a/config/erscfg_test.go +++ b/config/erscfg_test.go @@ -50,6 +50,9 @@ func TestEventRedearClone(t *testing.T) { }, CacheDumpFields: make([]*FCTemplate, 0), } + for _, v := range orig.Fields { + v.ComputePath() + } cloned := orig.Clone() if !reflect.DeepEqual(cloned, orig) { t.Errorf("expected: %s \n,received: %s", utils.ToJSON(orig), utils.ToJSON(cloned)) @@ -78,6 +81,9 @@ func TestEventRedearClone(t *testing.T) { }, CacheDumpFields: make([]*FCTemplate, 0), } + for _, v := range initialOrig.Fields { + v.ComputePath() + } orig.Filters = []string{"SingleFilter"} orig.Fields = []*FCTemplate{ { @@ -178,7 +184,11 @@ func TestEventReaderLoadFromJSON(t *testing.T) { }, }, } - + for _, profile := range expectedERsCfg.Readers { + for _, v := range profile.Fields { + v.ComputePath() + } + } cfgJSONStr := `{ "ers": { "enabled": true, diff --git a/config/fctemplate_test.go b/config/fctemplate_test.go index 0040fccf4..5056d672f 100755 --- a/config/fctemplate_test.go +++ b/config/fctemplate_test.go @@ -39,6 +39,7 @@ func TestNewFCTemplateFromFCTemplateJsonCfg(t *testing.T) { Filters: []string{"Filter1", "Filter2"}, Value: NewRSRParsersMustCompile("cgrates.org", true, utils.INFIELD_SEP), } + expected.ComputePath() if rcv, err := NewFCTemplateFromFCTemplateJsonCfg(jsonCfg, utils.INFIELD_SEP); err != nil { t.Error(err) } else if !reflect.DeepEqual(expected, rcv) { @@ -79,6 +80,9 @@ func TestFCTemplatesFromFCTemplatesJsonCfg(t *testing.T) { Value: NewRSRParsersMustCompile("SampleValue", true, utils.INFIELD_SEP), }, } + for _, v := range expected { + v.ComputePath() + } if rcv, err := FCTemplatesFromFCTemplatesJsonCfg(jsnCfgs, utils.INFIELD_SEP); err != nil { t.Error(err) } else if !reflect.DeepEqual(expected, rcv) { @@ -284,6 +288,14 @@ func TestFCTemplateInflate3(t *testing.T) { }, }, } + for _, v := range fcTmp1 { + v.ComputePath() + } + for _, tmpl := range fcTmpMp { + for _, v := range tmpl { + v.ComputePath() + } + } if _, err := InflateTemplates(fcTmp1, fcTmpMp); err == nil || err.Error() != "empty template with id: " { t.Error(err) @@ -298,6 +310,7 @@ func TestFCTemplateClone(t *testing.T) { Filters: []string{"Filter1", "Filter2"}, Value: NewRSRParsersMustCompile("cgrates.org", true, utils.INFIELD_SEP), } + smpl.ComputePath() cloned := smpl.Clone() if !reflect.DeepEqual(cloned, smpl) { t.Errorf("expected: %s ,received: %s", utils.ToJSON(smpl), utils.ToJSON(cloned)) @@ -309,6 +322,7 @@ func TestFCTemplateClone(t *testing.T) { Filters: []string{"Filter1", "Filter2"}, Value: NewRSRParsersMustCompile("cgrates.org", true, utils.INFIELD_SEP), } + initialSmpl.ComputePath() smpl.Filters = []string{"SingleFilter"} smpl.Value = NewRSRParsersMustCompile("cgrates.com", true, utils.INFIELD_SEP) if !reflect.DeepEqual(cloned, initialSmpl) { diff --git a/config/httpagntcfg_test.go b/config/httpagntcfg_test.go index 7388aa109..d60e25cac 100644 --- a/config/httpagntcfg_test.go +++ b/config/httpagntcfg_test.go @@ -82,6 +82,7 @@ func TestHttpAgentCfgsloadFromJsonCfg(t *testing.T) { }}, }}, }} + expected[0].RequestProcessors[0].ReplyFields[0].ComputePath() if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { t.Error(err) } else if jsnhttpCfg, err := jsnCfg.HttpAgentJsonCfg(); err != nil { @@ -142,19 +143,21 @@ func TestHttpAgentCfgsloadFromJsonCfg(t *testing.T) { SessionSConns: []string{utils.MetaLocalHost}, RequestPayload: "*url", ReplyPayload: "*xml", - RequestProcessors: []*RequestProcessor{{ - ID: "OutboundAUTHDryRun", - Filters: []string{"*string:*req.request_type:OutboundAUTH", "*string:*req.Msisdn:497700056231"}, - Tenant: NewRSRParsersMustCompile("cgrates.org", true, utils.INFIELD_SEP), - Flags: utils.FlagsWithParams{"*dryrun": []string{}}, - RequestFields: []*FCTemplate{}, - ReplyFields: []*FCTemplate{{ - Tag: "Allow", - Path: "response.Allow", - Type: "*constant", - Value: NewRSRParsersMustCompile("1", true, utils.INFIELD_SEP), - Mandatory: true, - }}}, + RequestProcessors: []*RequestProcessor{ + { + ID: "OutboundAUTHDryRun", + Filters: []string{"*string:*req.request_type:OutboundAUTH", "*string:*req.Msisdn:497700056231"}, + Tenant: NewRSRParsersMustCompile("cgrates.org", true, utils.INFIELD_SEP), + Flags: utils.FlagsWithParams{"*dryrun": []string{}}, + RequestFields: []*FCTemplate{}, + ReplyFields: []*FCTemplate{{ + Tag: "Allow", + Path: "response.Allow", + Type: "*constant", + Value: NewRSRParsersMustCompile("1", true, utils.INFIELD_SEP), + Mandatory: true, + }}, + }, { ID: "mtcall_cdr", Filters: []string{"*string:*req.request_type:MTCALL_CDR"}, @@ -189,6 +192,9 @@ func TestHttpAgentCfgsloadFromJsonCfg(t *testing.T) { ReplyFields: []*FCTemplate{}, }}, }} + expected[0].RequestProcessors[0].ReplyFields[0].ComputePath() + expected[0].RequestProcessors[1].ReplyFields[0].ComputePath() + expected[0].RequestProcessors[1].RequestFields[0].ComputePath() if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { t.Error(err) } else if jsnhttpCfg, err := jsnCfg.HttpAgentJsonCfg(); err != nil { @@ -275,6 +281,7 @@ func TestHttpAgentCfgappendHttpAgntProcCfgs(t *testing.T) { }}, }}, } + initial.RequestProcessors[0].ReplyFields[0].ComputePath() proceses := &[]*ReqProcessorJsnCfg{{ ID: utils.StringPointer("OutboundAUTHDryRun1"), Filters: &[]string{"*string:*req.request_type:OutboundAUTH", "*string:*req.Msisdn:497700056231"}, @@ -337,7 +344,8 @@ func TestHttpAgentCfgappendHttpAgntProcCfgs(t *testing.T) { }}, }}, } - + expected.RequestProcessors[0].ReplyFields[0].ComputePath() + expected.RequestProcessors[1].ReplyFields[0].ComputePath() if err = initial.appendHttpAgntProcCfgs(proceses, utils.INFIELD_SEP); err != nil { t.Error(err) } else if !reflect.DeepEqual(expected, initial) { diff --git a/config/loaderscfg_test.go b/config/loaderscfg_test.go index f3acf9869..55f3dac2c 100644 --- a/config/loaderscfg_test.go +++ b/config/loaderscfg_test.go @@ -84,6 +84,8 @@ func TestLoaderSCfgloadFromJsonCfg(t *testing.T) { { Tag: "TenantID", Path: "Tenant", + pathSlice: []string{"Tenant"}, + pathItems: utils.PathItems{{Field: "Tenant"}}, Type: "*composed", Value: val, Mandatory: true, diff --git a/utils/dataprovider_test.go b/utils/dataprovider_test.go index a197d958e..5da7d8a05 100644 --- a/utils/dataprovider_test.go +++ b/utils/dataprovider_test.go @@ -69,44 +69,42 @@ func TestDPDynamicString(t *testing.T) { } func TestAppendNavMapVal(t *testing.T) { + onm := NewOrderedNavigableMap() nm := NavigableMap2{ "Field1": NewNMData("1001"), "Field2": NewNMData("1003"), "Field3": NavigableMap2{"Field4": NewNMData("Val")}, "Field5": &NMSlice{NewNMData(10), NewNMData(101)}, } + onm.nm = nm expected := NavigableMap2{ "Field1": NewNMData("1001"), "Field2": NewNMData("1003"), "Field3": NavigableMap2{"Field4": NewNMData("Val")}, "Field5": &NMSlice{NewNMData(10), NewNMData(101), NewNMData(18)}, } - if err := AppendNavMapVal(nm, PathItems{{Field: "Field5"}}, NewNMData(18)); err != nil { - t.Error(err) - } else if !reflect.DeepEqual(expected, nm) { - t.Errorf("Expected %v ,received: %v", expected, nm) - } - if err := AppendNavMapVal(nm, nil, NewNMData(18)); err != ErrWrongPath { + if err := AppendNavMapVal(onm, &FullPath{Path: "Field5", PathItems: PathItems{{Field: "Field5"}}}, NewNMData(18)); err != nil { t.Error(err) + } else if !reflect.DeepEqual(expected, onm.nm) { + t.Errorf("Expected %v ,received: %v", expected, onm.nm) } } func TestComposeNavMapVal(t *testing.T) { + onm := NewOrderedNavigableMap() nm := NavigableMap2{ "Field4": &NMSlice{}, "Field5": &NMSlice{NewNMData(10), NewNMData(101)}, } - if err := ComposeNavMapVal(nm, nil, NewNMData(18)); err != ErrWrongPath { - t.Error(err) - } - if err := ComposeNavMapVal(nm, PathItems{{Field: "Field4"}}, NewNMData(18)); err != ErrWrongPath { + onm.nm = nm + if err := ComposeNavMapVal(onm, &FullPath{Path: "Field4", PathItems: PathItems{{Field: "Field4"}}}, NewNMData(18)); err != ErrWrongPath { t.Error(err) } expected := NavigableMap2{ "Field4": &NMSlice{}, "Field5": &NMSlice{NewNMData(10), NewNMData("10118")}, } - if err := ComposeNavMapVal(nm, PathItems{{Field: "Field5"}}, NewNMData(18)); err != nil { + if err := ComposeNavMapVal(onm, &FullPath{Path: "Field5", PathItems: PathItems{{Field: "Field5"}}}, NewNMData(18)); err != nil { t.Error(err) } else if !reflect.DeepEqual(expected, nm) { t.Errorf("Expected %v ,received: %v", expected, nm) @@ -117,7 +115,7 @@ func TestComposeNavMapVal(t *testing.T) { "Field5": &NMSlice{NewNMData(10), NewNMData("10118")}, "Field6": &NMSlice{NewNMData(10)}, } - if err := ComposeNavMapVal(nm, PathItems{{Field: "Field6"}}, NewNMData(10)); err != nil { + if err := ComposeNavMapVal(onm, &FullPath{Path: "Field6", PathItems: PathItems{{Field: "Field6"}}}, NewNMData(10)); err != nil { t.Error(err) } else if !reflect.DeepEqual(expected, nm) { t.Errorf("Expected %v ,received: %v", expected, nm) @@ -127,11 +125,11 @@ func TestComposeNavMapVal(t *testing.T) { "Field4": NewNMData(1), "Field5": &NMSlice{NewNMData(10), NewNMData(101)}, } - if err := ComposeNavMapVal(nm, PathItems{{Field: "Field4"}}, NewNMData(10)); err != ErrNotImplemented { + if err := ComposeNavMapVal(onm, &FullPath{Path: "Field4", PathItems: PathItems{{Field: "Field4"}}}, NewNMData(10)); err != ErrWrongPath { t.Error(err) } - if err := ComposeNavMapVal(nm, PathItems{{Field: "Field5"}}, &mockNMInterface{data: 10}); err != ErrNotImplemented { + if err := ComposeNavMapVal(onm, &FullPath{Path: "Field5", PathItems: PathItems{{Field: "Field5"}}}, &mockNMInterface{data: 10}); err != ErrNotImplemented { t.Error(err) } } diff --git a/utils/orderednavigablemap.go b/utils/orderednavigablemap.go index 940e0cad3..3ba67d8c9 100644 --- a/utils/orderednavigablemap.go +++ b/utils/orderednavigablemap.go @@ -91,17 +91,10 @@ func (onm *OrderedNavigableMap) Remove(fullPath *FullPath) (err error) { return } -// // Set sets the value at the given path -// // this is the old to be capable of building the code without updating all the code -// // will be replaced with Set2 after we decide that is the optimal solution -// func (onm *OrderedNavigableMap) Set(fldPath PathItems, val NMInterface) (addedNew bool, err error) { -// return onm.Set2(&FullPath{PathItems: fldPath, Path: fldPath.String()}, val) -// } - // Set sets the value at the given path // this used with full path and the processed path to not calculate them for every set func (onm *OrderedNavigableMap) Set(fullPath *FullPath, val NMInterface) (addedNew bool, err error) { - if len(fullPath.PathItems) == 0 { + if fullPath == nil || len(fullPath.PathItems) == 0 { return false, ErrWrongPath } if addedNew, err = onm.nm.Set(fullPath.PathItems, val); err != nil { diff --git a/utils/orderednavigablemap_test.go b/utils/orderednavigablemap_test.go index f51e68a03..fcf16725f 100644 --- a/utils/orderednavigablemap_test.go +++ b/utils/orderednavigablemap_test.go @@ -26,7 +26,10 @@ import ( func TestOrderedNavigableMap(t *testing.T) { onm := NewOrderedNavigableMap() - onm.Set(PathItems{{Field: "Field1"}}, NewNMData(10)) + onm.Set(&FullPath{ + Path: "Field1", + PathItems: PathItems{{Field: "Field1"}}, + }, NewNMData(10)) expOrder := []PathItems{ PathItems{{Field: "Field1"}}, } @@ -34,9 +37,10 @@ func TestOrderedNavigableMap(t *testing.T) { t.Errorf("Expected %s ,received: %s", expOrder, ToJSON(onm.GetOrder())) } - onm.Set( - PathItems{{Field: "Field2", Index: IntPointer(0)}}, - NewNMData("1001")) + onm.Set(&FullPath{ + Path: "Field2[0]", + PathItems: PathItems{{Field: "Field2", Index: IntPointer(0)}}, + }, NewNMData("1001")) expOrder = []PathItems{ PathItems{{Field: "Field1"}}, PathItems{{Field: "Field2", Index: IntPointer(0)}}, @@ -45,11 +49,12 @@ func TestOrderedNavigableMap(t *testing.T) { t.Errorf("Expected %s ,received: %s", expOrder, ToJSON(onm.GetOrder())) } - onm.Set( - PathItems{ + onm.Set(&FullPath{ + Path: "Field2[1].Account[0]", + PathItems: PathItems{ {Field: "Field2", Index: IntPointer(1)}, {Field: "Account", Index: IntPointer(0)}}, - NewNMData(10)) + }, NewNMData(10)) expOrder = []PathItems{ PathItems{{Field: "Field1"}}, PathItems{{Field: "Field2", Index: IntPointer(0)}}, @@ -61,11 +66,12 @@ func TestOrderedNavigableMap(t *testing.T) { t.Errorf("Expected %s ,received: %s", expOrder, ToJSON(onm.GetOrder())) } - onm.Set( - PathItems{ + onm.Set(&FullPath{ + Path: "Field2[1].Account[1]", + PathItems: PathItems{ {Field: "Field2", Index: IntPointer(1)}, {Field: "Account", Index: IntPointer(1)}}, - NewNMData(11)) + }, NewNMData(11)) expOrder = []PathItems{ PathItems{{Field: "Field1"}}, PathItems{{Field: "Field2", Index: IntPointer(0)}}, @@ -80,9 +86,10 @@ func TestOrderedNavigableMap(t *testing.T) { t.Errorf("Expected %s ,received: %s", expOrder, ToJSON(onm.GetOrder())) } - onm.Set( - PathItems{{Field: "Field2", Index: IntPointer(2)}}, - NewNMData(111)) + onm.Set(&FullPath{ + Path: "Field2[2]", + PathItems: PathItems{{Field: "Field2", Index: IntPointer(2)}}, + }, NewNMData(111)) expOrder = []PathItems{ PathItems{{Field: "Field1"}}, PathItems{{Field: "Field2", Index: IntPointer(0)}}, @@ -98,12 +105,13 @@ func TestOrderedNavigableMap(t *testing.T) { t.Errorf("Expected %s ,received: %s", expOrder, ToJSON(onm.GetOrder())) } - onm.Set( - PathItems{ + onm.Set(&FullPath{ + Path: "Field3.Field4.Field5", + PathItems: PathItems{ {Field: "Field3"}, {Field: "Field4"}, {Field: "Field5"}}, - NewNMData(5)) + }, NewNMData(5)) expOrder = []PathItems{ PathItems{{Field: "Field1"}}, PathItems{{Field: "Field2", Index: IntPointer(0)}}, @@ -151,7 +159,7 @@ func TestOrderedNavigableMap(t *testing.T) { // sliceDeNM exp := &NMSlice{NewNMData("500"), NewNMData("502")} path := PathItems{{Field: "Field2"}} - if _, err := onm.Set(path, exp); err != nil { + if _, err := onm.Set(&FullPath{Path: path.String(), PathItems: path}, exp); err != nil { t.Error(err) } path = PathItems{{Field: "Field2"}} @@ -290,11 +298,11 @@ func TestOrderedNavigableMapLen(t *testing.T) { func TestOrderedNavigableMapGetSet(t *testing.T) { nm := NewOrderedNavigableMap() - nm.Set2(&FullPath{ + nm.Set(&FullPath{ PathItems: PathItems{{Field: "Account", Index: IntPointer(0)}}, Path: "Account", }, NewNMData(1001)) - nm.Set2(&FullPath{ + nm.Set(&FullPath{ PathItems: PathItems{{Field: "Account", Index: IntPointer(1)}}, Path: "Account", }, NewNMData("account_on_new_branch")) @@ -340,7 +348,7 @@ func TestOrderedNavigableMapGetSet(t *testing.T) { } path = PathItems{{Field: "Field2", Index: IntPointer(2)}} - if _, err := nm.Set(path, NewNMData("500")); err != nil { + if _, err := nm.Set(&FullPath{Path: path.String(), PathItems: path}, NewNMData("500")); err != nil { t.Error(err) } if val, err := nm.Field(path); err != nil { @@ -350,7 +358,7 @@ func TestOrderedNavigableMapGetSet(t *testing.T) { } path = PathItems{{Field: "Field2", Index: IntPointer(1)}, {Field: "Account"}} - if _, err := nm.Set(path, NewNMData("5")); err != nil { + if _, err := nm.Set(&FullPath{Path: path.String(), PathItems: path}, NewNMData("5")); err != nil { t.Error(err) } path = PathItems{{Field: "Field2", Index: IntPointer(1)}, {Field: "Account"}} @@ -415,10 +423,22 @@ func TestOrderedNavigableMapFieldAsString(t *testing.T) { func TestOrderedNavigableMapGetOrder(t *testing.T) { nm := NewOrderedNavigableMap() - nm.Set(PathItems{{Field: "Field1"}, {Field: "Field2", Index: IntPointer(0)}}, NewNMData("1003")) - nm.Set(PathItems{{Field: "Field1"}, {Field: "Field2", Index: IntPointer(1)}}, NewNMData("Val")) - nm.Set(PathItems{{Field: "Field3"}, {Field: "Field4"}, {Field: "Field5", Index: IntPointer(0)}}, NewNMData("1001")) - nm.Set(PathItems{{Field: "Field1"}, {Field: "Field2", Index: IntPointer(2)}}, NewNMData(101)) + nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field1"}, {Field: "Field2", Index: IntPointer(0)}}, + Path: "Field1.Field2[0]", + }, NewNMData("1003")) + nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field1"}, {Field: "Field2", Index: IntPointer(1)}}, + Path: "Field1.Field2[1]", + }, NewNMData("Val")) + nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field3"}, {Field: "Field4"}, {Field: "Field5", Index: IntPointer(0)}}, + Path: "Field3.Field4.Field5", + }, NewNMData("1001")) + nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field1"}, {Field: "Field2", Index: IntPointer(2)}}, + Path: "Field1.Field2[2]", + }, NewNMData(101)) expected := []PathItems{ {{Field: "Field1"}, {Field: "Field2", Index: IntPointer(0)}}, {{Field: "Field1"}, {Field: "Field2", Index: IntPointer(1)}}, @@ -437,14 +457,23 @@ func TestOrderedNavigableMapSet(t *testing.T) { if _, err := nm.Set(nil, nil); err != ErrWrongPath { t.Error(err) } - if _, err := nm.Set(PathItems{{Field: "Field1", Index: IntPointer(10)}}, NewNMData("1001")); err != ErrWrongPath { + if _, err := nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field1", Index: IntPointer(10)}}, + Path: "Field1[10]", + }, NewNMData("1001")); err != ErrWrongPath { t.Error(err) } - if _, err := nm.Set(PathItems{{Field: "Field1", Index: IntPointer(10)}, {Field: "Field2"}}, NewNMData("1001")); err != ErrWrongPath { + if _, err := nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field1", Index: IntPointer(10)}, {Field: "Field2"}}, + Path: "Field1[10].Field2", + }, NewNMData("1001")); err != ErrWrongPath { t.Error(err) } path := PathItems{{Field: "Field1", Index: IntPointer(0)}} - if addedNew, err := nm.Set(path, NewNMData("1001")); err != nil { + if addedNew, err := nm.Set(&FullPath{ + PathItems: path, + Path: path.String(), + }, NewNMData("1001")); err != nil { t.Error(err) } else if !addedNew { t.Error("Expected the field to be added new") @@ -457,23 +486,38 @@ func TestOrderedNavigableMapSet(t *testing.T) { if !reflect.DeepEqual(nm.GetOrder(), order) { t.Errorf("Expected %s ,received: %s", order, nm.GetOrder()) } - if _, err := nm.Set(PathItems{{Field: "Field1", Index: IntPointer(0)}, {}}, NewNMData("1001")); err != ErrWrongPath { + if _, err := nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field1", Index: IntPointer(0)}, {}}, + Path: "Field1[0]", + }, NewNMData("1001")); err != ErrWrongPath { t.Error(err) } - if _, err := nm.Set(PathItems{{Field: "Field1", Index: IntPointer(10)}}, NewNMData("1001")); err != ErrWrongPath { + if _, err := nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field1", Index: IntPointer(10)}}, + Path: "Field1[10]", + }, NewNMData("1001")); err != ErrWrongPath { t.Error(err) } - if _, err := nm.Set(PathItems{{Field: "Field1", Index: IntPointer(0)}, {}}, &NMSlice{}); err != ErrWrongPath { + if _, err := nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field1", Index: IntPointer(0)}, {}}, + Path: "Field1[0]", + }, &NMSlice{}); err != ErrWrongPath { t.Error(err) } - if _, err := nm.Set(PathItems{{Field: "Field1", Index: IntPointer(10)}}, &NMSlice{}); err != ErrWrongPath { + if _, err := nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field1", Index: IntPointer(10)}}, + Path: "Field[10]", + }, &NMSlice{}); err != ErrWrongPath { t.Error(err) } nMap = NavigableMap2{"Field1": &NMSlice{NewNMData("1002")}} order = []PathItems{path} - if addedNew, err := nm.Set(path, NewNMData("1002")); err != nil { + if addedNew, err := nm.Set(&FullPath{ + PathItems: path, + Path: path.String(), + }, NewNMData("1002")); err != nil { t.Error(err) } else if addedNew { t.Error("Expected the field to be only updated") @@ -490,7 +534,10 @@ func TestOrderedNavigableMapSet(t *testing.T) { "Field2": NewNMData("1002"), } order = append(order, path) - if addedNew, err := nm.Set(path, NewNMData("1002")); err != nil { + if addedNew, err := nm.Set(&FullPath{ + PathItems: path, + Path: path.String(), + }, NewNMData("1002")); err != nil { t.Error(err) } else if !addedNew { t.Error("Expected the field to be added new") @@ -507,7 +554,10 @@ func TestOrderedNavigableMapSet(t *testing.T) { "Field2": NewNMData("1002"), } order = append(order, path) - if addedNew, err := nm.Set(path, NewNMData("1003")); err != nil { + if addedNew, err := nm.Set(&FullPath{ + PathItems: path, + Path: path.String(), + }, NewNMData("1003")); err != nil { t.Error(err) } else if !addedNew { t.Error("Expected the field to be added new") @@ -526,7 +576,10 @@ func TestOrderedNavigableMapSet(t *testing.T) { "Field3": obj, } order = append(order, PathItems{{Field: "Field3", Index: IntPointer(0)}}, PathItems{{Field: "Field3", Index: IntPointer(1)}}) - if addedNew, err := nm.Set(path, obj); err != nil { + if addedNew, err := nm.Set(&FullPath{ + PathItems: path, + Path: path.String(), + }, obj); err != nil { t.Error(err) } else if !addedNew { t.Error("Expected the field to be added new") @@ -550,7 +603,10 @@ func TestOrderedNavigableMapSet(t *testing.T) { {{Field: "Field1", Index: IntPointer(0)}}, {{Field: "Field1", Index: IntPointer(1)}}, } - if addedNew, err := nm.Set(PathItems{{Field: "Field1"}}, obj); err != nil { + if addedNew, err := nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field1"}}, + Path: "Field1", + }, obj); err != nil { t.Error(err) } else if addedNew { t.Error("Expected the field to be only updated") @@ -574,7 +630,10 @@ func TestOrderedNavigableMapSet(t *testing.T) { {{Field: "Field1", Index: IntPointer(1)}}, {{Field: "Field3", Index: IntPointer(1)}}, } - if addedNew, err := nm.Set(PathItems{{Field: "Field3", Index: IntPointer(-1)}}, NewNMData("1007")); err != nil { + if addedNew, err := nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field3", Index: IntPointer(-1)}}, + Path: "Field3[-1]", + }, NewNMData("1007")); err != nil { t.Error(err) } else if addedNew { t.Error("Expected the field to be only updated") @@ -589,21 +648,33 @@ func TestOrderedNavigableMapSet(t *testing.T) { func TestOrderedNavigableMapRemove(t *testing.T) { nm := NewOrderedNavigableMap() - nm.Set(PathItems{{Field: "Field2"}}, NewNMData("1003")) - nm.Set(PathItems{{Field: "Field3"}, {Field: "Field4"}}, NewNMData("Val")) - nm.Set(PathItems{{Field: "Field1"}}, NewNMData("1001")) - nm.Set(PathItems{{Field: "Field5"}}, &NMSlice{NewNMData(10), NewNMData(101)}) - if err := nm.Remove(FullPath{}); err != ErrWrongPath { + nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field2"}}, + Path: "Field2", + }, NewNMData("1003")) + nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field3"}, {Field: "Field4"}}, + Path: "Field3.Field4", + }, NewNMData("Val")) + nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field1"}}, + Path: "Field1", + }, NewNMData("1001")) + nm.Set(&FullPath{ + PathItems: PathItems{{Field: "Field5"}}, + Path: "Field5", + }, &NMSlice{NewNMData(10), NewNMData(101)}) + if err := nm.Remove(&FullPath{}); err != ErrWrongPath { t.Error(err) } - if err := nm.Remove(FullPath{PathItems: PathItems{}}); err != ErrWrongPath { + if err := nm.Remove(&FullPath{PathItems: PathItems{}}); err != ErrWrongPath { t.Error(err) } - if err := nm.Remove(FullPath{PathItems: PathItems{{Field: "field"}}, Path: "field"}); err != nil { + if err := nm.Remove(&FullPath{PathItems: PathItems{{Field: "field"}}, Path: "field"}); err != nil { t.Error(err) } - if err := nm.Remove(FullPath{PathItems: PathItems{{Index: IntPointer(-1)}, {}}}); err != ErrWrongPath { + if err := nm.Remove(&FullPath{PathItems: PathItems{{Index: IntPointer(-1)}, {}}}); err != ErrWrongPath { t.Error(err) } nMap := NavigableMap2{ @@ -637,7 +708,7 @@ func TestOrderedNavigableMapRemove(t *testing.T) { {{Field: "Field5", Index: IntPointer(1)}}, } - if err := nm.Remove(FullPath{PathItems: PathItems{{Field: "Field2"}}, Path: "Field2"}); err != nil { + if err := nm.Remove(&FullPath{PathItems: PathItems{{Field: "Field2"}}, Path: "Field2"}); err != nil { t.Error(err) } if !reflect.DeepEqual(nm.nm, nMap) { @@ -655,7 +726,7 @@ func TestOrderedNavigableMapRemove(t *testing.T) { {{Field: "Field1"}}, } - if err := nm.Remove(FullPath{PathItems: PathItems{{Field: "Field5"}}, Path: "Field5"}); err != nil { + if err := nm.Remove(&FullPath{PathItems: PathItems{{Field: "Field5"}}, Path: "Field5"}); err != nil { t.Error(err) } if !reflect.DeepEqual(nm.nm, nMap) { @@ -664,7 +735,7 @@ func TestOrderedNavigableMapRemove(t *testing.T) { if !reflect.DeepEqual(nm.GetOrder(), order) { t.Errorf("Expected %s ,received: %s", order, nm.GetOrder()) } - if err := nm.Remove(FullPath{PathItems: PathItems{{Field: "Field1", Index: IntPointer(0)}, {}}}); err != ErrWrongPath { + if err := nm.Remove(&FullPath{PathItems: PathItems{{Field: "Field1", Index: IntPointer(0)}, {}}}); err != ErrWrongPath { t.Error(err) } } @@ -712,7 +783,7 @@ func BenchmarkOrderdNavigableMapSet2(b *testing.B) { b.ResetTimer() for n := 0; n < b.N; n++ { for _, data := range gen { - if _, err := nm.Set2(&FullPath{PathItems: data.pathItems, Path: data.strPath}, NewNMData(data.data)); err != nil { + if _, err := nm.Set(&FullPath{PathItems: data.pathItems, Path: data.strPath}, NewNMData(data.data)); err != nil { b.Log(err, data.path) } }