-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Css 10530/filterning resources (#1353)
* add filter by name and entity with tests
- Loading branch information
1 parent
f423bbd
commit 4d88a29
Showing
12 changed files
with
472 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,7 @@ package utils | |
func IntToPointer(i int) *int { | ||
return &i | ||
} | ||
|
||
func StringToPointer(s string) *string { | ||
return &s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ import ( | |
"github.com/canonical/jimm/v3/internal/dbmodel" | ||
) | ||
|
||
func SetupDB(c *qt.C, database *db.Database) (dbmodel.Model, dbmodel.Controller, dbmodel.Cloud) { | ||
func SetupDB(c *qt.C, database *db.Database) (dbmodel.Model, dbmodel.Controller, dbmodel.Cloud, dbmodel.Identity) { | ||
u, err := dbmodel.NewIdentity("[email protected]") | ||
c.Assert(err, qt.IsNil) | ||
c.Assert(database.DB.Create(&u).Error, qt.IsNil) | ||
|
@@ -66,21 +66,27 @@ func SetupDB(c *qt.C, database *db.Database) (dbmodel.Model, dbmodel.Controller, | |
} | ||
err = database.AddModel(context.Background(), &model) | ||
c.Assert(err, qt.Equals, nil) | ||
return model, controller, cloud | ||
clientIDWithDomain := "abda51b2-d735-4794-a8bd-49c506baa4af@serviceaccount" | ||
sa, err := dbmodel.NewIdentity(clientIDWithDomain) | ||
c.Assert(err, qt.Equals, nil) | ||
err = database.GetIdentity(context.Background(), sa) | ||
c.Assert(err, qt.Equals, nil) | ||
|
||
return model, controller, cloud, *sa | ||
} | ||
|
||
func (s *dbSuite) TestGetResources(c *qt.C) { | ||
ctx := context.Background() | ||
err := s.Database.Migrate(context.Background(), true) | ||
c.Assert(err, qt.Equals, nil) | ||
res, err := s.Database.ListResources(ctx, 10, 0) | ||
res, err := s.Database.ListResources(ctx, 10, 0, "", "") | ||
c.Assert(err, qt.Equals, nil) | ||
c.Assert(res, qt.HasLen, 0) | ||
// create one model, one controller, one cloud | ||
model, controller, cloud := SetupDB(c, s.Database) | ||
res, err = s.Database.ListResources(ctx, 10, 0) | ||
model, controller, cloud, sva := SetupDB(c, s.Database) | ||
res, err = s.Database.ListResources(ctx, 10, 0, "", "") | ||
c.Assert(err, qt.Equals, nil) | ||
c.Assert(res, qt.HasLen, 3) | ||
c.Assert(res, qt.HasLen, 4) | ||
for _, r := range res { | ||
switch r.Type { | ||
case "model": | ||
|
@@ -90,6 +96,91 @@ func (s *dbSuite) TestGetResources(c *qt.C) { | |
c.Assert(r.ID.String, qt.Equals, controller.UUID) | ||
case "cloud": | ||
c.Assert(r.ID.String, qt.Equals, cloud.Name) | ||
case "service_account": | ||
c.Assert(r.ID.String, qt.Equals, sva.Name) | ||
} | ||
} | ||
} | ||
|
||
func (s *dbSuite) TestGetResourcesWithNameTypeFilter(c *qt.C) { | ||
ctx := context.Background() | ||
err := s.Database.Migrate(context.Background(), true) | ||
c.Assert(err, qt.Equals, nil) | ||
// create one model, one controller, one cloud | ||
model, controller, cloud, sva := SetupDB(c, s.Database) | ||
|
||
tests := []struct { | ||
description string | ||
nameFilter string | ||
typeFilter string | ||
limit int | ||
offset int | ||
expectedSize int | ||
expectedUUIDs []string | ||
}{ | ||
{ | ||
description: "filter on model name", | ||
nameFilter: model.Name, | ||
limit: 10, | ||
offset: 0, | ||
typeFilter: "", | ||
expectedSize: 1, | ||
expectedUUIDs: []string{model.UUID.String}, | ||
}, | ||
{ | ||
description: "filter name test prefix", | ||
nameFilter: "test", | ||
limit: 10, | ||
offset: 0, | ||
typeFilter: "", | ||
expectedSize: 3, | ||
expectedUUIDs: []string{cloud.Name, controller.UUID, model.UUID.String}, | ||
}, | ||
{ | ||
description: "filter name controller suffix", | ||
nameFilter: "controller", | ||
limit: 10, | ||
offset: 0, | ||
typeFilter: "", | ||
expectedSize: 0, | ||
expectedUUIDs: []string{}, | ||
}, | ||
{ | ||
description: "filter only models", | ||
nameFilter: "test", | ||
limit: 10, | ||
offset: 0, | ||
typeFilter: "models", | ||
expectedSize: 1, | ||
expectedUUIDs: []string{model.UUID.String}, | ||
}, | ||
{ | ||
description: "filter only service accounts", | ||
nameFilter: "", | ||
limit: 10, | ||
offset: 0, | ||
typeFilter: "identities", | ||
expectedSize: 1, | ||
expectedUUIDs: []string{sva.Name}, | ||
}, | ||
{ | ||
description: "filter only service accounts and name", | ||
nameFilter: "not-found", | ||
limit: 10, | ||
offset: 0, | ||
typeFilter: "identities", | ||
expectedSize: 0, | ||
expectedUUIDs: []string{}, | ||
}, | ||
} | ||
for _, t := range tests { | ||
c.Run(t.description, func(c *qt.C) { | ||
res, err := s.Database.ListResources(ctx, t.limit, t.offset, t.nameFilter, t.typeFilter) | ||
c.Assert(err, qt.Equals, nil) | ||
c.Assert(res, qt.HasLen, t.expectedSize) | ||
for i, r := range res { | ||
c.Assert(r.ID.String, qt.Equals, t.expectedUUIDs[i]) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.