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) { 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) { 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) { 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 { 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) { func createUpdate(t *testing.T, uri, token string) {
id := callCreate(t, uri, token, "") t.Run("create, update", func(t *testing.T) {
callUpdate(t, uri, token, id) id := callCreate(t, uri, token, "")
callUpdate(t, uri, token, id)
})
} }
func createDelete(t *testing.T, uri, token string) { func createDelete(t *testing.T, uri, token string) {
id := callCreate(t, uri, token, "") t.Run("create, delete", func(t *testing.T) {
callDelete(t, uri, token, id) id := callCreate(t, uri, token, "")
one := callGet(t, uri, token, id, http.StatusNotFound) callDelete(t, uri, token, id)
if one.ID == id { one := callGet(t, uri, token, id, http.StatusNotFound)
t.Fatal(one) if one.ID == id {
} t.Fatal(one)
if fmt.Sprint(one) != fmt.Sprint(entity.One{}) { }
t.Fatal(one) if fmt.Sprint(one) != fmt.Sprint(entity.One{}) {
} t.Fatal(one)
}
})
} }
func callVersion(t *testing.T, uri string) { func callVersion(t *testing.T, uri string) {
resp := call(t, "", http.MethodGet, uri+"/version", "") t.Run("call /version", func(t *testing.T) {
if resp.StatusCode != http.StatusOK { resp := call(t, "", http.MethodGet, uri+"/version", "")
t.Fatal(resp.StatusCode) if resp.StatusCode != http.StatusOK {
} t.Fatal(resp.StatusCode)
var response struct { }
Version string `json:"version"` var response struct {
} Version string `json:"version"`
b, err := ioutil.ReadAll(resp.Body) }
if err != nil { b, err := ioutil.ReadAll(resp.Body)
t.Fatal(err) if err != nil {
} t.Fatal(err)
if err := json.Unmarshal(b, &response); 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) if response.Version != GitCommit {
} t.Fatalf("%s: %v", b, response)
}
})
} }
func callGet(t *testing.T, uri, token, id string, status int) entity.One { 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 { if resp.StatusCode != status {
t.Fatal(resp.StatusCode) t.Fatal(resp.StatusCode)
} }
var one entity.One if status != http.StatusOK {
if err := json.NewDecoder(resp.Body).Decode(&one); err != nil { 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) 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) { func callDelete(t *testing.T, uri, token, id string) {
resp := call(t, token, http.MethodDelete, uri+"/entities/"+id, "") t.Run("call delete /entities/X", func(t *testing.T) {
if resp.StatusCode != http.StatusOK { resp := call(t, token, http.MethodDelete, uri+"/entities/"+id, "")
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))
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode) t.Fatal(resp.StatusCode)
} }
var response map[string]map[string]interface{} })
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { }
t.Fatal(err)
} func callUpdate(t *testing.T, uri, token, id string) {
found := false t.Run("call update /entities/X", func(t *testing.T) {
for k := range response { for _, method := range []string{http.MethodPut, http.MethodPatch} {
for k2 := range m { m := map[string]interface{}{
if k2 == entity.Connections { "name": "name-" + uuid.New().String()[:5],
if v, ok := response[k][k2]; !ok { "type": "type-" + uuid.New().String()[:5],
t.Fatal("missing connection", k2) "title": "titl-" + uuid.New().String()[:5],
} else if m2, ok := v.(map[string]interface{}); !ok { "text": "text-" + uuid.New().String()[:5],
t.Fatalf("wrong connection type %T", v) "connections": map[string]map[string]string{
} else { callCreate(t, uri, token, ""): map[string]string{"relationship": "callUpdate peer"},
wantConnections := m[k2].(map[string]map[string]string) },
for k3 := range wantConnections { "attachments": map[string]map[string]string{
wantRelationship := wantConnections[k3][entity.Relationship] "myfile": map[string]string{"location": "/files/my_file_location.txt"},
gotRelationship := m2[k3].(map[string]interface{})[entity.Relationship] },
if wantRelationship != gotRelationship { }
t.Fatal(gotRelationship, wantRelationship) b, err := json.Marshal(m)
} if err != nil {
b, _ := json.Marshal(m2[k3]) t.Fatal(err)
var gotOne entity.One }
json.Unmarshal(b, &gotOne) resp := call(t, token, method, uri+"/entities/"+id, string(b))
if k3 != gotOne.ID { if resp.StatusCode != http.StatusOK {
t.Fatal(gotOne.ID) t.Fatal(resp.StatusCode)
} }
if "" == gotOne.Text { var response map[string]map[string]interface{}
t.Fatal(gotOne) 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 {
} else { if fmt.Sprint(m[k2]) != fmt.Sprint(response[k][k2]) {
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]))
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 { 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) { func callList(t *testing.T, uri, token string, status int) {
resp := call(t, token, http.MethodGet, uri+"/entities", "") t.Run("call get /entities", func(t *testing.T) {
b, err := ioutil.ReadAll(resp.Body) resp := call(t, token, http.MethodGet, uri+"/entities", "")
if err != nil { b, err := ioutil.ReadAll(resp.Body)
t.Fatal(err) if err != nil {
} t.Fatal(err)
if resp.StatusCode != status { }
t.Fatalf("%v: %s", resp.StatusCode, b) if resp.StatusCode != status {
} t.Fatalf("%v: %s", resp.StatusCode, b)
}
})
} }
func callRegister(t *testing.T, uri string) (string, string) { func callRegister(t *testing.T, uri string) (string, string) {