Skip to content

Commit

Permalink
Split into separate tests, tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Jan 31, 2023
1 parent 3fb3291 commit ca3cb75
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
45 changes: 30 additions & 15 deletions storage/internal/test/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ const testDecl = `{
"Identifier": "test_mysql_9e6a3aa7-5e4b-4d38-aacf-0f8058b2a899"
}`

// TestDeclarations performs a simple store, retrieve and delete test for declarations
func TestDeclarations(t *testing.T, storage api.DeclarationAPIStorage, ctx context.Context) {
decl, err := ddm.ParseDeclaration([]byte(testDecl))
if err != nil {
t.Fatal(err)
}
_, err = storage.StoreDeclaration(ctx, decl)
func testStoreDeclaration(t *testing.T, storage api.DeclarationAPIStorage, ctx context.Context, decl *ddm.Declaration) {
_, err := storage.StoreDeclaration(ctx, decl)
if err != nil {
t.Fatal(err)
}
Expand All @@ -32,35 +27,39 @@ func TestDeclarations(t *testing.T, storage api.DeclarationAPIStorage, ctx conte
}
found := false
for _, v := range decls {
if v == "test_mysql_9e6a3aa7-5e4b-4d38-aacf-0f8058b2a899" {
if v == decl.Identifier {
found = true
break
}
}
if found != true {
t.Error("could not find declaration id in list")
}
decl2, err := storage.RetrieveDeclaration(ctx, "test_mysql_9e6a3aa7-5e4b-4d38-aacf-0f8058b2a899")
decl2, err := storage.RetrieveDeclaration(ctx, decl.Identifier)
if err != nil {
t.Fatal(err)
}
if have, want := decl2.Identifier, "test_mysql_9e6a3aa7-5e4b-4d38-aacf-0f8058b2a899"; have != want {
if have, want := decl2.Identifier, decl.Identifier; have != want {
t.Errorf("have %q; want %q", have, want)
}
if have, want := decl2.Type, "com.apple.configuration.management.test"; have != want {
if have, want := decl2.Type, decl.Type; have != want {
t.Errorf("have %q; want %q", have, want)
}
_, err = storage.DeleteDeclaration(ctx, "test_mysql_9e6a3aa7-5e4b-4d38-aacf-0f8058b2a899")
// TODO: compare PayloadJSON
}

func testDeleteDeclaration(t *testing.T, storage api.DeclarationAPIStorage, ctx context.Context, id string) {
_, err := storage.DeleteDeclaration(ctx, id)
if err != nil {
t.Fatal(err)
}
decls, err = storage.RetrieveDeclarations(ctx)
decls, err := storage.RetrieveDeclarations(ctx)
if err != nil {
t.Fatal(err)
}
found = false
found := false
for _, v := range decls {
if v == "test_mysql_9e6a3aa7-5e4b-4d38-aacf-0f8058b2a899" {
if v == id {
found = true
break
}
Expand All @@ -69,3 +68,19 @@ func TestDeclarations(t *testing.T, storage api.DeclarationAPIStorage, ctx conte
t.Error("found declaration id in list (should have been deleted)")
}
}

// TestDeclarations performs a simple store, retrieve and delete test for declarations
func TestDeclarations(t *testing.T, storage api.DeclarationAPIStorage, 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("DeleteDeclaration", func(t *testing.T) {
testDeleteDeclaration(t, storage, ctx, decl.Identifier)
})
}
2 changes: 1 addition & 1 deletion storage/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

var flDSN = flag.String("dsn", "", "DSN of test MySQL instance")

func TestStuff(t *testing.T) {
func TestMySQL(t *testing.T) {
if *flDSN == "" {
t.Fatal("MySQL DSN flag not provided to test")
}
Expand Down

0 comments on commit ca3cb75

Please sign in to comment.