Skip to content

Commit

Permalink
Add set tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Jan 31, 2023
1 parent ca3cb75 commit 1a89eb1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion storage/internal/test/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const testDecl = `{
"Payload": {
"Echo": "Foo"
},
"Identifier": "test_mysql_9e6a3aa7-5e4b-4d38-aacf-0f8058b2a899"
"Identifier": "test_golang_9e6a3aa7-5e4b-4d38-aacf-0f8058b2a899"
}`

func testStoreDeclaration(t *testing.T, storage api.DeclarationAPIStorage, ctx context.Context, decl *ddm.Declaration) {
Expand Down
95 changes: 95 additions & 0 deletions storage/internal/test/sets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package test

import (
"context"
"testing"

"github.com/jessepeterson/kmfddm/ddm"
"github.com/jessepeterson/kmfddm/http/api"
)

type setAndDeclStorage interface {
api.SetAPIStorage
api.DeclarationAPIStorage
}

func testSet(t *testing.T, storage setAndDeclStorage, ctx context.Context, decl *ddm.Declaration, setName string) {
// associate
_, err := storage.StoreSetDeclaration(ctx, setName, decl.Identifier)
if err != nil {
t.Fatal(err)
}

// find in list
sets, err := storage.RetrieveSets(ctx)
if err != nil {
t.Fatal(err)
}
found := false
for _, s := range sets {
if setName == s {
found = true
break
}
}
if !found {
t.Error("could not find set in list")
}

// find the ref
setNames, err := storage.RetrieveDeclarationSets(ctx, decl.Identifier)
if err != nil {
t.Fatal(err)
}
found = false
for _, v := range setNames {
if setName == v {
found = true
break
}
}
if !found {
t.Error("could not find set in declaration sets list")
}

// find the backref
decls, err := storage.RetrieveSetDeclarations(ctx, setName)
if err != nil {
t.Fatal(err)
}
found = false
for _, v := range decls {
if decl.Identifier == v {
found = true
break
}
}
if !found {
t.Error("could not find declaration in declaration sets list")
}

// dissociate
_, err = storage.RemoveSetDeclaration(ctx, setName, decl.Identifier)
if err != nil {
t.Fatal(err)
}
}

func TestSets(t *testing.T, storage setAndDeclStorage, ctx context.Context) {
decl, err := ddm.ParseDeclaration([]byte(testDecl))
if err != nil {
t.Fatal(err)
}

t.Run("StoreDeclaration", func(t *testing.T) {
testStoreDeclaration(t, storage, ctx, decl)
})

t.Run("TestSet", func(t *testing.T) {
testSet(t, storage, ctx, decl, "test_golang_set1")
})

t.Run("DeleteDeclaration", func(t *testing.T) {
testDeleteDeclaration(t, storage, ctx, decl.Identifier)
})
}
2 changes: 2 additions & 0 deletions storage/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ func TestMySQL(t *testing.T) {
ctx := context.Background()

test.TestDeclarations(t, storage, ctx)

test.TestSets(t, storage, ctx)
}

0 comments on commit 1a89eb1

Please sign in to comment.