|
| 1 | +package postgres_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/raystack/frontier/billing/subscription" |
| 9 | + "github.com/raystack/frontier/internal/store/postgres" |
| 10 | + "github.com/raystack/frontier/pkg/db" |
| 11 | + "github.com/stretchr/testify/suite" |
| 12 | +) |
| 13 | + |
| 14 | +// Runs the billing subscription reads against a real postgres to check that a |
| 15 | +// soft-deleted subscription stays out of every read. |
| 16 | +type BillingSubscriptionRepositoryPGTestSuite struct { |
| 17 | + suite.Suite |
| 18 | + ctx context.Context |
| 19 | + client *db.Client |
| 20 | + repository *postgres.BillingSubscriptionRepository |
| 21 | +} |
| 22 | + |
| 23 | +func (s *BillingSubscriptionRepositoryPGTestSuite) SetupSuite() { |
| 24 | + var err error |
| 25 | + s.client, err = newTestClient() |
| 26 | + if err != nil { |
| 27 | + s.T().Fatal(err) |
| 28 | + } |
| 29 | + s.ctx = context.TODO() |
| 30 | + s.repository = postgres.NewBillingSubscriptionRepository(s.client) |
| 31 | +} |
| 32 | + |
| 33 | +func (s *BillingSubscriptionRepositoryPGTestSuite) TearDownSuite() { |
| 34 | + if err := closeTestClient(s.client); err != nil { |
| 35 | + s.T().Fatal(err) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func (s *BillingSubscriptionRepositoryPGTestSuite) SetupTest() { |
| 40 | + s.exec(`INSERT INTO organizations (name, title) VALUES ('bs-live', 'Live Org')`) |
| 41 | + s.exec(`INSERT INTO billing_customers (org_id, provider_id, name, email) |
| 42 | + VALUES ((SELECT id FROM organizations WHERE name = 'bs-live'), 'bs-cust', 'bs-cust', 'bs-cust')`) |
| 43 | + s.exec(`INSERT INTO billing_plans (name, description) VALUES ('bs-plan', 'plan under test')`) |
| 44 | + |
| 45 | + s.subscription("bs-sub-live") |
| 46 | + s.subscription("bs-sub-gone") |
| 47 | + |
| 48 | + s.exec(`UPDATE billing_subscriptions SET deleted_at = now() WHERE provider_id = 'bs-sub-gone'`) |
| 49 | +} |
| 50 | + |
| 51 | +func (s *BillingSubscriptionRepositoryPGTestSuite) TearDownTest() { |
| 52 | + queries := []string{} |
| 53 | + for _, table := range []string{postgres.TABLE_BILLING_SUBSCRIPTIONS, postgres.TABLE_BILLING_CUSTOMERS, |
| 54 | + postgres.TABLE_BILLING_PLANS, postgres.TABLE_ORGANIZATIONS} { |
| 55 | + queries = append(queries, fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE", table)) |
| 56 | + } |
| 57 | + if err := execQueries(s.ctx, s.client, queries); err != nil { |
| 58 | + s.T().Fatal(err) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (s *BillingSubscriptionRepositoryPGTestSuite) exec(query string, args ...any) { |
| 63 | + s.T().Helper() |
| 64 | + execSQL(s.T(), s.ctx, s.client, query, args...) |
| 65 | +} |
| 66 | + |
| 67 | +func (s *BillingSubscriptionRepositoryPGTestSuite) customerID() string { |
| 68 | + s.T().Helper() |
| 69 | + return scalarSQL(s.T(), s.ctx, s.client, `SELECT id FROM billing_customers WHERE name = 'bs-cust'`) |
| 70 | +} |
| 71 | + |
| 72 | +func (s *BillingSubscriptionRepositoryPGTestSuite) subscriptionID(providerID string) string { |
| 73 | + s.T().Helper() |
| 74 | + return scalarSQL(s.T(), s.ctx, s.client, |
| 75 | + `SELECT id FROM billing_subscriptions WHERE provider_id = $1`, providerID) |
| 76 | +} |
| 77 | + |
| 78 | +func (s *BillingSubscriptionRepositoryPGTestSuite) subscription(providerID string) { |
| 79 | + s.T().Helper() |
| 80 | + s.exec(`INSERT INTO billing_subscriptions (customer_id, provider_id, plan_id, state) |
| 81 | + VALUES ((SELECT id FROM billing_customers WHERE name = 'bs-cust'), $1, |
| 82 | + (SELECT id FROM billing_plans WHERE name = 'bs-plan'), 'active')`, providerID) |
| 83 | +} |
| 84 | + |
| 85 | +func (s *BillingSubscriptionRepositoryPGTestSuite) TestGetByIDSkipsDeleted() { |
| 86 | + got, err := s.repository.GetByID(s.ctx, s.subscriptionID("bs-sub-live")) |
| 87 | + s.Require().NoError(err) |
| 88 | + s.Equal("bs-sub-live", got.ProviderID) |
| 89 | + |
| 90 | + _, err = s.repository.GetByID(s.ctx, s.subscriptionID("bs-sub-gone")) |
| 91 | + s.ErrorIs(err, subscription.ErrNotFound) |
| 92 | +} |
| 93 | + |
| 94 | +func (s *BillingSubscriptionRepositoryPGTestSuite) TestGetByProviderIDSkipsDeleted() { |
| 95 | + got, err := s.repository.GetByProviderID(s.ctx, "bs-sub-live") |
| 96 | + s.Require().NoError(err) |
| 97 | + s.Equal("bs-sub-live", got.ProviderID) |
| 98 | + |
| 99 | + _, err = s.repository.GetByProviderID(s.ctx, "bs-sub-gone") |
| 100 | + s.ErrorIs(err, subscription.ErrNotFound) |
| 101 | +} |
| 102 | + |
| 103 | +func (s *BillingSubscriptionRepositoryPGTestSuite) TestListSkipsDeleted() { |
| 104 | + got, err := s.repository.List(s.ctx, subscription.Filter{CustomerID: s.customerID()}) |
| 105 | + s.Require().NoError(err) |
| 106 | + s.Require().Len(got, 1) |
| 107 | + s.Equal("bs-sub-live", got[0].ProviderID) |
| 108 | +} |
| 109 | + |
| 110 | +func TestBillingSubscriptionRepositoryPG(t *testing.T) { |
| 111 | + suite.Run(t, new(BillingSubscriptionRepositoryPGTestSuite)) |
| 112 | +} |
0 commit comments