summaryrefslogtreecommitdiff
path: root/internal/controlplane/limits_integration_test.go
blob: 8087bddbfecdb851f6417c3f9fbe006f081a45f5 (plain)
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
package controlplane

import (
	"context"
	"fmt"
	"os"
	"testing"
	"time"
)

func TestSetProjectLimitPostgres(t *testing.T) {
	databaseURL := os.Getenv("AIGW_TEST_DATABASE_URL")
	if databaseURL == "" {
		t.Skip("AIGW_TEST_DATABASE_URL is not set")
	}

	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	if err := MigrateDatabase(ctx, databaseURL); err != nil {
		t.Fatal(err)
	}
	store, err := NewStore(ctx, Options{
		DatabaseURL:   databaseURL,
		CredentialKey: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() {
		if err := store.Close(); err != nil {
			t.Errorf("close control-plane store: %v", err)
		}
	})

	suffix := time.Now().UnixNano()
	tenant, _, err := store.CreateTenant(ctx, CreateTenantInput{Slug: fmt.Sprintf("limits-%d", suffix), Name: "Limits tenant"})
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() {
		if _, cleanupErr := store.db.Exec(context.Background(), `DELETE FROM tenants WHERE id=$1`, tenant.ID); cleanupErr != nil {
			t.Errorf("cleanup limit integration data: %v", cleanupErr)
		}
	})
	project, _, err := store.CreateProject(ctx, CreateProjectInput{TenantID: tenant.ID, Slug: "limits-project", Name: "Limits project"})
	if err != nil {
		t.Fatal(err)
	}

	got, _, err := store.SetProjectLimit(ctx, project.ID, SetProjectLimitInput{
		RequestsPerMinute: 10, TokensPerMinute: 20, ConcurrentRequests: 3, MonthlySpendMicros: 40,
	})
	if err != nil {
		t.Fatal(err)
	}
	if got.TenantID != tenant.ID || got.ProjectID != project.ID || got.TenantName != tenant.Name || got.ProjectName != project.Name {
		t.Fatalf("unexpected limit identity: %+v", got)
	}
	if got.RequestsPerMinute != 10 || got.TokensPerMinute != 20 || got.Concurrent != 3 || got.MonthlySpendMicros != 40 {
		t.Fatalf("unexpected limit values: %+v", got)
	}
}