Commit aedf02b1 authored by Tommi Virtanen's avatar Tommi Virtanen

Implement mount Has

parent cf78b943
...@@ -62,7 +62,11 @@ func (d *Datastore) Get(key datastore.Key) (value interface{}, err error) { ...@@ -62,7 +62,11 @@ func (d *Datastore) Get(key datastore.Key) (value interface{}, err error) {
} }
func (d *Datastore) Has(key datastore.Key) (exists bool, err error) { func (d *Datastore) Has(key datastore.Key) (exists bool, err error) {
return false, errors.New("TODO") ds, k := d.lookup(key)
if ds == nil {
return false, nil
}
return ds.Has(k)
} }
func (d *Datastore) Delete(key datastore.Key) error { func (d *Datastore) Delete(key datastore.Key) error {
......
...@@ -107,3 +107,64 @@ func TestGet(t *testing.T) { ...@@ -107,3 +107,64 @@ func TestGet(t *testing.T) {
t.Errorf("wrong value: %q != %q", g, e) t.Errorf("wrong value: %q != %q", g, e)
} }
} }
func TestHasBadNothing(t *testing.T) {
m := mount.New([]mount.Mount{})
found, err := m.Has(datastore.NewKey("/quux/thud"))
if err != nil {
t.Fatalf("Has error: %v", err)
}
if g, e := found, false; g != e {
t.Fatalf("wrong value: %v != %v", g, e)
}
}
func TestHasBadNoMount(t *testing.T) {
mapds := datastore.NewMapDatastore()
m := mount.New([]mount.Mount{
{Prefix: datastore.NewKey("/redherring"), Datastore: mapds},
})
found, err := m.Has(datastore.NewKey("/quux/thud"))
if err != nil {
t.Fatalf("Has error: %v", err)
}
if g, e := found, false; g != e {
t.Fatalf("wrong value: %v != %v", g, e)
}
}
func TestHasNotFound(t *testing.T) {
mapds := datastore.NewMapDatastore()
m := mount.New([]mount.Mount{
{Prefix: datastore.NewKey("/quux"), Datastore: mapds},
})
found, err := m.Has(datastore.NewKey("/quux/thud"))
if err != nil {
t.Fatalf("Has error: %v", err)
}
if g, e := found, false; g != e {
t.Fatalf("wrong value: %v != %v", g, e)
}
}
func TestHas(t *testing.T) {
mapds := datastore.NewMapDatastore()
m := mount.New([]mount.Mount{
{Prefix: datastore.NewKey("/quux"), Datastore: mapds},
})
if err := mapds.Put(datastore.NewKey("/thud"), []byte("foobar")); err != nil {
t.Fatalf("Put error: %v", err)
}
found, err := m.Has(datastore.NewKey("/quux/thud"))
if err != nil {
t.Fatalf("Has error: %v", err)
}
if g, e := found, true; g != e {
t.Fatalf("wrong value: %v != %v", g, e)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment