Revert "Remove Paginator from GetSuppliers"

This reverts commit 0de660a6e7.
This commit is contained in:
TeoV
2019-04-16 09:31:15 +03:00
parent 365032c4d9
commit 41c0a1f047
3 changed files with 98 additions and 0 deletions

View File

@@ -416,6 +416,16 @@ func (spS *SupplierService) sortedSuppliersForEvent(args *ArgsGetSuppliers) (sor
if err != nil {
return nil, err
}
if args.Paginator.Offset != nil {
if *args.Paginator.Offset <= len(sortedSuppliers.SortedSuppliers) {
sortedSuppliers.SortedSuppliers = sortedSuppliers.SortedSuppliers[*args.Paginator.Offset:]
}
}
if args.Paginator.Limit != nil {
if *args.Paginator.Limit <= len(sortedSuppliers.SortedSuppliers) {
sortedSuppliers.SortedSuppliers = sortedSuppliers.SortedSuppliers[:*args.Paginator.Limit]
}
}
return sortedSuppliers, nil
}
@@ -423,6 +433,7 @@ type ArgsGetSuppliers struct {
IgnoreErrors bool
MaxCost string // toDo: try with interface{} here
utils.CGREvent
utils.Paginator
*utils.ArgDispatcher
}

View File

@@ -492,6 +492,92 @@ func TestSuppliersSortedForEvent(t *testing.T) {
}
}
func TestSuppliersSortedForEventWithLimit(t *testing.T) {
eFirstSupplierProfile := &SortedSuppliers{
ProfileID: "SupplierProfile2",
Sorting: utils.MetaWeight,
SortedSuppliers: []*SortedSupplier{
{
SupplierID: "supplier1",
SortingData: map[string]interface{}{
"Weight": 30.0,
},
SupplierParameters: "param1",
},
{
SupplierID: "supplier2",
SortingData: map[string]interface{}{
"Weight": 20.0,
},
SupplierParameters: "param2",
},
},
}
argsGetSuppliers[1].Paginator = utils.Paginator{
Limit: utils.IntPointer(2),
}
sprf, err := splService.sortedSuppliersForEvent(argsGetSuppliers[1])
if err != nil {
t.Errorf("Error: %+v", err)
}
if !reflect.DeepEqual(eFirstSupplierProfile, sprf) {
t.Errorf("Expecting: %+v, received: %+v", eFirstSupplierProfile, sprf)
}
}
func TestSuppliersSortedForEventWithOffset(t *testing.T) {
eFirstSupplierProfile := &SortedSuppliers{
ProfileID: "SupplierProfile2",
Sorting: utils.MetaWeight,
SortedSuppliers: []*SortedSupplier{
{
SupplierID: "supplier3",
SortingData: map[string]interface{}{
"Weight": 10.0,
},
SupplierParameters: "param3",
},
},
}
argsGetSuppliers[1].Paginator = utils.Paginator{
Offset: utils.IntPointer(2),
}
sprf, err := splService.sortedSuppliersForEvent(argsGetSuppliers[1])
if err != nil {
t.Errorf("Error: %+v", err)
}
if !reflect.DeepEqual(eFirstSupplierProfile, sprf) {
t.Errorf("Expecting: %+v,received: %+v", utils.ToJSON(eFirstSupplierProfile), utils.ToJSON(sprf))
}
}
func TestSuppliersSortedForEventWithLimitAndOffset(t *testing.T) {
eFirstSupplierProfile := &SortedSuppliers{
ProfileID: "SupplierProfile2",
Sorting: utils.MetaWeight,
SortedSuppliers: []*SortedSupplier{
{
SupplierID: "supplier2",
SortingData: map[string]interface{}{
"Weight": 20.0,
},
SupplierParameters: "param2",
},
},
}
argsGetSuppliers[1].Paginator = utils.Paginator{
Limit: utils.IntPointer(1),
Offset: utils.IntPointer(1),
}
sprf, err := splService.sortedSuppliersForEvent(argsGetSuppliers[1])
if err != nil {
t.Errorf("Error: %+v", err)
}
if !reflect.DeepEqual(eFirstSupplierProfile, sprf) {
t.Errorf("Expecting: %+v,received: %+v", utils.ToJSON(eFirstSupplierProfile), utils.ToJSON(sprf))
}
}
func TestSuppliersAsOptsGetSuppliers(t *testing.T) {
s := &ArgsGetSuppliers{
IgnoreErrors: true,

View File

@@ -1720,6 +1720,7 @@ func (sS *SessionS) BiRPCv1AuthorizeEvent(clnt rpcclient.RpcClientConnection,
IgnoreErrors: args.SuppliersIgnoreErrors,
MaxCost: args.SuppliersMaxCost,
CGREvent: *cgrEv,
Paginator: args.Paginator,
ArgDispatcher: args.ArgDispatcher,
}
if err = sS.splS.Call(utils.SupplierSv1GetSuppliers,