Working but i shouldve made connections a map
parent
9338cf86c9
commit
82975a7101
6
main.go
6
main.go
|
|
@ -1,9 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"local/whodunit/config"
|
"local/dndex/config"
|
||||||
"local/whodunit/storage"
|
"local/dndex/storage"
|
||||||
"local/whodunit/view"
|
"local/dndex/view"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -59,5 +60,32 @@ func (o One) MarshalBSON() ([]byte, error) {
|
||||||
m[Name] = name
|
m[Name] = name
|
||||||
delete(m, JSONName)
|
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)
|
return bson.Marshal(m)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,9 @@ func TestOneMarshalBSON(t *testing.T) {
|
||||||
"modified changes": {
|
"modified changes": {
|
||||||
one: (One{Name: "hello", Type: "world", Modified: 1}),
|
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 {
|
for name, d := range cases {
|
||||||
|
|
@ -53,6 +56,9 @@ func TestOneMarshalBSON(t *testing.T) {
|
||||||
}
|
}
|
||||||
c.one.Modified = 0
|
c.one.Modified = 0
|
||||||
one.Modified = 0
|
one.Modified = 0
|
||||||
|
for i := range one.Connections {
|
||||||
|
one.Connections[i].Modified = 0
|
||||||
|
}
|
||||||
if fmt.Sprint(c.one) != fmt.Sprint(one) {
|
if fmt.Sprint(c.one) != fmt.Sprint(one) {
|
||||||
t.Error(c.one, one)
|
t.Error(c.one, one)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"local/whodunit/storage/entity"
|
"local/dndex/storage/entity"
|
||||||
"local/whodunit/storage/operator"
|
"local/dndex/storage/operator"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"local/whodunit/storage/entity"
|
"local/dndex/storage/entity"
|
||||||
"local/whodunit/storage/operator"
|
"local/dndex/storage/operator"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ package storage
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"local/whodunit/config"
|
"local/dndex/config"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ package operator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"local/whodunit/storage/entity"
|
"local/dndex/storage/entity"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
|
|
||||||
22
view/html.go
22
view/html.go
|
|
@ -3,9 +3,9 @@ package view
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"local/whodunit/config"
|
"local/dndex/config"
|
||||||
"local/whodunit/storage"
|
"local/dndex/storage"
|
||||||
"local/whodunit/storage/entity"
|
"local/dndex/storage/entity"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
@ -47,17 +47,25 @@ func who(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
|
||||||
ones = append(ones, entity.One{})
|
ones = append(ones, entity.One{})
|
||||||
}
|
}
|
||||||
one := ones[0]
|
one := ones[0]
|
||||||
results[id] = one
|
|
||||||
if verbose {
|
if verbose {
|
||||||
ones, err := g.List(r.Context(), one.Peers()...)
|
ones, err := g.List(r.Context(), one.Peers()...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for i, one := range ones {
|
for _, another := range ones {
|
||||||
one.Connections = nil
|
another.Connections = nil
|
||||||
results[id].Connections[i] = one
|
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)
|
log.Println("results:", results)
|
||||||
return json.NewEncoder(w).Encode(results)
|
return json.NewEncoder(w).Encode(results)
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,9 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"local/whodunit/config"
|
"local/dndex/config"
|
||||||
"local/whodunit/storage"
|
"local/dndex/storage"
|
||||||
"local/whodunit/storage/entity"
|
"local/dndex/storage/entity"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -25,6 +25,7 @@ func TestHtml(t *testing.T) {
|
||||||
|
|
||||||
g := storage.NewGraph()
|
g := storage.NewGraph()
|
||||||
ones := fillDB(t, g)
|
ones := fillDB(t, g)
|
||||||
|
want := ones[len(ones)-1]
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if err := Html(g); err != nil {
|
if err := Html(g); err != nil {
|
||||||
|
|
@ -32,7 +33,7 @@ func TestHtml(t *testing.T) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
time.Sleep(time.Millisecond * 250)
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
@ -45,6 +46,46 @@ func TestHtml(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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)
|
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 {
|
if err := g.Insert(context.TODO(), ones[i]); err != nil {
|
||||||
t.Fatal(err)
|
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
|
return ones
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue