draft tests
This commit is contained in:
@@ -11,13 +11,13 @@ import (
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type bookingMongoAccessor struct {
|
||||
type BookingMongoAccessor struct {
|
||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||
}
|
||||
|
||||
// New creates a new instance of the bookingMongoAccessor
|
||||
func NewAccessor(request *tools.APIRequest) *bookingMongoAccessor {
|
||||
return &bookingMongoAccessor{
|
||||
// New creates a new instance of the BookingMongoAccessor
|
||||
func NewAccessor(request *tools.APIRequest) *BookingMongoAccessor {
|
||||
return &BookingMongoAccessor{
|
||||
AbstractAccessor: utils.AbstractAccessor{
|
||||
Logger: logs.CreateLogger(tools.BOOKING.String()), // Create a logger with the data type
|
||||
Request: request,
|
||||
@@ -29,11 +29,11 @@ func NewAccessor(request *tools.APIRequest) *bookingMongoAccessor {
|
||||
/*
|
||||
* Nothing special here, just the basic CRUD operations
|
||||
*/
|
||||
func (a *bookingMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
func (a *BookingMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericDeleteOne(id, a)
|
||||
}
|
||||
|
||||
func (a *bookingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
func (a *BookingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
if set.(*Booking).State == 0 {
|
||||
return nil, 400, errors.New("state is required")
|
||||
}
|
||||
@@ -41,15 +41,15 @@ func (a *bookingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.D
|
||||
return utils.GenericUpdateOne(realSet, id, a, &Booking{})
|
||||
}
|
||||
|
||||
func (a *bookingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
func (a *BookingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data, a)
|
||||
}
|
||||
|
||||
func (a *bookingMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
func (a *BookingMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data, a)
|
||||
}
|
||||
|
||||
func (a *bookingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
func (a *BookingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericLoadOne[*Booking](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||
now := time.Now()
|
||||
now = now.Add(time.Second * -60)
|
||||
@@ -67,15 +67,15 @@ func (a *bookingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
}, a)
|
||||
}
|
||||
|
||||
func (a *bookingMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
func (a *BookingMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericLoadAll[*Booking](a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *bookingMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
func (a *BookingMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericSearch[*Booking](filters, search, (&Booking{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *bookingMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||
func (a *BookingMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||
now := time.Now()
|
||||
now = now.Add(time.Second * -60)
|
||||
|
||||
87
models/booking/tests/booking_test.go
Normal file
87
models/booking/tests/booking_test.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package booking_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
func TestBooking_GetDurations(t *testing.T) {
|
||||
start := time.Now().Add(-2 * time.Hour)
|
||||
end := start.Add(1 * time.Hour)
|
||||
realStart := start.Add(30 * time.Minute)
|
||||
realEnd := realStart.Add(90 * time.Minute)
|
||||
|
||||
b := &booking.Booking{
|
||||
ExpectedStartDate: start,
|
||||
ExpectedEndDate: &end,
|
||||
RealStartDate: &realStart,
|
||||
RealEndDate: &realEnd,
|
||||
}
|
||||
|
||||
assert.Equal(t, 30*time.Minute, b.GetDelayForLaunch())
|
||||
assert.Equal(t, 90*time.Minute, b.GetRealDuration())
|
||||
assert.Equal(t, end.Sub(start), b.GetUsualDuration())
|
||||
assert.Equal(t, b.GetRealDuration()-b.GetUsualDuration(), b.GetDelayOnDuration())
|
||||
assert.Equal(t, realEnd.Sub(start), b.GetDelayForFinishing())
|
||||
}
|
||||
|
||||
func TestBooking_GetAccessor(t *testing.T) {
|
||||
req := &tools.APIRequest{}
|
||||
b := &booking.Booking{}
|
||||
accessor := b.GetAccessor(req)
|
||||
|
||||
assert.NotNil(t, accessor)
|
||||
assert.Equal(t, tools.BOOKING, accessor.(*booking.BookingMongoAccessor).Type)
|
||||
}
|
||||
|
||||
func TestBooking_VerifyAuth(t *testing.T) {
|
||||
assert.True(t, (&booking.Booking{}).VerifyAuth(nil))
|
||||
}
|
||||
|
||||
func TestBooking_StoreDraftDefault(t *testing.T) {
|
||||
b := &booking.Booking{}
|
||||
b.StoreDraftDefault()
|
||||
assert.False(t, b.IsDraft)
|
||||
}
|
||||
|
||||
func TestBooking_CanUpdate(t *testing.T) {
|
||||
now := time.Now()
|
||||
b := &booking.Booking{
|
||||
State: enum.SCHEDULED,
|
||||
AbstractObject: utils.AbstractObject{IsDraft: false},
|
||||
RealStartDate: &now,
|
||||
}
|
||||
|
||||
set := &booking.Booking{
|
||||
State: enum.DELAYED,
|
||||
RealStartDate: &now,
|
||||
}
|
||||
|
||||
ok, result := b.CanUpdate(set)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, enum.DELAYED, result.(*booking.Booking).State)
|
||||
}
|
||||
|
||||
func TestBooking_CanDelete(t *testing.T) {
|
||||
b := &booking.Booking{AbstractObject: utils.AbstractObject{IsDraft: true}}
|
||||
assert.True(t, b.CanDelete())
|
||||
|
||||
b.IsDraft = false
|
||||
assert.False(t, b.CanDelete())
|
||||
}
|
||||
|
||||
func TestNewAccessor(t *testing.T) {
|
||||
req := &tools.APIRequest{}
|
||||
accessor := booking.NewAccessor(req)
|
||||
|
||||
assert.NotNil(t, accessor)
|
||||
assert.Equal(t, tools.BOOKING, accessor.Type)
|
||||
assert.Equal(t, req, accessor.Request)
|
||||
}
|
||||
Reference in New Issue
Block a user