From 82975a7101ede5d1771330de7488e7dc166d5086 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Wed, 22 Jul 2020 21:54:24 -0600 Subject: [PATCH] Working but i shouldve made connections a map --- main.go | 6 ++-- storage/entity/one.go | 28 ++++++++++++++++++ storage/entity/one_test.go | 6 ++++ storage/graph.go | 4 +-- storage/graph_test.go | 4 +-- storage/mon.go | 2 +- storage/operator/modify.go | 2 +- view/html.go | 22 ++++++++++----- view/html_test.go | 58 +++++++++++++++++++++++++++++++++++--- 9 files changed, 112 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index 94d9a2b..61896e5 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,9 @@ package main import ( - "local/whodunit/config" - "local/whodunit/storage" - "local/whodunit/view" + "local/dndex/config" + "local/dndex/storage" + "local/dndex/view" "log" ) diff --git a/storage/entity/one.go b/storage/entity/one.go index e849770..01e581b 100644 --- a/storage/entity/one.go +++ b/storage/entity/one.go @@ -6,6 +6,7 @@ import ( "time" "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" ) const ( @@ -59,5 +60,32 @@ func (o One) MarshalBSON() ([]byte, error) { m[Name] = name delete(m, JSONName) } + var connections primitive.A + switch m[Connections].(type) { + case nil: + case []interface{}: + connections = primitive.A(m[Connections].([]interface{})) + case primitive.A: + connections = m[Connections].(primitive.A) + default: + return nil, fmt.Errorf("bad type for %q: %T", Connections, m[Connections]) + } + curL := len(connections) + wantL := len(o.Connections) + for i := curL; i < wantL; i++ { + connections = append(connections, nil) + } + for i, connection := range o.Connections { + b, err := bson.Marshal(connection) + if err != nil { + return nil, err + } + m := bson.M{} + if err := bson.Unmarshal(b, &m); err != nil { + return nil, err + } + connections[i] = m + } + m[Connections] = connections return bson.Marshal(m) } diff --git a/storage/entity/one_test.go b/storage/entity/one_test.go index a19f93b..5aefbed 100644 --- a/storage/entity/one_test.go +++ b/storage/entity/one_test.go @@ -30,6 +30,9 @@ func TestOneMarshalBSON(t *testing.T) { "modified changes": { one: (One{Name: "hello", Type: "world", Modified: 1}), }, + "w/ connections": { + one: (One{Name: "hello", Type: "world", Modified: 1, Connections: []One{One{Name: "hi", Relationship: "mom"}}}), + }, } for name, d := range cases { @@ -53,6 +56,9 @@ func TestOneMarshalBSON(t *testing.T) { } c.one.Modified = 0 one.Modified = 0 + for i := range one.Connections { + one.Connections[i].Modified = 0 + } if fmt.Sprint(c.one) != fmt.Sprint(one) { t.Error(c.one, one) } diff --git a/storage/graph.go b/storage/graph.go index 77072f8..915c942 100644 --- a/storage/graph.go +++ b/storage/graph.go @@ -2,8 +2,8 @@ package storage import ( "context" - "local/whodunit/storage/entity" - "local/whodunit/storage/operator" + "local/dndex/storage/entity" + "local/dndex/storage/operator" "go.mongodb.org/mongo-driver/bson" ) diff --git a/storage/graph_test.go b/storage/graph_test.go index 60de57e..c6ca855 100644 --- a/storage/graph_test.go +++ b/storage/graph_test.go @@ -2,8 +2,8 @@ package storage import ( "context" - "local/whodunit/storage/entity" - "local/whodunit/storage/operator" + "local/dndex/storage/entity" + "local/dndex/storage/operator" "os" "testing" "time" diff --git a/storage/mon.go b/storage/mon.go index f391529..1b08c8a 100644 --- a/storage/mon.go +++ b/storage/mon.go @@ -3,7 +3,7 @@ package storage import ( "context" "errors" - "local/whodunit/config" + "local/dndex/config" "log" "time" diff --git a/storage/operator/modify.go b/storage/operator/modify.go index c588821..3e9f9ee 100644 --- a/storage/operator/modify.go +++ b/storage/operator/modify.go @@ -2,7 +2,7 @@ package operator import ( "fmt" - "local/whodunit/storage/entity" + "local/dndex/storage/entity" "time" "go.mongodb.org/mongo-driver/bson" diff --git a/view/html.go b/view/html.go index 454260d..25e26f0 100644 --- a/view/html.go +++ b/view/html.go @@ -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) diff --git a/view/html_test.go b/view/html_test.go index 8d86781..7dfc68c 100644 --- a/view/html_test.go +++ b/view/html_test.go @@ -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 }