Add attachments field to one

master
Bel LaPointe 2020-07-24 11:21:16 -06:00
parent ed3006d359
commit 99fb3bb1c3
5 changed files with 72 additions and 19 deletions

View File

@ -129,6 +129,9 @@ func TestBoltDBFind(t *testing.T) {
if o.Modified == 0 { if o.Modified == 0 {
t.Error(o.Modified) t.Error(o.Modified)
} }
if len(o.Attachments) == 0 {
t.Error(o.Attachments)
}
if len(o.Connections) == 0 { if len(o.Connections) == 0 {
t.Error(o.Connections) t.Error(o.Connections)
} }
@ -367,6 +370,12 @@ func fillBoltDB(t *testing.T, bdb *BoltDB) {
return err return err
} }
for i := 0; i < testN; i++ { for i := 0; i < testN; i++ {
p := entity.One{
Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5],
Relationship: "rshp-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5],
}
o := entity.One{ o := entity.One{
Name: "name-" + uuid.New().String()[:5], Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5], Type: "type-" + uuid.New().String()[:5],
@ -374,15 +383,9 @@ func fillBoltDB(t *testing.T, bdb *BoltDB) {
Image: "imge-" + uuid.New().String()[:5], Image: "imge-" + uuid.New().String()[:5],
Text: "text-" + uuid.New().String()[:5], Text: "text-" + uuid.New().String()[:5],
Modified: time.Now().UnixNano(), Modified: time.Now().UnixNano(),
Connections: map[string]entity.One{}, Connections: map[string]entity.One{p.Name: p},
Attachments: map[string]string{"filename": "/path/to/file"},
} }
p := entity.One{
Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5],
Relationship: "rshp-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5],
}
o.Connections[p.Name] = p
b, err := bson.Marshal(o) b, err := bson.Marshal(o)
if err != nil { if err != nil {
return err return err

View File

@ -18,6 +18,7 @@ const (
Text = "text" Text = "text"
Modified = "modified" Modified = "modified"
Connections = "connections" Connections = "connections"
Attachments = "attachments"
) )
type One struct { type One struct {
@ -29,6 +30,7 @@ type One struct {
Relationship string `bson:"relationship,omitempty" json:"relationship,omitempty"` Relationship string `bson:"relationship,omitempty" json:"relationship,omitempty"`
Modified int64 `bson:"modified,omitempty" json:"modified,omitempty"` Modified int64 `bson:"modified,omitempty" json:"modified,omitempty"`
Connections map[string]One `bson:"connections,omitempty" json:"connections,omitempty"` Connections map[string]One `bson:"connections,omitempty" json:"connections,omitempty"`
Attachments map[string]string `bson:"attachments,omitempty" json:"attachments,omitempty"`
} }
func (o One) Query() One { func (o One) Query() One {

View File

@ -28,10 +28,13 @@ func TestOneMarshalBSON(t *testing.T) {
one: (One{Name: "hello", Type: "world", Modified: 1}).Query(), one: (One{Name: "hello", Type: "world", Modified: 1}).Query(),
}, },
"modified changes": { "modified changes": {
one: (One{Name: "hello", Type: "world", Modified: 1}), one: One{Name: "hello", Type: "world", Modified: 1},
}, },
"w/ connections": { "w/ connections": {
one: (One{Name: "hello", Type: "world", Modified: 1, Connections: map[string]One{"hi": One{Name: "hi", Relationship: "mom"}}}), one: One{Name: "hello", Type: "world", Modified: 1, Connections: map[string]One{"hi": One{Name: "hi", Relationship: "mom"}}},
},
"w/ attachments": {
one: One{Name: "hello", Type: "world", Modified: 1, Attachments: map[string]string{"hello": "/world"}},
}, },
} }

View File

@ -140,6 +140,46 @@ func TestIntegration(t *testing.T) {
t.Fatal(some2[0].Peers()) t.Fatal(some2[0].Peers())
} }
}) })
t.Run("graph.Update(new attachment), Update(--new attachment)", func(t *testing.T) {
err := graph.Update(ctx, "col", ones[0].Query(), operator.Set{Key: fmt.Sprintf("%s.new attachment", entity.Attachments), Value: "my new attachment"})
if err != nil {
t.Fatal(err)
}
some1, err := graph.List(ctx, "col", ones[0].Name)
if err != nil {
t.Fatal(err)
}
t.Logf("sm1 = %+v", some1[0])
if len(some1) != 1 {
t.Fatal(len(some1))
}
if v, ok := some1[0].Attachments["new attachment"]; !ok {
t.Fatal(ok, some1[0].Attachments)
} else if v != "my new attachment" {
t.Fatal(v, some1[0].Attachments)
}
err = graph.Update(ctx, "col", ones[0].Query(), operator.Unset(fmt.Sprintf("%s.new attachment", entity.Attachments)))
if err != nil {
t.Fatal(err)
}
some2, err := graph.List(ctx, "col", ones[0].Name)
if err != nil {
t.Fatal(err)
}
t.Logf("sm1 = %+v", some2[0])
if len(some2) != 1 {
t.Fatal(len(some2))
}
if _, ok := some2[0].Attachments["new attachment"]; ok {
t.Fatal(ok, some2[0].Attachments)
} else if len(some2[0].Attachments) == 0 {
t.Fatal(len(some2[0].Attachments), some2[0].Attachments)
}
})
} }
func randomOne() entity.One { func randomOne() entity.One {
@ -151,5 +191,9 @@ func randomOne() entity.One {
Text: "tee hee xd", Text: "tee hee xd",
Modified: time.Now().UnixNano(), Modified: time.Now().UnixNano(),
Connections: map[string]entity.One{}, Connections: map[string]entity.One{},
Attachments: map[string]string{
"pdf file": "/path/to.pdf",
"png file": "/path/to.png",
},
} }
} }

View File

@ -3,6 +3,7 @@ package view
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"local/dndex/storage" "local/dndex/storage"
"local/dndex/storage/entity" "local/dndex/storage/entity"
@ -176,7 +177,7 @@ func whoPatch(namespace string, g storage.Graph, w http.ResponseWriter, r *http.
return err return err
} }
one.Relationship = relationship one.Relationship = relationship
if err := g.Update(r.Context(), namespace, entity.One{Name: id}, operator.Set{Key: "connections." + one.Name, Value: one.Peer()}); err != nil { if err := g.Update(r.Context(), namespace, entity.One{Name: id}, operator.Set{Key: fmt.Sprintf("%s.%s", entity.Connections, one.Name), Value: one.Peer()}); err != nil {
return err return err
} }