From f21ded2b77123b4b690157d7edd4230b00b80d8e Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Fri, 2 Mar 2018 23:23:06 -0800 Subject: [PATCH] Adds Available this is the database for available packages --- available.go | 40 ++++++++++++++++++++ available_test.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++ meta.go | 8 ++-- 3 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 available.go create mode 100644 available_test.go diff --git a/available.go b/available.go new file mode 100644 index 0000000..fc2d0a2 --- /dev/null +++ b/available.go @@ -0,0 +1,40 @@ +package pm + +import ( + "github.com/pkg/errors" +) + +// Name exists to document the keys in Available +type Name string + +// Version exists to document the keys in Available +type Version string + +// Available is the structure used to represent the collection of all packages +// that can be installed. +type Available map[Name]map[Version]Meta + +// Add inserts m into a. +func (a Available) Add(m Meta) error { + if _, err := m.Valid(); err != nil { + return errors.Wrap(err, "invalid meta") + } + + if _, ok := a[Name(m.Name)]; !ok { + a[m.Name] = map[Version]Meta{} + } + a[m.Name][m.Version] = m + return nil +} + +// Update inserts all data from o into a. +func (a Available) Update(o Available) error { + for _, vers := range o { + for _, m := range vers { + if err := a.Add(m); err != nil { + return errors.Wrap(err, "adding") + } + } + } + return nil +} diff --git a/available_test.go b/available_test.go new file mode 100644 index 0000000..18f466c --- /dev/null +++ b/available_test.go @@ -0,0 +1,95 @@ +package pm + +import ( + "errors" + "testing" +) + +func TestAvailableAdd(t *testing.T) { + + tests := []struct { + label string + m Meta + count int + err error + }{ + { + label: "good", + m: Meta{Name: "a", Version: "v1.0.0", Description: "test"}, + count: 1, + }, + { + label: "bad meta", + m: Meta{Name: "a"}, + count: 1, + err: errors.New("missing"), + }, + { + label: "dupe is last in", + m: Meta{Name: "a", Version: "v1.0.0", Description: "better version"}, + count: 1, + }, + { + label: "another good", + m: Meta{Name: "a", Version: "v1.0.0", Description: "better version"}, + count: 1, + }, + } + + a := Available{} + for _, test := range tests { + t.Run(test.label, func(t *testing.T) { + if err := a.Add(test.m); (err == nil) != (test.err == nil) { + t.Fatalf("adding meta%v", err) + } + + if got, want := len(a), test.count; got != want { + t.Fatalf("unexpected length after Add: got %v, want %v", got, want) + } + }) + } + + if got, want := a["a"]["v1.0.0"].Description, "better version"; got != want { + t.Fatalf("version: got %v, want %v", got, want) + } +} + +func TestAvailableUpdate(t *testing.T) { + a := Available{} + if err := a.Add(Meta{Name: "a", Version: "v1.0.0", Description: "test"}); err != nil { + t.Fatalf("add: %v", err) + } + if err := a.Add(Meta{Name: "b", Version: "v2.0.0", Description: "test"}); err != nil { + t.Fatalf("add: %v", err) + } + + b := Available{} + a.Update(b) + if got, want := len(a), 2; got != want { + t.Fatalf("len after empty update: got %v, want %v", got, want) + } + + if err := b.Add(Meta{Name: "a", Version: "v1.0.0", Description: "test last in"}); err != nil { + t.Fatalf("add: %v", err) + } + if err := b.Add(Meta{Name: "b", Version: "v2.1.0", Description: "test"}); err != nil { + t.Fatalf("add: %v", err) + } + + if err := a.Update(b); err != nil { + t.Fatalf("update: %v", err) + } + if got, want := len(a), 2; got != want { + t.Fatalf("len after update: got %v, want %v", got, want) + } + if got, want := len(a["a"]), 1; got != want { + t.Fatalf("len after update: got %v, want %v", got, want) + } + if got, want := len(a["b"]), 2; got != want { + t.Fatalf("len after update: got %v, want %v", got, want) + } + + if got, want := a["a"]["v1.0.0"].Description, "test last in"; got != want { + t.Fatalf("last in didn't override") + } +} diff --git a/meta.go b/meta.go index c542de8..3cebc04 100644 --- a/meta.go +++ b/meta.go @@ -3,10 +3,10 @@ package pm import "errors" type Meta struct { - Name string `json:"name"` - Version string `json:"version"` - Description string `json:"description"` - Namespace string `json:"namespace"` + Name Name `json:"name"` + Version Version `json:"version"` + Description string `json:"description"` + Namespace string `json:"namespace"` } func (m *Meta) Valid() (bool, error) {