Fix order sensitive and nondeterministic unit tests

This commit is contained in:
breel
2020-07-25 13:26:29 -06:00
parent d3c1cb8b4f
commit 34075cf19a
5 changed files with 199 additions and 5 deletions

View File

@@ -49,7 +49,7 @@ func whoGet(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Re
}
_, light := r.URL.Query()["light"]
ones, err := g.List(r.Context(), namespace, id)
ones, err := g.ListCaseInsensitive(r.Context(), namespace, id)
if err != nil {
return err
}
@@ -63,7 +63,7 @@ func whoGet(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Re
one := ones[0]
if !light && len(one.Connections) > 0 {
ones, err := g.List(r.Context(), namespace, one.Peers()...)
ones, err := g.ListCaseInsensitive(r.Context(), namespace, one.Peers()...)
if err != nil {
return err
}
@@ -187,7 +187,7 @@ func whoPatch(namespace string, g storage.Graph, w http.ResponseWriter, r *http.
}
func whoTrace(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error {
ones, err := g.List(r.Context(), namespace)
ones, err := g.ListCaseInsensitive(r.Context(), namespace)
if err != nil {
return err
}

View File

@@ -90,6 +90,68 @@ func TestWho(t *testing.T) {
t.Logf("POST GET:\n%s", b)
})
t.Run("get real light", func(t *testing.T) {
iwant := want
r := httptest.NewRequest(http.MethodGet, "/who?namespace=col&light&id="+iwant.Name, nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err)
}
if fmt.Sprint(o) == fmt.Sprint(iwant) {
t.Fatal(o, iwant)
}
if len(o.Connections) != len(iwant.Connections) {
t.Fatal(len(o.Connections), len(iwant.Connections))
}
iwant.Connections = o.Connections
iwant.Modified = 0
o.Modified = 0
if fmt.Sprint(o) != fmt.Sprint(iwant) {
t.Fatalf("after resolving connections and modified, iwant and got differ: \nwant %+v\n got %+v", iwant, o)
}
for _, connection := range iwant.Connections {
if len(connection.Connections) != 0 {
t.Fatal(connection)
}
}
b, _ := json.MarshalIndent(o, "", " ")
t.Logf("POST GET:\n%s", b)
})
t.Run("get real but case is wrong", func(t *testing.T) {
iwant := want
iwantName := strings.ToUpper(iwant.Name)
r := httptest.NewRequest(http.MethodGet, "/who?namespace=col&id="+iwantName, nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err)
}
if fmt.Sprint(o) == fmt.Sprint(iwant) {
t.Fatal(o, iwant)
}
if len(o.Connections) != len(iwant.Connections) {
t.Fatal(len(o.Connections), len(iwant.Connections))
}
iwant.Connections = o.Connections
iwant.Modified = 0
o.Modified = 0
if fmt.Sprint(o) != fmt.Sprint(iwant) {
t.Fatalf("after resolving connections and modified, iwant and got differ: \nwant %+v\n got %+v", iwant, o)
}
b, _ := json.MarshalIndent(o, "", " ")
t.Logf("POST GET:\n%s", b)
})
t.Run("put fake", func(t *testing.T) {
iwant := want
r := httptest.NewRequest(http.MethodPut, "/who?namespace=col&id=FAKER"+iwant.Name, strings.NewReader(`{"title":"this should fail to find someone"}`))