Files
schedtxt_api/internal/handler/mock_store_test.go
T
phoenixandphoenix 5f9bfa8fe2 tsk-9: Schedule queued message endpoint (#22)
Closes #9

Reviewed-on: phoenix/textsender-api#22
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2025-11-09 04:32:30 +00:00

241 lines
5.1 KiB
Go

package handler
import (
"context"
"errors"
"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
}