Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
FemiNoviaLina committed Oct 22, 2024
1 parent 0563da7 commit d952af2
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 46 deletions.
41 changes: 0 additions & 41 deletions internal/store/postgres/rule_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,47 +178,6 @@ func (r *RuleRepository) IsUpdated(ctx context.Context, since time.Time) bool {
return isUpdatedTest
}

func (r *RuleRepository) List(ctx context.Context) ([]rule.Config, error) {
query, params, err := dialect.From(TABLE_RULE_CONFIGS).ToSQL()
if err != nil {
return []rule.Config{}, err
}
ctx = otelsql.WithCustomAttributes(
ctx,
[]attribute.KeyValue{
attribute.String("db.repository.method", "List"),
attribute.String(string(semconv.DBSQLTableKey), TABLE_RULE_CONFIGS),
}...,
)

var ruleConfigModel []RuleConfig
if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error {
nrCtx := newrelic.FromContext(ctx)
if nrCtx != nil {
nr := newrelic.DatastoreSegment{
Product: newrelic.DatastorePostgres,
Collection: TABLE_RULE_CONFIGS,
Operation: "List",
StartTime: nrCtx.StartSegmentNow(),
}
defer nr.End()
}

return r.dbc.SelectContext(ctx, &ruleConfigModel, query, params...)
}); err != nil {
err = checkPostgresError(err)
if !errors.Is(err, sql.ErrNoRows) {
return []rule.Config{}, err
}
}

var res []rule.Config
for _, rule := range ruleConfigModel {
res = append(res, rule.transformToRuleConfig())
}
return res, nil
}

func (r *RuleRepository) GetAll(ctx context.Context) ([]rule.Ruleset, error) {
return r.cached, nil
}
Expand Down
128 changes: 123 additions & 5 deletions internal/store/postgres/rule_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package postgres_test

import (
"context"
"encoding/json"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand All @@ -21,7 +23,7 @@ type RuleRepositoryTestSuite struct {
pool *dockertest.Pool
resource *dockertest.Resource
repository *postgres.RuleRepository
Config []rule.Config
config []rule.Config
}

func (s *RuleRepositoryTestSuite) SetupSuite() {
Expand All @@ -34,12 +36,128 @@ func (s *RuleRepositoryTestSuite) SetupSuite() {
}

s.ctx = context.TODO()

s.config, err = bootstrapRuleConfig(s.client)
if err != nil {
s.T().Fatal(err)
}

s.repository = postgres.NewRuleRepository(s.client)
err = s.repository.InitCache(s.ctx)
if err != nil {
s.T().Fatal(err)
}
}

func (s *RuleRepositoryTestSuite) mergeRules() ([]rule.Ruleset, error) {
rules := []rule.Ruleset{}
for _, ruleConfig := range s.config {
var targetRuleset rule.Ruleset
if err := json.Unmarshal([]byte(ruleConfig.Config), &targetRuleset); err != nil {
return []rule.Ruleset{}, err
}
rules = append(rules, targetRuleset)
}

s.Config, err = bootstrapRuleConfig(s.client)
return rules, nil
}

func (s *RuleRepositoryTestSuite) TestGetAll() {
expected, err := s.mergeRules()
if err != nil {
s.T().Fatal(err)
}

type testCase struct {
Description string
Expected []rule.Ruleset
ErrString string
}

testCases := []testCase{
{
Description: "should get all rules from repository cache",
Expected: expected,
},
}

for _, tc := range testCases {
s.Run(tc.Description, func() {
got, err := s.repository.GetAll(s.ctx)
if tc.ErrString != "" {
if err.Error() != tc.ErrString {
s.T().Fatalf("got error %s, expected was %s", err.Error(), tc.ErrString)
}
}
if !cmp.Equal(got, tc.Expected) {
s.T().Fatalf("got result %+v, expected was %+v", got, tc.Expected)
}
})
}
}

func (s *RuleRepositoryTestSuite) TestFetch() {
expected, err := s.mergeRules()
if err != nil {
s.T().Fatal(err)
}

type testCase struct {
Description string
Expected []rule.Ruleset
ErrString string
}

testCases := []testCase{
{
Description: "should get all rules from repository cache",
Expected: expected,
},
}

for _, tc := range testCases {
s.Run(tc.Description, func() {
got, err := s.repository.Fetch(s.ctx)
if tc.ErrString != "" {
if err.Error() != tc.ErrString {
s.T().Fatalf("got error %s, expected was %s", err.Error(), tc.ErrString)
}
}
if !cmp.Equal(got, tc.Expected) {
s.T().Fatalf("got result %+v, expected was %+v", got, tc.Expected)
}
})
}
}

func (s *RuleRepositoryTestSuite) TestIsUpdated() {
type testCase struct {
Description string
Since time.Time
Expected bool
}

testCases := []testCase{
{
Description: "should get true if since before last updated",
Since: time.Time{},
Expected: true,
},
{
Description: "should get false if since after last updated",
Since: s.config[0].UpdatedAt.Add(10 * time.Hour),
Expected: false,
},
}

for _, tc := range testCases {
s.Run(tc.Description, func() {
got := s.repository.IsUpdated(s.ctx, tc.Since)
if !cmp.Equal(got, tc.Expected) {
s.T().Fatalf("got result %+v, expected was %+v", got, tc.Expected)
}
})
}
}

func (s *RuleRepositoryTestSuite) TestUpsert() {
Expand All @@ -66,13 +184,13 @@ func (s *RuleRepositoryTestSuite) TestUpsert() {
},
{
Description: "should update a resource config",
Name: s.Config[0].Name,
Name: s.config[0].Name,
Config: rule.Ruleset{
Rules: []rule.Rule{{}},
},
Expected: rule.Config{
ID: s.Config[0].ID,
Name: s.Config[0].Name,
ID: s.config[0].ID,
Name: s.config[0].Name,
Config: "{\"Rules\": [{\"Hooks\": null, \"Backend\": {\"URL\": \"\", \"Prefix\": \"\", \"Namespace\": \"\"}, \"Frontend\": {\"URL\": \"\", \"URLRx\": null, \"Method\": \"\"}, \"Middlewares\": null}]}",
},
},
Expand Down

0 comments on commit d952af2

Please sign in to comment.