Skip to content

Commit

Permalink
GOCBC-722: Split tests into integration and unit suites
Browse files Browse the repository at this point in the history
Motivation
----------
As part of a move to improve unit testing we should split
our tests into integration and unit test suites. This will
allow us to run unit tests without performing setup for
a real server or the Couchbase Mock.

Changes
-------
Added two test suites using testify.Suite. Split tests across these
two suites accordingly. Setup the testing -short flag to signal
that only unit tests should be run.

Change-Id: I69b53eb529ef27a36bb8239def42921c608aa598
Reviewed-on: http://review.couchbase.org/122111
Reviewed-by: Brett Lawson <brett19@gmail.com>
Tested-by: Charles Dixon <chvckd@gmail.com>
  • Loading branch information
chvck committed Feb 13, 2020
1 parent b025225 commit 02a6d4c
Show file tree
Hide file tree
Showing 24 changed files with 946 additions and 915 deletions.
21 changes: 10 additions & 11 deletions bucket_collectionsmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,60 @@ package gocb

import (
"errors"
"testing"
)

func TestCollectionManagerCrud(t *testing.T) {
func (suite *IntegrationTestSuite) TestCollectionManagerCrud() {
if !globalCluster.SupportsFeature(CollectionsFeature) {
t.Skip("Skipping test as collections not supported")
suite.T().Skip("Skipping test as collections not supported")
}

mgr := globalBucket.Collections()

err := mgr.CreateScope("testScope", nil)
if err != nil {
t.Fatalf("Failed to create scope %v", err)
suite.T().Fatalf("Failed to create scope %v", err)
}

err = mgr.CreateScope("testScope", nil)
if !errors.Is(err, ErrScopeExists) {
t.Fatalf("Expected create scope to error with ScopeExists but was %v", err)
suite.T().Fatalf("Expected create scope to error with ScopeExists but was %v", err)
}

err = mgr.CreateCollection(CollectionSpec{
Name: "testCollection",
ScopeName: "testScope",
}, nil)
if err != nil {
t.Fatalf("Failed to create collection %v", err)
suite.T().Fatalf("Failed to create collection %v", err)
}

err = mgr.CreateCollection(CollectionSpec{
Name: "testCollection",
ScopeName: "testScope",
}, nil)
if !errors.Is(err, ErrCollectionExists) {
t.Fatalf("Expected create collection to error with CollectionExists but was %v", err)
suite.T().Fatalf("Expected create collection to error with CollectionExists but was %v", err)
}

scopes, err := mgr.GetAllScopes(nil)
if err != nil {
t.Fatalf("Failed to GetAllScopes %v", err)
suite.T().Fatalf("Failed to GetAllScopes %v", err)
}

if len(scopes) < 2 {
t.Fatalf("Expected scopes to contain at least 2 scopes but was %v", scopes)
suite.T().Fatalf("Expected scopes to contain at least 2 scopes but was %v", scopes)
}

err = mgr.DropCollection(CollectionSpec{
Name: "testCollection",
ScopeName: "testScope",
}, nil)
if err != nil {
t.Fatalf("Expected DropCollection to not error but was %v", err)
suite.T().Fatalf("Expected DropCollection to not error but was %v", err)
}

err = mgr.DropScope("testScope", nil)
if err != nil {
t.Fatalf("Expected DropScope to not error but was %v", err)
suite.T().Fatalf("Expected DropScope to not error but was %v", err)
}
}
37 changes: 18 additions & 19 deletions bucket_ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package gocb

import (
"bytes"
"testing"
"time"

gocbcore "github.com/couchbase/gocbcore/v8"
"github.com/pkg/errors"
)

func TestPingAll(t *testing.T) {
func (suite *UnitTestSuite) TestPingAll() {
results := map[string]gocbcore.PingResult{
"server1": {
Endpoint: "server1",
Expand Down Expand Up @@ -112,76 +111,76 @@ func TestPingAll(t *testing.T) {

report, err := b.Ping(nil)
if err != nil {
t.Fatalf("Expected ping to not return error but was %v", err)
suite.T().Fatalf("Expected ping to not return error but was %v", err)
}

if report.ID == "" {
t.Fatalf("Report ID was empty")
suite.T().Fatalf("Report ID was empty")
}

if len(report.Services) != 4 {
t.Fatalf("Expected services length to be 6 but was %d", len(report.Services))
suite.T().Fatalf("Expected services length to be 6 but was %d", len(report.Services))
}

for serviceType, services := range report.Services {
for _, service := range services {
switch serviceType {
case ServiceTypeQuery:
if service.Remote != "http://localhost:8093" {
t.Fatalf("Expected service RemoteAddr to be http://localhost:8093 but was %s", service.Remote)
suite.T().Fatalf("Expected service RemoteAddr to be http://localhost:8093 but was %s", service.Remote)
}

if service.State != PingStateOk {
t.Fatalf("Expected service state to be ok but was %d", service.State)
suite.T().Fatalf("Expected service state to be ok but was %d", service.State)
}

if service.Latency < 50*time.Millisecond {
t.Fatalf("Expected service latency to be over 50ms but was %d", service.Latency)
suite.T().Fatalf("Expected service latency to be over 50ms but was %d", service.Latency)
}
case ServiceTypeSearch:
if service.Remote != "http://localhost:8094" {
t.Fatalf("Expected service RemoteAddr to be http://localhost:8094 but was %s", service.Remote)
suite.T().Fatalf("Expected service RemoteAddr to be http://localhost:8094 but was %s", service.Remote)
}

if service.State != PingStateError {
t.Fatalf("Expected service State to be error but was %d", service.State)
suite.T().Fatalf("Expected service State to be error but was %d", service.State)
}

if service.Latency != 0 {
t.Fatalf("Expected service latency to be 0 but was %d", service.Latency)
suite.T().Fatalf("Expected service latency to be 0 but was %d", service.Latency)
}
case ServiceTypeAnalytics:
if service.Remote != "http://localhost:8095" {
t.Fatalf("Expected service RemoteAddr to be http://localhost:8095 but was %s", service.Remote)
suite.T().Fatalf("Expected service RemoteAddr to be http://localhost:8095 but was %s", service.Remote)
}

if service.State != PingStateOk {
t.Fatalf("Expected service state to be ok but was %d", service.State)
suite.T().Fatalf("Expected service state to be ok but was %d", service.State)
}

if service.Latency < 20*time.Millisecond {
t.Fatalf("Expected service latency to be over 20ms but was %d", service.Latency)
suite.T().Fatalf("Expected service latency to be over 20ms but was %d", service.Latency)
}
case ServiceTypeKeyValue:
expected, ok := results[service.Remote]
if !ok {
t.Fatalf("Unexpected service endpoint: %s", service.Remote)
suite.T().Fatalf("Unexpected service endpoint: %s", service.Remote)
}
if service.Latency != expected.Latency {
t.Fatalf("Expected service Latency to be %s but was %s", expected.Latency, service.Latency)
suite.T().Fatalf("Expected service Latency to be %s but was %s", expected.Latency, service.Latency)
}

if expected.Error != nil {
if service.State != PingStateError {
t.Fatalf("Service success should have been error, was %d", service.State)
suite.T().Fatalf("Service success should have been error, was %d", service.State)
}
} else {
if service.State != PingStateOk {
t.Fatalf("Service success should have been ok, was %d", service.State)
suite.T().Fatalf("Service success should have been ok, was %d", service.State)
}
}
default:
t.Fatalf("Unexpected service type: %d", serviceType)
suite.T().Fatalf("Unexpected service type: %d", serviceType)
}
}
}
Expand Down
Loading

0 comments on commit 02a6d4c

Please sign in to comment.