Skip to content

Commit

Permalink
fix: unit test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ffforest committed Aug 16, 2024
1 parent 2fbada0 commit 5946ca7
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 92 deletions.
4 changes: 2 additions & 2 deletions pkg/domain/request/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func decode(r *http.Request, payload interface{}) error {
func validPath(path string) bool {
// Validate project and stack path contains one or more capturing group
// that contains a backslash with alphanumeric and underscore characters
return !regexp.MustCompile(`^([\/a-zA-Z0-9_])+$`).MatchString(path)
return !regexp.MustCompile(`^([\/a-zA-Z0-9_-])+$`).MatchString(path)
}

func validName(name string) bool {
// Validate project, stack and appconfig name contains only alphanumeric
// and underscore characters
return !regexp.MustCompile(`^[a-zA-Z0-9_]+$`).MatchString(name)
return !regexp.MustCompile(`^[a-zA-Z0-9_-]+$`).MatchString(name)
}
86 changes: 86 additions & 0 deletions pkg/engine/runtime/terraform/tfops/test_data/plan.out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"format_version": "1.0",
"terraform_version": "1.1.0",
"planned_values": {
"root_module": {
"resources": [
{
"address": "local_file.kusion_example",
"mode": "managed",
"type": "local_file",
"name": "kusion_example",
"provider_name": "registry.terraform.io/hashicorp/local",
"schema_version": 0,
"values": {
"content": "kusion",
"content_base64": null,
"directory_permission": "0777",
"file_permission": "0777",
"filename": "test.txt",
"sensitive_content": null,
"source": null
}
}
]
}
},
"resource_changes": [
{
"address": "local_file.kusion_example",
"mode": "managed",
"type": "local_file",
"name": "kusion_example",
"provider_name": "registry.terraform.io/hashicorp/local",
"change": {
"actions": [
"create"
],
"before": null,
"after": {
"content": "kusion",
"content_base64": null,
"directory_permission": "0777",
"file_permission": "0777",
"filename": "test.txt",
"sensitive_content": null,
"source": null
},
"after_unknown": {
"id": true
},
"before_sensitive": false,
"after_sensitive": {
"sensitive_content": true
}
}
}
],
"configuration": {
"provider_config": {
"local": {
"name": "local",
"version_constraint": "2.2.3"
}
},
"root_module": {
"resources": [
{
"address": "local_file.kusion_example",
"mode": "managed",
"type": "local_file",
"name": "kusion_example",
"provider_config_key": "local",
"expressions": {
"content": {
"constant_value": "kusion"
},
"filename": {
"constant_value": "test.txt"
}
},
"schema_version": 0
}
]
}
}
}
5 changes: 2 additions & 3 deletions pkg/infra/persistence/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ func TestBackendRepository(t *testing.T) {
defer CloseDB(t, fakeGDB)
defer sqlMock.ExpectClose()

var expectedID, expectedRows uint = 1, 1
var expectedID uint = 1
sqlMock.ExpectBegin()
sqlMock.ExpectQuery("SELECT").
WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow(1))
sqlMock.ExpectExec("UPDATE").
WillReturnResult(sqlmock.NewResult(int64(expectedID), int64(expectedRows)))
sqlMock.ExpectExec("DELETE").WillReturnResult(sqlmock.NewResult(int64(expectedID), int64(0)))
sqlMock.ExpectCommit()
err = repo.Delete(context.Background(), expectedID)
require.NoError(t, err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/infra/persistence/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ func TestOrganizationRepository(t *testing.T) {
defer CloseDB(t, fakeGDB)
defer sqlMock.ExpectClose()

var expectedID, expectedRows uint = 1, 1
var expectedID uint = 1
sqlMock.ExpectBegin()
sqlMock.ExpectQuery("SELECT").
WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow(1))
sqlMock.ExpectExec("UPDATE").
WillReturnResult(sqlmock.NewResult(int64(expectedID), int64(expectedRows)))
sqlMock.ExpectExec("DELETE").WillReturnResult(sqlmock.NewResult(int64(expectedID), int64(0)))
sqlMock.ExpectCommit()
err = repo.Delete(context.Background(), expectedID)
require.NoError(t, err)
Expand Down
9 changes: 5 additions & 4 deletions pkg/infra/persistence/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func TestProjectRepository(t *testing.T) {
Remote: mockRemoteURL,
},
Organization: &entity.Organization{
ID: 1,
Name: "mockedOrg",
ID: 1,
Owners: []string{"hua.li", "xiaoming.li"},
},
Path: "/path/to/project",
Labels: []string{"testLabel"},
Expand All @@ -57,13 +59,12 @@ func TestProjectRepository(t *testing.T) {
defer CloseDB(t, fakeGDB)
defer sqlMock.ExpectClose()

var expectedID, expectedRows uint = 1, 1
var expectedID uint = 1
sqlMock.ExpectBegin()
sqlMock.ExpectQuery("SELECT").
WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow(1))
sqlMock.ExpectExec("UPDATE").
WillReturnResult(sqlmock.NewResult(int64(expectedID), int64(expectedRows)))
sqlMock.ExpectExec("DELETE").WillReturnResult(sqlmock.NewResult(int64(expectedID), int64(0)))
sqlMock.ExpectCommit()
err = repo.Delete(context.Background(), expectedID)
require.NoError(t, err)
Expand Down
1 change: 0 additions & 1 deletion pkg/infra/persistence/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ func (r *sourceRepository) Get(ctx context.Context, id uint) (*entity.Source, er
if err != nil {
return nil, err
}

return dataModel.ToEntity()
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/infra/persistence/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ func TestSourceRepository(t *testing.T) {
defer CloseDB(t, fakeGDB)
defer sqlMock.ExpectClose()

var expectedID, expectedRows uint = 1, 1
var expectedID uint = 1
sqlMock.ExpectBegin()
sqlMock.ExpectQuery("SELECT").
WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow(1))
sqlMock.ExpectExec("UPDATE").
WillReturnResult(sqlmock.NewResult(int64(expectedID), int64(expectedRows)))
sqlMock.ExpectExec("DELETE").WillReturnResult(sqlmock.NewResult(int64(expectedID), int64(0)))
sqlMock.ExpectCommit()
err = repo.Delete(context.Background(), expectedID)
require.NoError(t, err)
Expand Down
9 changes: 5 additions & 4 deletions pkg/infra/persistence/stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func TestStackRepository(t *testing.T) {
Remote: mockRemoteURL,
},
Organization: &entity.Organization{
ID: 1,
Name: "mockedOrg",
ID: 1,
Owners: []string{"hua.li", "xiaoming.li"},
},
},
Path: "/path/to/stack",
Expand All @@ -66,13 +68,12 @@ func TestStackRepository(t *testing.T) {
defer CloseDB(t, fakeGDB)
defer sqlMock.ExpectClose()

var expectedID, expectedRows uint = 1, 1
var expectedID uint = 1
sqlMock.ExpectBegin()
sqlMock.ExpectQuery("SELECT").
WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow(1))
sqlMock.ExpectExec("UPDATE").
WillReturnResult(sqlmock.NewResult(int64(expectedID), int64(expectedRows)))
sqlMock.ExpectExec("DELETE").WillReturnResult(sqlmock.NewResult(int64(expectedID), int64(0)))
sqlMock.ExpectCommit()
err = repo.Delete(context.Background(), expectedID)
require.NoError(t, err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/infra/persistence/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ func TestWorkspaceRepository(t *testing.T) {
defer CloseDB(t, fakeGDB)
defer sqlMock.ExpectClose()

var expectedID, expectedRows uint = 1, 1
var expectedID uint = 1
sqlMock.ExpectBegin()
sqlMock.ExpectQuery("SELECT").
WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow(1))
sqlMock.ExpectExec("UPDATE").
WillReturnResult(sqlmock.NewResult(int64(expectedID), int64(expectedRows)))
sqlMock.ExpectExec("DELETE").WillReturnResult(sqlmock.NewResult(int64(expectedID), int64(0)))
sqlMock.ExpectCommit()
err = repo.Delete(context.Background(), expectedID)
require.NoError(t, err)
Expand Down
16 changes: 7 additions & 9 deletions pkg/server/handler/backend/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestBackendHandler(t *testing.T) {
AddRow(1, backendName))

// Create a new HTTP request
req, err := http.NewRequest("GET", "/backend/{backendID}", nil)
req, err := http.NewRequest("GET", "/backends/{backendID}", nil)
assert.NoError(t, err)

rctx := chi.NewRouteContext()
Expand Down Expand Up @@ -92,11 +92,10 @@ func TestBackendHandler(t *testing.T) {
defer sqlMock.ExpectClose()

// Create a new HTTP request
req, err := http.NewRequest("POST", "/backend/{backendID}", nil)
req, err := http.NewRequest("POST", "/backends", nil)
assert.NoError(t, err)

rctx := chi.NewRouteContext()
rctx.URLParams.Add("backendID", "1")
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

// Set request body
Expand Down Expand Up @@ -135,7 +134,7 @@ func TestBackendHandler(t *testing.T) {
defer sqlMock.ExpectClose()

// Update a new HTTP request
req, err := http.NewRequest("POST", "/backend/{backendID}", nil)
req, err := http.NewRequest("POST", "/backends/{backendID}", nil)
assert.NoError(t, err)

rctx := chi.NewRouteContext()
Expand Down Expand Up @@ -183,7 +182,7 @@ func TestBackendHandler(t *testing.T) {
defer sqlMock.ExpectClose()

// Create a new HTTP request
req, err := http.NewRequest("DELETE", "/backend/{backendID}", nil)
req, err := http.NewRequest("DELETE", "/backends/{backendID}", nil)
assert.NoError(t, err)

rctx := chi.NewRouteContext()
Expand All @@ -195,8 +194,7 @@ func TestBackendHandler(t *testing.T) {
sqlMock.ExpectQuery("SELECT").
WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow(1))
sqlMock.ExpectExec("UPDATE").
WillReturnResult(sqlmock.NewResult(1, 1))
sqlMock.ExpectExec("DELETE").WillReturnResult(sqlmock.NewResult(1, 0))
sqlMock.ExpectCommit()

// Call the DeleteBackend handler function
Expand All @@ -219,7 +217,7 @@ func TestBackendHandler(t *testing.T) {
defer sqlMock.ExpectClose()

// Create a new HTTP request
req, err := http.NewRequest("DELETE", "/backend/{backendID}", nil)
req, err := http.NewRequest("DELETE", "/backends/{backendID}", nil)
assert.NoError(t, err)

rctx := chi.NewRouteContext()
Expand Down Expand Up @@ -250,7 +248,7 @@ func TestBackendHandler(t *testing.T) {
defer sqlMock.ExpectClose()

// Update a new HTTP request
req, err := http.NewRequest("POST", "/backend/{backendID}", nil)
req, err := http.NewRequest("POST", "/backends/{backendID}", nil)
assert.NoError(t, err)

rctx := chi.NewRouteContext()
Expand Down
16 changes: 7 additions & 9 deletions pkg/server/handler/organization/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestOrganizationHandler(t *testing.T) {
AddRow(1, orgName))

// Create a new HTTP request
req, err := http.NewRequest("GET", "/organization/{organizationID}", nil)
req, err := http.NewRequest("GET", "/organizations/{organizationID}", nil)
assert.NoError(t, err)

rctx := chi.NewRouteContext()
Expand Down Expand Up @@ -94,11 +94,10 @@ func TestOrganizationHandler(t *testing.T) {
defer sqlMock.ExpectClose()

// Create a new HTTP request
req, err := http.NewRequest("POST", "/organization/{organizationID}", nil)
req, err := http.NewRequest("POST", "/organizations", nil)
assert.NoError(t, err)

rctx := chi.NewRouteContext()
rctx.URLParams.Add("organizationID", "1")
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

// Set request body
Expand Down Expand Up @@ -138,7 +137,7 @@ func TestOrganizationHandler(t *testing.T) {
defer sqlMock.ExpectClose()

// Update a new HTTP request
req, err := http.NewRequest("POST", "/organization/{organizationID}", nil)
req, err := http.NewRequest("POST", "/organizations/{organizationID}", nil)
assert.NoError(t, err)

rctx := chi.NewRouteContext()
Expand Down Expand Up @@ -186,7 +185,7 @@ func TestOrganizationHandler(t *testing.T) {
defer sqlMock.ExpectClose()

// Create a new HTTP request
req, err := http.NewRequest("DELETE", "/organization/{organizationID}", nil)
req, err := http.NewRequest("DELETE", "/organizations/{organizationID}", nil)
assert.NoError(t, err)

rctx := chi.NewRouteContext()
Expand All @@ -198,8 +197,7 @@ func TestOrganizationHandler(t *testing.T) {
sqlMock.ExpectQuery("SELECT").
WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow(1))
sqlMock.ExpectExec("UPDATE").
WillReturnResult(sqlmock.NewResult(1, 1))
sqlMock.ExpectExec("DELETE").WillReturnResult(sqlmock.NewResult(1, 0))
sqlMock.ExpectCommit()

// Call the DeleteOrganization handler function
Expand All @@ -222,7 +220,7 @@ func TestOrganizationHandler(t *testing.T) {
defer sqlMock.ExpectClose()

// Create a new HTTP request
req, err := http.NewRequest("DELETE", "/organization/{organizationID}", nil)
req, err := http.NewRequest("DELETE", "/organizations/{organizationID}", nil)
assert.NoError(t, err)

rctx := chi.NewRouteContext()
Expand Down Expand Up @@ -253,7 +251,7 @@ func TestOrganizationHandler(t *testing.T) {
defer sqlMock.ExpectClose()

// Update a new HTTP request
req, err := http.NewRequest("POST", "/organization/{organizationID}", nil)
req, err := http.NewRequest("POST", "/organizations/{organizationID}", nil)
assert.NoError(t, err)

rctx := chi.NewRouteContext()
Expand Down
Loading

0 comments on commit 5946ca7

Please sign in to comment.