78 lines
2.4 KiB
Go
78 lines
2.4 KiB
Go
package processor
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
"github.com/stretchr/testify/require"
|
||
|
|
|
||
|
|
"apskel-pos-be/internal/models"
|
||
|
|
"apskel-pos-be/internal/repository"
|
||
|
|
)
|
||
|
|
|
||
|
|
type customerOutletRepoFake struct {
|
||
|
|
orgs map[uuid.UUID]uuid.UUID
|
||
|
|
outlets map[uuid.UUID][]repository.CustomerOutlet
|
||
|
|
}
|
||
|
|
|
||
|
|
func (f customerOutletRepoFake) CustomerOrganizationID(_ context.Context, id uuid.UUID) (uuid.UUID, error) {
|
||
|
|
org, ok := f.orgs[id]
|
||
|
|
if !ok {
|
||
|
|
return uuid.Nil, repository.ErrWalletNotFound
|
||
|
|
}
|
||
|
|
return org, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (f customerOutletRepoFake) ListActiveOutlets(_ context.Context, org uuid.UUID) ([]repository.CustomerOutlet, error) {
|
||
|
|
return f.outlets[org], nil
|
||
|
|
}
|
||
|
|
|
||
|
|
type outletSettingsFake map[uuid.UUID]models.OutletLoyaltySettings
|
||
|
|
|
||
|
|
func (f outletSettingsFake) Outlet(_ context.Context, id uuid.UUID) (*models.OutletLoyaltySettings, error) {
|
||
|
|
s := f[id]
|
||
|
|
return &s, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestCustomerOutlets_ListsTheCustomersOrganizationWithLoyaltyFlags(t *testing.T) {
|
||
|
|
customer, org, other := uuid.New(), uuid.New(), uuid.New()
|
||
|
|
kemang, blokm := uuid.New(), uuid.New()
|
||
|
|
addr := "Jl. Kemang Raya 10"
|
||
|
|
repo := customerOutletRepoFake{
|
||
|
|
orgs: map[uuid.UUID]uuid.UUID{customer: org},
|
||
|
|
outlets: map[uuid.UUID][]repository.CustomerOutlet{
|
||
|
|
org: {{ID: blokm, Name: "Blok M"}, {ID: kemang, Name: "Kemang", Address: &addr}},
|
||
|
|
other: {{ID: uuid.New(), Name: "Not mine"}},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
settings := outletSettingsFake{
|
||
|
|
kemang: {
|
||
|
|
Point: models.LoyaltyEarnSettings{Enabled: true},
|
||
|
|
PointPayment: models.LoyaltyPointPaymentSettings{AcceptPayment: true},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
got, err := NewCustomerOutletProcessor(repo, settings).List(context.Background(), customer)
|
||
|
|
require.NoError(t, err)
|
||
|
|
assert.Equal(t, []models.CustomerOutlet{
|
||
|
|
{ID: blokm, Name: "Blok M"},
|
||
|
|
{ID: kemang, Name: "Kemang", Address: &addr, AcceptsPointPayment: true, EarnsPoints: true},
|
||
|
|
}, got)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestCustomerOutlets_UnknownCustomer(t *testing.T) {
|
||
|
|
_, err := NewCustomerOutletProcessor(customerOutletRepoFake{}, outletSettingsFake{}).List(context.Background(), uuid.New())
|
||
|
|
assert.ErrorIs(t, err, repository.ErrWalletNotFound)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestCustomerOutlets_NoOutletsIsAnEmptyList(t *testing.T) {
|
||
|
|
customer := uuid.New()
|
||
|
|
repo := customerOutletRepoFake{orgs: map[uuid.UUID]uuid.UUID{customer: uuid.New()}}
|
||
|
|
got, err := NewCustomerOutletProcessor(repo, outletSettingsFake{}).List(context.Background(), customer)
|
||
|
|
require.NoError(t, err)
|
||
|
|
assert.NotNil(t, got)
|
||
|
|
assert.Empty(t, got)
|
||
|
|
}
|