From 4ca8aec7d685a0438df61395d8078b1bc43b2c67 Mon Sep 17 00:00:00 2001 From: Kenneth Date: Thu, 1 Jan 2026 18:59:57 +0000 Subject: [PATCH] test(backend): organization service tests --- .../organization/service_integration_test.go | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 apps/backend/internal/organization/service_integration_test.go diff --git a/apps/backend/internal/organization/service_integration_test.go b/apps/backend/internal/organization/service_integration_test.go new file mode 100644 index 0000000..f6f11fe --- /dev/null +++ b/apps/backend/internal/organization/service_integration_test.go @@ -0,0 +1,82 @@ +//go:build integration + +package organization + +import ( + "context" + "fmt" + "testing" + "time" + + "github.com/get-drexa/drexa/internal/database" + "github.com/testcontainers/testcontainers-go/modules/postgres" +) + +func TestService_CreatePersonalOrganization(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + + pg, err := runPostgres(ctx) + if err != nil { + t.Skipf("postgres testcontainer unavailable (docker not running/configured?): %v", err) + } + t.Cleanup(func() { _ = pg.Terminate(ctx) }) + + postgresURL, err := pg.ConnectionString(ctx, "sslmode=disable") + if err != nil { + t.Fatalf("postgres connection string: %v", err) + } + + db := database.NewFromPostgres(postgresURL) + t.Cleanup(func() { _ = db.Close() }) + + if err := database.RunMigrations(ctx, db); err != nil { + t.Fatalf("RunMigrations: %v", err) + } + + svc := NewService() + + org, err := svc.CreatePersonalOrganization(ctx, db, "Personal Org") + if err != nil { + t.Fatalf("CreatePersonalOrganization: %v", err) + } + if org.Kind != KindPersonal { + t.Fatalf("unexpected org kind: got %q want %q", org.Kind, KindPersonal) + } + if org.Name != "Personal Org" { + t.Fatalf("unexpected org name: got %q want %q", org.Name, "Personal Org") + } + + t.Run("organization by id", func(t *testing.T) { + got, err := svc.OrganizationByID(ctx, db, org.ID) + if err != nil { + t.Fatalf("OrganizationByID: %v", err) + } + if got.ID != org.ID { + t.Fatalf("unexpected org id: got %q want %q", got.ID, org.ID) + } + if got.Kind != KindPersonal { + t.Fatalf("unexpected org kind: got %q want %q", got.Kind, KindPersonal) + } + if got.Name != "Personal Org" { + t.Fatalf("unexpected org name: got %q want %q", got.Name, "Personal Org") + } + }) +} + +func runPostgres(ctx context.Context) (_ *postgres.PostgresContainer, err error) { + defer func() { + if r := recover(); r != nil { + err = fmt.Errorf("testcontainers panic: %v", r) + } + }() + + return postgres.Run( + ctx, + "postgres:16-alpine", + postgres.WithDatabase("drexa"), + postgres.WithUsername("drexa"), + postgres.WithPassword("drexa"), + postgres.BasicWaitStrategies(), + ) +}