added some tests for the json storage.

This commit is contained in:
Stephen McQuay 2016-02-15 01:38:11 -08:00
parent 9e9bb74c4a
commit 456d502b72
4 changed files with 96 additions and 13 deletions

View File

@ -11,7 +11,7 @@ import (
)
func TestAdd(t *testing.T) {
ms := NewMemStore("")
ms := NewSimpleStore("")
s := &Server{
storage: ms,
}
@ -78,7 +78,7 @@ func TestAdd(t *testing.T) {
}
func TestInvalidPath(t *testing.T) {
ms := NewMemStore("")
ms := NewSimpleStore("")
s := &Server{
storage: ms,
}
@ -98,7 +98,7 @@ func TestInvalidPath(t *testing.T) {
}
func TestCannotDuplicateExistingPath(t *testing.T) {
ms := NewMemStore("")
ms := NewSimpleStore("")
s := &Server{
storage: ms,
}
@ -123,7 +123,7 @@ func TestCannotDuplicateExistingPath(t *testing.T) {
}
func TestCannotAddExistingSubPath(t *testing.T) {
ms := NewMemStore("")
ms := NewSimpleStore("")
s := &Server{
storage: ms,
}
@ -151,7 +151,7 @@ func TestCannotAddExistingSubPath(t *testing.T) {
}
func TestMissingRepo(t *testing.T) {
ms := NewMemStore("")
ms := NewSimpleStore("")
s := &Server{
storage: ms,
}
@ -171,7 +171,7 @@ func TestMissingRepo(t *testing.T) {
}
func TestBadJson(t *testing.T) {
ms := NewMemStore("")
ms := NewSimpleStore("")
s := &Server{
storage: ms,
}
@ -191,7 +191,7 @@ func TestBadJson(t *testing.T) {
}
func TestUnsupportedMethod(t *testing.T) {
ms := NewMemStore("")
ms := NewSimpleStore("")
s := &Server{
storage: ms,
}
@ -213,7 +213,7 @@ func TestUnsupportedMethod(t *testing.T) {
}
func TestNewServer(t *testing.T) {
ms := NewMemStore("")
ms := NewSimpleStore("")
sm := http.NewServeMux()
s := NewServer(sm, ms, "foo")
ts := httptest.NewServer(s)

View File

@ -96,10 +96,9 @@ func main() {
}
log.Printf("serving at: http://%s:%d/", hostname, c.Port)
sm := http.NewServeMux()
ms := vain.NewMemStore(c.DB)
ms := vain.NewSimpleStore(c.DB)
if err := ms.Load(); err != nil {
log.Printf("unable to load db: %v", err)
os.Exit(1)
log.Printf("unable to load db: %v; creating fresh database", err)
}
vain.NewServer(sm, ms, c.Host)
addr := fmt.Sprintf(":%d", c.Port)

View File

@ -35,8 +35,8 @@ type SimpleStore struct {
path string
}
// NewMemStore returns a ready-to-use SimpleStore storing json at path.
func NewMemStore(path string) *SimpleStore {
// NewSimpleStore returns a ready-to-use SimpleStore storing json at path.
func NewSimpleStore(path string) *SimpleStore {
return &SimpleStore{
path: path,
p: make(map[string]Package),

84
storage_test.go Normal file
View File

@ -0,0 +1,84 @@
package vain
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func equiv(a, b map[string]Package) (bool, []error) {
equiv := true
errs := []error{}
if got, want := len(a), len(b); got != want {
equiv = false
errs = append(errs, fmt.Errorf("uncorrect number of elements: got %d, want %d", got, want))
return false, errs
}
for k := range a {
v, ok := b[k]
if !ok || v != a[k] {
errs = append(errs, fmt.Errorf("missing key: %s", k))
equiv = false
break
}
}
return equiv, errs
}
func TestSimpleStorage(t *testing.T) {
root, err := ioutil.TempDir("", "vain-")
if err != nil {
t.Fatalf("problem creating temp dir: %v", err)
}
defer func() { os.RemoveAll(root) }()
db := filepath.Join(root, "vain.json")
ms := NewSimpleStore(db)
orig := map[string]Package{
"foo": {},
"bar": {},
"baz": {},
}
ms.p = orig
if err := ms.Save(); err != nil {
t.Errorf("should have been able to Save: %v", err)
}
ms.p = map[string]Package{}
if err := ms.Load(); err != nil {
t.Errorf("should have been able to Load: %v", err)
}
if ok, errs := equiv(orig, ms.p); !ok {
for _, err := range errs {
t.Error(err)
}
}
}
func TestRemove(t *testing.T) {
root, err := ioutil.TempDir("", "vain-")
if err != nil {
t.Fatalf("problem creating temp dir: %v", err)
}
defer func() { os.RemoveAll(root) }()
db := filepath.Join(root, "vain.json")
ms := NewSimpleStore(db)
ms.p = map[string]Package{
"foo": {},
"bar": {},
"baz": {},
}
if err := ms.Remove("foo"); err != nil {
t.Errorf("unexpected error during remove: %v", err)
}
want := map[string]Package{
"bar": {},
"baz": {},
}
if ok, errs := equiv(ms.p, want); !ok {
for _, err := range errs {
t.Error(err)
}
}
}