2024-09-14 20:37:51 -05:00
package store
import (
2025-01-27 11:41:32 -06:00
"errors"
2024-09-14 20:37:51 -05:00
"testing"
"github.com/stretchr/testify/assert"
2025-01-24 16:27:05 -06:00
"go.uber.org/mock/gomock"
"codeberg.org/danjones000/combluotion/config"
2025-01-26 20:07:45 -06:00
confMock "codeberg.org/danjones000/combluotion/internal/testmocks/config"
2025-01-24 16:27:05 -06:00
storeMock "codeberg.org/danjones000/combluotion/internal/testmocks/store"
2024-09-14 20:37:51 -05:00
)
2025-01-26 20:07:45 -06:00
type testHelp struct {
ctrl * gomock . Controller
store * storeMock . MockStore
fact StoreFactory
conf * confMock . MockConfig
}
func setupFactoryTest ( t * testing . T ) testHelp {
2025-01-24 16:27:05 -06:00
t . Helper ( )
ctrl := gomock . NewController ( t )
2025-01-26 20:07:45 -06:00
store := storeMock . NewMockStore ( ctrl )
fact := func ( config . Store ) ( Store , error ) {
return store , nil
2025-01-24 16:27:05 -06:00
}
2025-01-26 20:07:45 -06:00
return testHelp { ctrl , store , fact , confMock . NewMockConfig ( ctrl ) }
2024-09-14 20:37:51 -05:00
}
func TestAddFactory ( t * testing . T ) {
2025-01-26 20:07:45 -06:00
th := setupFactoryTest ( t )
AddFactory ( "mock" , th . fact )
2024-09-14 20:37:51 -05:00
defer delete ( factories , "mock" )
_ , ok := factories [ "mock" ]
assert . True ( t , ok )
}
func TestGetFactoryNil ( t * testing . T ) {
f := GetFactory ( "mock" )
assert . Nil ( t , f )
}
func TestGetFactoryNotNil ( t * testing . T ) {
2025-01-26 20:07:45 -06:00
th := setupFactoryTest ( t )
AddFactory ( "mock" , th . fact )
2024-09-14 20:37:51 -05:00
defer delete ( factories , "mock" )
2025-01-26 20:07:45 -06:00
f := GetFactory ( "mock" )
2024-09-14 20:37:51 -05:00
assert . NotNil ( t , f )
}
func TestMakeStoreError ( t * testing . T ) {
2025-01-26 20:07:45 -06:00
th := setupFactoryTest ( t )
th . conf .
EXPECT ( ) .
Store ( "mock" ) .
Return ( nil , nil )
s , e := MakeStore ( "mock" , th . conf )
2024-09-14 20:37:51 -05:00
assert . Nil ( t , s )
assert . ErrorIs ( t , e , ErrNoFactory )
assert . ErrorContains ( t , e , ErrNoFactory . Error ( ) + ": mock" )
}
func TestMakeStoreNoError ( t * testing . T ) {
2025-01-26 20:07:45 -06:00
th := setupFactoryTest ( t )
th . conf .
EXPECT ( ) .
Store ( "mock" ) .
Return ( nil , nil )
AddFactory ( "mock" , th . fact )
2024-09-14 20:37:51 -05:00
defer delete ( factories , "mock" )
2025-01-26 20:07:45 -06:00
s , e := MakeStore ( "mock" , th . conf )
2024-09-14 20:37:51 -05:00
assert . NotNil ( t , s )
assert . NoError ( t , e )
}
2024-09-15 12:37:42 -05:00
func TestMakeStoreNoName ( t * testing . T ) {
2025-01-26 20:07:45 -06:00
th := setupFactoryTest ( t )
confStore := confMock . NewMockStore ( th . ctrl )
th . conf .
EXPECT ( ) .
Store ( "" ) .
Return ( confStore , nil )
confStore .
EXPECT ( ) .
Name ( ) .
Return ( "mock" )
AddFactory ( "mock" , th . fact )
2024-09-15 12:37:42 -05:00
defer delete ( factories , "mock" )
2025-01-26 20:07:45 -06:00
s , e := MakeStore ( "" , th . conf )
2024-09-15 12:37:42 -05:00
assert . NotNil ( t , s )
assert . NoError ( t , e )
}
2025-01-27 11:41:32 -06:00
func TestMakeStoreConfError ( t * testing . T ) {
th := setupFactoryTest ( t )
mockError := errors . New ( "leave me alone" )
th . conf . EXPECT ( ) . Store ( "mock" ) . Return ( nil , mockError )
s , e := MakeStore ( "mock" , th . conf )
assert . Zero ( t , s )
assert . ErrorIs ( t , e , mockError )
}