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.Run("files CRUD", func(t *testing.T) {
t.Error("not impl") t.Error("not impl")
})
} }
func register(t *testing.T, uri string) string { func register(t *testing.T, uri string) string {
@ -96,11 +129,14 @@ func muckedToken(t *testing.T, uri, obf string) {
} }
func createUpdate(t *testing.T, uri, token string) { func createUpdate(t *testing.T, uri, token string) {
t.Run("create, update", func(t *testing.T) {
id := callCreate(t, uri, token, "") id := callCreate(t, uri, token, "")
callUpdate(t, uri, token, id) callUpdate(t, uri, token, id)
})
} }
func createDelete(t *testing.T, uri, token string) { func createDelete(t *testing.T, uri, token string) {
t.Run("create, delete", func(t *testing.T) {
id := callCreate(t, uri, token, "") id := callCreate(t, uri, token, "")
callDelete(t, uri, token, id) callDelete(t, uri, token, id)
one := callGet(t, uri, token, id, http.StatusNotFound) one := callGet(t, uri, token, id, http.StatusNotFound)
@ -110,9 +146,11 @@ func createDelete(t *testing.T, uri, token string) {
if fmt.Sprint(one) != fmt.Sprint(entity.One{}) { if fmt.Sprint(one) != fmt.Sprint(entity.One{}) {
t.Fatal(one) t.Fatal(one)
} }
})
} }
func callVersion(t *testing.T, uri string) { func callVersion(t *testing.T, uri string) {
t.Run("call /version", func(t *testing.T) {
resp := call(t, "", http.MethodGet, uri+"/version", "") resp := call(t, "", http.MethodGet, uri+"/version", "")
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode) t.Fatal(resp.StatusCode)
@ -130,6 +168,7 @@ func callVersion(t *testing.T, uri string) {
if response.Version != GitCommit { if response.Version != GitCommit {
t.Fatalf("%s: %v", b, response) 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,21 +176,35 @@ 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)
} }
for _, one := range response {
if fmt.Sprint(one) == fmt.Sprint(entity.One{}) {
t.Fatal(id, status, one, string(b))
}
return one 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) {
t.Run("call delete /entities/X", func(t *testing.T) {
resp := call(t, token, http.MethodDelete, uri+"/entities/"+id, "") resp := call(t, token, http.MethodDelete, uri+"/entities/"+id, "")
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode) t.Fatal(resp.StatusCode)
} }
})
} }
func callUpdate(t *testing.T, uri, token, id string) { 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} { for _, method := range []string{http.MethodPut, http.MethodPatch} {
m := map[string]interface{}{ m := map[string]interface{}{
"name": "name-" + uuid.New().String()[:5], "name": "name-" + uuid.New().String()[:5],
@ -216,6 +269,7 @@ func callUpdate(t *testing.T, uri, token, id string) {
t.Fatal(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,6 +312,7 @@ 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) {
t.Run("call get /entities", func(t *testing.T) {
resp := call(t, token, http.MethodGet, uri+"/entities", "") resp := call(t, token, http.MethodGet, uri+"/entities", "")
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
@ -266,6 +321,7 @@ func callList(t *testing.T, uri, token string, status int) {
if resp.StatusCode != status { if resp.StatusCode != status {
t.Fatalf("%v: %s", resp.StatusCode, b) 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) {