2022-06-15 19:08:19 +00:00
|
|
|
package netmap_test
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
import (
|
2022-06-07 08:25:34 +00:00
|
|
|
"strings"
|
2021-10-27 10:00:35 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
. "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
netmaptest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap/test"
|
2021-10-27 10:00:35 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func TestEncode(t *testing.T) {
|
|
|
|
testCases := []string{
|
|
|
|
`REP 1 IN X
|
|
|
|
CBF 1
|
|
|
|
SELECT 2 IN SAME Location FROM * AS X`,
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
`REP 1
|
|
|
|
SELECT 2 IN City FROM Good
|
|
|
|
FILTER Country EQ RU AS FromRU
|
|
|
|
FILTER @FromRU AND Rating GT 7 AS Good`,
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
`REP 7 IN SPB
|
|
|
|
SELECT 1 IN City FROM SPBSSD AS SPB
|
|
|
|
FILTER City EQ SPB AND SSD EQ true OR City EQ SPB AND Rating GE 5 AS SPBSSD`,
|
2023-05-24 09:50:13 +00:00
|
|
|
|
|
|
|
`REP 7 IN SPB
|
|
|
|
SELECT 1 IN City FROM SPBSSD AS SPB
|
|
|
|
FILTER NOT (NOT (City EQ SPB) AND SSD EQ true OR City EQ SPB AND Rating GE 5) AS SPBSSD`,
|
2023-06-05 10:38:15 +00:00
|
|
|
|
|
|
|
`UNIQUE
|
|
|
|
REP 1
|
|
|
|
REP 1`,
|
2023-07-05 11:25:24 +00:00
|
|
|
|
|
|
|
`REP 1 IN X
|
|
|
|
SELECT 1 FROM F AS X
|
|
|
|
FILTER 'UN-LOCODE' EQ 'RU LED' AS F`,
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-28 10:42:35 +00:00
|
|
|
var p PlacementPolicy
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
for _, testCase := range testCases {
|
|
|
|
require.NoError(t, p.DecodeString(testCase))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
var b strings.Builder
|
|
|
|
require.NoError(t, p.WriteStringTo(&b))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
require.Equal(t, testCase, b.String())
|
|
|
|
}
|
2022-06-28 10:42:35 +00:00
|
|
|
|
|
|
|
invalidTestCases := []string{
|
|
|
|
`?REP 1`,
|
2022-09-09 11:34:49 +00:00
|
|
|
`REP 1 trailing garbage`,
|
2022-06-28 10:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range invalidTestCases {
|
|
|
|
require.Error(t, p.DecodeString(invalidTestCases[i]), "#%d", i)
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
2022-06-15 19:08:19 +00:00
|
|
|
|
|
|
|
func TestPlacementPolicyEncoding(t *testing.T) {
|
|
|
|
v := netmaptest.PlacementPolicy()
|
|
|
|
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
var v2 PlacementPolicy
|
|
|
|
require.NoError(t, v2.Unmarshal(v.Marshal()))
|
|
|
|
|
|
|
|
require.Equal(t, v, v2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
data, err := v.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var v2 PlacementPolicy
|
|
|
|
require.NoError(t, v2.UnmarshalJSON(data))
|
|
|
|
|
|
|
|
require.Equal(t, v, v2)
|
|
|
|
})
|
|
|
|
}
|
2023-07-13 13:12:47 +00:00
|
|
|
|
|
|
|
func TestDecodeSelectFilterExpr(t *testing.T) {
|
|
|
|
for _, s := range []string{
|
|
|
|
"SELECT 1 FROM *",
|
|
|
|
"FILTER Color EQ 'Red' AS RedNode",
|
|
|
|
`
|
|
|
|
FILTER Color EQ 'Red' AS RedNode
|
|
|
|
FILTER @RedNode AND Shape EQ 'Cirle' AS RedCircleNode
|
|
|
|
`,
|
|
|
|
`
|
|
|
|
SELECT 1 FROM RedCircleNode
|
|
|
|
FILTER Color EQ 'Red' AS RedNode
|
|
|
|
FILTER @RedNode AND Shape EQ 'Cirle' AS RedCircleNode
|
|
|
|
`,
|
|
|
|
`
|
|
|
|
CBF 1
|
|
|
|
SELECT 1 FROM RedCircleNode
|
|
|
|
FILTER Color EQ 'Red' AS RedNode
|
|
|
|
FILTER @RedNode AND Shape EQ 'Cirle' AS RedCircleNode
|
|
|
|
`,
|
|
|
|
} {
|
|
|
|
_, err := DecodeSelectFilterString(s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|