Skip to content

Commit

Permalink
refactor: simplify radom event processing checks
Browse files Browse the repository at this point in the history
  • Loading branch information
clD11 committed Aug 19, 2024
1 parent 23b2895 commit bdfe9e3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
17 changes: 7 additions & 10 deletions services/skus/radom/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

const (
ErrUnsupportedEvent = Error("radom: unsupported event")
ErrNoCheckoutSessionData = Error("radom: no checkout session data")
ErrBraveOrderIDNotFound = Error("radom: brave order id not found")
ErrSubscriptionIDNotFound = Error("radom: subscription id not found")
ErrUnsupportedEvent = Error("radom: unsupported event")
ErrNoCheckoutSessionData = Error("radom: no checkout session data")
ErrBraveOrderIDNotFound = Error("radom: brave order id not found")
ErrNoRadomPaymentData = Error("radom: no radom payment data")

ErrDisabled = Error("radom: disabled")
ErrVerificationKeyEmpty = Error("radom: verification key is empty")
Expand Down Expand Up @@ -94,7 +94,7 @@ func (e *Event) SubID() (uuid.UUID, error) {

case e.EventData.Payment != nil:
if e.EventData.Payment.RadomData == nil || e.EventData.Payment.RadomData.Subscription == nil {
return uuid.Nil, ErrSubscriptionIDNotFound
return uuid.Nil, ErrNoRadomPaymentData
}

return e.EventData.Payment.RadomData.Subscription.SubscriptionID, nil
Expand All @@ -111,18 +111,15 @@ func (e *Event) SubID() (uuid.UUID, error) {
}

func (e *Event) IsNewSub() bool {
return e != nil && e.EventData != nil && e.EventData.New != nil
return e.EventData != nil && e.EventData.New != nil
}

func (e *Event) ShouldRenew() bool {
return e != nil && e.EventData != nil && e.EventData.Payment != nil
return e.EventData != nil && e.EventData.Payment != nil
}

func (e *Event) ShouldCancel() bool {
switch {
case e == nil:
return false

case e.EventData == nil:
return false

Expand Down
63 changes: 35 additions & 28 deletions services/skus/radom/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func TestEvent_SubID(t *testing.T) {
exp: tcExpected{
sid: uuid.Nil,
mustErr: func(t must.TestingT, err error, i ...interface{}) {
must.ErrorIs(t, err, ErrSubscriptionIDNotFound)
must.ErrorIs(t, err, ErrNoRadomPaymentData)
},
},
},
Expand All @@ -465,7 +465,7 @@ func TestEvent_SubID(t *testing.T) {
exp: tcExpected{
sid: uuid.Nil,
mustErr: func(t must.TestingT, err error, i ...interface{}) {
must.ErrorIs(t, err, ErrSubscriptionIDNotFound)
must.ErrorIs(t, err, ErrNoRadomPaymentData)
},
},
},
Expand Down Expand Up @@ -573,7 +573,7 @@ func TestEvent_SubID(t *testing.T) {

func TestEvent_IsNewSub(t *testing.T) {
type tcGiven struct {
event Event
event *Event
}

type testCase struct {
Expand All @@ -586,7 +586,7 @@ func TestEvent_IsNewSub(t *testing.T) {
{
name: "new_subscription",
given: tcGiven{
event: Event{
event: &Event{
EventData: &EventData{
New: &NewSubscription{},
},
Expand All @@ -598,7 +598,7 @@ func TestEvent_IsNewSub(t *testing.T) {
{
name: "not_new_subscription",
given: tcGiven{
event: Event{
event: &Event{
EventData: &EventData{},
},
},
Expand All @@ -607,7 +607,10 @@ func TestEvent_IsNewSub(t *testing.T) {

{
name: "not_new_subscription_event_data",
exp: false,
given: tcGiven{
event: &Event{},
},
exp: false,
},
}

Expand All @@ -623,7 +626,7 @@ func TestEvent_IsNewSub(t *testing.T) {

func TestEvent_ShouldRenew(t *testing.T) {
type tcGiven struct {
event Event
event *Event
}

type testCase struct {
Expand All @@ -636,7 +639,7 @@ func TestEvent_ShouldRenew(t *testing.T) {
{
name: "subscription_payment",
given: tcGiven{
event: Event{
event: &Event{
EventData: &EventData{
Payment: &SubscriptionPayment{},
},
Expand All @@ -648,16 +651,19 @@ func TestEvent_ShouldRenew(t *testing.T) {
{
name: "not_subscription_payment",
given: tcGiven{
event: Event{
event: &Event{
EventData: &EventData{},
},
},
exp: false,
},

{
name: "not_subscription_payment_event_data",
exp: false,
name: "no_event_data",
given: tcGiven{
event: &Event{},
},
exp: false,
},
}

Expand All @@ -683,11 +689,6 @@ func TestEvent_ShouldCancel(t *testing.T) {
}

tests := []testCase{
{
name: "event_nil",
exp: false,
},

{
name: "event_data_nil",
given: tcGiven{
Expand Down Expand Up @@ -743,7 +744,7 @@ func TestEvent_ShouldCancel(t *testing.T) {

func TestEvent_ShouldProcess(t *testing.T) {
type tcGiven struct {
event Event
event *Event
}

type testCase struct {
Expand All @@ -756,7 +757,7 @@ func TestEvent_ShouldProcess(t *testing.T) {
{
name: "new_subscription",
given: tcGiven{
event: Event{
event: &Event{
EventData: &EventData{
New: &NewSubscription{},
},
Expand All @@ -768,7 +769,7 @@ func TestEvent_ShouldProcess(t *testing.T) {
{
name: "subscription_payment",
given: tcGiven{
event: Event{
event: &Event{
EventData: &EventData{
Payment: &SubscriptionPayment{},
},
Expand All @@ -780,7 +781,7 @@ func TestEvent_ShouldProcess(t *testing.T) {
{
name: "subscription_cancelled",
given: tcGiven{
event: Event{
event: &Event{
EventData: &EventData{
Cancelled: &SubscriptionCancelled{},
},
Expand All @@ -792,7 +793,7 @@ func TestEvent_ShouldProcess(t *testing.T) {
{
name: "subscription_expired",
given: tcGiven{
event: Event{
event: &Event{
EventData: &EventData{
Expired: &SubscriptionExpired{},
},
Expand All @@ -803,7 +804,10 @@ func TestEvent_ShouldProcess(t *testing.T) {

{
name: "not_should_process",
exp: false,
given: tcGiven{
event: &Event{},
},
exp: false,
},
}

Expand All @@ -819,7 +823,7 @@ func TestEvent_ShouldProcess(t *testing.T) {

func TestEvent_Effect(t *testing.T) {
type tcGiven struct {
event Event
event *Event
}

type testCase struct {
Expand All @@ -832,7 +836,7 @@ func TestEvent_Effect(t *testing.T) {
{
name: "new",
given: tcGiven{
event: Event{
event: &Event{
EventData: &EventData{
New: &NewSubscription{},
},
Expand All @@ -844,7 +848,7 @@ func TestEvent_Effect(t *testing.T) {
{
name: "renew",
given: tcGiven{
event: Event{
event: &Event{
EventData: &EventData{
Payment: &SubscriptionPayment{},
},
Expand All @@ -856,7 +860,7 @@ func TestEvent_Effect(t *testing.T) {
{
name: "cancel",
given: tcGiven{
event: Event{
event: &Event{
EventData: &EventData{
Cancelled: &SubscriptionCancelled{},
},
Expand All @@ -868,7 +872,7 @@ func TestEvent_Effect(t *testing.T) {
{
name: "expired",
given: tcGiven{
event: Event{
event: &Event{
EventData: &EventData{
Expired: &SubscriptionExpired{},
},
Expand All @@ -879,7 +883,10 @@ func TestEvent_Effect(t *testing.T) {

{
name: "skip",
exp: "skip",
given: tcGiven{
event: &Event{},
},
exp: "skip",
},
}

Expand Down
8 changes: 7 additions & 1 deletion services/skus/service_nonint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4673,7 +4673,10 @@ func TestService_processRadomEvent(t *testing.T) {

tests := []testCase{
{
name: "event_nil",
name: "should_not_process",
given: tcGiven{
event: &radom.Event{},
},
},
}

Expand Down Expand Up @@ -4827,6 +4830,9 @@ func TestService_processRadomEventTx(t *testing.T) {

{
name: "unknown_action",
given: tcGiven{
event: &radom.Event{},
},
exp: tcExpected{
shouldErr: func(t should.TestingT, err error, i ...interface{}) bool {
return should.ErrorIs(t, err, errUnknownAction)
Expand Down

0 comments on commit bdfe9e3

Please sign in to comment.