Fix tests for collections
Go / lint (push) Failing after 1m7s Details
Go / build (push) Failing after 2m53s Details

This commit is contained in:
Marcel M. Otte 2023-10-30 22:39:53 +01:00
parent 0e93087a83
commit 32b2f6054d
3 changed files with 44 additions and 17 deletions

View File

@ -28,7 +28,7 @@ type ProtocolCollectionList struct {
// WriteCache updates the cache file of the available protocols // WriteCache updates the cache file of the available protocols
func (pcl *ProtocolCollectionList) WriteCache() error { func (pcl *ProtocolCollectionList) WriteCache() error {
// [impl->dsn~protocol-collection-cache~0>>utest] // [impl->dsn~protocol-collection-cache~0>>utest]
data, err := json.MarshalIndent(*pcl, "", " ") data, err := json.MarshalIndent(pcl, "", " ")
if err != nil { if err != nil {
return err return err
} }
@ -45,7 +45,10 @@ func (pcl *ProtocolCollectionList) ReadCache() error {
if err != nil { if err != nil {
return err return err
} }
//fmt.Println(string(data))
err = json.Unmarshal(data, pcl) err = json.Unmarshal(data, pcl)
data, err = json.MarshalIndent(pcl, "", " ")
fmt.Println(string(data))
return err return err
} }
@ -85,7 +88,7 @@ func GetCOP() *ProtocolCollectionList {
// COP represents a Collection of Protocols // COP represents a Collection of Protocols
type COP struct { type COP struct {
protocols map[string]ProtocolCollectionEntry Protocols map[string]ProtocolCollectionEntry
closed bool closed bool
} }
@ -93,7 +96,7 @@ type COP struct {
type COPer interface { type COPer interface {
Open(string) error Open(string) error
Close() error Close() error
Protocols() ([]ProtocolCollectionEntry, error) GetProtocols() ([]ProtocolCollectionEntry, error)
Add(*protocol.Protocol) error Add(*protocol.Protocol) error
Sync() error Sync() error
Update(*protocol.Protocol) error Update(*protocol.Protocol) error
@ -145,14 +148,14 @@ func (fd *FileCOP) Close() error {
return nil return nil
} }
// Protocols returns the map of protocols or an error if the first read failed // GetProtocols returns the map of protocols or an error if the first read failed
func (fd *FileCOP) Protocols() ([]ProtocolCollectionEntry, error) { func (fd *FileCOP) GetProtocols() ([]ProtocolCollectionEntry, error) {
if fd.closed { if fd.closed {
return nil, errors.New("COP closed, no protocols to deliver") return nil, errors.New("COP closed, no protocols to deliver")
} }
entries := []ProtocolCollectionEntry{} entries := []ProtocolCollectionEntry{}
if !fd.closed && len(fd.protocols) == 0 { if !fd.closed && len(fd.Protocols) == 0 {
fd.protocols = map[string]ProtocolCollectionEntry{} fd.Protocols = map[string]ProtocolCollectionEntry{}
err := fd.Sync() err := fd.Sync()
if err != nil { if err != nil {
return nil, err return nil, err
@ -160,7 +163,7 @@ func (fd *FileCOP) Protocols() ([]ProtocolCollectionEntry, error) {
} }
if entries == nil || len(entries) == 0 { if entries == nil || len(entries) == 0 {
entries = make([]ProtocolCollectionEntry, 0) entries = make([]ProtocolCollectionEntry, 0)
for _, v := range fd.protocols { for _, v := range fd.Protocols {
entries = append(entries, v) 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") p := path.Join(fd.Path, prot.Metadata.Name, ".protocoljson")
err := prot.Save(p) err := prot.Save(p)
if err == nil { if err == nil {
fd.protocols[prot.Metadata.Name] = ProtocolCollectionEntry{p, prot.Metadata} fd.Protocols[prot.Metadata.Name] = ProtocolCollectionEntry{p, prot.Metadata}
} }
return err return err
} }
@ -185,7 +188,7 @@ func (fd *FileCOP) Update(prot *protocol.Protocol) error {
if fd.closed { if fd.closed {
return errors.New("Cannot update, FileCOP is 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 v.Protocol = prot.Metadata
return prot.Save(v.Path) return prot.Save(v.Path)
} }
@ -198,8 +201,8 @@ func (fd *FileCOP) Sync() error {
return errors.New("Cannot sync, FileCOP is closed") return errors.New("Cannot sync, FileCOP is closed")
} }
// recurse recursively through the path // recurse recursively through the path
if fd.protocols == nil { if fd.Protocols == nil {
fd.protocols = make(map[string]ProtocolCollectionEntry) fd.Protocols = make(map[string]ProtocolCollectionEntry)
} }
err := filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error { err := filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error {
if !info.IsDir() && strings.HasSuffix(info.Name(), ".protocoljson") { if !info.IsDir() && strings.HasSuffix(info.Name(), ".protocoljson") {
@ -209,7 +212,7 @@ func (fd *FileCOP) Sync() error {
// add to map // add to map
entry := ProtocolCollectionEntry{path, prot.Metadata} entry := ProtocolCollectionEntry{path, prot.Metadata}
fd.protocols[prot.Metadata.Name] = entry fd.Protocols[prot.Metadata.Name] = entry
} }
return err return err
} }
@ -220,6 +223,6 @@ func (fd *FileCOP) Sync() error {
// Get a protocol by name // Get a protocol by name
func (fd *FileCOP) Get(prot string) (*protocol.Protocol, error) { func (fd *FileCOP) Get(prot string) (*protocol.Protocol, error) {
entry := fd.protocols[prot] entry := fd.Protocols[prot]
return protocol.LoadNew(entry.Path) return protocol.LoadNew(entry.Path)
} }

View File

@ -68,7 +68,7 @@ func TestOpen(t *testing.T) {
t.Fail() t.Fail()
} }
fcop.Open(path.Join(wd, "test", "cop")) fcop.Open(path.Join(wd, "test", "cop"))
l, err := fcop.Protocols() l, err := fcop.GetProtocols()
if err != nil { if err != nil {
t.Fail() t.Fail()
} }
@ -89,7 +89,7 @@ func TestClosed(t *testing.T) {
} }
fcop.Open(path.Join(wd, "test", "cop")) fcop.Open(path.Join(wd, "test", "cop"))
fcop.Close() fcop.Close()
l, err := fcop.Protocols() l, err := fcop.GetProtocols()
if err == nil || l != nil { if err == nil || l != nil {
fmt.Println("Protocols did not deliver error") fmt.Println("Protocols did not deliver error")
t.Fail() t.Fail()
@ -140,8 +140,16 @@ func TestGlobalCOP(t *testing.T) {
func TestCOPCache(t *testing.T) { func TestCOPCache(t *testing.T) {
td := setupSuite(t) td := setupSuite(t)
defer td(t) defer td(t)
gcop := cop.GlobalCOP 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() err := gcop.WriteCache()
if err != nil { if err != nil {
fmt.Println("Writing the cache file failed") fmt.Println("Writing the cache file failed")
@ -153,10 +161,18 @@ func TestCOPCache(t *testing.T) {
t.Fail() t.Fail()
} }
// reset again and check if reading the cache works
gcop.PCs = []cop.COP{}
err = gcop.ReadCache() err = gcop.ReadCache()
if err != nil { if err != nil {
fmt.Println("Reading the cache file failed") fmt.Println("Reading the cache file failed")
t.Fail() t.Fail()
} }
c = gcop.PCs[0]
protos = c.Protocols
if len(protos) != 4 {
t.Fail()
}
} }

View File

@ -23,3 +23,11 @@ var COPCacheFileName string = "cop-cache.json"
// Global Mockings // Global Mockings
var MockUserCurrent = user.Current 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
}
*/