Skip to content

Commit

Permalink
fix collection/namespace switching, and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bubbajoe committed Jun 2, 2024
1 parent cb7827a commit cd90c20
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 35 deletions.
16 changes: 11 additions & 5 deletions internal/admin/routes/collection_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func ConfigureCollectionAPI(server chi.Router, logger *zap.Logger, cs changestat
if oldCollection, ok := rm.GetCollection(collection.Name, collection.NamespaceName); ok {
if oldCollection.Type == spec.CollectionTypeDocument {
docs, err := dm.GetDocuments(
collection.NamespaceName, collection.Name, 0, 0)
collection.Name,
collection.NamespaceName,
0, 0,
)

Check warning on line 54 in internal/admin/routes/collection_routes.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/routes/collection_routes.go#L51-L54

Added lines #L51 - L54 were not covered by tests
if err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -142,7 +145,7 @@ func ConfigureCollectionAPI(server chi.Router, logger *zap.Logger, cs changestat
util.JsonError(w, http.StatusBadRequest, "offset must be an integer")
return
}
docs, err := dm.GetDocuments(namespaceName, collectionName, offset, limit)
docs, err := dm.GetDocuments(collectionName, namespaceName, offset, limit)

Check warning on line 148 in internal/admin/routes/collection_routes.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/routes/collection_routes.go#L148

Added line #L148 was not covered by tests
if err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -194,7 +197,7 @@ func ConfigureCollectionAPI(server chi.Router, logger *zap.Logger, cs changestat
return
}

document, err := dm.GetDocumentByID(namespaceName, collectionName, documentId)
document, err := dm.GetDocumentByID(collectionName, namespaceName, documentId)

Check warning on line 200 in internal/admin/routes/collection_routes.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/routes/collection_routes.go#L200

Added line #L200 was not covered by tests
if err != nil {
util.JsonError(w, http.StatusNotFound, err.Error())
return
Expand Down Expand Up @@ -348,7 +351,7 @@ func ConfigureCollectionAPI(server chi.Router, logger *zap.Logger, cs changestat
util.JsonError(w, http.StatusBadRequest, "document_id is required")
return
}
document, err := dm.GetDocumentByID(namespaceName, collectionName, documentId)
document, err := dm.GetDocumentByID(collectionName, namespaceName, documentId)

Check warning on line 354 in internal/admin/routes/collection_routes.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/routes/collection_routes.go#L354

Added line #L354 was not covered by tests
if err != nil {
util.JsonError(w, http.StatusNotFound, err.Error())
return
Expand Down Expand Up @@ -386,7 +389,10 @@ func ConfigureCollectionAPI(server chi.Router, logger *zap.Logger, cs changestat
}
if collection.Type == spec.CollectionTypeDocument {
docs, err := dm.GetDocuments(
namespaceName, collectionName, 0, 1)
collectionName,
namespaceName,
1, 1,
)

Check warning on line 395 in internal/admin/routes/collection_routes.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/routes/collection_routes.go#L392-L395

Added lines #L392 - L395 were not covered by tests
if err != nil {
util.JsonError(w, http.StatusInternalServerError, err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion internal/admin/routes/module_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
func TestAdminRoutes_Module(t *testing.T) {
namespaces := []string{"default", "test"}
for _, ns := range namespaces {
config := configtest.NewTest3DGateConfig()
config := configtest.NewTest4DGateConfig()
ps := proxy.NewProxyState(zap.NewNop(), config)
mux := chi.NewMux()
mux.Route("/api/v1", func(r chi.Router) {
Expand Down
2 changes: 1 addition & 1 deletion internal/admin/routes/service_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
func TestAdminRoutes_Service(t *testing.T) {
namespaces := []string{"default", "test"}
for _, ns := range namespaces {
config := configtest.NewTest3DGateConfig()
config := configtest.NewTest4DGateConfig()
ps := proxy.NewProxyState(zap.NewNop(), config)
mux := chi.NewMux()
mux.Route("/api/v1", func(r chi.Router) {
Expand Down
17 changes: 17 additions & 0 deletions internal/config/configtest/dgate_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ func NewTest3DGateConfig() *config.DGateConfig {
return conf
}

func NewTest4DGateConfig() *config.DGateConfig {
conf := NewTestDGateConfig()
conf.DisableDefaultNamespace = false
conf.ProxyConfig = config.DGateProxyConfig{
Host: "localhost",
Port: 16436,
InitResources: &config.DGateResources{
Namespaces: []spec.Namespace{
{
Name: "test",
},
},
},
}
return conf
}

func NewTestDGateConfig_DomainAndNamespaces() *config.DGateConfig {
conf := NewTestDGateConfig()
conf.ProxyConfig.InitResources.Namespaces = []spec.Namespace{
Expand Down
4 changes: 2 additions & 2 deletions internal/proxy/proxy_documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (ps *ProxyState) GetDocuments(collection, namespace string, limit, offset i
if _, ok := ps.rm.GetNamespace(namespace); !ok {
return nil, spec.ErrNamespaceNotFound(namespace)
}
if _, ok := ps.rm.GetCollection(namespace, collection); !ok {
if _, ok := ps.rm.GetCollection(collection, namespace); !ok {
return nil, spec.ErrCollectionNotFound(collection)
}
return ps.store.FetchDocuments(namespace, collection, limit, offset)
Expand All @@ -26,7 +26,7 @@ func (ps *ProxyState) GetDocumentByID(namespace, collection, docId string) (*spe
if _, ok := ps.rm.GetNamespace(namespace); !ok {
return nil, spec.ErrNamespaceNotFound(namespace)
}
if _, ok := ps.rm.GetCollection(collection, namespace); !ok {
if _, ok := ps.rm.GetCollection(namespace, collection); !ok {

Check warning on line 29 in internal/proxy/proxy_documents.go

View check run for this annotation

Codecov / codecov/patch

internal/proxy/proxy_documents.go#L29

Added line #L29 was not covered by tests
return nil, spec.ErrCollectionNotFound(collection)
}
return ps.store.FetchDocument(namespace, collection, docId)
Expand Down
2 changes: 1 addition & 1 deletion internal/proxy/proxy_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestProxyHandler_ReverseProxy(t *testing.T) {
modBuf := NewMockModulePool()
modBuf.On("Borrow").Return(modExt).Once()
modBuf.On("Return", modExt).Return().Once()
modBuf.On("Close").Return().Once()
reqCtxProvider.SetModulePool(modBuf)

modPool := NewMockModulePool()
Expand Down Expand Up @@ -178,7 +179,6 @@ func TestProxyHandler_ProxyHandlerError(t *testing.T) {
modPool := NewMockModulePool()
modPool.On("Borrow").Return(modExt).Once()
modPool.On("Return", modExt).Return().Once()

reqCtxProvider := proxy.NewRequestContextProvider(rt, ps)
reqCtxProvider.SetModulePool(modPool)
reqCtx := reqCtxProvider.CreateRequestContext(
Expand Down
42 changes: 22 additions & 20 deletions internal/proxy/proxy_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ func TestProcessChangeLog_Route(t *testing.T) {
}

func TestProcessChangeLog_Service(t *testing.T) {
conf := configtest.NewTestDGateConfig()
conf := configtest.NewTest4DGateConfig()
ps := proxy.NewProxyState(zap.NewNop(), conf)
if err := ps.Store().InitStore(); err != nil {
t.Fatal(err)
}

s := &spec.Service{
Name: "test",
Name: "test123",
NamespaceName: "test",
URLs: []string{"http://localhost:8080"},
Tags: []string{"test"},
Expand Down Expand Up @@ -302,14 +302,14 @@ func TestProcessChangeLog_Service(t *testing.T) {
}

func TestProcessChangeLog_Module(t *testing.T) {
conf := configtest.NewTestDGateConfig()
conf := configtest.NewTest4DGateConfig()
ps := proxy.NewProxyState(zap.NewNop(), conf)
if err := ps.Store().InitStore(); err != nil {
t.Fatal(err)
}

m := &spec.Module{
Name: "test",
Name: "test123",
NamespaceName: "test",
Payload: "",
Tags: []string{"test"},
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestProcessChangeLog_Module(t *testing.T) {
}

func TestProcessChangeLog_Namespace(t *testing.T) {
ps := proxy.NewProxyState(zap.NewNop(), configtest.NewTestDGateConfig())
ps := proxy.NewProxyState(zap.NewNop(), configtest.NewTest4DGateConfig())
if err := ps.Store().InitStore(); err != nil {
t.Fatal(err)
}
Expand All @@ -351,28 +351,30 @@ func TestProcessChangeLog_Namespace(t *testing.T) {
if !assert.Nil(t, err, "error should be nil") {
return
}
namespaces := ps.ResourceManager().GetNamespaces()
assert.Equal(t, 2, len(namespaces), "should have 2 items")
assert.Equal(t, n.Name, namespaces[1].Name, "should have the same name")
ns, ok := ps.ResourceManager().GetNamespace(n.Name)
if !assert.True(t, ok, "should be true") {
return
}
assert.Equal(t, n.Name, ns.Name, "should have the same name")

cl = spec.NewChangeLog(n, n.Name, spec.DeleteNamespaceCommand)
err = ps.ProcessChangeLog(cl, false)
if !assert.Nil(t, err, "error should be nil") {
return
}
namespaces = ps.ResourceManager().GetNamespaces()
assert.Equal(t, 1, len(namespaces), "should have 0 item")
_, ok = ps.ResourceManager().GetNamespace(n.Name)
assert.False(t, ok, "should be false")
}

func TestProcessChangeLog_Collection(t *testing.T) {
conf := configtest.NewTestDGateConfig()
conf := configtest.NewTest4DGateConfig()
ps := proxy.NewProxyState(zap.NewNop(), conf)
if err := ps.Store().InitStore(); err != nil {
t.Fatal(err)
}

c := &spec.Collection{
Name: "test",
Name: "test123",
NamespaceName: "test",
// Type: spec.CollectionTypeDocument,
Visibility: spec.CollectionVisibilityPrivate,
Expand Down Expand Up @@ -409,11 +411,11 @@ func TestProcessChangeLog_Document(t *testing.T) {
}

c := &spec.Collection{
Name: "test",
Name: "test123",
NamespaceName: "test",
// Type: spec.CollectionTypeDocument,
Visibility: spec.CollectionVisibilityPrivate,
Tags: []string{"test"},
Type: spec.CollectionTypeDocument,
Visibility: spec.CollectionVisibilityPrivate,
Tags: []string{"test"},
}

cl := spec.NewChangeLog(c, c.NamespaceName, spec.AddCollectionCommand)
Expand All @@ -423,9 +425,9 @@ func TestProcessChangeLog_Document(t *testing.T) {
}

d := &spec.Document{
ID: "test",
ID: "test123",
CollectionName: "test123",
NamespaceName: "test",
CollectionName: "test",
Data: "",
}

Expand All @@ -435,7 +437,7 @@ func TestProcessChangeLog_Document(t *testing.T) {
return
}
documents, err := ps.DocumentManager().GetDocuments(
"test", "test", 999, 0,
d.CollectionName, d.NamespaceName, 999, 0,
)
if !assert.Nil(t, err, "error should be nil") {
return
Expand All @@ -452,7 +454,7 @@ func TestProcessChangeLog_Document(t *testing.T) {
return
}
documents, err = ps.DocumentManager().GetDocuments(
"test", "test", 999, 0,
d.CollectionName, d.NamespaceName, 999, 0,
)
if !assert.Nil(t, err, "error should be nil") {
return
Expand Down
11 changes: 7 additions & 4 deletions pkg/modules/dgate/state/state_mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (hp *ResourcesModule) fetchDocument(collection, docId string) *goja.Promise
return
}
doc, err := state.DocumentManager().
GetDocumentByID(namespace.(string), collection, docId)
GetDocumentByID(collection, namespace.(string), docId)

Check warning on line 74 in pkg/modules/dgate/state/state_mod.go

View check run for this annotation

Codecov / codecov/patch

pkg/modules/dgate/state/state_mod.go#L74

Added line #L74 was not covered by tests
if err != nil {
reject(rt.NewGoError(err))
return
Expand All @@ -87,11 +87,11 @@ func (hp *ResourcesModule) fetchDocuments(args goja.FunctionCall) (*goja.Promise
loop := hp.modCtx.EventLoop()
rt := hp.modCtx.Runtime()

collection_name := ""
collection := ""

Check warning on line 90 in pkg/modules/dgate/state/state_mod.go

View check run for this annotation

Codecov / codecov/patch

pkg/modules/dgate/state/state_mod.go#L90

Added line #L90 was not covered by tests
if args.Argument(0) == goja.Undefined() {
return nil, errors.New("collection name is required")
} else {
collection_name = args.Argument(0).String()
collection = args.Argument(0).String()

Check warning on line 94 in pkg/modules/dgate/state/state_mod.go

View check run for this annotation

Codecov / codecov/patch

pkg/modules/dgate/state/state_mod.go#L94

Added line #L94 was not covered by tests
}
limit := 0
if args.Argument(1) != goja.Undefined() {
Expand All @@ -111,7 +111,10 @@ func (hp *ResourcesModule) fetchDocuments(args goja.FunctionCall) (*goja.Promise
docPromise, resolve, reject := rt.NewPromise()
loop.RunOnLoop(func(rt *goja.Runtime) {
doc, err := state.DocumentManager().
GetDocuments(namespace, collection_name, limit, offset)
GetDocuments(
collection, namespace,
limit, offset,
)

Check warning on line 117 in pkg/modules/dgate/state/state_mod.go

View check run for this annotation

Codecov / codecov/patch

pkg/modules/dgate/state/state_mod.go#L114-L117

Added lines #L114 - L117 were not covered by tests
if err != nil {
reject(rt.NewGoError(err))
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/resources/document_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
)

type DocumentManager interface {
GetDocumentByID(namespace, collection, id string) (*spec.Document, error)
GetDocumentByID(collection, namespace, id string) (*spec.Document, error)
GetDocuments(collection, namespace string, limit, offset int) ([]*spec.Document, error)
}

0 comments on commit cd90c20

Please sign in to comment.