Export TestSets helper function (#292)

This commit is contained in:
Michael Gaffney authored and GitHub committed 2020-08-20 15:13:12 -04:00
1 parent 3a3cdd7219
commit b24fc185a7
6 files changed
+56 -34

No files matched your search

+2 -2
View File
@@ -20,13 +20,13 @@ func TestHostSetMember_New(t *testing.T) {
cats := TestCatalogs(t, conn, prj.PublicId, 2)
blueCat := cats[0]
blueSets := testSets(t, conn, blueCat.GetPublicId(), 2)
blueSets := TestSets(t, conn, blueCat.GetPublicId(), 2)
blueHosts := TestHosts(t, conn, blueCat.GetPublicId(), 2)
// TODO(mgaffney) 05/2020:
// these will be needed when the repository code is done
// greenCat := cats[1]
// greenSets := testSets(t, conn, greenCat.GetPublicId(), 2)
// greenSets := TestSets(t, conn, greenCat.GetPublicId(), 2)
// greenHosts := TestHosts(t, conn, greenCat.GetPublicId(), 2)
var tests = []struct {
-23
View File
@@ -7,7 +7,6 @@ import (
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/host/static/store"
"github.com/hashicorp/boundary/internal/iam"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -110,28 +109,6 @@ func TestHostSet_New(t *testing.T) {
}
}
func testSets(t *testing.T, conn *gorm.DB, catalogId string, count int) []*HostSet {
t.Helper()
assert := assert.New(t)
var sets []*HostSet
for i := 0; i < count; i++ {
set, err := NewHostSet(catalogId)
assert.NoError(err)
assert.NotNil(set)
id, err := newHostSetId()
assert.NoError(err)
assert.NotEmpty(id)
set.PublicId = id
w := db.New(conn)
err2 := w.Create(context.Background(), set)
assert.NoError(err2)
sets = append(sets, set)
}
return sets
}
func TestHostSet_SetTableName(t *testing.T) {
defaultTableName := "static_host_set"
tests := []struct {
@@ -162,7 +162,7 @@ func TestStaticHostSet_ImmutableFields(t *testing.T) {
ts := timestamp.Timestamp{Timestamp: &timestamppb.Timestamp{Seconds: 0, Nanos: 0}}
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
cat := testCatalog(t, conn, prj.PublicId)
sets := testSets(t, conn, cat.GetPublicId(), 1)
sets := TestSets(t, conn, cat.GetPublicId(), 1)
new := sets[0]
@@ -236,7 +236,7 @@ func TestStaticHostSetMember_ImmutableFields(t *testing.T) {
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
cat := testCatalog(t, conn, prj.PublicId)
sets := testSets(t, conn, cat.GetPublicId(), 1)
sets := TestSets(t, conn, cat.GetPublicId(), 1)
hosts := TestHosts(t, conn, cat.GetPublicId(), 1)
new, err := NewHostSetMember(sets[0].PublicId, hosts[0].PublicId)
@@ -517,7 +517,7 @@ func TestRepository_UpdateSet(t *testing.T) {
name := "test-dup-name"
_, prj := iam.TestScopes(t, iamRepo)
catalog := TestCatalogs(t, conn, prj.PublicId, 1)[0]
ss := testSets(t, conn, catalog.PublicId, 2)
ss := TestSets(t, conn, catalog.PublicId, 2)
sA, sB := ss[0], ss[1]
@@ -593,8 +593,8 @@ func TestRepository_UpdateSet(t *testing.T) {
catalogA, catalogB := catalogs[0], catalogs[1]
sA := testSets(t, conn, catalogA.PublicId, 1)[0]
sB := testSets(t, conn, catalogB.PublicId, 1)[0]
sA := TestSets(t, conn, catalogA.PublicId, 1)[0]
sB := TestSets(t, conn, catalogB.PublicId, 1)[0]
assert.NotEqual(sA.CatalogId, sB.CatalogId)
orig := sA.clone()
@@ -621,7 +621,7 @@ func TestRepository_LookupSet(t *testing.T) {
_, prj := iam.TestScopes(t, iamRepo)
catalog := TestCatalogs(t, conn, prj.PublicId, 1)[0]
hostSet := testSets(t, conn, catalog.PublicId, 1)[0]
hostSet := TestSets(t, conn, catalog.PublicId, 1)[0]
hostSetId, err := newHostSetId()
require.NoError(t, err)
@@ -676,7 +676,7 @@ func TestRepository_ListSets(t *testing.T) {
catalogs := TestCatalogs(t, conn, prj.PublicId, 2)
catalogA, catalogB := catalogs[0], catalogs[1]
hostSets := testSets(t, conn, catalogA.PublicId, 3)
hostSets := TestSets(t, conn, catalogA.PublicId, 3)
var tests = []struct {
name string
@@ -734,7 +734,7 @@ func TestRepository_ListSets_Limits(t *testing.T) {
_, prj := iam.TestScopes(t, iamRepo)
catalog := TestCatalogs(t, conn, prj.PublicId, 1)[0]
count := 10
hostSets := testSets(t, conn, catalog.PublicId, count)
hostSets := TestSets(t, conn, catalog.PublicId, count)
var tests = []struct {
name string
@@ -803,7 +803,7 @@ func TestRepository_DeleteSet(t *testing.T) {
_, prj := iam.TestScopes(t, iamRepo)
catalog := TestCatalogs(t, conn, prj.PublicId, 1)[0]
hostSet := testSets(t, conn, catalog.PublicId, 1)[0]
hostSet := TestSets(t, conn, catalog.PublicId, 1)[0]
newHostSetId, err := newHostSetId()
require.NoError(t, err)
+25
View File
@@ -59,3 +59,28 @@ func TestHosts(t *testing.T, conn *gorm.DB, catalogId string, count int) []*Host
}
return hosts
}
// TestSets creates count number of static host sets in the provided DB
// with the provided catalog id. The catalog must have been created
// previously. The test will fail if any errors are encountered.
func TestSets(t *testing.T, conn *gorm.DB, catalogId string, count int) []*HostSet {
t.Helper()
assert := assert.New(t)
var sets []*HostSet
for i := 0; i < count; i++ {
set, err := NewHostSet(catalogId)
assert.NoError(err)
assert.NotNil(set)
id, err := newHostSetId()
assert.NoError(err)
assert.NotEmpty(id)
set.PublicId = id
w := db.New(conn)
err2 := w.Create(context.Background(), set)
assert.NoError(err2)
sets = append(sets, set)
}
return sets
}
+20
View File
@@ -44,3 +44,23 @@ func Test_TestHosts(t *testing.T) {
assert.NotEmpty(a.GetPublicId())
}
}
func Test_TestSets(t *testing.T) {
t.Helper()
assert, require := assert.New(t), require.New(t)
conn, _ := db.TestSetup(t, "postgres")
wrapper := db.TestWrapper(t)
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
require.NotNil(prj)
assert.NotEmpty(prj.GetPublicId())
c := TestCatalogs(t, conn, prj.GetPublicId(), 1)[0]
count := 4
sets := TestSets(t, conn, c.GetPublicId(), count)
assert.Len(sets, count)
for _, s := range sets {
assert.NotEmpty(s.GetPublicId())
}
}