unit tests new entities/abc

This commit is contained in:
breel
2020-08-09 14:25:14 -06:00
parent 360a686906
commit 02c5d795cf
4 changed files with 119 additions and 37 deletions

View File

@@ -6,7 +6,6 @@ import (
"io"
"local/dndex/storage/entity"
"local/dndex/storage/operator"
"log"
"net/http"
"path"
"strings"
@@ -107,12 +106,20 @@ func (rest *REST) entitiesGetOne(w http.ResponseWriter, r *http.Request) {
rest.respNotFound(w)
return
}
resp := one.Generic()
if _, ok := r.URL.Query()["light"]; !ok {
log.Println("TODO need to get all connections")
//http.Error(w, "not impl", http.StatusNotImplemented)
//return
m := bson.M{}
for id := range one.Connections {
one2, err := rest.g.Get(r.Context(), scope.Namespace, id)
if err == nil {
m2 := one2.Generic()
m2[entity.Relationship] = one.Connections[id].Relationship
m[id] = m2
}
}
resp[entity.Connections] = m
}
rest.respMap(w, entityScope[0], one)
rest.respMap(w, entityScope[0], resp)
}
func (rest *REST) entitiesGetOneSub(w http.ResponseWriter, r *http.Request) {

View File

@@ -56,12 +56,20 @@ func TestEntities(t *testing.T) {
})
t.Run("create+get0", func(t *testing.T) {
w := testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"name":"myname", "attachments": {"abc": {}}, "connections": {"def": {"relationship": "ghi"}}}`)
w := testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"name":"my friend"}`)
if w.Code != http.StatusOK {
t.Fatal(w.Code)
}
friend := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
return one.Name == "my friend"
})
w = testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"name":"myname", "attachments": {"abc": {}}, "connections": {"`+friend+`": {"relationship": "ghi"}}}`)
if w.Code != http.StatusOK {
t.Fatal(w.Code)
}
id := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
return one.Name == "myname" && len(one.Attachments) == 1 && len(one.Connections) == 1 && one.Connections["def"].Relationship == "ghi"
return one.Name == "myname" && len(one.Attachments) == 1 && len(one.Connections) == 1 && one.Connections[friend].Relationship == "ghi"
})
w = testEntitiesMethod(t, authit, rest, http.MethodGet, "/", ``)
@@ -168,21 +176,29 @@ func TestEntities(t *testing.T) {
})
t.Run("create+update/replace.connection.abc.relationship", func(t *testing.T) {
w := testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"name": "my friend"}`)
if w.Code != http.StatusOK {
t.Fatal(w.Code)
}
friend := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
return one.Name == "my friend"
})
for _, method := range []string{http.MethodPut, http.MethodPatch} {
w := testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"connections": {"abc": {"relationship": "def"}}}`)
w := testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"connections": {"`+friend+`": {"relationship": "def"}}}`)
if w.Code != http.StatusOK {
t.Fatal(w.Code)
}
id := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
return one.Connections["abc"].Relationship == "def"
return one.Connections[friend].Relationship == "def"
})
w = testEntitiesMethod(t, authit, rest, method, "/"+id+"/connections/abc/relationship", `"new"`)
w = testEntitiesMethod(t, authit, rest, method, "/"+id+"/connections/"+friend+"/relationship", `"new"`)
if w.Code != http.StatusOK {
t.Fatal(w.Code)
}
id2 := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
return one.Connections["abc"].Relationship == "new"
return one.Connections[friend].Relationship == "new"
})
if id != id2 {
@@ -192,21 +208,29 @@ func TestEntities(t *testing.T) {
})
t.Run("create+update/replace.connection.abc", func(t *testing.T) {
w := testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"name": "my friend"}`)
if w.Code != http.StatusOK {
t.Fatal(w.Code)
}
friend := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
return one.Name == "my friend"
})
for _, method := range []string{http.MethodPut, http.MethodPatch} {
w := testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"connections": {"abc": {"relationship": "def"}}}`)
w := testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"connections": {"`+friend+`": {"relationship": "def"}}}`)
if w.Code != http.StatusOK {
t.Fatal(w.Code)
}
id := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
return one.Connections["abc"].Relationship == "def"
return one.Connections[friend].Relationship == "def"
})
w = testEntitiesMethod(t, authit, rest, method, "/"+id+"/connections/abc", `{"relationship": "new"}`)
w = testEntitiesMethod(t, authit, rest, method, "/"+id+"/connections/"+friend, `{"relationship": "new"}`)
if w.Code != http.StatusOK {
t.Fatal(w.Code)
}
id2 := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
return one.Connections["abc"].Relationship == "new"
return one.Connections[friend].Relationship == "new"
})
if id != id2 {
@@ -238,16 +262,24 @@ func TestEntities(t *testing.T) {
})
t.Run("create w connection.abc+delete.connections.abc", func(t *testing.T) {
w := testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"name":"myname", "title": "mytitle", "connections": {"abc": {"relationship": "good"}}}`)
w := testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"name": "my friend"}`)
if w.Code != http.StatusOK {
t.Fatal(w.Code)
}
friend := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
return one.Name == "my friend"
})
w = testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"name":"myname", "title": "mytitle", "connections": {"`+friend+`": {"relationship": "good"}}}`)
if w.Code != http.StatusOK {
t.Fatalf("%v: %s", w.Code, w.Body.Bytes())
}
id := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
abc, ok := one.Connections["abc"]
return one.Name == "myname" && one.Title == "mytitle" && ok && abc.Relationship == "good"
friended, ok := one.Connections[friend]
return one.Name == "myname" && one.Title == "mytitle" && ok && friended.Relationship == "good"
})
w = testEntitiesMethod(t, authit, rest, http.MethodDelete, "/"+id+"/connections/abc", ``)
w = testEntitiesMethod(t, authit, rest, http.MethodDelete, "/"+id+"/connections/"+friend, ``)
if w.Code != http.StatusOK {
t.Fatalf("%v: %s", w.Code, w.Body.Bytes())
}
@@ -261,21 +293,29 @@ func TestEntities(t *testing.T) {
})
t.Run("create w connection.abc.relationship+delete.connections.abc.relationship", func(t *testing.T) {
w := testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"connections": {"abc": {"relationship": "good"}}}`)
w := testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"name": "my friend"}`)
if w.Code != http.StatusOK {
t.Fatal(w.Code)
}
friend := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
return one.Name == "my friend"
})
w = testEntitiesMethod(t, authit, rest, http.MethodPost, "/", `{"connections": {"`+friend+`": {"relationship": "good"}}}`)
if w.Code != http.StatusOK {
t.Fatalf("%v: %s", w.Code, w.Body.Bytes())
}
id := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
abc, ok := one.Connections["abc"]
return ok && abc.Relationship == "good"
friended, ok := one.Connections[friend]
return ok && friended.Relationship == "good"
})
w = testEntitiesMethod(t, authit, rest, http.MethodDelete, "/"+id+"/connections/abc/relationship", ``)
w = testEntitiesMethod(t, authit, rest, http.MethodDelete, "/"+id+"/connections/"+friend+"/relationship", ``)
if w.Code != http.StatusOK {
t.Fatalf("%v: %s", w.Code, w.Body.Bytes())
}
id2 := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
return len(one.Connections) == 1 && one.Connections["abc"].Relationship == ""
return len(one.Connections) == 1 && one.Connections[friend].Relationship == ""
})
if id != id2 {