75 lines
2.3 KiB
Go
75 lines
2.3 KiB
Go
/*
|
|
Copyright 2021 The Dapr Authors
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package mongodb
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/dapr/components-contrib/state/query"
|
|
)
|
|
|
|
func TestMongoQuery(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
query string
|
|
}{
|
|
{
|
|
input: "../../tests/state/query/q1.json",
|
|
query: ``,
|
|
},
|
|
{
|
|
input: "../../tests/state/query/q2.json",
|
|
query: `{ "value.state": "CA" }`,
|
|
},
|
|
{
|
|
input: "../../tests/state/query/q3.json",
|
|
query: `{ "$and": [ { "value.person.org": "A" }, { "value.state": { "$in": [ "CA", "WA" ] } } ] }`,
|
|
},
|
|
{
|
|
input: "../../tests/state/query/q4.json",
|
|
query: `{ "$or": [ { "value.person.org": "A" }, { "$and": [ { "value.person.org": "B" }, { "value.state": { "$in": [ "CA", "WA" ] } } ] } ] }`,
|
|
},
|
|
{
|
|
input: "../../tests/state/query/q6.json",
|
|
query: `{ "$or": [ { "value.person.id": 123 }, { "$and": [ { "value.person.org": "B" }, { "value.person.id": { "$in": [ 567, 890 ] } } ] } ] }`,
|
|
},
|
|
{
|
|
input: "../../tests/state/query/q6-notequal.json",
|
|
query: `{ "$or": [ { "value.person.id": 123 }, { "$and": [ { "value.person.org": {"$ne": "B"} }, { "value.person.id": { "$in": [ 567, 890 ] } } ] } ] }`,
|
|
},
|
|
{
|
|
input: "../../tests/state/query/q7.json",
|
|
query: `{ "$or": [ { "value.person.id": {"$lt": 123} }, { "$and": [ { "value.person.org": {"$gte": 2} }, { "value.person.id": { "$in": [ 567, 890 ] } } ] } ] }`,
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
data, err := os.ReadFile(test.input)
|
|
require.NoError(t, err)
|
|
var qq query.Query
|
|
err = json.Unmarshal(data, &qq)
|
|
require.NoError(t, err)
|
|
|
|
q := &Query{}
|
|
qbuilder := query.NewQueryBuilder(q)
|
|
err = qbuilder.BuildQuery(&qq)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, test.query, q.query)
|
|
}
|
|
}
|