From af721ad9174a9d484f979e6bb873d68df96511c6 Mon Sep 17 00:00:00 2001 From: "popov.dmitriy" <dp@sessia.dev> Date: Fri, 6 Jun 2025 11:17:03 +0300 Subject: [PATCH] Add unit tests --- cps/customerCreated_test.go | 44 +++++++++++++++++++++++++++++++++++++ cps/customerUpdated_test.go | 44 +++++++++++++++++++++++++++++++++++++ topics_test.go | 35 +++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 cps/customerCreated_test.go create mode 100644 cps/customerUpdated_test.go create mode 100644 topics_test.go diff --git a/cps/customerCreated_test.go b/cps/customerCreated_test.go new file mode 100644 index 0000000..13a9cc6 --- /dev/null +++ b/cps/customerCreated_test.go @@ -0,0 +1,44 @@ +package cps + +import ( + "gitlab.sessia.com/sdk/events" + "testing" + + "github.com/google/uuid" + "github.com/stretchr/testify/assert" +) + +func TestCustomerCreated_Validate_Success(t *testing.T) { + customer := &CustomerCreated{ + ID: uuid.New(), + Name: "John", + Surname: "Doe", + Nickname: "johnd", + Image: "https://example.com/image.jpg", + Phone: "+1234567890", + Email: "john.doe@example.com", + Gender: "male", + Birthdate: "1990-01-01", + CityID: 123, + } + + err := customer.Validate() + assert.NoError(t, err) +} + +func TestCustomerCreated_Validate_Fail(t *testing.T) { + customer := &CustomerCreated{ + Name: "John", + } + err := customer.Validate() + assert.Error(t, err, "Validation should fail if ID is missing") + + customer = &CustomerCreated{} + err = customer.Validate() + assert.Error(t, err, "Validation should fail if ID is missing") +} + +func TestCustomerCreated_GetEventName(t *testing.T) { + customer := &CustomerCreated{} + assert.Equal(t, events.EventNameCustomerCreated, customer.GetEventName()) +} diff --git a/cps/customerUpdated_test.go b/cps/customerUpdated_test.go new file mode 100644 index 0000000..05c023b --- /dev/null +++ b/cps/customerUpdated_test.go @@ -0,0 +1,44 @@ +package cps + +import ( + "gitlab.sessia.com/sdk/events" + "testing" + + "github.com/google/uuid" + "github.com/stretchr/testify/assert" +) + +func TestCustomerUpdated_Validate_Success(t *testing.T) { + customer := &CustomerUpdated{ + ID: uuid.New(), + Name: "Alice", + Surname: "Smith", + Nickname: "alice123", + Image: "https://example.com/avatar.png", + Phone: "+19876543210", + Email: "alice.smith@example.com", + Gender: "female", + Birthdate: "1985-05-15", + CityID: 321, + } + + err := customer.Validate() + assert.NoError(t, err) +} + +func TestCustomerUpdated_Validate_Fail(t *testing.T) { + customer := &CustomerUpdated{ + Name: "Alice", + } + err := customer.Validate() + assert.Error(t, err, "Validation should fail if ID is missing") + + customer = &CustomerUpdated{} + err = customer.Validate() + assert.Error(t, err, "Validation should fail if ID is missing") +} + +func TestCustomerUpdated_GetEventName(t *testing.T) { + customer := &CustomerUpdated{} + assert.Equal(t, events.EventNameCustomerUpdated, customer.GetEventName()) +} diff --git a/topics_test.go b/topics_test.go new file mode 100644 index 0000000..9b4455a --- /dev/null +++ b/topics_test.go @@ -0,0 +1,35 @@ +package events + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetTopicByEventName(t *testing.T) { + tests := []struct { + eventName string + expected string + }{ + {EventNameCustomerCreated, "cps.customer.created"}, + {EventNameWalletDebited, "wms.wallet.debited"}, + {EventNameBrandCreated, "bsm.brand.created"}, + {EventNameStoreUpdated, "bsm.store.updated"}, + {EventNameCategoryDeleted, "pim.category.deleted"}, + {EventNameProductRemainUpdated, "pim.remain.updated"}, + {EventNameGiftDeleted, "gim.gift.deleted"}, + {EventNameOrderCompleted, "eom.order.completed"}, + {EventNameCouponCreated, "lms.coupon.created"}, + {EventNameDecisionApproved, "lms.decision.approved"}, + {EventNamePostViewed, "cms.post.viewed"}, + {EventNameUserFriendRequestDeleted, "fms.user_friend_request.deleted"}, + {"non_existing_event", ""}, + } + + for _, tt := range tests { + t.Run(tt.eventName, func(t *testing.T) { + topic := GetTopicByEventName(tt.eventName) + assert.Equal(t, tt.expected, topic) + }) + } +} -- GitLab