mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-20 09:20:11 +00:00
Added support for Badger as an alternative storage backend
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||
"github.com/pikami/cosmium/api"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/datastore"
|
||||
badgerdatastore "github.com/pikami/cosmium/internal/datastore/badger_datastore"
|
||||
mapdatastore "github.com/pikami/cosmium/internal/datastore/map_datastore"
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type TestServer struct {
|
||||
@@ -16,14 +21,29 @@ type TestServer struct {
|
||||
URL string
|
||||
}
|
||||
|
||||
func runTestServerCustomConfig(config *config.ServerConfig) *TestServer {
|
||||
dataStore := mapdatastore.NewMapDataStore(mapdatastore.MapDataStoreOptions{})
|
||||
func getDefaultTestServerConfig() *config.ServerConfig {
|
||||
return &config.ServerConfig{
|
||||
AccountKey: config.DefaultAccountKey,
|
||||
ExplorerPath: "/tmp/nothing",
|
||||
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
|
||||
DataStore: "map",
|
||||
}
|
||||
}
|
||||
|
||||
api := api.NewApiServer(dataStore, config)
|
||||
func runTestServerCustomConfig(configuration *config.ServerConfig) *TestServer {
|
||||
var dataStore datastore.DataStore
|
||||
switch configuration.DataStore {
|
||||
case config.DataStoreBadger:
|
||||
dataStore = badgerdatastore.NewBadgerDataStore()
|
||||
default:
|
||||
dataStore = mapdatastore.NewMapDataStore(mapdatastore.MapDataStoreOptions{})
|
||||
}
|
||||
|
||||
api := api.NewApiServer(dataStore, configuration)
|
||||
|
||||
server := httptest.NewServer(api.GetRouter())
|
||||
|
||||
config.DatabaseEndpoint = server.URL
|
||||
configuration.DatabaseEndpoint = server.URL
|
||||
|
||||
return &TestServer{
|
||||
Server: server,
|
||||
@@ -33,11 +53,7 @@ func runTestServerCustomConfig(config *config.ServerConfig) *TestServer {
|
||||
}
|
||||
|
||||
func runTestServer() *TestServer {
|
||||
config := &config.ServerConfig{
|
||||
AccountKey: config.DefaultAccountKey,
|
||||
ExplorerPath: "/tmp/nothing",
|
||||
ExplorerBaseUrlLocation: config.ExplorerBaseUrlLocation,
|
||||
}
|
||||
config := getDefaultTestServerConfig()
|
||||
|
||||
config.LogLevel = "debug"
|
||||
logger.SetLogLevel(logger.LogLevelDebug)
|
||||
@@ -50,3 +66,46 @@ const (
|
||||
testDatabaseName = "test-db"
|
||||
testCollectionName = "test-coll"
|
||||
)
|
||||
|
||||
type testFunc func(t *testing.T, ts *TestServer, cosmosClient *azcosmos.Client)
|
||||
type testPreset string
|
||||
|
||||
const (
|
||||
PresetMapStore testPreset = "MapDS"
|
||||
PresetBadgerStore testPreset = "BadgerDS"
|
||||
)
|
||||
|
||||
func runTestsWithPreset(t *testing.T, name string, testPreset testPreset, f testFunc) {
|
||||
serverConfig := getDefaultTestServerConfig()
|
||||
|
||||
serverConfig.LogLevel = "debug"
|
||||
logger.SetLogLevel(logger.LogLevelDebug)
|
||||
|
||||
switch testPreset {
|
||||
case PresetBadgerStore:
|
||||
serverConfig.DataStore = config.DataStoreBadger
|
||||
case PresetMapStore:
|
||||
serverConfig.DataStore = config.DataStoreMap
|
||||
}
|
||||
|
||||
ts := runTestServerCustomConfig(serverConfig)
|
||||
defer ts.Server.Close()
|
||||
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey),
|
||||
&azcosmos.ClientOptions{},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
testName := fmt.Sprintf("%s_%s", testPreset, name)
|
||||
|
||||
t.Run(testName, func(t *testing.T) {
|
||||
f(t, ts, client)
|
||||
})
|
||||
}
|
||||
|
||||
func runTestsWithPresets(t *testing.T, name string, testPresets []testPreset, f testFunc) {
|
||||
for _, testPreset := range testPresets {
|
||||
runTestsWithPreset(t, name, testPreset, f)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user