tsk-24: Fixed tests (#25)
Closes #24 Reviewed-on: phoenix/textsender-api#25 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-api/internal/db"
|
||||
"git.kundeng.us/phoenix/textsender-api/internal/handler/endpoint"
|
||||
"git.kundeng.us/phoenix/textsender-api/internal/store/mock"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
@@ -23,7 +24,7 @@ type Request struct {
|
||||
}
|
||||
|
||||
func TestCreateContactWithMock(t *testing.T) {
|
||||
mockstore := NewMockContactStore()
|
||||
mockstore := mock.NewMockContactStore()
|
||||
handler := NewContactHandler(mockstore)
|
||||
|
||||
testUserId := uuid.New()
|
||||
@@ -47,7 +48,7 @@ func TestCreateContactWithMock(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetContactWithMock(t *testing.T) {
|
||||
mockstore := NewMockContactStore()
|
||||
mockstore := mock.NewMockContactStore()
|
||||
testUserId := uuid.New()
|
||||
|
||||
testCon := contact.Contact{PhoneNumber: "+12335403383", UserId: testUserId}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-api/internal/handler/endpoint"
|
||||
"git.kundeng.us/phoenix/textsender-api/internal/store/mock"
|
||||
)
|
||||
|
||||
type CreateMessageRequest struct {
|
||||
@@ -21,7 +22,7 @@ type CreateMessageRequest struct {
|
||||
}
|
||||
|
||||
func TestCreateMessageWithMock(t *testing.T) {
|
||||
mockStore := NewMockMessageStore()
|
||||
mockStore := mock.NewMockMessageStore()
|
||||
handler := NewMessageHandler(mockStore)
|
||||
|
||||
testUserId := uuid.New()
|
||||
@@ -45,7 +46,7 @@ func TestCreateMessageWithMock(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetMessageWithMock(t *testing.T) {
|
||||
mockstore := NewMockMessageStore()
|
||||
mockstore := mock.NewMockMessageStore()
|
||||
testUserId := uuid.New()
|
||||
|
||||
testCon := message.Message{Content: "Who is the one that benefits?", UserId: testUserId}
|
||||
|
||||
@@ -1,300 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-models/pkg/contact"
|
||||
"git.kundeng.us/phoenix/textsender-models/pkg/message"
|
||||
"git.kundeng.us/phoenix/textsender-models/pkg/message/scheduling"
|
||||
)
|
||||
|
||||
type Key struct {
|
||||
PhoneNumber string
|
||||
UserId uuid.UUID
|
||||
}
|
||||
|
||||
type MockContactStore struct {
|
||||
Contacts map[uuid.UUID]*contact.Contact
|
||||
ContactsByKey map[Key]*contact.Contact
|
||||
mu sync.RWMutex
|
||||
Error error // Optional: simulate errors
|
||||
}
|
||||
|
||||
func NewMockContactStore() *MockContactStore {
|
||||
return &MockContactStore{
|
||||
Contacts: make(map[uuid.UUID]*contact.Contact),
|
||||
ContactsByKey: make(map[Key]*contact.Contact),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockContactStore) CreateContact(ctx context.Context, con *contact.Contact) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return m.Error
|
||||
}
|
||||
|
||||
if con.Id == uuid.Nil {
|
||||
con.Id = uuid.New()
|
||||
}
|
||||
|
||||
key := Key{PhoneNumber: con.PhoneNumber, UserId: con.UserId}
|
||||
if _, exists := m.ContactsByKey[key]; exists {
|
||||
return errors.New("Contact with phone number already exists")
|
||||
}
|
||||
|
||||
m.Contacts[con.Id] = con
|
||||
m.ContactsByKey[key] = con
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockContactStore) GetContactByID(ctx context.Context, id uuid.UUID) (*contact.Contact, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
con, exists := m.Contacts[id]
|
||||
if !exists {
|
||||
return nil, errors.New("Contact not found")
|
||||
}
|
||||
|
||||
return con, nil
|
||||
}
|
||||
|
||||
func (m *MockContactStore) GetContactByPhone(ctx context.Context, phoneNumber string, userId uuid.UUID) (*contact.Contact, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
con, exists := m.ContactsByKey[Key{PhoneNumber: phoneNumber, UserId: userId}]
|
||||
if !exists {
|
||||
return nil, errors.New("Contact not found")
|
||||
}
|
||||
|
||||
return con, nil
|
||||
}
|
||||
|
||||
func (m *MockContactStore) GetAllContacts(ctx context.Context) ([]*contact.Contact, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
cons := make([]*contact.Contact, 0, len(m.Contacts))
|
||||
for _, con := range m.Contacts {
|
||||
cons = append(cons, con)
|
||||
}
|
||||
|
||||
return cons, nil
|
||||
}
|
||||
|
||||
func (m *MockContactStore) ContactExists(ctx context.Context, phoneNumber string, userId uuid.UUID) (bool, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return false, m.Error
|
||||
}
|
||||
|
||||
_, exists := m.ContactsByKey[Key{PhoneNumber: phoneNumber, UserId: userId}]
|
||||
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
type MessageKey struct {
|
||||
Content string
|
||||
UserId uuid.UUID
|
||||
}
|
||||
|
||||
type MockMessageStore struct {
|
||||
Messages map[uuid.UUID]*message.Message
|
||||
MessagesByKey map[MessageKey]*message.Message
|
||||
mu sync.RWMutex
|
||||
Error error // Optional: simulate errors
|
||||
}
|
||||
|
||||
func NewMockMessageStore() *MockMessageStore {
|
||||
return &MockMessageStore{
|
||||
Messages: make(map[uuid.UUID]*message.Message),
|
||||
MessagesByKey: make(map[MessageKey]*message.Message),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockMessageStore) CreateMessage(ctx context.Context, msg *message.Message) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return m.Error
|
||||
}
|
||||
|
||||
if msg.Id == uuid.Nil {
|
||||
msg.Id = uuid.New()
|
||||
}
|
||||
|
||||
key := MessageKey{Content: msg.Content, UserId: msg.UserId}
|
||||
if _, exists := m.MessagesByKey[key]; exists {
|
||||
return errors.New("Message already exists")
|
||||
}
|
||||
|
||||
m.Messages[msg.Id] = msg
|
||||
m.MessagesByKey[key] = msg
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockMessageStore) GetAllMessages(ctx context.Context) ([]*message.Message, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
var msgs []*message.Message
|
||||
|
||||
for _, msg := range m.Messages {
|
||||
msgs = append(msgs, msg)
|
||||
}
|
||||
|
||||
return msgs, nil
|
||||
}
|
||||
|
||||
func (m *MockMessageStore) GetMessageByID(ctx context.Context, id uuid.UUID) (*message.Message, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
msg := m.Messages[id]
|
||||
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func (m *MockMessageStore) MessageExists(ctx context.Context, msg *message.Message) (bool, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return false, m.Error
|
||||
}
|
||||
|
||||
_, exists := m.MessagesByKey[MessageKey{Content: msg.Content, UserId: msg.UserId}]
|
||||
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
type ScheduledMessageKey struct {
|
||||
UserId uuid.UUID
|
||||
}
|
||||
|
||||
type MockScheduledMessageStore struct {
|
||||
ScheduledMessages map[uuid.UUID]*scheduling.ScheduledMessage
|
||||
ScheduledMessagesByKey map[ScheduledMessageKey]*scheduling.ScheduledMessage
|
||||
mu sync.RWMutex
|
||||
Error error // Optional: simulate errors
|
||||
}
|
||||
|
||||
func NewMockScheduledMessageStore() *MockScheduledMessageStore {
|
||||
return &MockScheduledMessageStore{
|
||||
ScheduledMessages: make(map[uuid.UUID]*scheduling.ScheduledMessage),
|
||||
ScheduledMessagesByKey: make(map[ScheduledMessageKey]*scheduling.ScheduledMessage),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockScheduledMessageStore) CreateScheduledMessage(ctx context.Context, schedMsg *scheduling.ScheduledMessage) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return m.Error
|
||||
}
|
||||
|
||||
if schedMsg.Id == uuid.Nil {
|
||||
schedMsg.Id = uuid.New()
|
||||
}
|
||||
|
||||
key := ScheduledMessageKey{UserId: schedMsg.UserId}
|
||||
|
||||
m.ScheduledMessages[schedMsg.Id] = schedMsg
|
||||
m.ScheduledMessagesByKey[key] = schedMsg
|
||||
return nil
|
||||
}
|
||||
|
||||
type ScheduledMessageEventKey struct {
|
||||
RecipientId uuid.UUID
|
||||
MessageId uuid.UUID
|
||||
ScheduledMessageId uuid.UUID
|
||||
}
|
||||
|
||||
type MockScheduledMessageEventStore struct {
|
||||
ScheduledMessageEvents map[uuid.UUID]*scheduling.ScheduledMessageEvent
|
||||
ScheduledMessageEventsByKey map[ScheduledMessageEventKey]*scheduling.ScheduledMessageEvent
|
||||
mu sync.RWMutex
|
||||
Error error // Optional: simulate errors
|
||||
}
|
||||
|
||||
func NewMockScheduledMessageEventStore() *MockScheduledMessageEventStore {
|
||||
return &MockScheduledMessageEventStore{
|
||||
ScheduledMessageEvents: make(map[uuid.UUID]*scheduling.ScheduledMessageEvent),
|
||||
ScheduledMessageEventsByKey: make(map[ScheduledMessageEventKey]*scheduling.ScheduledMessageEvent),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockScheduledMessageEventStore) CreateScheduledMessageEvent(ctx context.Context, event *scheduling.ScheduledMessageEvent) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return m.Error
|
||||
}
|
||||
|
||||
if event.Id == uuid.Nil {
|
||||
event.Id = uuid.New()
|
||||
}
|
||||
|
||||
if exists, err := m.Exists(ctx, event); err != nil {
|
||||
return err
|
||||
} else if exists {
|
||||
return fmt.Errorf("Already exists")
|
||||
} else {
|
||||
key := ScheduledMessageEventKey{RecipientId: event.RecipientId, MessageId: event.MessageId, ScheduledMessageId: event.ScheduledMessageId}
|
||||
m.ScheduledMessageEvents[event.Id] = event
|
||||
m.ScheduledMessageEventsByKey[key] = event
|
||||
return nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (m *MockScheduledMessageEventStore) Exists(ctx context.Context, event *scheduling.ScheduledMessageEvent) (bool, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return false, m.Error
|
||||
}
|
||||
|
||||
key := ScheduledMessageEventKey{RecipientId: event.RecipientId, MessageId: event.MessageId, ScheduledMessageId: event.ScheduledMessageId}
|
||||
_, exists := m.ScheduledMessageEventsByKey[key]
|
||||
|
||||
return exists, nil
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-api/internal/handler/endpoint"
|
||||
"git.kundeng.us/phoenix/textsender-api/internal/store/mock"
|
||||
)
|
||||
|
||||
type CreateScheduledMessageEventRequest struct {
|
||||
@@ -26,10 +27,11 @@ type CreateScheduledMessageEventRequest struct {
|
||||
func TestCreateScheduledMessageEventWithMock(t *testing.T) {
|
||||
now := time.Now()
|
||||
|
||||
mockStore := NewMockScheduledMessageEventStore()
|
||||
contactStore := NewMockContactStore()
|
||||
messageStore := NewMockMessageStore()
|
||||
schMsgStore := NewMockScheduledMessageStore()
|
||||
mockStore := mock.NewMockScheduledMessageEventStore()
|
||||
contactStore := mock.NewMockContactStore()
|
||||
messageStore := mock.NewMockMessageStore()
|
||||
schMsgStore := mock.NewMockScheduledMessageStore()
|
||||
|
||||
handler := NewScheduledMessageEventHandler(mockStore)
|
||||
|
||||
recipientId := uuid.New()
|
||||
@@ -50,7 +52,7 @@ func TestCreateScheduledMessageEventWithMock(t *testing.T) {
|
||||
schMsg := scheduling.ScheduledMessage{}
|
||||
schMsg.Id = scheduledMessageId
|
||||
schMsg.UserId = testUserId
|
||||
schMsg.Scheduled = now.Add(20*time.Minute)
|
||||
schMsg.Scheduled = now.Add(20 * time.Minute)
|
||||
|
||||
ctx := t.Context()
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-api/internal/handler/endpoint"
|
||||
"git.kundeng.us/phoenix/textsender-api/internal/store/mock"
|
||||
)
|
||||
|
||||
type CreateScheduledMessageRequest struct {
|
||||
@@ -23,7 +24,7 @@ type CreateScheduledMessageRequest struct {
|
||||
|
||||
func TestCreateScheduledMessageWithMock(t *testing.T) {
|
||||
now := time.Now()
|
||||
mockStore := NewMockScheduledMessageStore()
|
||||
mockStore := mock.NewMockScheduledMessageStore()
|
||||
handler := NewScheduledMessageHandler(mockStore)
|
||||
|
||||
testUserId := uuid.New()
|
||||
|
||||
Reference in New Issue
Block a user