world

package
v0.0.0-...-1064994 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package world builds the rows a test needs through the functions the console itself calls: identity and organization queries, the provisioning step that gives a workspace its pool, the catalog queries, the rule-change function, the conferral queries, and the plan fixtures (prices, price mappings, issued grants, subscriptions and their scheduled changes), billing accounts, invoices and their lines, payments, the Stripe mappings of billing rows and of products, and the grant, tier and subscription acts (an extension, a reorder, a tier's removal, a subscription's end and its suspension), organization members and a second pool. Every method fails the test on any error. It suffixes the organization, product, set and ladder names, subjects and emails it writes with the World's suffix, and deletes nothing. Workspace reads the default pool of the organization it is given.

A World holds the testing.TB it was built with, so it belongs to the test or subtest that built it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account struct {
	ID    string
	OrgID string
	Name  string
}

Account is a billing account of an organization.

type Grant

type Grant struct {
	ID          string
	ProvisionID string
	// contains filtered or unexported fields
}

Grant is a conferred grant and the provision it created.

type Invoice

type Invoice struct {
	ID        string
	AccountID string
	Number    string
	AmountDue int32
}

Invoice is an invoice of a billing account.

type InvoiceOpts

type InvoiceOpts struct {
	Number      string
	Status      string
	AmountDue   int32
	AmountPaid  int32
	DueDate     time.Time
	PeriodStart time.Time
	PeriodEnd   time.Time
	IssuedAt    time.Time
	PaidAt      time.Time
}

InvoiceOpts shapes an invoice. Status is "open" and AmountDue 1000 cents when zero; IssuedAt is now when zero. Number is the invoice number as written; when empty, the account's counter assigns it, as the invoice reconcile does.

type IssueOpts

type IssueOpts struct {
	Marked     bool
	Note       string
	Reason     string
	By         Person
	ValidFrom  time.Time
	ValidUntil time.Time
}

