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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
87f937ff
Commit
87f937ff
authored
1 year ago
by
Danis Kirasirov
Browse files
Options
Downloads
Patches
Plain Diff
Добавлены тесты для структур в expr
parent
19841a65
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
pkg/expr/expr_test.go
+64
-0
64 additions, 0 deletions
pkg/expr/expr_test.go
pkg/expr/mongo_test.go
+33
-0
33 additions, 0 deletions
pkg/expr/mongo_test.go
pkg/files/file.go
+6
-6
6 additions, 6 deletions
pkg/files/file.go
with
103 additions
and
6 deletions
pkg/expr/expr_test.go
+
64
−
0
View file @
87f937ff
package
expr
import
(
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func
TestIsExpression
(
t
*
testing
.
T
)
{
...
...
@@ -58,3 +60,65 @@ func TestIsExpression(t *testing.T) {
})
}
}
type
testEnvStruct
struct
{
ID
string
`expr:"id"`
Size
int
`expr:"size"`
Data
interface
{}
`expr:"data"`
}
func
TestEval_StructInEnv
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
tests
:=
[]
struct
{
name
string
exp
string
env
map
[
string
]
interface
{}
wantErr
bool
wantResult
interface
{}
}{
{
name
:
"get field by expr tag"
,
exp
:
"s.id"
,
env
:
map
[
string
]
interface
{}{
"s"
:
&
testEnvStruct
{
ID
:
"id1"
}},
wantResult
:
"id1"
,
},
{
name
:
"get field by field name"
,
exp
:
"s.ID"
,
env
:
map
[
string
]
interface
{}{
"s"
:
&
testEnvStruct
{
ID
:
"id1"
}},
wantResult
:
"id1"
,
},
{
name
:
"get nested field"
,
exp
:
"m.s.size"
,
env
:
map
[
string
]
interface
{}{
"m"
:
map
[
string
]
interface
{}{
"s"
:
&
testEnvStruct
{
Size
:
1
}}},
wantResult
:
1
,
},
{
name
:
"check field"
,
exp
:
"s.data.size < 100"
,
env
:
map
[
string
]
interface
{}{
"s"
:
&
testEnvStruct
{
Data
:
&
testEnvStruct
{
Size
:
0
}}},
wantResult
:
true
,
},
{
name
:
"field not exists"
,
exp
:
"s.not_exists"
,
env
:
map
[
string
]
interface
{}{
"s"
:
&
testEnvStruct
{}},
wantErr
:
true
,
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
result
,
err
:=
Eval
(
ctx
,
tt
.
exp
,
tt
.
env
)
if
tt
.
wantErr
{
require
.
Error
(
t
,
err
)
return
}
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
tt
.
wantResult
,
result
)
})
}
}
This diff is collapsed.
Click to expand it.
pkg/expr/mongo_test.go
+
33
−
0
View file @
87f937ff
...
...
@@ -90,6 +90,39 @@ func TestConvertToMongo(t *testing.T) {
}
}
func
TestConvertToMongo_StructInEnv
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
tests
:=
[]
struct
{
name
string
exp
string
env
map
[
string
]
interface
{}
wantErr
bool
wantResult
interface
{}
}{
{
name
:
"get field"
,
exp
:
"db_item.id == env_item.id"
,
env
:
map
[
string
]
interface
{}{
"env_item"
:
&
testEnvStruct
{
ID
:
"id1"
}},
wantResult
:
bson
.
M
{
"db_item.id"
:
"id1"
},
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
result
,
err
:=
ConvertToMongo
(
ctx
,
tt
.
exp
,
tt
.
env
,
nil
)
if
tt
.
wantErr
{
require
.
Error
(
t
,
err
)
return
}
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
tt
.
wantResult
,
result
)
})
}
}
func
BenchmarkConvertToMongo
(
b
*
testing
.
B
)
{
const
idsNum
=
1
_000_000
ctx
:=
context
.
Background
()
...
...
This diff is collapsed.
Click to expand it.
pkg/files/file.go
+
6
−
6
View file @
87f937ff
This diff is collapsed.
Click to expand it.
Danis Kirasirov
@kirasirov
mentioned in commit
3f099ffd
·
1 year ago
mentioned in commit
3f099ffd
mentioned in commit 3f099ffdf71a92e3651b0f45798af3012f49d6e7
Toggle commit list
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