Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SimoneDutto committed Sep 4, 2024
1 parent 1a882de commit 5ded876
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 48 deletions.
46 changes: 23 additions & 23 deletions internal/db/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,6 @@ import (

// MULTI_TABLES_RAW_SQL contains the raw query fetching entities from multiple tables, with their respective entity parents.
const MULTI_TABLES_RAW_SQL = `
(
SELECT 'controller' AS type,
controllers.uuid AS id,
controllers.name AS name,
'' AS parent_id,
'' AS parent_name,
'' AS parent_type
FROM controllers
)
UNION
(
SELECT 'model' AS type,
models.uuid AS id,
models.name AS name,
controllers.uuid AS parent_id,
controllers.name AS parent_name,
'controller' AS parent_type
FROM models
JOIN controllers ON models.controller_id = controllers.id
)
UNION
(
SELECT 'application_offer' AS type,
application_offers.uuid AS id,
Expand All @@ -53,6 +32,27 @@ UNION
FROM clouds
)
UNION
(
SELECT 'controller' AS type,
controllers.uuid AS id,
controllers.name AS name,
'' AS parent_id,
'' AS parent_name,
'' AS parent_type
FROM controllers
)
UNION
(
SELECT 'model' AS type,
models.uuid AS id,
models.name AS name,
controllers.uuid AS parent_id,
controllers.name AS parent_name,
'controller' AS parent_type
FROM models
JOIN controllers ON models.controller_id = controllers.id
)
UNION
(
SELECT 'service_account' AS type,
identities.name AS id,
Expand All @@ -61,9 +61,9 @@ UNION
'' AS parent_name,
'' AS parent_type
FROM identities
WHERE name NOT LIKE '%@%'
WHERE name LIKE '%@serviceaccount'
)
ORDER BY id
ORDER BY type, id
OFFSET ?
LIMIT ?;
`
Expand Down
14 changes: 8 additions & 6 deletions internal/db/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import (
"github.com/canonical/jimm/v3/internal/dbmodel"
)

func (s *dbSuite) Setup(c *qt.C) (dbmodel.Model, dbmodel.Controller, dbmodel.Cloud) {
err := s.Database.Migrate(context.Background(), true)
c.Assert(err, qt.Equals, nil)

func (s *dbSuite) SetupDB(c *qt.C) (dbmodel.Model, dbmodel.Controller, dbmodel.Cloud) {
u, err := dbmodel.NewIdentity("[email protected]")
c.Assert(err, qt.IsNil)
c.Assert(s.Database.DB.Create(&u).Error, qt.IsNil)
Expand Down Expand Up @@ -73,11 +70,16 @@ func (s *dbSuite) Setup(c *qt.C) (dbmodel.Model, dbmodel.Controller, dbmodel.Clo
}

func (s *dbSuite) TestGetResources(c *qt.C) {
// create one model, one controller, one cloud
model, controller, cloud := s.Setup(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)
c.Assert(err, qt.Equals, nil)
c.Assert(res, qt.HasLen, 0)
// create one model, one controller, one cloud
model, controller, cloud := s.SetupDB(c)
res, err = s.Database.ListResources(ctx, 10, 0)
c.Assert(err, qt.Equals, nil)
c.Assert(res, qt.HasLen, 3)
for _, r := range res {
switch r.Type {
Expand Down
8 changes: 3 additions & 5 deletions internal/jimm/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package jimm_test

import (
"context"
"slices"
"testing"
"time"

Expand Down Expand Up @@ -35,10 +34,9 @@ func TestGetResources(t *testing.T) {

err = j.Database.Migrate(ctx, false)
c.Assert(err, qt.IsNil)
user, _, controller, model, applicationOffer, cloud, _ := createTestControllerEnvironment(ctx, c, j.Database)
_, _, controller, model, applicationOffer, cloud, _ := createTestControllerEnvironment(ctx, c, j.Database)

ids := []string{user.Name, controller.UUID, model.UUID.String, applicationOffer.UUID, cloud.Name}
slices.Sort(ids)
ids := []string{applicationOffer.UUID, cloud.Name, controller.UUID, model.UUID.String}

u := openfga.NewUser(&dbmodel.Identity{Name: "[email protected]"}, ofgaClient)
u.JimmAdmin = true
Expand All @@ -59,7 +57,7 @@ func TestGetResources(t *testing.T) {
desc: "test with remianing ids",
limit: 3,
offset: 3,
identities: []string{ids[3], ids[4]},
identities: []string{ids[3]},
},
{
desc: "test out of range",
Expand Down
41 changes: 27 additions & 14 deletions internal/rebac_admin/resources_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rebac_admin_test

import (
"context"
"slices"

rebac_handlers "github.com/canonical/rebac-admin-ui-handlers/v1"
"github.com/canonical/rebac-admin-ui-handlers/v1/resources"
Expand Down Expand Up @@ -73,27 +72,38 @@ func (s *resourcesSuite) TestPatchIdentityEntitlements(c *gc.C) {
tester := jimmtest.GocheckTester{C: c}
env := jimmtest.ParseEnvironment(tester, resourcesTestEnv)
env.PopulateDB(tester, s.JIMM.Database)
ids := make([]string, 0)
for _, m := range env.Models {
ids = append(ids, m.UUID)
type testEntity struct {
Id string
ParentId string
}
ids := make([]testEntity, 0)
for _, c := range env.Clouds {
ids = append(ids, testEntity{
Id: c.Name,
ParentId: "",
})
}
for _, c := range env.Controllers {
ids = append(ids, c.UUID)
ids = append(ids, testEntity{
Id: c.UUID,
ParentId: "",
})
}
for _, c := range env.Clouds {
ids = append(ids, c.Name)
for _, m := range env.Models {
ids = append(ids, testEntity{
Id: m.UUID,
ParentId: env.Controller(m.Controller).UUID,
})
}

slices.Sort(ids)

testCases := []struct {
desc string
size *int
page *int
wantPage int
wantSize int
wantNextpage *int
ids []string
ids []testEntity
}{
{
desc: "test with first page",
Expand All @@ -102,7 +112,7 @@ func (s *resourcesSuite) TestPatchIdentityEntitlements(c *gc.C) {
wantPage: 0,
wantSize: 2,
wantNextpage: utils.IntToPointer(1),
ids: []string{ids[0], ids[1]},
ids: []testEntity{ids[0], ids[1]},
},
{
desc: "test with second page",
Expand All @@ -111,7 +121,7 @@ func (s *resourcesSuite) TestPatchIdentityEntitlements(c *gc.C) {
wantPage: 1,
wantSize: 2,
wantNextpage: utils.IntToPointer(2),
ids: []string{ids[2], ids[3]},
ids: []testEntity{ids[2], ids[3]},
},
{
desc: "test with last page",
Expand All @@ -120,7 +130,7 @@ func (s *resourcesSuite) TestPatchIdentityEntitlements(c *gc.C) {
wantPage: 2,
wantSize: 1,
wantNextpage: nil,
ids: []string{ids[4]},
ids: []testEntity{ids[4]},
},
}
for _, t := range testCases {
Expand All @@ -138,7 +148,10 @@ func (s *resourcesSuite) TestPatchIdentityEntitlements(c *gc.C) {
c.Assert(*resources.Next.Page, gc.Equals, *t.wantNextpage)
}
for i := range len(t.ids) {
c.Assert(resources.Data[i].Entity.Id, gc.Equals, t.ids[i])
c.Assert(resources.Data[i].Entity.Id, gc.Equals, t.ids[i].Id)
if t.ids[i].ParentId != "" {
c.Assert(resources.Data[i].Parent.Id, gc.Equals, t.ids[i].ParentId)
}
}
}

Expand Down

0 comments on commit 5ded876

Please sign in to comment.