IssueOpts shapes an issued grant: whether it is marked to resume after a plan supersedes it, its reason ("complimentary" when empty), the person who issues it (the organization's owner when zero), and its bounds (unbounded, and starting now, when zero).

type Ladder

type Ladder struct {
	ID   string
	Name string
}

Ladder is a plan ladder.

type Org

type Org struct {
	ID     string
	Name   string
	PoolID string
	Owner  Person
}

Org is an organization with its default resource pool.

type Payment

type Payment struct {
	ID        string
	InvoiceID string
}

Payment is a payment of an invoice.

type PaymentOpts

type PaymentOpts struct {
	Status string
	At     time.Time
}

PaymentOpts shapes a payment: Status "succeeded" when empty, paid at At (now when zero); a "failed" payment fails at At instead.

type Person

type Person struct {
	ID      string
	UserID  string
	Subject string
	Email   string
	Name    string
}

Person is an identity: a user and the person it signs in as.

func (Person) SignIn

func (p Person) SignIn() auth.SignIn

SignIn returns the identity a sign-in as p writes into the session.

type Price

type Price struct {
	ID        string
	ProductID string
}

Price is a price of a product.

type Product

type Product struct {
	ID    string
	SetID string
	Name  string
}

Product is a product and the entitlement set it delivers.

type SubOpts

type SubOpts struct {
	PeriodEnd     time.Time
	NullPeriodEnd bool
	CommitmentEnd time.Time
	Policy        string
}

SubOpts shapes a subscription: its period end (12:00 UTC, 29 days out, by default; NULL when NullPeriodEnd), and a commitment ending at CommitmentEnd under the early-termination Policy ("block", "allow" or "fee"), which the schema requires beside it.

type Subscription

type Subscription struct {
	ID               string
	BillingAccountID string
	// contains filtered or unexported fields
}

Subscription is a subscription on the organization's billing account.

type Workspace

type Workspace struct {
	ID     string
	OrgID  string
	PoolID string
	Name   string
}

Workspace is a workspace with its primary assignment to the organization's default pool.

type World

type World struct {
	TB  testing.TB
	Ctx context.Context
	DB  *sql.DB
	Sfx string // suffix of every name the World writes
}

World is one test's builder over a database.

func New

func New(tb testing.TB, d *sql.DB) *World

New returns a World over d, any database the console's migrations built.

func (*World) AddTier

func (w *World) AddTier(l Ladder, p Product)

AddTier appends p to l as its last tier. It inserts the tier row only: unlike the operator's add-tier form it does not align the product's live provisions to the new rung, so it is for a product no pool holds.

func (*World) BareSubscription

func (w *World) BareSubscription(a Account, start, end time.Time) Subscription

BareSubscription creates an active subscription row on a and nothing else: no item, no conferral, no pool provision. The reconcile writes those beside the row; a billing list reads only the row and its Stripe mapping. start and end are its current period, NULL when zero.

func (*World) BillingAccount

func (w *World) BillingAccount(o Org, name string) Account

BillingAccount creates an active billing account of o, as provisioning does when an organization first buys.

func (*World) CancelAtPeriodEnd

func (w *World) CancelAtPeriodEnd(sub Subscription)

CancelAtPeriodEnd sets the flag the subscription projection writes when Stripe reports a cancellation at the period end, keeping every other field.

func (*World) Confer

func (w *World) Confer(o Org, p Product) Grant

Confer records a manual grant of p to o, made by o's owner, and confers it on o's default pool. It does not materialize the pool, so the pool keeps the rows the test wrote beside it.

func (*World) ConferOn

func (w *World) ConferOn(o Org, poolID string, p Product) Grant

ConferOn is Confer into the pool poolID, which Pool made. The console confers only into an organization's default pool, so it writes a state the app does not produce.

func (*World) ConferUnprojected

func (w *World) ConferUnprojected(o Org, p Product) string

ConferUnprojected confers p on o's pool from a subscription id that names no row, as the reconcile does before it has projected the subscription (pool_provisions.subscription_id carries no foreign key), and returns the provision id.

func (*World) ConferUnprojectedSuspended

func (w *World) ConferUnprojectedSuspended(o Org, p Product) string

ConferUnprojectedSuspended confers p as ConferUnprojected does, then suspends the provision, and returns the provision id. The reconcile never writes this state, because it projects the subscription before it suspends; the state is the payment-due default a position whose subscription row is missing reads as. The subscription id names no row, so no status is written.

func (*World) DefaultLadder

func (w *World) DefaultLadder(orgType string, l Ladder)

DefaultLadder makes l the default plan of orgType, as the operator's org type page does.

func (*World) Draft

func (w *World) Draft(p Product)

Draft returns p to the draft lifecycle. The create form writes published and no act moves a product back, so the builder writes the column in one statement.

func (*World) EditProduct

func (w *World) EditProduct(p Product, edit func(*billing.UpdateProductParams))

EditProduct changes p through the operator's edit form: edit receives the product's current fields and the call writes what it leaves.

func (*World) EndSubscription

func (w *World) EndSubscription(sub Subscription)

EndSubscription ends sub as the subscription reconcile's terminal branch does when Stripe reports it canceled, in one materializing transaction: the row is canceled and ended now, every live provision it backs is ended, the grants it replaced and marked to resume deliver again when that ended something, the floor runs on a vacant pool, the pool is materialized, and its pending scheduled changes are closed.

func (*World) Extend

func (w *World) Extend(g Grant, until time.Time) Grant

Extend moves g's Valid until to until through the Manage act, as the operator's Manage form does when only the bound changes: a delivering grant continues its position as a transfer, a waiting one gets a waiting successor. The note and the mark stay as they are. It returns the extension.

func (*World) Floor

func (w *World) Floor(o Org)

Floor restores the organization type's default plan into o's pool when the pool holds no rung, as provisioning does.

func (*World) ForeignPause

func (w *World) ForeignPause(s Subscription)

ForeignPause records a collection pause on s without the console's marker, as a pause made in the Dashboard reads.

func (*World) Invoice

func (w *World) Invoice(a Account, o InvoiceOpts) Invoice

Invoice creates an invoice on a in US dollars through the query the invoice reconcile writes with, the number and the row in one transaction.

func (*World) Issue

func (w *World) Issue(o Org, p Product, opts IssueOpts) Grant

Issue issues a grant of p to o and confers it, as the operator's Issue grant act does, made by opts.By, or by o's owner when that is zero.

func (*World) Ladder

func (w *World) Ladder(name string, tiers ...Product) Ladder

Ladder creates an active plan ladder and appends tiers to it in argument order, so the first has rank 0, in one materializing transaction, as the operator's add-tier form does.

func (*World) Lifecycle

func (w *World) Lifecycle(p Product, status string)

Lifecycle sets p's lifecycle status to status ("draft", "published" or "retired"). The create form writes published and no act changes the column, so draft and retired are states the console never writes; the builder writes the column in one statement.

func (*World) LineItem

func (w *World) LineItem(inv Invoice, price Price, description string, cents int32, start, end time.Time)

LineItem adds a line of quantity 1 at price to inv, as the invoice reconcile writes each line it reads back.

func (*World) MapAccount

func (w *World) MapAccount(a Account)

MapAccount gives a a synced Stripe customer mapping with no recorded environment, which every key mode shows; see MapInvoice. The Stripe id is a's id behind "cus_".

func (*World) MapAccountIn

func (w *World) MapAccountIn(a Account, live bool)

MapAccountIn gives a a synced Stripe customer mapping recorded in the live environment when live is true and in the test environment otherwise. The Stripe id is a's id behind "cus_".

func (*World) MapInvoice

func (w *World) MapInvoice(inv Invoice)

MapInvoice gives inv a synced Stripe invoice mapping with no recorded environment, which every key mode shows; the console stamps every mapping it writes, so only a row from before the stamp has this state. The Stripe id is inv's id behind "in_".

func (*World) MapInvoiceIn

func (w *World) MapInvoiceIn(inv Invoice, live bool)

MapInvoiceIn gives inv a synced Stripe invoice mapping recorded in the live environment when live is true and in the test environment otherwise. The Stripe id is inv's id behind "in_".

func (*World) MapPayment

func (w *World) MapPayment(p Payment)

MapPayment gives p a synced Stripe payment mapping with no recorded environment; see MapInvoice. Its charge is p's id behind "ch_" and its payment intent p's id behind "pi_".

func (*World) MapPaymentIn

func (w *World) MapPaymentIn(p Payment, live bool)

MapPaymentIn gives p a synced Stripe payment mapping recorded in the live environment when live is true and in the test environment otherwise. Its charge is p's id behind "ch_" and its payment intent p's id behind "pi_".

func (*World) MapPrice

func (w *World) MapPrice(price Price)

MapPrice gives price a synced Stripe price mapping with no recorded environment, which every key mode reaches.

func (*World) MapPriceIn

func (w *World) MapPriceIn(price Price, live bool)

MapPriceIn gives price a synced Stripe price mapping recorded in the live environment when live is true and in the test environment otherwise.

func (*World) MapProduct

func (w *World) MapProduct(p Product)

MapProduct gives p a synced Stripe product mapping with no recorded environment, which every key mode reaches. The Stripe id is p's id behind "prod_".

func (*World) MapProductIn

func (w *World) MapProductIn(p Product, live bool)

MapProductIn gives p a synced Stripe product mapping recorded in the live environment when live is true and in the test environment otherwise.

func (*World) MapSubscription

func (w *World) MapSubscription(s Subscription)

MapSubscription gives s a synced Stripe subscription mapping with no recorded environment; see MapInvoice. The Stripe id is s's id behind "sub_".

func (*World) MapSubscriptionIn

func (w *World) MapSubscriptionIn(s Subscription, live bool)

MapSubscriptionIn gives s a synced Stripe subscription mapping recorded in the live environment when live is true and in the test environment otherwise. The Stripe id is s's id behind "sub_".

func (*World) Member

func (w *World) Member(o Org, p Person)

Member adds p to o as a member with the member role. The console adds only an organization's owner, with the owner role, at provisioning, so a member-role member is a state the app does not produce.

func (*World) OneTimePrice

func (w *World) OneTimePrice(p Product, cents int32) Price

OneTimePrice creates a one-time price of p in US dollars.

func (*World) Org

func (w *World) Org(name, orgType string, owner Person) Org

Org creates an organization of orgType owned by owner, with its default pool, in one materializing transaction. It does not give the organization a default plan, so a fixture does not inherit one another package committed.

func (*World) OrgType

func (w *World) OrgType(name string) string

OrgType creates an active organization type with no default plan and returns its key: name, a hyphen and the last twelve digits of the World's suffix. The column holds twenty characters, so name is at most seven; the tail keeps two Worlds from colliding on the key. It is the one raw INSERT among the builders, because no console act creates an organization type.

func (*World) PauseCollection

func (w *World) PauseCollection(s Subscription)

PauseCollection stands for the reconcile's record of the console's own collection pause on s: behavior void and the console's marker.

func (*World) Payment

func (w *World) Payment(inv Invoice, o PaymentOpts) Payment

Payment records a payment of inv's amount due, as the payment reconcile does.

func (*World) Person

func (w *World) Person(name string) Person

Person creates a user and its person.

func (*World) Pool

func (w *World) Pool(o Org, name, poolType string) string

Pool creates a pool of the given type in o and returns its id. The console writes only an organization's default pool, so a second pool is a state the app does not produce.

func (*World) Price

func (w *World) Price(p Product, cents int32) Price

Price creates a monthly, recurring price of p in US dollars, as the operator's create form does. The first price of a product is its default.

func (*World) Product

func (w *World) Product(name string) Product

Product creates a published, public product and its own active set, as the operator's create form does.

func (*World) Providers

func (w *World) Providers(srcs ...integration.ProviderSource)

Providers registers the providers as boot does and then ensures the System tenant, so the provider, resource-key and connection rows exist before a fixture names them.

func (*World) RecurringPrice

func (w *World) RecurringPrice(p Product, cents int32, currency, interval string) Price

RecurringPrice creates a recurring price of p in the given currency and interval ("month" or "year"); Price is RecurringPrice in usd, monthly.

func (*World) RemoveTier

func (w *World) RemoveTier(l Ladder, p Product)

RemoveTier removes p's tier from l and renumbers the tiers left from 0, as the operator's remove does for a tier no organization holds. A tier with holders goes through the removal act and its dispositions, which this does not write, so it fails the test.

func (*World) ReorderTiers

func (w *World) ReorderTiers(l Ladder, tiers ...Product)

ReorderTiers gives l's tiers the ranks of their order in tiers, from 0, as the operator's reorder renumbers a ladder no organization type defaults to: every rank is parked below zero, then set. The reorder of a default ladder also runs the dispositions its preview asks for, which this does not.

func (*World) Retire

func (w *World) Retire(p Product)

Retire takes p out of the member catalog by deactivating it, as the operator's edit form does. Its tiers and the rungs held on it stay.

func (*World) Revoke

func (w *World) Revoke(g Grant)

Revoke ends the grant through the revoke act, which re-materializes the pool as production does, so a boolean entitlement it delivered lapses.

func (*World) Rule

func (w *World) Rule(p Product, r entitlements.RuleFields)

Rule adds r to p's set through the rule-change function, the only write path to a set's rules.

func (*World) RuleChange

func (w *World) RuleChange(p Product, r entitlements.RuleFields, by Person, note string)

RuleChange adds r to p's set as the operator's rule-change form commits it: one rule-added delta through the batch commit, made by the person by with the note, which settles the pools carrying the set in the commit. Rule is the system's path and leaves the carrying pools unsettled.

func (*World) ScheduleCancellation

func (w *World) ScheduleCancellation(sub Subscription, at time.Time)

ScheduleCancellation records a cancellation of sub taking effect at at, at a commitment's boundary, as the cancel does under a block commitment.

func (*World) ScheduleDowngrade

func (w *World) ScheduleDowngrade(sub Subscription, price Price, at time.Time)

ScheduleDowngrade records a downgrade of sub to price taking effect at the period end at, as the switch does.

func (*World) SortOrder

func (w *World) SortOrder(l Ladder, n int32)

SortOrder places l among the ladders, as the operator's reorder does.

func (*World) Subscribe

func (w *World) Subscribe(o Org, opts SubOpts, products ...Product) Subscription

Subscribe creates a subscription on o's billing account, making the account when o has none, and confers each product from it in turn, as reconcile does for a purchase and then for each switch. The subscription's one item is the last product's default price.

func (*World) Suspend

func (w *World) Suspend(sub Subscription, status string)

Suspend writes the pair the subscription reconcile writes for a suspending status: the subscription's status, then the suspension of the provisions sub confers and the settlement of the pool. status is past_due, unpaid or paused, and the row keeps its period, cancellation, cancelled and ended fields.

func (*World) Use

func (w *World) Use(ws Workspace, key string, n int)

Use sets the usage of key against the pool of ws's primary assignment to n, as the key's owner writes it: an absolute figure, never an increment. It fails the test when the write changes no row, because the pool has no usage row for the key; only the materializer creates one.

func (*World) WithoutSet

func (w *World) WithoutSet(p Product)

WithoutSet clears p's entitlement set, as the operator's edit form does when it is saved with no set chosen on a product that is no plan tier. A tier's set cannot be cleared once migration 00035 is in place (chk_products_tier_keeps_set), so the kit clears it only on a product no ladder holds. A product in this state confers nothing: a grant or a subscription to it is refused or left out.

func (*World) Workspace

func (w *World) Workspace(o Org, name string) Workspace

Workspace creates a workspace in o and assigns it to the default pool, as the member's create-workspace handler does.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址