test sub operations from main

master
breel 2020-08-26 19:58:54 -06:00
parent 69438cbeab
commit 8efdaecf9a
1 changed files with 160 additions and 104 deletions

View File

@ -52,15 +52,48 @@ func Test(t *testing.T) {
}
func createDeleteSub(t *testing.T, uri, token string) {
t.Error("not impl")
t.Run("create, delete sub", func(t *testing.T) {
id := callCreate(t, uri, token, "")
was := callGet(t, uri, token, id, http.StatusOK)
callDelete(t, uri, token, id+"/title")
one := callGet(t, uri, token, id, http.StatusOK)
if one.Title != "" {
t.Fatal(one.Title)
}
was.Title = ""
was.Modified = 0
one.Modified = 0
if fmt.Sprint(one) != fmt.Sprint(was) {
t.Fatalf("partial del failed: want \n%+v, got \n%+v", was, one)
}
})
}
func createUpdateSub(t *testing.T, uri, token string) {
t.Error("not impl")
t.Run("create, update sub", func(t *testing.T) {
id := callCreate(t, uri, token, "")
was := callGet(t, uri, token, id, http.StatusOK)
resp := call(t, token, http.MethodPut, uri+"/entities/"+id+"/title", `"newtitle"`)
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
one := callGet(t, uri, token, id, http.StatusOK)
if one.Title != "newtitle" {
t.Fatal(one.Title)
}
was.Title = "newtitle"
was.Modified = 0
one.Modified = 0
if fmt.Sprint(one) != fmt.Sprint(was) {
t.Fatalf("partial updated failed: want \n%+v, got \n%+v", was, one)
}
})
}
func filesCRUD(t *testing.T, uri, token string) {
t.Error("not impl")
t.Run("files CRUD", func(t *testing.T) {
t.Error("not impl")
})
}
func register(t *testing.T, uri string) string {
@ -96,40 +129,46 @@ func muckedToken(t *testing.T, uri, obf string) {
}
func createUpdate(t *testing.T, uri, token string) {
id := callCreate(t, uri, token, "")
callUpdate(t, uri, token, id)
t.Run("create, update", func(t *testing.T) {
id := callCreate(t, uri, token, "")
callUpdate(t, uri, token, id)
})
}
func createDelete(t *testing.T, uri, token string) {
id := callCreate(t, uri, token, "")
callDelete(t, uri, token, id)
one := callGet(t, uri, token, id, http.StatusNotFound)
if one.ID == id {
t.Fatal(one)
}
if fmt.Sprint(one) != fmt.Sprint(entity.One{}) {
t.Fatal(one)
}
t.Run("create, delete", func(t *testing.T) {
id := callCreate(t, uri, token, "")
callDelete(t, uri, token, id)
one := callGet(t, uri, token, id, http.StatusNotFound)
if one.ID == id {
t.Fatal(one)
}
if fmt.Sprint(one) != fmt.Sprint(entity.One{}) {
t.Fatal(one)
}
})
}
func callVersion(t *testing.T, uri string) {
resp := call(t, "", http.MethodGet, uri+"/version", "")
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
var response struct {
Version string `json:"version"`
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(b, &response); err != nil {
t.Fatal(err)
}
if response.Version != GitCommit {
t.Fatalf("%s: %v", b, response)
}
t.Run("call /version", func(t *testing.T) {
resp := call(t, "", http.MethodGet, uri+"/version", "")
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
var response struct {
Version string `json:"version"`
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(b, &response); err != nil {
t.Fatal(err)
}
if response.Version != GitCommit {
t.Fatalf("%s: %v", b, response)
}
})
}
func callGet(t *testing.T, uri, token, id string, status int) entity.One {
@ -137,85 +176,100 @@ func callGet(t *testing.T, uri, token, id string, status int) entity.One {
if resp.StatusCode != status {
t.Fatal(resp.StatusCode)
}
var one entity.One
if err := json.NewDecoder(resp.Body).Decode(&one); err != nil {
if status != http.StatusOK {
return entity.One{}
}
b, _ := ioutil.ReadAll(resp.Body)
var response map[string]entity.One
if err := json.Unmarshal(b, &response); err != nil {
t.Fatal(err)
}
return one
for _, one := range response {
if fmt.Sprint(one) == fmt.Sprint(entity.One{}) {
t.Fatal(id, status, one, string(b))
}
return one
}
t.Fatal("no items in response")
return entity.One{}
}
func callDelete(t *testing.T, uri, token, id string) {
resp := call(t, token, http.MethodDelete, uri+"/entities/"+id, "")
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
}
func callUpdate(t *testing.T, uri, token, id string) {
for _, method := range []string{http.MethodPut, http.MethodPatch} {
m := map[string]interface{}{
"name": "name-" + uuid.New().String()[:5],
"type": "type-" + uuid.New().String()[:5],
"title": "titl-" + uuid.New().String()[:5],
"text": "text-" + uuid.New().String()[:5],
"connections": map[string]map[string]string{
callCreate(t, uri, token, ""): map[string]string{"relationship": "callUpdate peer"},
},
"attachments": map[string]map[string]string{
"myfile": map[string]string{"location": "/files/my_file_location.txt"},
},
}
b, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}
resp := call(t, token, method, uri+"/entities/"+id, string(b))
t.Run("call delete /entities/X", func(t *testing.T) {
resp := call(t, token, http.MethodDelete, uri+"/entities/"+id, "")
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
var response map[string]map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
t.Fatal(err)
}
found := false
for k := range response {
for k2 := range m {
if k2 == entity.Connections {
if v, ok := response[k][k2]; !ok {
t.Fatal("missing connection", k2)
} else if m2, ok := v.(map[string]interface{}); !ok {
t.Fatalf("wrong connection type %T", v)
} else {
wantConnections := m[k2].(map[string]map[string]string)
for k3 := range wantConnections {
wantRelationship := wantConnections[k3][entity.Relationship]
gotRelationship := m2[k3].(map[string]interface{})[entity.Relationship]
if wantRelationship != gotRelationship {
t.Fatal(gotRelationship, wantRelationship)
}
b, _ := json.Marshal(m2[k3])
var gotOne entity.One
json.Unmarshal(b, &gotOne)
if k3 != gotOne.ID {
t.Fatal(gotOne.ID)
}
if "" == gotOne.Text {
t.Fatal(gotOne)
})
}
func callUpdate(t *testing.T, uri, token, id string) {
t.Run("call update /entities/X", func(t *testing.T) {
for _, method := range []string{http.MethodPut, http.MethodPatch} {
m := map[string]interface{}{
"name": "name-" + uuid.New().String()[:5],
"type": "type-" + uuid.New().String()[:5],
"title": "titl-" + uuid.New().String()[:5],
"text": "text-" + uuid.New().String()[:5],
"connections": map[string]map[string]string{
callCreate(t, uri, token, ""): map[string]string{"relationship": "callUpdate peer"},
},
"attachments": map[string]map[string]string{
"myfile": map[string]string{"location": "/files/my_file_location.txt"},
},
}
b, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}
resp := call(t, token, method, uri+"/entities/"+id, string(b))
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
var response map[string]map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
t.Fatal(err)
}
found := false
for k := range response {
for k2 := range m {
if k2 == entity.Connections {
if v, ok := response[k][k2]; !ok {
t.Fatal("missing connection", k2)
} else if m2, ok := v.(map[string]interface{}); !ok {
t.Fatalf("wrong connection type %T", v)
} else {
wantConnections := m[k2].(map[string]map[string]string)
for k3 := range wantConnections {
wantRelationship := wantConnections[k3][entity.Relationship]
gotRelationship := m2[k3].(map[string]interface{})[entity.Relationship]
if wantRelationship != gotRelationship {
t.Fatal(gotRelationship, wantRelationship)
}
b, _ := json.Marshal(m2[k3])
var gotOne entity.One
json.Unmarshal(b, &gotOne)
if k3 != gotOne.ID {
t.Fatal(gotOne.ID)
}
if "" == gotOne.Text {
t.Fatal(gotOne)
}
}
}
}
} else {
if fmt.Sprint(m[k2]) != fmt.Sprint(response[k][k2]) {
t.Fatalf("@%q.%q, want %q, got %q", k, k2, fmt.Sprint(m[k2]), fmt.Sprint(response[k][k2]))
} else {
if fmt.Sprint(m[k2]) != fmt.Sprint(response[k][k2]) {
t.Fatalf("@%q.%q, want %q, got %q", k, k2, fmt.Sprint(m[k2]), fmt.Sprint(response[k][k2]))
}
}
}
found = true
}
if !found {
t.Fatal(found)
}
found = true
}
if !found {
t.Fatal(found)
}
}
})
}
func callCreate(t *testing.T, uri, token, peerID string) string {
@ -258,14 +312,16 @@ func callCreate(t *testing.T, uri, token, peerID string) string {
}
func callList(t *testing.T, uri, token string, status int) {
resp := call(t, token, http.MethodGet, uri+"/entities", "")
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != status {
t.Fatalf("%v: %s", resp.StatusCode, b)
}
t.Run("call get /entities", func(t *testing.T) {
resp := call(t, token, http.MethodGet, uri+"/entities", "")
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != status {
t.Fatalf("%v: %s", resp.StatusCode, b)
}
})
}
func callRegister(t *testing.T, uri string) (string, string) {