DataStore is interface now. Liskov would be proud.

This commit is contained in:
Pijus Kamandulis
2025-03-09 18:34:07 +02:00
parent bd4fe5abec
commit 221f029a1d
41 changed files with 836 additions and 747 deletions

View File

@@ -17,7 +17,7 @@ func Test_Authentication(t *testing.T) {
defer ts.Server.Close()
t.Run("Should get 200 when correct account key is used", func(t *testing.T) {
ts.Repository.DeleteDatabase(testDatabaseName)
ts.DataStore.DeleteDatabase(testDatabaseName)
client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
&azcosmos.ClientOptions{},
@@ -33,7 +33,7 @@ func Test_Authentication(t *testing.T) {
})
t.Run("Should get 401 when wrong account key is used", func(t *testing.T) {
ts.Repository.DeleteDatabase(testDatabaseName)
ts.DataStore.DeleteDatabase(testDatabaseName)
client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"),
&azcosmos.ClientOptions{},
@@ -70,7 +70,7 @@ func Test_Authentication_Disabled(t *testing.T) {
defer ts.Server.Close()
t.Run("Should get 200 when wrong account key is used, but authentication is dissabled", func(t *testing.T) {
ts.Repository.DeleteDatabase(testDatabaseName)
ts.DataStore.DeleteDatabase(testDatabaseName)
client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"),
&azcosmos.ClientOptions{},

View File

@@ -10,7 +10,7 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/pikami/cosmium/api/config"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
"github.com/pikami/cosmium/internal/datastore"
"github.com/stretchr/testify/assert"
)
@@ -24,7 +24,7 @@ func Test_Collections(t *testing.T) {
)
assert.Nil(t, err)
ts.Repository.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
ts.DataStore.CreateDatabase(datastore.Database{ID: testDatabaseName})
databaseClient, err := client.NewDatabase(testDatabaseName)
assert.Nil(t, err)
@@ -39,7 +39,7 @@ func Test_Collections(t *testing.T) {
})
t.Run("Should return conflict when collection exists", func(t *testing.T) {
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName,
})
@@ -59,7 +59,7 @@ func Test_Collections(t *testing.T) {
t.Run("Collection Read", func(t *testing.T) {
t.Run("Should read collection", func(t *testing.T) {
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName,
})
@@ -73,7 +73,7 @@ func Test_Collections(t *testing.T) {
})
t.Run("Should return not found when collection does not exist", func(t *testing.T) {
ts.Repository.DeleteCollection(testDatabaseName, testCollectionName)
ts.DataStore.DeleteCollection(testDatabaseName, testCollectionName)
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
assert.Nil(t, err)
@@ -92,7 +92,7 @@ func Test_Collections(t *testing.T) {
t.Run("Collection Delete", func(t *testing.T) {
t.Run("Should delete collection", func(t *testing.T) {
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName,
})
@@ -105,7 +105,7 @@ func Test_Collections(t *testing.T) {
})
t.Run("Should return not found when collection does not exist", func(t *testing.T) {
ts.Repository.DeleteCollection(testDatabaseName, testCollectionName)
ts.DataStore.DeleteCollection(testDatabaseName, testCollectionName)
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
assert.Nil(t, err)

View File

@@ -5,29 +5,30 @@ import (
"github.com/pikami/cosmium/api"
"github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/internal/datastore"
mapdatastore "github.com/pikami/cosmium/internal/datastore/map_datastore"
"github.com/pikami/cosmium/internal/logger"
"github.com/pikami/cosmium/internal/repositories"
)
type TestServer struct {
Server *httptest.Server
Repository *repositories.DataRepository
URL string
Server *httptest.Server
DataStore datastore.DataStore
URL string
}
func runTestServerCustomConfig(config *config.ServerConfig) *TestServer {
repository := repositories.NewDataRepository(repositories.RepositoryOptions{})
dataStore := mapdatastore.NewMapDataStore(mapdatastore.MapDataStoreOptions{})
api := api.NewApiServer(repository, config)
api := api.NewApiServer(dataStore, config)
server := httptest.NewServer(api.GetRouter())
config.DatabaseEndpoint = server.URL
return &TestServer{
Server: server,
Repository: repository,
URL: server.URL,
Server: server,
DataStore: dataStore,
URL: server.URL,
}
}

View File

@@ -10,7 +10,7 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/pikami/cosmium/api/config"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
"github.com/pikami/cosmium/internal/datastore"
"github.com/stretchr/testify/assert"
)
@@ -26,7 +26,7 @@ func Test_Databases(t *testing.T) {
t.Run("Database Create", func(t *testing.T) {
t.Run("Should create database", func(t *testing.T) {
ts.Repository.DeleteDatabase(testDatabaseName)
ts.DataStore.DeleteDatabase(testDatabaseName)
createResponse, err := client.CreateDatabase(context.TODO(), azcosmos.DatabaseProperties{
ID: testDatabaseName,
@@ -37,7 +37,7 @@ func Test_Databases(t *testing.T) {
})
t.Run("Should return conflict when database exists", func(t *testing.T) {
ts.Repository.CreateDatabase(repositorymodels.Database{
ts.DataStore.CreateDatabase(datastore.Database{
ID: testDatabaseName,
})
@@ -57,7 +57,7 @@ func Test_Databases(t *testing.T) {
t.Run("Database Read", func(t *testing.T) {
t.Run("Should read database", func(t *testing.T) {
ts.Repository.CreateDatabase(repositorymodels.Database{
ts.DataStore.CreateDatabase(datastore.Database{
ID: testDatabaseName,
})
@@ -71,7 +71,7 @@ func Test_Databases(t *testing.T) {
})
t.Run("Should return not found when database does not exist", func(t *testing.T) {
ts.Repository.DeleteDatabase(testDatabaseName)
ts.DataStore.DeleteDatabase(testDatabaseName)
databaseResponse, err := client.NewDatabase(testDatabaseName)
assert.Nil(t, err)
@@ -90,7 +90,7 @@ func Test_Databases(t *testing.T) {
t.Run("Database Delete", func(t *testing.T) {
t.Run("Should delete database", func(t *testing.T) {
ts.Repository.CreateDatabase(repositorymodels.Database{
ts.DataStore.CreateDatabase(datastore.Database{
ID: testDatabaseName,
})
@@ -103,7 +103,7 @@ func Test_Databases(t *testing.T) {
})
t.Run("Should return not found when database does not exist", func(t *testing.T) {
ts.Repository.DeleteDatabase(testDatabaseName)
ts.DataStore.DeleteDatabase(testDatabaseName)
databaseResponse, err := client.NewDatabase(testDatabaseName)
assert.Nil(t, err)

View File

@@ -14,7 +14,7 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/pikami/cosmium/api/config"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
"github.com/pikami/cosmium/internal/datastore"
"github.com/stretchr/testify/assert"
)
@@ -56,8 +56,8 @@ func testCosmosQuery(t *testing.T,
func documents_InitializeDb(t *testing.T) (*TestServer, *azcosmos.ContainerClient) {
ts := runTestServer()
ts.Repository.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
ts.Repository.CreateCollection(testDatabaseName, repositorymodels.Collection{
ts.DataStore.CreateDatabase(datastore.Database{ID: testDatabaseName})
ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
ID: testCollectionName,
PartitionKey: struct {
Paths []string "json:\"paths\""
@@ -67,8 +67,8 @@ func documents_InitializeDb(t *testing.T) (*TestServer, *azcosmos.ContainerClien
Paths: []string{"/pk"},
},
})
ts.Repository.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3}})
ts.Repository.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true, "arr": []int{6, 7, 8}})
ts.DataStore.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3}})
ts.DataStore.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true, "arr": []int{6, 7, 8}})
client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
@@ -408,7 +408,7 @@ func Test_Documents_TransactionalBatch(t *testing.T) {
json.Unmarshal(operationResponse.ResourceBody, &itemResponseBody)
assert.Equal(t, newItem["id"], itemResponseBody["id"])
createdDoc, _ := ts.Repository.GetDocument(testDatabaseName, testCollectionName, newItem["id"].(string))
createdDoc, _ := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, newItem["id"].(string))
assert.Equal(t, newItem["id"], createdDoc["id"])
})
@@ -426,8 +426,8 @@ func Test_Documents_TransactionalBatch(t *testing.T) {
assert.NotNil(t, operationResponse)
assert.Equal(t, int32(http.StatusNoContent), operationResponse.StatusCode)
_, status := ts.Repository.GetDocument(testDatabaseName, testCollectionName, "12345")
assert.Equal(t, repositorymodels.StatusNotFound, int(status))
_, status := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, "12345")
assert.Equal(t, datastore.StatusNotFound, int(status))
})
t.Run("Should execute REPLACE transactional batch", func(t *testing.T) {
@@ -457,7 +457,7 @@ func Test_Documents_TransactionalBatch(t *testing.T) {
assert.Equal(t, newItem["id"], itemResponseBody["id"])
assert.Equal(t, newItem["pk"], itemResponseBody["pk"])
updatedDoc, _ := ts.Repository.GetDocument(testDatabaseName, testCollectionName, newItem["id"].(string))
updatedDoc, _ := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, newItem["id"].(string))
assert.Equal(t, newItem["id"], updatedDoc["id"])
assert.Equal(t, newItem["pk"], updatedDoc["pk"])
})
@@ -489,7 +489,7 @@ func Test_Documents_TransactionalBatch(t *testing.T) {
assert.Equal(t, newItem["id"], itemResponseBody["id"])
assert.Equal(t, newItem["pk"], itemResponseBody["pk"])
updatedDoc, _ := ts.Repository.GetDocument(testDatabaseName, testCollectionName, newItem["id"].(string))
updatedDoc, _ := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, newItem["id"].(string))
assert.Equal(t, newItem["id"], updatedDoc["id"])
assert.Equal(t, newItem["pk"], updatedDoc["pk"])
})