diff --git a/internal/store/postgres/rule_repository.go b/internal/store/postgres/rule_repository.go index 283c50bb5..33bee734d 100644 --- a/internal/store/postgres/rule_repository.go +++ b/internal/store/postgres/rule_repository.go @@ -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 } diff --git a/internal/store/postgres/rule_repository_test.go b/internal/store/postgres/rule_repository_test.go index 5bebc9ea1..609e8c048 100644 --- a/internal/store/postgres/rule_repository_test.go +++ b/internal/store/postgres/rule_repository_test.go @@ -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" @@ -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() { @@ -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() { @@ -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}]}", }, },