Adjust + Test
This commit is contained in:
@@ -4,23 +4,31 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type computeUnitsMongoAccessor[T LiveInterface] struct {
|
||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||
utils.AbstractAccessor[LiveInterface] // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||
}
|
||||
|
||||
// New creates a new instance of the computeUnitsMongoAccessor
|
||||
func NewAccessor[T LiveInterface](t tools.DataType, request *tools.APIRequest) *computeUnitsMongoAccessor[T] {
|
||||
return &computeUnitsMongoAccessor[T]{
|
||||
AbstractAccessor: utils.AbstractAccessor{
|
||||
AbstractAccessor: utils.AbstractAccessor[LiveInterface]{
|
||||
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
||||
Request: request,
|
||||
Type: t,
|
||||
New: func() LiveInterface {
|
||||
switch t {
|
||||
case tools.LIVE_DATACENTER:
|
||||
return &LiveDatacenter{}
|
||||
case tools.LIVE_STORAGE:
|
||||
return &LiveStorage{}
|
||||
}
|
||||
return &LiveDatacenter{}
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -28,19 +36,6 @@ func NewAccessor[T LiveInterface](t tools.DataType, request *tools.APIRequest) *
|
||||
/*
|
||||
* Nothing special here, just the basic CRUD operations
|
||||
*/
|
||||
func (a *computeUnitsMongoAccessor[T]) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericDeleteOne(id, a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
// should verify if a source is existing...
|
||||
return utils.GenericUpdateOne(set, id, a, &LiveDatacenter{})
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data.(*LiveDatacenter), a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
// is a publisher... that become a resources.
|
||||
if data.IsDrafted() {
|
||||
@@ -95,23 +90,3 @@ func (a *computeUnitsMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObj
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericLoadOne[T](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||
return d, 200, nil
|
||||
}, a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericLoadAll[T](a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericSearch[*LiveDatacenter](filters, search, (&LiveDatacenter{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||
return d
|
||||
}
|
||||
}
|
||||
|
||||
144
models/live/tests/live_test.go
Normal file
144
models/live/tests/live_test.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package live_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/live"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// ---- AbstractLive (via LiveDatacenter embedding) ----
|
||||
|
||||
func TestAbstractLive_StoreDraftDefault(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
dc.StoreDraftDefault()
|
||||
assert.True(t, dc.IsDraft)
|
||||
}
|
||||
|
||||
func TestAbstractLive_CanDelete_Draft(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
dc.IsDraft = true
|
||||
assert.True(t, dc.CanDelete())
|
||||
}
|
||||
|
||||
func TestAbstractLive_CanDelete_NonDraft(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
dc.IsDraft = false
|
||||
assert.False(t, dc.CanDelete())
|
||||
}
|
||||
|
||||
func TestAbstractLive_GetMonitorPath(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
dc.MonitorPath = "/metrics"
|
||||
assert.Equal(t, "/metrics", dc.GetMonitorPath())
|
||||
}
|
||||
|
||||
func TestAbstractLive_GetResourcesID_Empty(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
assert.Empty(t, dc.GetResourcesID())
|
||||
}
|
||||
|
||||
func TestAbstractLive_SetResourcesID_Append(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
dc.SetResourcesID("res-1")
|
||||
assert.Equal(t, []string{"res-1"}, dc.GetResourcesID())
|
||||
}
|
||||
|
||||
func TestAbstractLive_SetResourcesID_NoDuplication(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
dc.SetResourcesID("res-1")
|
||||
dc.SetResourcesID("res-1") // second call should not duplicate
|
||||
assert.Len(t, dc.GetResourcesID(), 1)
|
||||
}
|
||||
|
||||
func TestAbstractLive_SetResourcesID_MultipleDistinct(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
dc.SetResourcesID("res-1")
|
||||
dc.SetResourcesID("res-2")
|
||||
assert.Len(t, dc.GetResourcesID(), 2)
|
||||
}
|
||||
|
||||
func TestAbstractLive_GetResourceType(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
assert.Equal(t, tools.INVALID, dc.GetResourceType())
|
||||
}
|
||||
|
||||
// ---- LiveDatacenter ----
|
||||
|
||||
func TestLiveDatacenter_GetAccessor(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
acc := dc.GetAccessor(&tools.APIRequest{})
|
||||
assert.NotNil(t, acc)
|
||||
}
|
||||
|
||||
func TestLiveDatacenter_GetAccessor_NilRequest(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
acc := dc.GetAccessor(nil)
|
||||
assert.NotNil(t, acc)
|
||||
}
|
||||
|
||||
func TestLiveDatacenter_GetResource(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
res := dc.GetResource()
|
||||
assert.NotNil(t, res)
|
||||
}
|
||||
|
||||
func TestLiveDatacenter_GetResourceInstance(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
inst := dc.GetResourceInstance()
|
||||
assert.NotNil(t, inst)
|
||||
}
|
||||
|
||||
func TestLiveDatacenter_IDAndName(t *testing.T) {
|
||||
dc := &live.LiveDatacenter{}
|
||||
dc.AbstractLive.AbstractObject = utils.AbstractObject{UUID: "dc-id", Name: "dc-name"}
|
||||
assert.Equal(t, "dc-id", dc.GetID())
|
||||
assert.Equal(t, "dc-name", dc.GetName())
|
||||
}
|
||||
|
||||
// ---- LiveStorage ----
|
||||
|
||||
func TestLiveStorage_StoreDraftDefault(t *testing.T) {
|
||||
s := &live.LiveStorage{}
|
||||
s.StoreDraftDefault()
|
||||
assert.True(t, s.IsDraft)
|
||||
}
|
||||
|
||||
func TestLiveStorage_CanDelete_Draft(t *testing.T) {
|
||||
s := &live.LiveStorage{}
|
||||
s.IsDraft = true
|
||||
assert.True(t, s.CanDelete())
|
||||
}
|
||||
|
||||
func TestLiveStorage_CanDelete_NonDraft(t *testing.T) {
|
||||
s := &live.LiveStorage{}
|
||||
s.IsDraft = false
|
||||
assert.False(t, s.CanDelete())
|
||||
}
|
||||
|
||||
func TestLiveStorage_GetAccessor(t *testing.T) {
|
||||
s := &live.LiveStorage{}
|
||||
acc := s.GetAccessor(&tools.APIRequest{})
|
||||
assert.NotNil(t, acc)
|
||||
}
|
||||
|
||||
func TestLiveStorage_GetResource(t *testing.T) {
|
||||
s := &live.LiveStorage{}
|
||||
res := s.GetResource()
|
||||
assert.NotNil(t, res)
|
||||
}
|
||||
|
||||
func TestLiveStorage_GetResourceInstance(t *testing.T) {
|
||||
s := &live.LiveStorage{}
|
||||
inst := s.GetResourceInstance()
|
||||
assert.NotNil(t, inst)
|
||||
}
|
||||
|
||||
func TestLiveStorage_SetResourcesID_NoDuplication(t *testing.T) {
|
||||
s := &live.LiveStorage{}
|
||||
s.SetResourcesID("storage-1")
|
||||
s.SetResourcesID("storage-1")
|
||||
assert.Len(t, s.GetResourcesID(), 1)
|
||||
}
|
||||
Reference in New Issue
Block a user