1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00
kube-arangodb/tests/db_util.go
Ewout Prangsma d8e270f9cd
Added tests for query cursors on various deployments.
[ci LONG=1] [ci TESTOPTIONS="-test.run ^TestCursor"]
2018-04-06 11:53:17 +02:00

64 lines
2 KiB
Go

//
// DISCLAIMER
//
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Ewout Prangsma
//
package tests
import (
"context"
"testing"
driver "github.com/arangodb/go-driver"
)
// ensureDatabase is a helper to check if a database exists and create it if needed.
// It will fail the test when an error occurs.
func ensureDatabase(ctx context.Context, c driver.Client, name string, options *driver.CreateDatabaseOptions, t *testing.T) driver.Database {
db, err := c.Database(ctx, name)
if driver.IsNotFound(err) {
db, err = c.CreateDatabase(ctx, name, options)
if err != nil {
if driver.IsConflict(err) {
t.Fatalf("Failed to create database (conflict) '%s': %s %#v", name, err, err)
} else {
t.Fatalf("Failed to create database '%s': %s %#v", name, err, err)
}
}
} else if err != nil {
t.Fatalf("Failed to open database '%s': %s", name, err)
}
return db
}
// ensureCollection is a helper to check if a collection exists and create if if needed.
// It will fail the test when an error occurs.
func ensureCollection(ctx context.Context, db driver.Database, name string, options *driver.CreateCollectionOptions, t *testing.T) driver.Collection {
c, err := db.Collection(ctx, name)
if driver.IsNotFound(err) {
c, err = db.CreateCollection(ctx, name, options)
if err != nil {
t.Fatalf("Failed to create collection '%s': %s", name, err)
}
} else if err != nil {
t.Fatalf("Failed to open collection '%s': %s", name, err)
}
return c
}