Skip to content

Commit

Permalink
integration test coverage for new RunInTx method
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanulit committed Nov 21, 2024
1 parent 8798736 commit 356f9cb
Showing 1 changed file with 129 additions and 0 deletions.
129 changes: 129 additions & 0 deletions service/integration/policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package integration

import (
"context"
"fmt"
"log/slog"
"testing"

"github.com/opentdf/platform/service/internal/fixtures"
"github.com/opentdf/platform/service/policy/db"
"github.com/stretchr/testify/suite"
)

type PolicyDBClientSuite struct {
suite.Suite
f fixtures.Fixtures
db fixtures.DBInterface
ctx context.Context
}

func (s *PolicyDBClientSuite) SetupSuite() {
s.ctx = context.Background()
c := *Config
c.DB.Schema = "text_opentdf_policy_db_client"
s.db = fixtures.NewDBInterface(c)
s.f = fixtures.NewFixture(s.db)
s.f.Provision()
}

func (s *PolicyDBClientSuite) TearDownSuite() {
slog.Info("tearing down db.PolicyDbClient test suite")
s.f.TearDown()
}

func (s *PolicyDBClientSuite) Test_RunInTx_CommitsOnSuccess() {
var (
nsName string = "success.com"

Check failure on line 37 in service/integration/policy_test.go

View workflow job for this annotation

GitHub Actions / go (service)

var-declaration: should omit type string from declaration of var nsName; it will be inferred from the right-hand side (revive)
attrName string = fmt.Sprintf("http://%s/attr/attr_one", nsName)

Check failure on line 38 in service/integration/policy_test.go

View workflow job for this annotation

GitHub Actions / go (service)

var-declaration: should omit type string from declaration of var attrName; it will be inferred from the right-hand side (revive)
valName string = fmt.Sprintf("http://%s/attr/%s/value/attr_one_value", nsName, attrName)

Check failure on line 39 in service/integration/policy_test.go

View workflow job for this annotation

GitHub Actions / go (service)

var-declaration: should omit type string from declaration of var valName; it will be inferred from the right-hand side (revive)

nsID string
attrID string
valID string
err error
)

txErr := s.db.PolicyClient.RunInTx(s.ctx, func(txClient *db.PolicyDBClient) error {
nsID, err = txClient.Queries.CreateNamespace(s.ctx, db.CreateNamespaceParams{
Name: nsName,
})
s.Require().NoError(err)
s.Require().NotNil(nsID)

attrID, err = txClient.Queries.CreateAttribute(s.ctx, db.CreateAttributeParams{
NamespaceID: nsID,
Name: attrName,
Rule: db.AttributeDefinitionRuleALLOF,
})
s.Require().NoError(err)
s.Require().NotNil(attrID)

valID, err = txClient.Queries.CreateAttributeValue(s.ctx, db.CreateAttributeValueParams{
AttributeDefinitionID: attrID,
Value: valName,
})
s.Require().NoError(err)
s.Require().NotNil(valID)

return nil
})
s.Require().NoError(txErr)

ns, err := s.db.PolicyClient.GetNamespace(s.ctx, nsID)
s.Require().NoError(err)
s.Equal(ns.Name, nsName)

Check failure on line 75 in service/integration/policy_test.go

View workflow job for this annotation

GitHub Actions / go (service)

avoid direct access to proto field ns.Name, use ns.GetName() instead (protogetter)

attr, err := s.db.PolicyClient.GetAttribute(s.ctx, attrID)
s.Require().NoError(err)
s.Equal(attr.Name, attrName)

Check failure on line 79 in service/integration/policy_test.go

View workflow job for this annotation

GitHub Actions / go (service)

avoid direct access to proto field attr.Name, use attr.GetName() instead (protogetter)

val, err := s.db.PolicyClient.GetAttributeValue(s.ctx, valID)
s.Require().NoError(err)
s.Equal(val.Value, valName)

Check failure on line 83 in service/integration/policy_test.go

View workflow job for this annotation

GitHub Actions / go (service)

avoid direct access to proto field val.Value, use val.GetValue() instead (protogetter)
}

func (s *PolicyDBClientSuite) Test_RunInTx_RollsBackOnFailure() {
var (
nsName string = "failure.com"

Check failure on line 88 in service/integration/policy_test.go

View workflow job for this annotation

GitHub Actions / go (service)

var-declaration: should omit type string from declaration of var nsName; it will be inferred from the right-hand side (revive)
attrName string = fmt.Sprintf("http://%s/attr/attr_one", nsName)

Check failure on line 89 in service/integration/policy_test.go

View workflow job for this annotation

GitHub Actions / go (service)

var-declaration: should omit type string from declaration of var attrName; it will be inferred from the right-hand side (revive)

nsID string
attrID string
err error
)

txErr := s.db.PolicyClient.RunInTx(s.ctx, func(txClient *db.PolicyDBClient) error {
nsID, err = txClient.Queries.CreateNamespace(s.ctx, db.CreateNamespaceParams{
Name: nsName,
})
s.Require().NoError(err)
s.Require().NotNil(nsID)

attrID, err = txClient.Queries.CreateAttribute(s.ctx, db.CreateAttributeParams{
NamespaceID: "invalid_ns_id",
Name: attrName,
Rule: db.AttributeDefinitionRuleALLOF,
})
s.Require().Error(err)
s.Require().Zero(attrID)

return nil
})
s.Require().Error(txErr)

ns, err := s.db.PolicyClient.GetNamespace(s.ctx, nsID)
s.Require().Error(err)
s.Nil(ns)

attr, err := s.db.PolicyClient.GetAttribute(s.ctx, attrID)
s.Require().Error(err)
s.Nil(attr)
}

func TestPolicySuite(t *testing.T) {
if testing.Short() {
t.Skip("skipping policy integration tests")
}
suite.Run(t, new(PolicyDBClientSuite))
}

0 comments on commit 356f9cb

Please sign in to comment.