From 71b1de33ec9a4a8ee397deed5d11b705c9066651 Mon Sep 17 00:00:00 2001 From: breel Date: Sat, 1 Aug 2020 01:09:35 -0600 Subject: [PATCH] cleaner tests i thought --- storage/driver/boltdb.go | 6 +- storage/driver/boltdb_test.go | 3 +- storage/driver/driver.go | 9 +- storage/driver/mon.go | 4 +- storage/graph.go | 13 +- storage/ratelimitedgraph.go | 4 +- view/who_test.go | 243 ++++++++++++++++++---------------- 7 files changed, 141 insertions(+), 141 deletions(-) diff --git a/storage/driver/boltdb.go b/storage/driver/boltdb.go index 80d1872..5e6ad26 100644 --- a/storage/driver/boltdb.go +++ b/storage/driver/boltdb.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "local/dndex/config" "local/dndex/storage/entity" "os" "regexp" @@ -20,9 +19,8 @@ type BoltDB struct { db *bolt.DB } -func NewBoltDB() *BoltDB { - config := config.New() - db, err := bolt.Open(config.DBURI, os.ModePerm, &bolt.Options{ +func NewBoltDB(path string) *BoltDB { + db, err := bolt.Open(path, os.ModePerm, &bolt.Options{ Timeout: time.Second * 3, }) if err != nil { diff --git a/storage/driver/boltdb_test.go b/storage/driver/boltdb_test.go index 833d2f0..afc99ef 100644 --- a/storage/driver/boltdb_test.go +++ b/storage/driver/boltdb_test.go @@ -364,8 +364,7 @@ func tempBoltDB(t *testing.T) (*BoltDB, func()) { t.Fatal(err) } f.Close() - os.Args = []string{"a", "-dburi", f.Name()} - bdb := NewBoltDB() + bdb := NewBoltDB(f.Name()) fillBoltDB(t, bdb) return bdb, func() { bdb.db.Close() diff --git a/storage/driver/driver.go b/storage/driver/driver.go index 948ac42..5ef12bf 100644 --- a/storage/driver/driver.go +++ b/storage/driver/driver.go @@ -15,12 +15,15 @@ type Driver interface { Delete(context.Context, string, interface{}) error } -func New() Driver { +func New(path ...string) Driver { + if len(path) == 0 { + path = []string{config.New().DBURI} + } switch strings.ToLower(config.New().DriverType) { case "mongo": - return NewMongo() + return NewMongo(path[0]) case "boltdb": - return NewBoltDB() + return NewBoltDB(path[0]) } panic("unknown driver type " + strings.ToLower(config.New().DriverType)) } diff --git a/storage/driver/mon.go b/storage/driver/mon.go index 8548997..95c4b81 100644 --- a/storage/driver/mon.go +++ b/storage/driver/mon.go @@ -17,8 +17,8 @@ type Mongo struct { db string } -func NewMongo() Mongo { - opts := options.Client().ApplyURI(config.New().DBURI) +func NewMongo(path string) Mongo { + opts := options.Client().ApplyURI(path) c, err := mongo.NewClient(opts) if err != nil { panic(err) diff --git a/storage/graph.go b/storage/graph.go index 609841c..10ae8a7 100644 --- a/storage/graph.go +++ b/storage/graph.go @@ -3,11 +3,9 @@ package storage import ( "context" "fmt" - "local/dndex/config" "local/dndex/storage/driver" "local/dndex/storage/entity" "local/dndex/storage/operator" - "strings" "go.mongodb.org/mongo-driver/bson" ) @@ -16,16 +14,9 @@ type Graph struct { driver driver.Driver } -func NewGraph() Graph { - var d driver.Driver - switch strings.ToLower(config.New().DriverType) { - case "mongo": - d = driver.NewMongo() - case "boltdb": - d = driver.NewBoltDB() - } +func NewGraph(path ...string) Graph { return Graph{ - driver: d, + driver: driver.New(path...), } } diff --git a/storage/ratelimitedgraph.go b/storage/ratelimitedgraph.go index 2c344c0..b99652b 100644 --- a/storage/ratelimitedgraph.go +++ b/storage/ratelimitedgraph.go @@ -16,9 +16,9 @@ type RateLimitedGraph struct { limiters *sync.Map } -func NewRateLimitedGraph() RateLimitedGraph { +func NewRateLimitedGraph(path ...string) RateLimitedGraph { return RateLimitedGraph{ - g: NewGraph(), + g: NewGraph(path...), rps: config.New().RPS, limiters: &sync.Map{}, } diff --git a/view/who_test.go b/view/who_test.go index d453e42..0907acd 100644 --- a/view/who_test.go +++ b/view/who_test.go @@ -25,38 +25,12 @@ import ( ) func TestWho(t *testing.T) { + t.Parallel() os.Args = os.Args[:1] - if _, ok := os.LookupEnv("DBURI"); !ok { - f, err := ioutil.TempFile(os.TempDir(), "pattern*") - if err != nil { - t.Fatal(err) - } - f.Close() - defer os.Remove(f.Name()) - os.Setenv("DBURI", f.Name()) - } - - t.Logf("config: %+v", config.New()) - - g := storage.NewRateLimitedGraph() - ones := fillDB(t, g) - want := ones[len(ones)-1] - - reset := func() { - if err := g.Delete(context.TODO(), "col", map[string]string{}); err != nil { - t.Fatal(err) - } - ones = fillDB(t, g) - want = ones[len(ones)-1] - } - - handler := jsonHandler(g) - - t.Log(handler, want) t.Run("get no namespace is 404", func(t *testing.T) { - reset() - iwant := want + handler, _, iwant, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodGet, "/who?id="+iwant.Name, nil) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -66,8 +40,8 @@ func TestWho(t *testing.T) { }) t.Run("get fake", func(t *testing.T) { - reset() - iwant := want + handler, _, iwant, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodGet, "/who?namespace=col&id=FAKER"+iwant.Name, nil) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -77,8 +51,8 @@ func TestWho(t *testing.T) { }) t.Run("get real", func(t *testing.T) { - reset() - iwant := want + handler, _, iwant, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodGet, "/who?namespace=col&id="+iwant.Name, nil) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -124,8 +98,8 @@ func TestWho(t *testing.T) { }) t.Run("get real md", func(t *testing.T) { - reset() - iwant := want + handler, _, iwant, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodGet, "/who?namespace=col&md&id="+iwant.Name, nil) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -159,8 +133,8 @@ func TestWho(t *testing.T) { }) t.Run("get real light", func(t *testing.T) { - reset() - iwant := want + handler, _, iwant, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodGet, "/who?namespace=col&light&id="+iwant.Name, nil) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -194,10 +168,9 @@ func TestWho(t *testing.T) { }) t.Run("get real but case is wrong", func(t *testing.T) { - reset() - iwant := want - iwantName := strings.ToUpper(iwant.Name) - r := httptest.NewRequest(http.MethodGet, "/who?namespace=col&id="+iwantName, nil) + handler, _, iwant, _, can := fresh(t) + defer can() + r := httptest.NewRequest(http.MethodGet, "/who?namespace=col&id="+strings.ToUpper(iwant.Name), nil) w := httptest.NewRecorder() handler.ServeHTTP(w, r) if w.Code != http.StatusOK { @@ -226,7 +199,8 @@ func TestWho(t *testing.T) { t.Run("get sorted type asc/desc", func(t *testing.T) { for _, order := range []string{"1", "-1"} { - reset() + handler, ones, _, _, can := fresh(t) + defer can() want := ones[len(ones)-1] r := httptest.NewRequest(http.MethodGet, "/who?namespace=col&light&sort=type&order="+order+"&id="+want.Name, nil) w := httptest.NewRecorder() @@ -268,8 +242,8 @@ func TestWho(t *testing.T) { }) t.Run("put fake", func(t *testing.T) { - reset() - iwant := want + handler, _, iwant, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodPut, "/who?namespace=col&id=FAKER"+iwant.Name, strings.NewReader(`{"title":"this should fail to find someone"}`)) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -279,8 +253,8 @@ func TestWho(t *testing.T) { }) t.Run("put real", func(t *testing.T) { - reset() - iwant := want + handler, _, iwant, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodPut, "/who?namespace=col&id="+iwant.Name, strings.NewReader(`{"title":"this should work"}`)) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -302,9 +276,9 @@ func TestWho(t *testing.T) { }) t.Run("post exists", func(t *testing.T) { - reset() - iwant := want - iwant.Name = "" + handler, _, iwant, _, can := fresh(t) + defer can() + want := iwant r := httptest.NewRequest(http.MethodPost, "/who?namespace=col&id="+want.Name, strings.NewReader(`{"title":"this should fail to insert"}`)) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -314,7 +288,8 @@ func TestWho(t *testing.T) { }) t.Run("post real", func(t *testing.T) { - reset() + handler, _, want, _, can := fresh(t) + defer can() iwant := want iwant.Name = "" b, err := json.Marshal(iwant) @@ -342,7 +317,8 @@ func TestWho(t *testing.T) { }) t.Run("post real with spaces, #, and special chars in name", func(t *testing.T) { - reset() + handler, _, want, _, can := fresh(t) + defer can() want.Name = "hello world #1 e ę" want.Connections = nil b, err := json.Marshal(want) @@ -373,7 +349,8 @@ func TestWho(t *testing.T) { }) t.Run("delete real", func(t *testing.T) { - reset() + handler, _, want, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodDelete, "/who?namespace=col&id=NEWBIE"+want.Name, nil) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -389,7 +366,8 @@ func TestWho(t *testing.T) { }) t.Run("delete real with %20%23 (' #')", func(t *testing.T) { - reset() + handler, _, want, _, can := fresh(t) + defer can() want.Name = "hello world #4" want.Connections = nil @@ -429,7 +407,8 @@ func TestWho(t *testing.T) { }) t.Run("delete real with special chars", func(t *testing.T) { - reset() + handler, _, want, _, can := fresh(t) + defer can() want.Name = "hello world #1 e ę" want.Connections = nil @@ -468,7 +447,8 @@ func TestWho(t *testing.T) { }) t.Run("delete fake", func(t *testing.T) { - reset() + handler, _, want, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodDelete, "/who?namespace=col&id=FAKER"+want.Name, nil) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -478,7 +458,8 @@ func TestWho(t *testing.T) { }) t.Run("delete regexp should be sanitized", func(t *testing.T) { - reset() + handler, _, _, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodDelete, "/who?namespace=col&id=.*", nil) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -503,7 +484,8 @@ func TestWho(t *testing.T) { }) t.Run("patch fake", func(t *testing.T) { - reset() + handler, _, want, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodPatch, "/who?namespace=col&id=FAKER"+want.Name, nil) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -512,9 +494,9 @@ func TestWho(t *testing.T) { } }) - want = ones[4] t.Run("patch real against existing", func(t *testing.T) { - reset() + handler, ones, _, _, can := fresh(t) + defer can() from := ones[4] push := ones[10].Peer() push.Relationship = "spawn" @@ -561,9 +543,9 @@ func TestWho(t *testing.T) { t.Logf("%s", w.Body.Bytes()) }) - want = ones[2] t.Run("patch real", func(t *testing.T) { - reset() + handler, _, want, _, can := fresh(t) + defer can() iwant := want iwant.Relationship = "spawn" iwant.Name = "child of " + want.Name @@ -599,7 +581,8 @@ func TestWho(t *testing.T) { }) t.Run("trace fake", func(t *testing.T) { - reset() + handler, _, _, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodTrace, "/who?namespace=notcol", nil) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -609,7 +592,8 @@ func TestWho(t *testing.T) { }) t.Run("trace real", func(t *testing.T) { - reset() + handler, _, _, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodTrace, "/who?namespace=col", nil) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -627,7 +611,8 @@ func TestWho(t *testing.T) { }) t.Run("get without id == trace real", func(t *testing.T) { - reset() + handler, _, _, _, can := fresh(t) + defer can() r := httptest.NewRequest(http.MethodGet, "/who?namespace=col", nil) w := httptest.NewRecorder() handler.ServeHTTP(w, r) @@ -645,7 +630,8 @@ func TestWho(t *testing.T) { }) t.Run("trace real sorted asc/desc name", func(t *testing.T) { - reset() + handler, _, _, _, can := fresh(t) + defer can() for _, order := range []string{"1", "-1"} { r := httptest.NewRequest(http.MethodTrace, "/who?namespace=col&sort=name&order="+order, nil) w := httptest.NewRecorder() @@ -672,8 +658,8 @@ func TestWho(t *testing.T) { }) t.Run("delete connection 1 of 0 noop but ok", func(t *testing.T) { - reset() - want := ones[0] + handler, _, want, _, can := fresh(t) + defer can() if len(want.Connections) > 0 { t.Fatal(want) } @@ -700,7 +686,9 @@ func TestWho(t *testing.T) { }) t.Run("delete connection 1 of 1 ok", func(t *testing.T) { - reset() + handler, ones, _, _, can := fresh(t) + defer can() + want := ones[5] deleted := want.Peers()[0] r := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/who?namespace=col&id=%s&connection=%s", want.Name, deleted), nil) w := httptest.NewRecorder() @@ -725,8 +713,8 @@ func TestWho(t *testing.T) { }) t.Run("delete connection 1 of 4 ok", func(t *testing.T) { - reset() - + handler, ones, _, _, can := fresh(t) + defer can() want := ones[0] put := entity.One{ @@ -805,53 +793,6 @@ func TestWho(t *testing.T) { }) } -func fillDB(t *testing.T, g storage.RateLimitedGraph) []entity.One { - ones := make([]entity.One, 13) - for i := range ones { - ones[i] = randomOne() - for j := 0; j < i; j++ { - ones[i].Connections[ones[j].Name] = entity.One{ - Name: ones[j].Name, - Type: ones[j].Type, - Relationship: ":D", - } - } - } - for i := range ones { - if err := g.Insert(context.TODO(), "col", ones[i]); err != nil { - t.Fatal(err) - } - if results, err := g.List(context.TODO(), "col", 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 { - for k, v := range results[0].Connections { - if k == "" || v.Name == "" { - t.Fatalf("name is gone: %q:%+v", k, v) - } - } - } - } - return ones -} - -func randomOne() entity.One { - return entity.One{ - Name: "name-" + uuid.New().String()[:5], - Type: "type-" + uuid.New().String()[:5], - Title: "titl-" + uuid.New().String()[:5], - Text: "text-" + uuid.New().String()[:5], - Modified: time.Now().UnixNano(), - Connections: map[string]entity.One{}, - Attachments: map[string]string{ - uuid.New().String()[:5]: uuid.New().String()[:5], - }, - } -} - func TestSortOnes(t *testing.T) { oneA := entity.One{Name: "A", Title: "c", Modified: 2} oneB := entity.One{Name: "B", Title: "b", Modified: 1} @@ -953,3 +894,71 @@ func TestSortOnes(t *testing.T) { }) } } + +func fresh(t *testing.T) (http.Handler, []entity.One, entity.One, storage.RateLimitedGraph, func()) { + f, err := ioutil.TempFile(os.TempDir(), "pattern*") + if err != nil { + t.Fatal(err) + } + f.Close() + + g := storage.NewRateLimitedGraph(f.Name()) + + if err := g.Delete(context.TODO(), "col", map[string]string{}); err != nil { + t.Fatal(err) + } + ones := fillDB(t, g) + + handler := jsonHandler(g) + + return handler, ones, ones[0], g, func() { + os.Remove(f.Name()) + } +} + +func fillDB(t *testing.T, g storage.RateLimitedGraph) []entity.One { + ones := make([]entity.One, 13) + for i := range ones { + ones[i] = randomOne() + for j := 0; j < i; j++ { + ones[i].Connections[ones[j].Name] = entity.One{ + Name: ones[j].Name, + Type: ones[j].Type, + Relationship: ":D", + } + } + } + for i := range ones { + if err := g.Insert(context.TODO(), "col", ones[i]); err != nil { + t.Fatal(err) + } + if results, err := g.List(context.TODO(), "col", 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 { + for k, v := range results[0].Connections { + if k == "" || v.Name == "" { + t.Fatalf("name is gone: %q:%+v", k, v) + } + } + } + } + return ones +} + +func randomOne() entity.One { + return entity.One{ + Name: "name-" + uuid.New().String()[:5], + Type: "type-" + uuid.New().String()[:5], + Title: "titl-" + uuid.New().String()[:5], + Text: "text-" + uuid.New().String()[:5], + Modified: time.Now().UnixNano(), + Connections: map[string]entity.One{}, + Attachments: map[string]string{ + uuid.New().String()[:5]: uuid.New().String()[:5], + }, + } +}