-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs_test.go
More file actions
148 lines (128 loc) · 3.36 KB
/
docs_test.go
File metadata and controls
148 lines (128 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package ledger_test
import (
"context"
"log"
"testing"
"time"
xlog "github.com/xraph/go-utils/log"
"github.com/xraph/ledger"
"github.com/xraph/ledger/plan"
"github.com/xraph/ledger/store/memory"
"github.com/xraph/ledger/subscription"
"github.com/xraph/ledger/types"
)
// TestDocumentationExamples verifies that all examples in the documentation compile
func TestDocumentationExamples(t *testing.T) {
// Test Quick Start example from README
t.Run("QuickStartExample", func(t *testing.T) {
// Create store (memory for demo, use PostgreSQL in production)
store := memory.New()
// Initialize Ledger with plugins
l := ledger.New(store,
ledger.WithLogger(xlog.NewNoopLogger()),
ledger.WithMeterConfig(100, 5*time.Second),
ledger.WithEntitlementCacheTTL(30*time.Second),
)
// Start the engine
ctx := context.Background()
if err := l.Start(ctx); err != nil {
t.Fatal(err)
}
defer l.Stop()
// Create a plan
p := &plan.Plan{
Name: "Pro Plan",
Slug: "pro",
Currency: "usd",
Status: plan.StatusActive,
Features: []plan.Feature{
{
Key: "api_calls",
Name: "API Calls",
Type: plan.FeatureMetered,
Limit: 10000,
Period: plan.PeriodMonthly,
SoftLimit: true, // Allow overage
},
{
Key: "seats",
Name: "Team Seats",
Type: plan.FeatureSeat,
Limit: 5,
},
},
Pricing: &plan.Pricing{
BaseAmount: types.USD(4900), // $49.00
BillingPeriod: plan.PeriodMonthly,
Tiers: []plan.PriceTier{
{
FeatureKey: "api_calls",
Type: plan.TierGraduated,
UpTo: 10000,
UnitAmount: types.Zero("usd"), // Included
},
{
FeatureKey: "api_calls",
Type: plan.TierGraduated,
UpTo: -1, // Unlimited
UnitAmount: types.USD(1), // $0.01 per call
},
},
},
}
if err := l.CreatePlan(ctx, p); err != nil {
t.Fatal(err)
}
// Create a subscription
sub := &subscription.Subscription{
TenantID: "tenant_123",
PlanID: p.ID,
Status: subscription.StatusActive,
AppID: "app_456",
}
if err := l.CreateSubscription(ctx, sub); err != nil {
t.Fatal(err)
}
// Set context for tenant/app isolation
ctx = context.WithValue(ctx, "tenant_id", "tenant_123")
ctx = context.WithValue(ctx, "app_id", "app_456")
// Check entitlement (< 1ms with cache)
result, err := l.Entitled(ctx, "api_calls")
if err != nil {
t.Fatal(err)
}
if result.Allowed {
log.Printf("API calls allowed. Remaining: %d\n", result.Remaining)
// Meter usage (non-blocking, batched)
l.Meter(ctx, "api_calls", 100)
} else {
log.Printf("API calls denied: %s\n", result.Reason)
}
// Generate invoice
invoice, err := l.GenerateInvoice(ctx, sub.ID)
if err != nil {
t.Fatal(err)
}
log.Printf("Invoice generated: %s\n", invoice.Total.String())
})
// Test Money type examples
t.Run("MoneyExamples", func(t *testing.T) {
// Constructors
_ = types.USD(4900) // $49.00
_ = types.EUR(9900) // €99.00
_ = types.Zero("usd") // $0.00
// Arithmetic
m1 := types.USD(100)
m2 := types.USD(200)
_ = m1.Add(m2) // $3.00
_ = m1.Multiply(3) // $3.00
_ = m1.Divide(2) // $0.50
// Comparison
if m1.LessThan(m2) {
// m1 is less than m2
}
// Formatting
_ = m1.String() // "$1.00"
_ = m1.FormatMajor() // "1.00"
})
}