Support primary key ID and unique key Name where api uses name

master
breel 2020-08-02 09:59:47 -06:00
parent 37fe9415e7
commit 8e6e86955e
14 changed files with 68 additions and 49 deletions

View File

@ -124,9 +124,9 @@ func (bdb *BoltDB) Insert(ctx context.Context, namespace string, doc interface{}
if err := bson.Unmarshal(b, &m); err != nil { if err := bson.Unmarshal(b, &m); err != nil {
return err return err
} }
if _, ok := m[entity.Name]; !ok { if _, ok := m[entity.ID]; !ok {
return errors.New("primary key required to insert: did not find " + entity.Name) return errors.New("primary key required to insert: did not find " + entity.ID)
} else if _, ok := m[entity.Name].(string); !ok { } else if _, ok := m[entity.ID].(string); !ok {
return errors.New("primary key must be a string") return errors.New("primary key must be a string")
} }
return bdb.db.Update(func(tx *bolt.Tx) error { return bdb.db.Update(func(tx *bolt.Tx) error {
@ -134,7 +134,7 @@ func (bdb *BoltDB) Insert(ctx context.Context, namespace string, doc interface{}
if err != nil { if err != nil {
return err return err
} }
k := []byte(m[entity.Name].(string)) k := []byte(m[entity.ID].(string))
v := bucket.Get(k) v := bucket.Get(k)
if len(v) > 0 { if len(v) > 0 {
return errors.New("cannot insert: collision on primary key") return errors.New("cannot insert: collision on primary key")
@ -272,7 +272,7 @@ func apply(doc bson.M, operator interface{}) (bson.M, error) {
func applyUnset(doc, operator bson.M) (bson.M, error) { func applyUnset(doc, operator bson.M) (bson.M, error) {
for k := range operator { for k := range operator {
if k == entity.Name { if k == entity.ID {
continue continue
} }
nesting := strings.Split(k, ".") nesting := strings.Split(k, ".")
@ -305,7 +305,7 @@ func applyUnset(doc, operator bson.M) (bson.M, error) {
func applySet(doc, operator bson.M) (bson.M, error) { func applySet(doc, operator bson.M) (bson.M, error) {
for k, v := range operator { for k, v := range operator {
if k == entity.Name { if k == entity.ID {
continue continue
} }
nesting := strings.Split(k, ".") nesting := strings.Split(k, ".")

View File

@ -283,15 +283,16 @@ func TestBoltDBInsert(t *testing.T) {
t.Fatal("could insert colliding object:", err) t.Fatal("could insert colliding object:", err)
} }
ones[0].Name = "NEWNAME" ones[0].ID = "NEWID"
ones[0].Name = "NEWID"
if err := driver.Insert(context.TODO(), testNS, ones[0]); err != nil { if err := driver.Insert(context.TODO(), testNS, ones[0]); err != nil {
t.Fatal("could not insert object with new Name:", err) t.Fatal("could not insert object with new ID:", err)
} }
if n, err := driver.Count(context.TODO(), testNS, ones[0].Query()); err != nil { if n, err := driver.Count(context.TODO(), testNS, ones[0].Query()); err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 1 { } else if n != 1 {
t.Fatal(err) t.Fatal(n, err)
} }
ch, err = driver.Find(context.TODO(), testNS, ones[0].Query()) ch, err = driver.Find(context.TODO(), testNS, ones[0].Query())
@ -400,25 +401,27 @@ func fillBoltDB(t *testing.T, bdb *BoltDB) {
} }
for i := 0; i < testN; i++ { for i := 0; i < testN; i++ {
p := entity.One{ p := entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5], Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5], Type: "type-" + uuid.New().String()[:5],
Relationship: "rshp-" + uuid.New().String()[:5], Relationship: "rshp-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5], Title: "titl-" + uuid.New().String()[:5],
} }
o := entity.One{ o := entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5], Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5], Type: "type-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5], Title: "titl-" + 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{p.Name: p}, Connections: map[string]entity.One{p.ID: p},
Attachments: map[string]string{"filename": "/path/to/file"}, Attachments: map[string]string{"filename": "/path/to/file"},
} }
b, err := bson.Marshal(o) b, err := bson.Marshal(o)
if err != nil { if err != nil {
return err return err
} }
if err := bucket.Put([]byte(o.Name), b); err != nil { if err := bucket.Put([]byte(o.ID), b); err != nil {
return err return err
} }
} }

View File

@ -106,18 +106,18 @@ func (mp *Map) Insert(_ context.Context, namespace string, doc interface{}) erro
if err := bson.Unmarshal(b, &m); err != nil { if err := bson.Unmarshal(b, &m); err != nil {
return err return err
} }
if _, ok := m[entity.Name]; !ok { if _, ok := m[entity.ID]; !ok {
return errors.New("primary key required to insert: did not find " + entity.Name) return errors.New("primary key required to insert: did not find " + entity.ID)
} else if _, ok := m[entity.Name].(string); !ok { } else if _, ok := m[entity.ID].(string); !ok {
return errors.New("primary key must be a string") return errors.New("primary key must be a string")
} }
if _, ok := mp.db[namespace][m[entity.Name].(string)]; ok { if _, ok := mp.db[namespace][m[entity.ID].(string)]; ok {
return errors.New("cannot insert: collision on primary key") return errors.New("cannot insert: collision on primary key")
} }
if _, ok := mp.db[namespace]; !ok { if _, ok := mp.db[namespace]; !ok {
mp.db[namespace] = make(map[string][]byte) mp.db[namespace] = make(map[string][]byte)
} }
mp.db[namespace][m[entity.Name].(string)] = b mp.db[namespace][m[entity.ID].(string)] = b
return nil return nil
} }

View File

@ -14,25 +14,27 @@ func tempMap(t *testing.T) *Map {
mp.db[testNS] = map[string][]byte{} mp.db[testNS] = map[string][]byte{}
for i := 0; i < testN; i++ { for i := 0; i < testN; i++ {
p := entity.One{ p := entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5], Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5], Type: "type-" + uuid.New().String()[:5],
Relationship: "rshp-" + uuid.New().String()[:5], Relationship: "rshp-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5], Title: "titl-" + uuid.New().String()[:5],
} }
o := entity.One{ o := entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5], Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5], Type: "type-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5], Title: "titl-" + 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{p.Name: p}, Connections: map[string]entity.One{p.ID: p},
Attachments: map[string]string{"filename": "/path/to/file"}, Attachments: map[string]string{"filename": "/path/to/file"},
} }
b, err := bson.Marshal(o) b, err := bson.Marshal(o)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
mp.db[testNS][o.Name] = b mp.db[testNS][o.ID] = b
} }
return mp return mp
} }

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"local/dndex/config" "local/dndex/config"
"local/dndex/storage/entity"
"log" "log"
"time" "time"
@ -77,9 +78,9 @@ func (m Mongo) Insert(ctx context.Context, namespace string, apply interface{})
if err := bson.Unmarshal(b, &mapp); err != nil { if err := bson.Unmarshal(b, &mapp); err != nil {
return err return err
} }
if _, ok := mapp["_id"]; !ok { if _, ok := mapp[entity.ID]; !ok {
return errors.New("no _id in new object") return errors.New("no _id in new object")
} else if _, ok := mapp["_id"].(string); !ok { } else if _, ok := mapp[entity.ID].(string); !ok {
return errors.New("non-string _id in new object") return errors.New("non-string _id in new object")
} }
c := m.client.Database(m.db).Collection(namespace) c := m.client.Database(m.db).Collection(namespace)

View File

@ -10,8 +10,8 @@ import (
) )
const ( const (
Name = "_id" ID = "_id"
JSONName = "name" Name = "name"
Relationship = "relationship" Relationship = "relationship"
Type = "type" Type = "type"
Title = "title" Title = "title"
@ -22,14 +22,15 @@ const (
) )
type One struct { type One struct {
Name string `bson:"_id,omitempty" json:"name,omitempty"` ID string `bson:"_id,omitempty" json:"_id,omitempty"`
Name string `bson:"name,omitempty" json:"name,omitempty"`
Type string `bson:"type,omitempty" json:"type,omitempty"` Type string `bson:"type,omitempty" json:"type,omitempty"`
Title string `bson:"title,omitempty" json:"title,omitempty"` Title string `bson:"title,omitempty" json:"title,omitempty"`
Text string `bson:"text,omitempty" json:"text,omitempty"` Text string `bson:"text,omitempty" json:"text,omitempty"`
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" json:"connections,omitempty"` Connections map[string]One `bson:"connections" json:"connections,omitempty"`
Attachments map[string]string `bson:"attachments,omitempty" json:"attachments,omitempty"` Attachments map[string]string `bson:"attachments" json:"attachments,omitempty"`
} }
func (o One) Query() One { func (o One) Query() One {
@ -75,10 +76,6 @@ func (o One) MarshalBSON() ([]byte, error) {
m[k] = strings.TrimSpace(v.(string)) m[k] = strings.TrimSpace(v.(string))
} }
} }
if name, ok := m[JSONName]; ok {
m[Name] = name
delete(m, JSONName)
}
if !isMin { if !isMin {
connections := map[string]interface{}{} connections := map[string]interface{}{}
switch m[Connections].(type) { switch m[Connections].(type) {

View File

@ -2,6 +2,7 @@ package storage
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"local/dndex/storage/driver" "local/dndex/storage/driver"
"local/dndex/storage/entity" "local/dndex/storage/entity"
@ -56,6 +57,9 @@ func (g Graph) gatherOnes(ctx context.Context, ch <-chan bson.Raw) ([]entity.One
} }
func (g Graph) Insert(ctx context.Context, namespace string, one entity.One) error { func (g Graph) Insert(ctx context.Context, namespace string, one entity.One) error {
if one.Name == "" || one.ID == "" {
return errors.New("cannot create document without both name and id")
}
if ones, err := g.ListCaseInsensitive(ctx, namespace, one.Name); err != nil { if ones, err := g.ListCaseInsensitive(ctx, namespace, one.Name); err != nil {
return err return err
} else if len(ones) > 0 { } else if len(ones) > 0 {

View File

@ -366,6 +366,7 @@ func TestIntegration(t *testing.T) {
func randomOne() entity.One { func randomOne() entity.One {
return entity.One{ return entity.One{
ID: "id-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5], Name: "name-" + uuid.New().String()[:5],
Type: "Humman", Type: "Humman",
Title: "Biggus", Title: "Biggus",

View File

@ -47,6 +47,7 @@ func auth(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) er
func isPublic(g storage.RateLimitedGraph, r *http.Request) bool { func isPublic(g storage.RateLimitedGraph, r *http.Request) bool {
namespace, err := getAuthNamespace(r) namespace, err := getAuthNamespace(r)
if err != nil { if err != nil {
return false return false
} }
@ -103,8 +104,10 @@ func requestAuth(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Requ
} }
userKey := ones[0] userKey := ones[0]
id := uuid.New().String()
token := entity.One{ token := entity.One{
Name: uuid.New().String(), ID: id,
Name: id,
Title: namespace, Title: namespace,
} }
if err := g.Insert(r.Context(), namespace, token); err != nil { if err := g.Insert(r.Context(), namespace, token); err != nil {

View File

@ -22,7 +22,7 @@ func TestAuth(t *testing.T) {
g := storage.NewRateLimitedGraph() g := storage.NewRateLimitedGraph()
handler := jsonHandler(g) handler := jsonHandler(g)
if err := g.Insert(context.TODO(), "col."+AuthKey, entity.One{Name: UserKey, Title: "password"}); err != nil { if err := g.Insert(context.TODO(), "col."+AuthKey, entity.One{ID: UserKey, Name: UserKey, Title: "password"}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -48,13 +48,13 @@ func TestAuth(t *testing.T) {
t.Run("auth: expired provided", func(t *testing.T) { t.Run("auth: expired provided", func(t *testing.T) {
os.Setenv("AUTHLIFETIME", "1ms") os.Setenv("AUTHLIFETIME", "1ms")
defer os.Setenv("AUTHLIFETIME", "1h") defer os.Setenv("AUTHLIFETIME", "1h")
one := entity.One{Name: uuid.New().String(), Title: "title"} one := entity.One{ID: uuid.New().String(), Name: uuid.New().String(), Title: "title"}
if err := g.Insert(context.TODO(), "col", one); err != nil { if err := g.Insert(context.TODO(), "col", one); err != nil {
t.Fatal(err) t.Fatal(err)
} }
time.Sleep(time.Millisecond * 50) time.Sleep(time.Millisecond * 50)
r := httptest.NewRequest(http.MethodGet, "/who?namespace=col", nil) r := httptest.NewRequest(http.MethodGet, "/who?namespace=col", nil)
r.Header.Set("Cookie", fmt.Sprintf("%s=%s", AuthKey, one.Name)) r.Header.Set("Cookie", fmt.Sprintf("%s=%s", AuthKey, one.ID))
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Code != http.StatusSeeOther { if w.Code != http.StatusSeeOther {
@ -82,7 +82,7 @@ func TestAuth(t *testing.T) {
t.Run("auth: provided", func(t *testing.T) { t.Run("auth: provided", func(t *testing.T) {
os.Setenv("AUTHLIFETIME", "1h") os.Setenv("AUTHLIFETIME", "1h")
one := entity.One{Name: uuid.New().String(), Title: "title"} one := entity.One{ID: uuid.New().String(), Name: uuid.New().String(), Title: "title"}
if err := g.Insert(context.TODO(), "col."+AuthKey, one); err != nil { if err := g.Insert(context.TODO(), "col."+AuthKey, one); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -24,7 +24,7 @@ func TestPort(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
for _, name := range []string{"A", "B", "C"} { for _, name := range []string{"A", "B", "C"} {
if err := g.Insert(context.TODO(), "col", entity.One{Name: name}); err != nil { if err := g.Insert(context.TODO(), "col", entity.One{ID: "id-" + name, Name: name}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }

View File

@ -5,6 +5,8 @@ import (
"local/dndex/storage" "local/dndex/storage"
"local/dndex/storage/entity" "local/dndex/storage/entity"
"net/http" "net/http"
"github.com/google/uuid"
) )
func register(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error { func register(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
@ -33,6 +35,7 @@ func registerPost(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Req
return nil return nil
} }
one := entity.One{ one := entity.One{
ID: uuid.New().String(),
Name: UserKey, Name: UserKey,
Title: password, Title: password,
} }

View File

@ -170,7 +170,6 @@ func whoPut(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter,
return nil return nil
} }
body = bytes.ReplaceAll(body, []byte(`"`+entity.JSONName+`"`), []byte(`"`+entity.Name+`"`))
op := bson.M{} op := bson.M{}
if err := json.Unmarshal(body, &op); err != nil { if err := json.Unmarshal(body, &op); err != nil {
return err return err

View File

@ -58,7 +58,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
} }
o := entity.One{} var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -105,7 +105,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
} }
o := entity.One{} var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -140,7 +140,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
} }
o := entity.One{} var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -175,7 +175,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
} }
o := entity.One{} var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -207,7 +207,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
} }
o := entity.One{} var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -260,7 +260,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
} }
o := entity.One{} var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -278,7 +278,7 @@ func TestWho(t *testing.T) {
handler, _, iwant, _, can := fresh(t) handler, _, iwant, _, can := fresh(t)
defer can() defer can()
want := iwant want := iwant
r := httptest.NewRequest(http.MethodPost, "/who?namespace=col&id="+want.Name, strings.NewReader(`{"title":"this should fail to insert"}`)) r := httptest.NewRequest(http.MethodPost, "/who?namespace=col&id="+want.Name, strings.NewReader(`{"title":"this should fail to insert", "_id":"trash"}`))
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Code != http.StatusConflict { if w.Code != http.StatusConflict {
@ -291,6 +291,7 @@ func TestWho(t *testing.T) {
defer can() defer can()
iwant := want iwant := want
iwant.Name = "" iwant.Name = ""
iwant.ID = "NEWBIE" + iwant.ID
b, err := json.Marshal(iwant) b, err := json.Marshal(iwant)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -301,7 +302,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
} }
o := entity.One{} var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -319,6 +320,7 @@ func TestWho(t *testing.T) {
handler, _, want, _, can := fresh(t) handler, _, want, _, can := fresh(t)
defer can() defer can()
want.Name = "hello world #1 e ę" want.Name = "hello world #1 e ę"
want.ID = "hello world #1 e ę" + want.ID
want.Connections = nil want.Connections = nil
b, err := json.Marshal(want) b, err := json.Marshal(want)
if err != nil { if err != nil {
@ -336,7 +338,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
} }
o := entity.One{} var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -369,6 +371,7 @@ func TestWho(t *testing.T) {
defer can() defer can()
want.Name = "hello world #4" want.Name = "hello world #4"
want.ID = "hello world #4" + want.ID
want.Connections = nil want.Connections = nil
b, err := json.Marshal(want) b, err := json.Marshal(want)
if err != nil { if err != nil {
@ -410,6 +413,7 @@ func TestWho(t *testing.T) {
defer can() defer can()
want.Name = "hello world #1 e ę" want.Name = "hello world #1 e ę"
want.ID = "hello world #1 e ę" + want.ID
want.Connections = nil want.Connections = nil
b, err := json.Marshal(want) b, err := json.Marshal(want)
if err != nil { if err != nil {
@ -499,6 +503,7 @@ func TestWho(t *testing.T) {
from := ones[4] from := ones[4]
push := ones[10].Peer() push := ones[10].Peer()
push.Relationship = "spawn" push.Relationship = "spawn"
push.ID = uuid.New().String()
b, err := json.Marshal(push) b, err := json.Marshal(push)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -509,7 +514,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
} }
got := entity.One{} var got entity.One
if err := json.NewDecoder(w.Body).Decode(&got); err != nil { if err := json.NewDecoder(w.Body).Decode(&got); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -558,7 +563,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
} }
got := entity.One{} var got entity.One
if err := json.NewDecoder(w.Body).Decode(&got); err != nil { if err := json.NewDecoder(w.Body).Decode(&got); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -675,7 +680,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
} }
o := entity.One{} var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -702,7 +707,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
} }
o := entity.One{} var o entity.One
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -744,7 +749,7 @@ func TestWho(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
} }
o := entity.One{} var o entity.One
if err := json.NewDecoder(w.Body).Decode(&o); err != nil { if err := json.NewDecoder(w.Body).Decode(&o); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -943,6 +948,7 @@ func fillDB(t *testing.T, g storage.RateLimitedGraph) []entity.One {
func randomOne() entity.One { func randomOne() entity.One {
return entity.One{ return entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5], Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5], Type: "type-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5], Title: "titl-" + uuid.New().String()[:5],