From 32b2f6054d72f3779378cc573936ede40b1dc45c Mon Sep 17 00:00:00 2001 From: "Marcel M. Otte" Date: Mon, 30 Oct 2023 22:39:53 +0100 Subject: [PATCH] Fix tests for collections --- cop/collection.go | 31 +++++++++++++++++-------------- cop/collection_test.go | 22 +++++++++++++++++++--- globals/globals.go | 8 ++++++++ 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/cop/collection.go b/cop/collection.go index 4f806c5..852dc1a 100644 --- a/cop/collection.go +++ b/cop/collection.go @@ -28,7 +28,7 @@ type ProtocolCollectionList struct { // WriteCache updates the cache file of the available protocols func (pcl *ProtocolCollectionList) WriteCache() error { // [impl->dsn~protocol-collection-cache~0>>utest] - data, err := json.MarshalIndent(*pcl, "", " ") + data, err := json.MarshalIndent(pcl, "", " ") if err != nil { return err } @@ -45,7 +45,10 @@ func (pcl *ProtocolCollectionList) ReadCache() error { if err != nil { return err } + //fmt.Println(string(data)) err = json.Unmarshal(data, pcl) + data, err = json.MarshalIndent(pcl, "", " ") + fmt.Println(string(data)) return err } @@ -85,7 +88,7 @@ func GetCOP() *ProtocolCollectionList { // COP represents a Collection of Protocols type COP struct { - protocols map[string]ProtocolCollectionEntry + Protocols map[string]ProtocolCollectionEntry closed bool } @@ -93,7 +96,7 @@ type COP struct { type COPer interface { Open(string) error Close() error - Protocols() ([]ProtocolCollectionEntry, error) + GetProtocols() ([]ProtocolCollectionEntry, error) Add(*protocol.Protocol) error Sync() error Update(*protocol.Protocol) error @@ -145,14 +148,14 @@ func (fd *FileCOP) Close() error { return nil } -// Protocols returns the map of protocols or an error if the first read failed -func (fd *FileCOP) Protocols() ([]ProtocolCollectionEntry, error) { +// GetProtocols returns the map of protocols or an error if the first read failed +func (fd *FileCOP) GetProtocols() ([]ProtocolCollectionEntry, error) { if fd.closed { return nil, errors.New("COP closed, no protocols to deliver") } entries := []ProtocolCollectionEntry{} - if !fd.closed && len(fd.protocols) == 0 { - fd.protocols = map[string]ProtocolCollectionEntry{} + if !fd.closed && len(fd.Protocols) == 0 { + fd.Protocols = map[string]ProtocolCollectionEntry{} err := fd.Sync() if err != nil { return nil, err @@ -160,7 +163,7 @@ func (fd *FileCOP) Protocols() ([]ProtocolCollectionEntry, error) { } if entries == nil || len(entries) == 0 { entries = make([]ProtocolCollectionEntry, 0) - for _, v := range fd.protocols { + for _, v := range fd.Protocols { entries = append(entries, v) } } @@ -175,7 +178,7 @@ func (fd *FileCOP) Add(prot *protocol.Protocol) error { p := path.Join(fd.Path, prot.Metadata.Name, ".protocoljson") err := prot.Save(p) if err == nil { - fd.protocols[prot.Metadata.Name] = ProtocolCollectionEntry{p, prot.Metadata} + fd.Protocols[prot.Metadata.Name] = ProtocolCollectionEntry{p, prot.Metadata} } return err } @@ -185,7 +188,7 @@ func (fd *FileCOP) Update(prot *protocol.Protocol) error { if fd.closed { return errors.New("Cannot update, FileCOP is closed") } - if v, ok := fd.protocols[prot.Metadata.Name]; ok { + if v, ok := fd.Protocols[prot.Metadata.Name]; ok { v.Protocol = prot.Metadata return prot.Save(v.Path) } @@ -198,8 +201,8 @@ func (fd *FileCOP) Sync() error { return errors.New("Cannot sync, FileCOP is closed") } // recurse recursively through the path - if fd.protocols == nil { - fd.protocols = make(map[string]ProtocolCollectionEntry) + if fd.Protocols == nil { + fd.Protocols = make(map[string]ProtocolCollectionEntry) } err := filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error { if !info.IsDir() && strings.HasSuffix(info.Name(), ".protocoljson") { @@ -209,7 +212,7 @@ func (fd *FileCOP) Sync() error { // add to map entry := ProtocolCollectionEntry{path, prot.Metadata} - fd.protocols[prot.Metadata.Name] = entry + fd.Protocols[prot.Metadata.Name] = entry } return err } @@ -220,6 +223,6 @@ func (fd *FileCOP) Sync() error { // Get a protocol by name func (fd *FileCOP) Get(prot string) (*protocol.Protocol, error) { - entry := fd.protocols[prot] + entry := fd.Protocols[prot] return protocol.LoadNew(entry.Path) } diff --git a/cop/collection_test.go b/cop/collection_test.go index 1e45973..a4d9c88 100644 --- a/cop/collection_test.go +++ b/cop/collection_test.go @@ -68,7 +68,7 @@ func TestOpen(t *testing.T) { t.Fail() } fcop.Open(path.Join(wd, "test", "cop")) - l, err := fcop.Protocols() + l, err := fcop.GetProtocols() if err != nil { t.Fail() } @@ -89,7 +89,7 @@ func TestClosed(t *testing.T) { } fcop.Open(path.Join(wd, "test", "cop")) fcop.Close() - l, err := fcop.Protocols() + l, err := fcop.GetProtocols() if err == nil || l != nil { fmt.Println("Protocols did not deliver error") t.Fail() @@ -140,8 +140,16 @@ func TestGlobalCOP(t *testing.T) { func TestCOPCache(t *testing.T) { td := setupSuite(t) defer td(t) - gcop := cop.GlobalCOP + // reset to zero because of other tests + gcop.PCs = []cop.COP{} + cop.Init() + c := gcop.PCs[0] + protos := c.Protocols + if len(protos) != 4 { + t.Fail() + } + err := gcop.WriteCache() if err != nil { fmt.Println("Writing the cache file failed") @@ -153,10 +161,18 @@ func TestCOPCache(t *testing.T) { t.Fail() } + // reset again and check if reading the cache works + gcop.PCs = []cop.COP{} + err = gcop.ReadCache() if err != nil { fmt.Println("Reading the cache file failed") t.Fail() } + c = gcop.PCs[0] + protos = c.Protocols + if len(protos) != 4 { + t.Fail() + } } diff --git a/globals/globals.go b/globals/globals.go index bd5c576..27bbc8b 100644 --- a/globals/globals.go +++ b/globals/globals.go @@ -23,3 +23,11 @@ var COPCacheFileName string = "cop-cache.json" // Global Mockings var MockUserCurrent = user.Current + +// Marshaler interface to enrich structs which can be exported/imported even with private fields +/* +type Marshaler interface { + toJSON() ([]byte, error) + fromJSON() error +} +*/