mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-18 16:30:44 +00:00
Added tests for Databases and Collections
This commit is contained in:
122
api/tests/collections_test.go
Normal file
122
api/tests/collections_test.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_Collections(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "asas"),
|
||||
&azcosmos.ClientOptions{},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
databaseClient, err := client.NewDatabase(testDatabaseName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
t.Run("Collection Create", func(t *testing.T) {
|
||||
t.Run("Should create collection", func(t *testing.T) {
|
||||
createResponse, err := databaseClient.CreateContainer(context.TODO(), azcosmos.ContainerProperties{
|
||||
ID: testCollectionName,
|
||||
}, &azcosmos.CreateContainerOptions{})
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, createResponse.ContainerProperties.ID, testCollectionName)
|
||||
})
|
||||
|
||||
t.Run("Should return conflict when collection exists", func(t *testing.T) {
|
||||
repositories.CreateCollection(testDatabaseName, repositories.Collection{
|
||||
ID: testCollectionName,
|
||||
})
|
||||
|
||||
_, err := databaseClient.CreateContainer(context.TODO(), azcosmos.ContainerProperties{
|
||||
ID: testCollectionName,
|
||||
}, &azcosmos.CreateContainerOptions{})
|
||||
assert.NotNil(t, err)
|
||||
|
||||
var respErr *azcore.ResponseError
|
||||
if errors.As(err, &respErr) {
|
||||
assert.Equal(t, respErr.StatusCode, http.StatusConflict)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Collection Read", func(t *testing.T) {
|
||||
t.Run("Should read collection", func(t *testing.T) {
|
||||
repositories.CreateCollection(testDatabaseName, repositories.Collection{
|
||||
ID: testCollectionName,
|
||||
})
|
||||
|
||||
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
readResponse, err := collectionResponse.Read(context.TODO(), &azcosmos.ReadContainerOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, readResponse.RawResponse.StatusCode, http.StatusOK)
|
||||
assert.Equal(t, readResponse.ContainerProperties.ID, testCollectionName)
|
||||
})
|
||||
|
||||
t.Run("Should return not found when collection does not exist", func(t *testing.T) {
|
||||
repositories.DeleteCollection(testDatabaseName, testCollectionName)
|
||||
|
||||
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, err = collectionResponse.Read(context.TODO(), &azcosmos.ReadContainerOptions{})
|
||||
assert.NotNil(t, err)
|
||||
|
||||
var respErr *azcore.ResponseError
|
||||
if errors.As(err, &respErr) {
|
||||
assert.Equal(t, respErr.StatusCode, http.StatusNotFound)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Collection Delete", func(t *testing.T) {
|
||||
t.Run("Should delete collection", func(t *testing.T) {
|
||||
repositories.CreateCollection(testDatabaseName, repositories.Collection{
|
||||
ID: testCollectionName,
|
||||
})
|
||||
|
||||
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
readResponse, err := collectionResponse.Delete(context.TODO(), &azcosmos.DeleteContainerOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, readResponse.RawResponse.StatusCode, http.StatusNoContent)
|
||||
})
|
||||
|
||||
t.Run("Should return not found when collection does not exist", func(t *testing.T) {
|
||||
repositories.DeleteCollection(testDatabaseName, testCollectionName)
|
||||
|
||||
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, err = collectionResponse.Read(context.TODO(), &azcosmos.ReadContainerOptions{})
|
||||
assert.NotNil(t, err)
|
||||
|
||||
var respErr *azcore.ResponseError
|
||||
if errors.As(err, &respErr) {
|
||||
assert.Equal(t, respErr.StatusCode, http.StatusNotFound)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
17
api/tests/config_test.go
Normal file
17
api/tests/config_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
|
||||
"github.com/pikami/cosmium/api"
|
||||
)
|
||||
|
||||
func runTestServer() *httptest.Server {
|
||||
return httptest.NewServer(api.CreateRouter())
|
||||
}
|
||||
|
||||
const (
|
||||
testAccountKey = "account-key"
|
||||
testDatabaseName = "test-db"
|
||||
testCollectionName = "test-coll"
|
||||
)
|
||||
119
api/tests/databases_test.go
Normal file
119
api/tests/databases_test.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_Databases(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "asas"),
|
||||
&azcosmos.ClientOptions{},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
t.Run("Database Create", func(t *testing.T) {
|
||||
t.Run("Should create database", func(t *testing.T) {
|
||||
createResponse, err := client.CreateDatabase(context.TODO(), azcosmos.DatabaseProperties{
|
||||
ID: testDatabaseName,
|
||||
}, &azcosmos.CreateDatabaseOptions{})
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName)
|
||||
})
|
||||
|
||||
t.Run("Should return conflict when database exists", func(t *testing.T) {
|
||||
repositories.CreateDatabase(repositories.Database{
|
||||
ID: testDatabaseName,
|
||||
})
|
||||
|
||||
_, err := client.CreateDatabase(context.TODO(), azcosmos.DatabaseProperties{
|
||||
ID: testDatabaseName,
|
||||
}, &azcosmos.CreateDatabaseOptions{})
|
||||
assert.NotNil(t, err)
|
||||
|
||||
var respErr *azcore.ResponseError
|
||||
if errors.As(err, &respErr) {
|
||||
assert.Equal(t, respErr.StatusCode, http.StatusConflict)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Database Read", func(t *testing.T) {
|
||||
t.Run("Should read database", func(t *testing.T) {
|
||||
repositories.CreateDatabase(repositories.Database{
|
||||
ID: testDatabaseName,
|
||||
})
|
||||
|
||||
databaseResponse, err := client.NewDatabase(testDatabaseName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
readResponse, err := databaseResponse.Read(context.TODO(), &azcosmos.ReadDatabaseOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, readResponse.RawResponse.StatusCode, http.StatusOK)
|
||||
assert.Equal(t, readResponse.DatabaseProperties.ID, testDatabaseName)
|
||||
})
|
||||
|
||||
t.Run("Should return not found when database does not exist", func(t *testing.T) {
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
|
||||
databaseResponse, err := client.NewDatabase(testDatabaseName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, err = databaseResponse.Read(context.TODO(), &azcosmos.ReadDatabaseOptions{})
|
||||
assert.NotNil(t, err)
|
||||
|
||||
var respErr *azcore.ResponseError
|
||||
if errors.As(err, &respErr) {
|
||||
assert.Equal(t, respErr.StatusCode, http.StatusNotFound)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Database Delete", func(t *testing.T) {
|
||||
t.Run("Should delete database", func(t *testing.T) {
|
||||
repositories.CreateDatabase(repositories.Database{
|
||||
ID: testDatabaseName,
|
||||
})
|
||||
|
||||
databaseResponse, err := client.NewDatabase(testDatabaseName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
readResponse, err := databaseResponse.Delete(context.TODO(), &azcosmos.DeleteDatabaseOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, readResponse.RawResponse.StatusCode, http.StatusNoContent)
|
||||
})
|
||||
|
||||
t.Run("Should return not found when database does not exist", func(t *testing.T) {
|
||||
repositories.DeleteDatabase(testDatabaseName)
|
||||
|
||||
databaseResponse, err := client.NewDatabase(testDatabaseName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, err = databaseResponse.Read(context.TODO(), &azcosmos.ReadDatabaseOptions{})
|
||||
assert.NotNil(t, err)
|
||||
|
||||
var respErr *azcore.ResponseError
|
||||
if errors.As(err, &respErr) {
|
||||
assert.Equal(t, respErr.StatusCode, http.StatusNotFound)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user