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
8dbe03c5
Commit
8dbe03c5
authored
1 year ago
by
Danis Kirasirov
Browse files
Options
Downloads
Patches
Plain Diff
Добавлены тесты для структур в expr
parent
32dc837e
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/expr/expr_test.go
+64
-0
64 additions, 0 deletions
pkg/expr/expr_test.go
pkg/expr/mongo_test.go
+32
-0
32 additions, 0 deletions
pkg/expr/mongo_test.go
pkg/files/file.go
+6
-6
6 additions, 6 deletions
pkg/files/file.go
with
102 additions
and
6 deletions
pkg/expr/expr_test.go
+
64
−
0
View file @
8dbe03c5
package
expr
package
expr
import
(
import
(
"context"
"fmt"
"fmt"
"testing"
"testing"
"time"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
)
func
TestIsExpression
(
t
*
testing
.
T
)
{
func
TestIsExpression
(
t
*
testing
.
T
)
{
...
@@ -49,3 +51,65 @@ func TestIsExpression(t *testing.T) {
...
@@ -49,3 +51,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
+
32
−
0
View file @
8dbe03c5
...
@@ -69,6 +69,38 @@ func TestConvertToMongo(t *testing.T) {
...
@@ -69,6 +69,38 @@ 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
)
{
func
BenchmarkConvertToMongo
(
b
*
testing
.
B
)
{
const
idsNum
=
1
_000_000
const
idsNum
=
1
_000_000
ctx
:=
context
.
Background
()
ctx
:=
context
.
Background
()
...
...
This diff is collapsed.
Click to expand it.
pkg/files/file.go
+
6
−
6
View file @
8dbe03c5
...
@@ -16,13 +16,13 @@ const (
...
@@ -16,13 +16,13 @@ const (
// File - описание файла в системе хранения perxis
// File - описание файла в системе хранения perxis
type
File
struct
{
type
File
struct
{
ID
string
`mapstructure:"id,omitempty" json:"id" expr:"id"`
// Уникальный идентификатор файла в хранилище
ID
string
`mapstructure:"id,omitempty" json:"id" expr:"id"`
// Уникальный идентификатор файла в хранилище
Name
string
`mapstructure:"name,omitempty" json:"name" bson:"name,omitempty" expr:"name"`
// Имя файла
Name
string
`mapstructure:"name,omitempty" json:"name" bson:"name,omitempty" expr:"name"`
// Имя файла
Size
int
`mapstructure:"size,omitempty" json:"size" bson:"size,omitempty" expr:"size"`
// Размер файла
Size
int
`mapstructure:"size,omitempty" json:"size" bson:"size,omitempty" expr:"size"`
// Размер файла
MimeType
string
`mapstructure:"mimeType,omitempty" json:"mimeType" bson:"mimeType,omitempty" expr:"mime_type"`
// Mime-type файла
MimeType
string
`mapstructure:"mimeType,omitempty" json:"mimeType" bson:"mimeType,omitempty" expr:"mime_type"`
// Mime-type файла
URL
string
`mapstructure:"url,omitempty" json:"url" bson:"url,omitempty" expr:"url"`
// Адрес для загрузки файла
URL
string
`mapstructure:"url,omitempty" json:"url" bson:"url,omitempty" expr:"url"`
// Адрес для загрузки файла
Key
string
`mapstructure:"key,omitempty" json:"key" bson:"key,omitempty" expr:"key"`
// Ключ для хранения файла в хранилище
Key
string
`mapstructure:"key,omitempty" json:"key" bson:"key,omitempty" expr:"key"`
// Ключ для хранения файла в хранилище
File
fs
.
File
`mapstructure:"-" json:"-" bson:"-"`
// Файл для загрузки(из файловой системы)
File
fs
.
File
`mapstructure:"-" json:"-" bson:"-"`
// Файл для загрузки(из файловой системы)
}
}
func
(
f
File
)
Clone
()
*
File
{
func
(
f
File
)
Clone
()
*
File
{
...
...
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