Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-go
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package Registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
ce608c18
Commit
ce608c18
authored
1 year ago
by
Danis Kirasirov
Browse files
Options
Downloads
Patches
Plain Diff
Добавлен тест BatchProcessor
parent
3ced7628
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
pkg/items/dummy.go
+33
-0
33 additions, 0 deletions
pkg/items/dummy.go
pkg/items/pagination.go
+6
-1
6 additions, 1 deletion
pkg/items/pagination.go
pkg/items/pagination_test.go
+57
-0
57 additions, 0 deletions
pkg/items/pagination_test.go
with
96 additions
and
1 deletion
pkg/items/dummy.go
0 → 100644
+
33
−
0
View file @
ce608c18
package
items
import
(
"context"
)
type
FindResultDummy
struct
{
Items
[]
*
Item
Total
int
Error
error
}
type
Dummy
struct
{
Items
FindResult
*
FindResultDummy
}
func
(
d
*
Dummy
)
Find
(
_
context
.
Context
,
_
,
_
,
_
string
,
_
*
Filter
,
_
...*
FindOptions
)
([]
*
Item
,
int
,
error
)
{
return
d
.
FindResult
.
Items
,
d
.
FindResult
.
Total
,
d
.
FindResult
.
Error
}
type
DummyWithOptions
struct
{
Items
Total
int
}
func
(
d
*
DummyWithOptions
)
Find
(
_
context
.
Context
,
_
,
_
,
_
string
,
_
*
Filter
,
opts
...*
FindOptions
)
([]
*
Item
,
int
,
error
)
{
fo
:=
MergeFindOptions
(
opts
...
)
limit
:=
fo
.
Limit
if
limit
==
0
{
limit
=
fo
.
PageSize
}
return
make
([]
*
Item
,
limit
),
d
.
Total
,
nil
}
This diff is collapsed.
Click to expand it.
pkg/items/pagination.go
+
6
−
1
View file @
ce608c18
...
@@ -16,7 +16,7 @@ type BatchProcessor struct {
...
@@ -16,7 +16,7 @@ type BatchProcessor struct {
FindPublishedOptions
*
FindPublishedOptions
FindPublishedOptions
*
FindPublishedOptions
Filter
*
Filter
Filter
*
Filter
limit
int
limit
int
sort
[]
string
sort
[]
string
processed
int
processed
int
}
}
...
@@ -130,6 +130,11 @@ func (b *BatchProcessor) Do(ctx context.Context, f func(batch []*Item) error) (i
...
@@ -130,6 +130,11 @@ func (b *BatchProcessor) Do(ctx context.Context, f func(batch []*Item) error) (i
return
0
,
err
return
0
,
err
}
}
// на случай, когда первый запрос вернул 0 элементов
if
len
(
batch
)
==
0
{
break
}
if
err
=
f
(
batch
);
err
!=
nil
{
if
err
=
f
(
batch
);
err
!=
nil
{
return
0
,
err
return
0
,
err
}
}
...
...
This diff is collapsed.
Click to expand it.
pkg/items/pagination_test.go
0 → 100644
+
57
−
0
View file @
ce608c18
package
items
import
(
"context"
"testing"
"git.perx.ru/perxis/perxis-go/pkg/environments"
"git.perx.ru/perxis/perxis-go/pkg/options"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func
TestBatchProcessor
(
t
*
testing
.
T
)
{
t
.
Run
(
"EmptyResults"
,
func
(
t
*
testing
.
T
)
{
itemssvc
:=
&
Dummy
{
FindResult
:
&
FindResultDummy
{
Items
:
nil
,
Total
:
0
,
Error
:
nil
}}
b
:=
&
BatchProcessor
{
Items
:
itemssvc
,
SpaceID
:
"sp"
,
EnvID
:
environments
.
DefaultEnvironment
,
CollectionID
:
"col"
,
FindOptions
:
&
FindOptions
{
Regular
:
true
,
Hidden
:
true
,
Templates
:
true
,
FindOptions
:
*
options
.
NewFindOptions
(
0
,
10
),
},
Filter
:
NewFilter
(
"a > 5"
),
}
var
counter
int
_
,
err
:=
b
.
Do
(
context
.
Background
(),
func
(
batch
[]
*
Item
)
error
{
counter
++
;
return
nil
})
require
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
0
,
counter
)
})
t
.
Run
(
"Success"
,
func
(
t
*
testing
.
T
)
{
itemssvc
:=
&
DummyWithOptions
{
Items
:
nil
,
Total
:
1000
}
b
:=
&
BatchProcessor
{
Items
:
itemssvc
,
SpaceID
:
"sp"
,
EnvID
:
environments
.
DefaultEnvironment
,
CollectionID
:
"col"
,
FindOptions
:
&
FindOptions
{
Regular
:
true
,
Hidden
:
true
,
Templates
:
true
,
FindOptions
:
*
options
.
New
(
0
,
25
),
},
}
var
counter
int
_
,
err
:=
b
.
Do
(
context
.
Background
(),
func
(
batch
[]
*
Item
)
error
{
counter
++
;
return
nil
})
require
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
1000
/
25
,
counter
)
})
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment