Working but i shouldve made connections a map

This commit is contained in:
Bel LaPointe
2020-07-22 21:54:24 -06:00
parent 9338cf86c9
commit 82975a7101
9 changed files with 112 additions and 20 deletions

View File

@@ -3,9 +3,9 @@ package view
import (
"encoding/json"
"fmt"
"local/whodunit/config"
"local/whodunit/storage"
"local/whodunit/storage/entity"
"local/dndex/config"
"local/dndex/storage"
"local/dndex/storage/entity"
"log"
"net/http"
)
@@ -47,17 +47,25 @@ func who(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
ones = append(ones, entity.One{})
}
one := ones[0]
results[id] = one
if verbose {
ones, err := g.List(r.Context(), one.Peers()...)
if err != nil {
return err
}
for i, one := range ones {
one.Connections = nil
results[id].Connections[i] = one
for _, another := range ones {
another.Connections = nil
for j := range one.Connections {
if one.Connections[j].Name == another.Name {
one.Connections[j] = entity.One{
Name: another.Name,
Relationship: one.Connections[j].Relationship,
}
break
}
}
}
}
results[id] = one
}
log.Println("results:", results)
return json.NewEncoder(w).Encode(results)

View File

@@ -4,9 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"local/whodunit/config"
"local/whodunit/storage"
"local/whodunit/storage/entity"
"local/dndex/config"
"local/dndex/storage"
"local/dndex/storage/entity"
"net/http"
"os"
"testing"
@@ -25,6 +25,7 @@ func TestHtml(t *testing.T) {
g := storage.NewGraph()
ones := fillDB(t, g)
want := ones[len(ones)-1]
go func() {
if err := Html(g); err != nil {
@@ -32,7 +33,7 @@ func TestHtml(t *testing.T) {
}
}()
time.Sleep(time.Millisecond * 250)
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/who?id=%s&v", config.New().Port, ones[len(ones)-1].Name))
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/who?id=%s&v", config.New().Port, want.Name))
if err != nil {
t.Fatal(err)
}
@@ -45,6 +46,46 @@ func TestHtml(t *testing.T) {
if err != nil {
t.Fatal(err)
}
var gotOnes map[string]entity.One
if err := json.Unmarshal(b, &gotOnes); err != nil {
t.Fatal(err)
}
if len(gotOnes) != 1 {
t.Fatal("too many results: ", len(gotOnes))
}
var o entity.One
for _, v := range gotOnes {
o = v
}
for i := range want.Connections {
found := false
for j := range o.Connections {
if want.Connections[i].Name == o.Connections[j].Name {
found = true
if want.Connections[i].Relationship != o.Connections[j].Relationship {
t.Errorf("connection is wrong: want %+v, got %+v", want.Connections[i], o.Connections[j])
}
}
}
if !found {
t.Errorf("didn't find connection %+v in %+v", want.Connections[i], o.Connections)
}
}
if o.Image != want.Image {
t.Errorf("wrong image: want %q, got %q", want.Image, o.Image)
}
if o.Text != want.Text {
t.Errorf("wrong text: want %q, got %q", want.Text, o.Text)
}
if o.Title != want.Title {
t.Errorf("wrong title: want %q, got %q", want.Title, o.Title)
}
if o.Type != want.Type {
t.Errorf("wrong type: want %q, got %q", want.Type, o.Type)
}
if o.Name != want.Name {
t.Errorf("wrong name: want %q, got %q", want.Name, o.Name)
}
t.Logf("\n%s\n", b)
}
@@ -63,6 +104,15 @@ func fillDB(t *testing.T, g storage.Graph) []entity.One {
if err := g.Insert(context.TODO(), ones[i]); err != nil {
t.Fatal(err)
}
if results, err := g.List(context.TODO(), ones[i].Name); err != nil {
t.Fatal(err)
} else if len(results) != 1 {
t.Fatal(len(results))
} else if len(results[0].Connections) != len(ones[i].Connections) {
t.Fatal(len(results[0].Connections), len(ones[i].Connections))
} else if len(results[0].Connections) > 0 && len(results[0].Connections[0].Name) == 0 {
t.Fatalf("name is gone: %+v", results)
}
}
return ones
}