mirror of
https://github.com/3ybactuk/marketplace-go-service-project.git
synced 2025-10-30 14:03:45 +03:00
[hw-4] add postgres db
This commit is contained in:
@@ -5,20 +5,27 @@ package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/ozontech/allure-go/pkg/framework/provider"
|
||||
"github.com/ozontech/allure-go/pkg/framework/suite"
|
||||
"github.com/pressly/goose/v3"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
"route256/loms/internal/app/server"
|
||||
"route256/loms/internal/domain/entity"
|
||||
ordersRepository "route256/loms/internal/domain/repository/orders"
|
||||
stocksRepository "route256/loms/internal/domain/repository/stocks"
|
||||
ordersRepository "route256/loms/internal/domain/repository/orders/sqlc"
|
||||
stocksRepository "route256/loms/internal/domain/repository/stocks/sqlc"
|
||||
lomsService "route256/loms/internal/domain/service"
|
||||
"route256/loms/internal/infra/postgres"
|
||||
|
||||
pb "route256/pkg/api/loms/v1"
|
||||
)
|
||||
@@ -28,16 +35,77 @@ const (
|
||||
// "reserved": 35
|
||||
testSKU = entity.Sku(1076963)
|
||||
|
||||
testUID = entity.ID(1337)
|
||||
testCount = uint32(2)
|
||||
testUID = entity.ID(1337)
|
||||
testCount = uint32(2)
|
||||
migrationsDir = "../../db/migrations"
|
||||
)
|
||||
|
||||
func startPostgres(ctx context.Context, migrationsDir string) (*pgxpool.Pool, func(), error) {
|
||||
req := testcontainers.ContainerRequest{
|
||||
Image: "gitlab-registry.ozon.dev/go/classroom-18/students/base/postgres:16",
|
||||
Env: map[string]string{
|
||||
"POSTGRESQL_USERNAME": "user",
|
||||
"POSTGRESQL_PASSWORD": "postgres",
|
||||
"POSTGRESQL_DATABASE": "loms_test",
|
||||
"POSTGRESQL_PORT": "5437",
|
||||
},
|
||||
ExposedPorts: []string{"5437/tcp"},
|
||||
WaitingFor: wait.ForListeningPort("5437/tcp").WithStartupTimeout(30 * time.Second),
|
||||
}
|
||||
container, err := testcontainers.GenericContainer(ctx,
|
||||
testcontainers.GenericContainerRequest{ContainerRequest: req, Started: true})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
endpoint, err := container.Endpoint(ctx, "")
|
||||
if err != nil {
|
||||
container.Terminate(ctx)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
dsn := fmt.Sprintf("postgresql://user:postgres@%s/loms_test?sslmode=disable", endpoint)
|
||||
|
||||
var pool *pgxpool.Pool
|
||||
for i := 0; i < 30; i++ {
|
||||
pool, err = pgxpool.New(ctx, dsn)
|
||||
if err == nil && pool.Ping(ctx) == nil {
|
||||
break
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
if err != nil {
|
||||
container.Terminate(ctx)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
std, err := sql.Open("pgx", dsn)
|
||||
if err != nil {
|
||||
container.Terminate(ctx)
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := goose.Up(std, migrationsDir); err != nil {
|
||||
container.Terminate(ctx)
|
||||
return nil, nil, err
|
||||
}
|
||||
std.Close()
|
||||
|
||||
cleanup := func() {
|
||||
pool.Close()
|
||||
_ = container.Terminate(context.Background())
|
||||
}
|
||||
return pool, cleanup, nil
|
||||
}
|
||||
|
||||
type LomsIntegrationSuite struct {
|
||||
suite.Suite
|
||||
|
||||
grpcSrv *grpc.Server
|
||||
grpcConn *grpc.ClientConn
|
||||
grpcSrv *grpc.Server
|
||||
grpcConn *grpc.ClientConn
|
||||
|
||||
lomsClient pb.LOMSClient
|
||||
|
||||
cleanup func()
|
||||
}
|
||||
|
||||
func TestLomsIntegrationSuite(t *testing.T) {
|
||||
@@ -45,12 +113,17 @@ func TestLomsIntegrationSuite(t *testing.T) {
|
||||
}
|
||||
|
||||
func (s *LomsIntegrationSuite) BeforeAll(t provider.T) {
|
||||
ctx := context.Background()
|
||||
t.WithNewStep("init cart-service", func(sCtx provider.StepCtx) {
|
||||
orderRepo := ordersRepository.NewInMemoryRepository(100)
|
||||
stockRepo, err := stocksRepository.NewInMemoryRepository(100)
|
||||
sCtx.Require().NoError(err)
|
||||
pool, cleanup, err := startPostgres(ctx, migrationsDir)
|
||||
s.cleanup = cleanup
|
||||
|
||||
svc := lomsService.NewLomsService(orderRepo, stockRepo)
|
||||
orderRepo := ordersRepository.NewOrderRepository(pool)
|
||||
stockRepo := stocksRepository.NewStockRepository(pool, pool)
|
||||
|
||||
txManager := postgres.NewTxManager(pool, pool)
|
||||
|
||||
svc := lomsService.NewLomsService(orderRepo, stockRepo, txManager)
|
||||
lomsServer := server.NewServer(svc)
|
||||
|
||||
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
@@ -74,6 +147,7 @@ func (s *LomsIntegrationSuite) BeforeAll(t provider.T) {
|
||||
func (s *LomsIntegrationSuite) AfterAll(t provider.T) {
|
||||
s.grpcSrv.Stop()
|
||||
_ = s.grpcConn.Close()
|
||||
s.cleanup()
|
||||
}
|
||||
|
||||
func (s *LomsIntegrationSuite) TestOrderProcessPositive(t provider.T) {
|
||||
|
||||
Reference in New Issue
Block a user