cosmium/api/tests/documents_test.go

111 lines
3.0 KiB
Go
Raw Normal View History

2024-02-11 23:54:12 +00:00
package tests_test
import (
"context"
"encoding/json"
"fmt"
"reflect"
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/pikami/cosmium/internal/repositories"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
2024-02-11 23:54:12 +00:00
"github.com/stretchr/testify/assert"
)
2024-02-13 19:57:33 +00:00
func testCosmosQuery(t *testing.T, collectionClient *azcosmos.ContainerClient, query string, expectedData []interface{}) {
2024-02-11 23:54:12 +00:00
pager := collectionClient.NewQueryItemsPager(
query,
azcosmos.PartitionKey{},
&azcosmos.QueryOptions{})
context := context.TODO()
2024-02-13 19:57:33 +00:00
items := make([]interface{}, 0)
2024-02-11 23:54:12 +00:00
for pager.More() {
response, err := pager.NextPage(context)
assert.Nil(t, err)
for _, bytes := range response.Items {
2024-02-13 19:57:33 +00:00
var item interface{}
2024-02-11 23:54:12 +00:00
err := json.Unmarshal(bytes, &item)
assert.Nil(t, err)
items = append(items, item)
}
}
assert.Equal(t, len(expectedData), len(items))
if !reflect.DeepEqual(items, expectedData) {
t.Errorf("executed query does not match expected data.\nExpected: %+v\nGot: %+v", expectedData, items)
}
}
func Test_Documents(t *testing.T) {
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
2024-02-11 23:54:12 +00:00
ID: testCollectionName,
PartitionKey: struct {
Paths []string "json:\"paths\""
Kind string "json:\"kind\""
Version int "json:\"Version\""
}{
Paths: []string{"/pk"},
},
})
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false})
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true})
ts := runTestServer()
defer ts.Close()
client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "asas"),
&azcosmos.ClientOptions{},
)
assert.Nil(t, err)
collectionClient, err := client.NewContainer(testDatabaseName, testCollectionName)
assert.Nil(t, err)
t.Run("Should query document", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
"SELECT c.id, c[\"pk\"] FROM c",
2024-02-13 19:57:33 +00:00
[]interface{}{
map[string]interface{}{"id": "12345", "pk": "123"},
map[string]interface{}{"id": "67890", "pk": "456"},
},
)
})
t.Run("Should query VALUE array", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
"SELECT VALUE [c.id, c[\"pk\"]] FROM c",
[]interface{}{
[]interface{}{"12345", "123"},
[]interface{}{"67890", "456"},
2024-02-11 23:54:12 +00:00
},
)
})
2024-02-13 20:42:18 +00:00
t.Run("Should query VALUE object", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
"SELECT VALUE { id: c.id, _pk: c.pk } FROM c",
[]interface{}{
map[string]interface{}{"id": "12345", "_pk": "123"},
map[string]interface{}{"id": "67890", "_pk": "456"},
},
)
})
2024-02-11 23:54:12 +00:00
t.Run("Should query document with single WHERE condition", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
`select c.id
FROM c
WHERE c.isCool=true`,
2024-02-13 19:57:33 +00:00
[]interface{}{
map[string]interface{}{"id": "67890"},
2024-02-11 23:54:12 +00:00
},
)
})
}