Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macros mock unit test #508

Merged
merged 3 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
267 changes: 267 additions & 0 deletions spectrocloud/resource_macros_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package spectrocloud

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spectrocloud/palette-sdk-go/api/models"
"github.com/stretchr/testify/assert"
"testing"
Expand Down Expand Up @@ -206,3 +208,268 @@ func TestMergeExistingMacros_NoMacros(t *testing.T) {
// // Assertions for error case
// assert.Equal(t, "failed to get project UID", diagsWithError[0].Summary)
//}

func prepareBaseTenantMacrosSchema() *schema.ResourceData {
// Get an initialized ResourceData from resourceMacros
d := resourceMacros().TestResourceData()

// Set values for the macros and project fields
err := d.Set("macros", map[string]interface{}{
"macro1": "value1",
"macro2": "value2",
})
if err != nil {
panic(err) // Handle the error as appropriate in your test setup
}
return d
}

func prepareBaseProjectMacrosSchema() *schema.ResourceData {
// Get an initialized ResourceData from resourceMacros
d := resourceMacros().TestResourceData()

// Set values for the macros and project fields
err := d.Set("macros", map[string]interface{}{
"macro1": "value1",
"macro2": "value2",
})
if err != nil {
panic(err) // Handle the error as appropriate in your test setup
}

err = d.Set("project", "Default")
if err != nil {
panic(err) // Handle the error as appropriate in your test setup
}
return d
}

func TestResourceProjectMacrosCreate(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseProjectMacrosSchema()

// Call the function
diags := resourceMacrosCreate(ctx, resourceData, unitTestMockAPIClient)

// Assertions
assert.Equal(t, 0, len(diags))

}

func TestResourceTenantMacrosCreate(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseTenantMacrosSchema()

// Call the function
diags := resourceMacrosCreate(ctx, resourceData, unitTestMockAPIClient)

// Assertions
assert.Equal(t, 0, len(diags))

}

func TestResourceProjectMacrosRead(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseProjectMacrosSchema()

// Call the function
diags := resourceMacrosRead(ctx, resourceData, unitTestMockAPIClient)

// Assertions
assert.Equal(t, 0, len(diags))

}

func TestResourceTenantMacrosRead(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseTenantMacrosSchema()

// Call the function
diags := resourceMacrosRead(ctx, resourceData, unitTestMockAPIClient)

// Assertions
assert.Equal(t, 0, len(diags))

}

func TestResourceProjectMacrosUpdate(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseProjectMacrosSchema()
// Set values for the macros update
err := resourceData.Set("macros", map[string]interface{}{
"macro1": "value12",
"macro2": "value23",
})
if err != nil {
panic(err) // Handle the error as appropriate in your test setup
}

// Call the function
diags := resourceMacrosUpdate(ctx, resourceData, unitTestMockAPIClient)

// Assertions
assert.Equal(t, 0, len(diags))

}

func TestResourceTenantMacrosUpdate(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseTenantMacrosSchema()
// Set values for the macros update
err := resourceData.Set("macros", map[string]interface{}{
"macro1": "value12",
"macro2": "value23",
})
if err != nil {
panic(err) // Handle the error as appropriate in your test setup
}

// Call the function
diags := resourceMacrosUpdate(ctx, resourceData, unitTestMockAPIClient)

// Assertions
assert.Equal(t, 0, len(diags))
}

func TestResourceProjectMacrosDelete(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseProjectMacrosSchema()

// Call the function
diags := resourceMacrosDelete(ctx, resourceData, unitTestMockAPIClient)

// Assertions
assert.Equal(t, 0, len(diags))

}

func TestResourceTenantMacrosDelete(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseTenantMacrosSchema()

// Call the function
diags := resourceMacrosDelete(ctx, resourceData, unitTestMockAPIClient)

// Assertions
assert.Equal(t, 0, len(diags))
}

func TestResourceProjectMacrosCreateNegative(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseProjectMacrosSchema() // Assuming this prepares the schema data correctly

// Call the function
diags := resourceMacrosCreate(ctx, resourceData, unitTestMockAPINegativeClient)

// Assertions
if assert.NotEmpty(t, diags) { // Check that diags is not empty
assert.Contains(t, diags[0].Summary, "Macro already exists") // Verify the error message
}
}

func TestResourceTenantMacrosCreateNegative(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseTenantMacrosSchema()

// Call the function
diags := resourceMacrosCreate(ctx, resourceData, unitTestMockAPINegativeClient)

// Assertions
if assert.NotEmpty(t, diags) { // Check that diags is not empty
assert.Contains(t, diags[0].Summary, "Macro already exists") // Verify the error message
}
}

func TestResourceProjectMacrosReadNegative(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseProjectMacrosSchema()

// Call the function
diags := resourceMacrosRead(ctx, resourceData, unitTestMockAPINegativeClient)

// Assertions
if assert.NotEmpty(t, diags) { // Check that diags is not empty
assert.Contains(t, diags[0].Summary, "Macro not found") // Verify the error message
}

}

func TestResourceTenantMacrosReadNegative(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseTenantMacrosSchema()

// Call the function
diags := resourceMacrosRead(ctx, resourceData, unitTestMockAPINegativeClient)

// Assertions
if assert.NotEmpty(t, diags) { // Check that diags is not empty
assert.Contains(t, diags[0].Summary, "Macro not found") // Verify the error message
}

}

func TestResourceProjectMacrosUpdateNegative(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseProjectMacrosSchema()
// Set values for the macros update
err := resourceData.Set("macros", map[string]interface{}{
"macro1": "value12",
"macro2": "value23",
})
if err != nil {
panic(err) // Handle the error as appropriate in your test setup
}

// Call the function
diags := resourceMacrosUpdate(ctx, resourceData, unitTestMockAPINegativeClient)

// Assertions
assert.Empty(t, diags)

}

func TestResourceTenantMacrosUpdateNegative(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseTenantMacrosSchema()
// Set values for the macros update
err := resourceData.Set("macros", map[string]interface{}{
"macro1": "value12",
"macro2": "value23",
})
if err != nil {
panic(err) // Handle the error as appropriate in your test setup
}

// Call the function
diags := resourceMacrosUpdate(ctx, resourceData, unitTestMockAPINegativeClient)

// Assertions
assert.Empty(t, diags)
}

func TestResourceProjectMacrosDeleteNegative(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseProjectMacrosSchema()

// Call the function
diags := resourceMacrosDelete(ctx, resourceData, unitTestMockAPINegativeClient)

// Assertions
if assert.NotEmpty(t, diags) { // Check that diags is not empty
assert.Contains(t, diags[0].Summary, "Macro not found") // Verify the error message
}

}

func TestResourceTenantMacrosDeleteNegative(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseTenantMacrosSchema()

// Call the function
diags := resourceMacrosDelete(ctx, resourceData, unitTestMockAPINegativeClient)

// Assertions
if assert.NotEmpty(t, diags) { // Check that diags is not empty
assert.Contains(t, diags[0].Summary, "Macro not found") // Verify the error message
}
}
5 changes: 3 additions & 2 deletions tests/mockApiServer/apiServerMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func aggregateRoutes(routeFuncs ...func() []routes.Route) []routes.Route {
}

func init() {
// Initialize routes for port 8080
// Initialize routes for port 8080
allRoutesPositive = aggregateRoutes(
routes.CommonProjectRoutes,
routes.ProjectRoutes,
Expand All @@ -106,6 +106,7 @@ func init() {
routes.ApplicationRoutes,
routes.BackupRoutes,
routes.IPPoolRoutes,
routes.MacrosRoutes,
)
// Initialize routes for port 8888
allRoutesNegative = aggregateRoutes(
Expand All @@ -120,6 +121,6 @@ func init() {
routes.ClusterProfileNegativeRoutes,
routes.CloudAccountsNegativeRoutes,
routes.ClusterCommonNegativeRoutes,
routes.MacrosNegativeRoutes,
)

}
Loading
Loading