vibeStonk/server/services/stockService_test.go
2025-06-12 16:57:42 -04:00

92 lines
2.1 KiB
Go

package services
import (
"errors"
"testing"
"vibeStonk/server/repository"
)
func TestCreateStock(t *testing.T) {
configs := repository.GetTestConfigs()
abc := "ABCDEFGHIJKLMNOP"
for _, config := range configs {
service, err := NewStockService(config)
if err != nil {
t.Errorf("got error while initializing stock service: %v", err)
}
for i := 0; i < 10; i++ {
symbol := "A" + string(abc[i])
name := "Test Stock " + string(abc[i])
stock, err := service.NewStock(symbol, name)
if err != nil {
if errors.Is(err, ErrExistingStock) {
eStock, sErr := service.GetBySymbol(symbol)
if sErr != nil {
t.Errorf("the stock existed, but we couldn't fetch it: %+v", sErr)
}
stock = eStock
} else {
t.Errorf("error creating stock: %v", err)
}
}
if stock == nil {
t.Error("newly created stock was nil")
}
if stock.Symbol != symbol {
t.Error("stock symbols did not match")
}
if stock.Name != name {
t.Error("stock names did not match")
}
}
}
}
func TestStockService_GetAll(t *testing.T) {
configs := repository.GetTestConfigs()
for _, config := range configs {
service, err := NewStockService(config)
if err != nil {
t.Errorf("got error while initializing stock service: %v", err)
}
stocks, err := service.GetAll()
if err != nil {
t.Errorf("got error while getting all stocks: %v", err)
}
if len(stocks) != 10 {
t.Errorf("Expected 10 stocks, got %d", len(stocks))
}
}
}
func TestStockService_GetByIDs(t *testing.T) {
configs := repository.GetTestConfigs()
abc := "ABCDEFGHIJKLMNOP"
for _, config := range configs {
service, err := NewStockService(config)
if err != nil {
t.Errorf("got error while initializing stock service: %v", err)
}
ids := []int{4, 5, 6}
stocks, err := service.GetByIDs(ids)
if err != nil {
t.Errorf("got error while getting stocks by id: %v", err)
}
for _, stock := range stocks {
symbol := "A" + string(abc[stock.Id-1])
if stock.Symbol != symbol {
t.Errorf("stock symbols didn't match. expected %s got %s", symbol, stock.Symbol)
}
}
}
}