[hw-4] add postgres db

This commit is contained in:
Никита Шубин
2025-06-26 12:08:46 +00:00
parent 3ebaad5558
commit 77ed9fcf85
46 changed files with 1582 additions and 369 deletions

View File

@@ -21,6 +21,24 @@ const (
testSku = entity.Sku(199)
)
type mockTxManager struct{}
func (t *mockTxManager) WriteWithTransaction(ctx context.Context, fn func(ctx context.Context) error) (err error) {
return fn(ctx)
}
func (t *mockTxManager) ReadWithTransaction(ctx context.Context, fn func(ctx context.Context) error) (err error) {
return fn(ctx)
}
func (t *mockTxManager) WriteWithRepeatableRead(ctx context.Context, fn func(ctx context.Context) error) (err error) {
return fn(ctx)
}
func (t *mockTxManager) ReadWithRepeatableRead(ctx context.Context, fn func(ctx context.Context) error) (err error) {
return fn(ctx)
}
func TestLomsService_OrderCreate(t *testing.T) {
t.Parallel()
@@ -130,8 +148,7 @@ func TestLomsService_OrderCreate(t *testing.T) {
OrderCreateMock.Return(1, nil).
OrderSetStatusMock.Return(errors.New("unexpected error")),
stocks: mock.NewStockRepositoryMock(mc).
StockReserveMock.Return(nil).
StockCancelMock.Return(nil),
StockReserveMock.Return(nil),
},
args: args{
req: goodReq,
@@ -145,7 +162,7 @@ func TestLomsService_OrderCreate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
svc := NewLomsService(tt.fields.orders, tt.fields.stocks)
svc := NewLomsService(tt.fields.orders, tt.fields.stocks, &mockTxManager{})
_, err := svc.OrderCreate(ctx, tt.args.req)
tt.wantErr(t, err)
})
@@ -256,24 +273,46 @@ func TestLomsService_OrderPay(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
svc := NewLomsService(tt.fields.orders, tt.fields.stocks)
svc := NewLomsService(tt.fields.orders, tt.fields.stocks, &mockTxManager{})
err := svc.OrderPay(ctx, tt.args.id)
tt.wantErr(t, err)
})
}
}
func TestLomsService_OrderInfo(t *testing.T) {
func TestLomsService_OrderInfoBadInput(t *testing.T) {
t.Parallel()
svc := NewLomsService(
nil,
nil,
&mockTxManager{},
)
_, err := svc.OrderInfo(context.Background(), 0)
require.ErrorIs(t, err, model.ErrInvalidInput)
}
func TestLomsService_OrderInfoSuccess(t *testing.T) {
t.Parallel()
order := &entity.Order{
OrderID: 123,
Status: "payed",
UserID: 1337,
Items: []entity.OrderItem{},
}
mc := minimock.NewController(t)
svc := NewLomsService(
mock.NewOrderRepositoryMock(mc),
mock.NewStockRepositoryMock(mc),
mock.NewOrderRepositoryMock(mc).OrderGetByIDMock.Return(order, nil),
nil,
&mockTxManager{},
)
err := svc.OrderPay(context.Background(), 0)
require.ErrorIs(t, err, model.ErrInvalidInput)
gotOrder, err := svc.OrderInfo(context.Background(), 123)
require.NoError(t, err)
require.Equal(t, order, gotOrder)
}
func TestLomsService_OrderCancel(t *testing.T) {
@@ -370,7 +409,7 @@ func TestLomsService_OrderCancel(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
svc := NewLomsService(tt.fields.orders, tt.fields.stocks)
svc := NewLomsService(tt.fields.orders, tt.fields.stocks, &mockTxManager{})
err := svc.OrderCancel(ctx, tt.args.id)
tt.wantErr(t, err)
})
@@ -437,7 +476,7 @@ func TestLomsService_StocksInfo(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
svc := NewLomsService(nil, tt.fields.stocks)
svc := NewLomsService(nil, tt.fields.stocks, &mockTxManager{})
got, err := svc.StocksInfo(ctx, tt.args.sku)
tt.wantErr(t, err)
if err == nil {