func TestNewManagerGroupInit(t *testing.T) { err := getTestManager(func(ms *mockStorage) { ms.g = func() (*store.Group, error) { return store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, store.SetGroupWebsite(&store.TableWebsite{WebsiteID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "euro", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Europe", Valid: true}}, SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: true, Valid: true}}}), ), nil } }).Init(config.ScopeID(1), config.ScopeGroupID) assert.EqualError(t, store.ErrGroupDefaultStoreNotFound, err.Error(), "Incorrect DefaultStore for a Group") err = getTestManager().Init(config.ScopeID(21), config.ScopeGroupID) assert.EqualError(t, store.ErrGroupNotFound, err.Error()) tm3 := getTestManager(func(ms *mockStorage) { ms.g = func() (*store.Group, error) { return store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, store.SetGroupWebsite(&store.TableWebsite{WebsiteID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "euro", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Europe", Valid: true}}, SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: true, Valid: true}}}), ).SetStores(store.TableStoreSlice{ &store.TableStore{StoreID: 2, Code: dbr.NullString{NullString: sql.NullString{String: "at", Valid: true}}, WebsiteID: 1, GroupID: 1, Name: "Österreich", SortOrder: 20, IsActive: true}, }, nil), nil } }) err = tm3.Init(config.ScopeID(1), config.ScopeGroupID) assert.NoError(t, err) g, err := tm3.Group() assert.NoError(t, err) assert.NotNil(t, g) assert.Equal(t, int64(2), g.Data.DefaultStoreID) }
func TestNewGroupSetStores(t *testing.T) { g := store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, nil, ) g.SetStores( store.TableStoreSlice{ &store.TableStore{StoreID: 0, Code: dbr.NullString{NullString: sql.NullString{String: "admin", Valid: true}}, WebsiteID: 0, GroupID: 0, Name: "Admin", SortOrder: 0, IsActive: true}, &store.TableStore{StoreID: 5, Code: dbr.NullString{NullString: sql.NullString{String: "au", Valid: true}}, WebsiteID: 2, GroupID: 3, Name: "Australia", SortOrder: 10, IsActive: true}, &store.TableStore{StoreID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "de", Valid: true}}, WebsiteID: 1, GroupID: 1, Name: "Germany", SortOrder: 10, IsActive: true}, &store.TableStore{StoreID: 4, Code: dbr.NullString{NullString: sql.NullString{String: "uk", Valid: true}}, WebsiteID: 1, GroupID: 2, Name: "UK", SortOrder: 10, IsActive: true}, &store.TableStore{StoreID: 2, Code: dbr.NullString{NullString: sql.NullString{String: "at", Valid: true}}, WebsiteID: 1, GroupID: 1, Name: "Österreich", SortOrder: 20, IsActive: true}, &store.TableStore{StoreID: 6, Code: dbr.NullString{NullString: sql.NullString{String: "nz", Valid: true}}, WebsiteID: 2, GroupID: 3, Name: "Kiwi", SortOrder: 30, IsActive: true}, &store.TableStore{StoreID: 3, Code: dbr.NullString{NullString: sql.NullString{String: "ch", Valid: true}}, WebsiteID: 1, GroupID: 1, Name: "Schweiz", SortOrder: 30, IsActive: true}, }, &store.TableWebsite{WebsiteID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "euro", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Europe", Valid: true}}, SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: true, Valid: true}}}, ) gStores, err := g.Stores() assert.NoError(t, err) assert.EqualValues(t, utils.StringSlice{"de", "at", "ch"}, gStores.Codes()) gDefaultStore, err := g.DefaultStore() assert.NoError(t, err) assert.EqualValues(t, "euro", gDefaultStore.Website().Data().Code.String) assert.EqualValues(t, "DACH Group", gDefaultStore.Group().Data().Name) assert.EqualValues(t, "at", gDefaultStore.Data().Code.String) }
func TestNewGroupSetStoresPanicWebsiteIncorrect(t *testing.T) { defer func() { if r := recover(); r != nil { if err, ok := r.(error); ok { assert.EqualError(t, store.ErrGroupWebsiteNotFound, err.Error()) } else { t.Errorf("Failed to convert to type error: %#v", err) } } else { t.Error("Cannot find panic") } }() g := store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, nil, ) g.SetStores( store.TableStoreSlice{ &store.TableStore{StoreID: 0, Code: dbr.NullString{NullString: sql.NullString{String: "admin", Valid: true}}, WebsiteID: 0, GroupID: 0, Name: "Admin", SortOrder: 0, IsActive: true}, }, &store.TableWebsite{WebsiteID: 2, Code: dbr.NullString{NullString: sql.NullString{String: "oz", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "OZ", Valid: true}}, SortOrder: 20, DefaultGroupID: 3, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: false, Valid: true}}}, ) }
func TestNewGroupSetStores(t *testing.T) { g, err := store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, nil, // this is just for to confuse the NewGroup ApplyOption function store.SetGroupStores( store.TableStoreSlice{ &store.TableStore{StoreID: 0, Code: dbr.NewNullString("admin"), WebsiteID: 0, GroupID: 0, Name: "Admin", SortOrder: 0, IsActive: true}, &store.TableStore{StoreID: 5, Code: dbr.NewNullString("au"), WebsiteID: 2, GroupID: 3, Name: "Australia", SortOrder: 10, IsActive: true}, &store.TableStore{StoreID: 1, Code: dbr.NewNullString("de"), WebsiteID: 1, GroupID: 1, Name: "Germany", SortOrder: 10, IsActive: true}, &store.TableStore{StoreID: 4, Code: dbr.NewNullString("uk"), WebsiteID: 1, GroupID: 2, Name: "UK", SortOrder: 10, IsActive: true}, &store.TableStore{StoreID: 2, Code: dbr.NewNullString("at"), WebsiteID: 1, GroupID: 1, Name: "Österreich", SortOrder: 20, IsActive: true}, &store.TableStore{StoreID: 6, Code: dbr.NewNullString("nz"), WebsiteID: 2, GroupID: 3, Name: "Kiwi", SortOrder: 30, IsActive: true}, &store.TableStore{StoreID: 3, Code: dbr.NewNullString("ch"), WebsiteID: 1, GroupID: 1, Name: "Schweiz", SortOrder: 30, IsActive: true}, }, &store.TableWebsite{WebsiteID: 1, Code: dbr.NewNullString("euro"), Name: dbr.NewNullString("Europe"), SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NewNullBool(true)}, ), ) assert.NoError(t, err) assert.NotNil(t, g.Stores) assert.EqualValues(t, utils.StringSlice{"de", "at", "ch"}, g.Stores.Codes()) gDefaultStore, err := g.DefaultStore() assert.NoError(t, err) assert.EqualValues(t, "euro", gDefaultStore.Website.Data.Code.String) assert.EqualValues(t, "DACH Group", gDefaultStore.Group.Data.Name) assert.EqualValues(t, "at", gDefaultStore.Data.Code.String) }
func TestNewGroupPanicWebsiteIncorrect(t *testing.T) { ng, err := store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, store.SetGroupWebsite(&store.TableWebsite{WebsiteID: 2, Code: dbr.NewNullString("oz"), Name: dbr.NewNullString("OZ"), SortOrder: 20, DefaultGroupID: 3, IsDefault: dbr.NewNullBool(false)}), ) assert.Nil(t, ng) assert.EqualError(t, store.ErrGroupWebsiteNotFound, err.Error()) }
func TestNewGroup(t *testing.T) { g := store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, nil, ) assert.EqualValues(t, "DACH Group", g.Data.Name) assert.Nil(t, g.Stores) gStores2, err := g.DefaultStore() assert.Nil(t, gStores2) assert.EqualError(t, store.ErrGroupDefaultStoreNotFound, err.Error()) }
func TestNewGroupSetStoresErrorWebsiteIncorrect(t *testing.T) { g, err := store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, store.SetGroupStores( store.TableStoreSlice{ &store.TableStore{StoreID: 0, Code: dbr.NewNullString("admin"), WebsiteID: 0, GroupID: 0, Name: "Admin", SortOrder: 0, IsActive: true}, }, &store.TableWebsite{WebsiteID: 2, Code: dbr.NewNullString("oz"), Name: dbr.NewNullString("OZ"), SortOrder: 20, DefaultGroupID: 3, IsDefault: dbr.NewNullBool(false)}, ), ) assert.Nil(t, g) assert.EqualError(t, store.ErrGroupWebsiteIntegrityFailed, err.Error()) }
func TestNewGroupPanic(t *testing.T) { defer func() { if r := recover(); r != nil { if err, ok := r.(error); ok { assert.EqualError(t, store.ErrStoreNewArgNil, err.Error()) } else { t.Errorf("Failed to convert to type error: %#v", err) } } else { t.Error("Cannot find panic") } }() _ = store.NewGroup(nil, nil) }
func TestNewGroupSetStoresErrorWebsiteIsNil(t *testing.T) { g, err := store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, nil, store.SetGroupStores( store.TableStoreSlice{ &store.TableStore{StoreID: 0, Code: dbr.NewNullString("admin"), WebsiteID: 0, GroupID: 0, Name: "Admin", SortOrder: 0, IsActive: true}, }, nil, ), ) assert.Nil(t, g) assert.EqualError(t, store.ErrGroupWebsiteNotFound, err.Error()) }
func TestNewManagerGroup(t *testing.T) { var managerGroupSimpleTest = getTestManager(func(ms *mockStorage) { ms.g = func() (*store.Group, error) { return store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, store.SetGroupWebsite(&store.TableWebsite{WebsiteID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "euro", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Europe", Valid: true}}, SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: true, Valid: true}}}), ), nil } ms.s = func() (*store.Store, error) { return store.NewStore( &store.TableStore{StoreID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "de", Valid: true}}, WebsiteID: 1, GroupID: 1, Name: "Germany", SortOrder: 10, IsActive: true}, &store.TableWebsite{WebsiteID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "euro", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Europe", Valid: true}}, SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: true, Valid: true}}}, &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, ), nil } }) tests := []struct { m *store.Manager have config.ScopeIDer wantErr error wantGroupName string wantWebsiteCode string }{ {managerGroupSimpleTest, nil, store.ErrAppStoreNotSet, "", ""}, {getTestManager(), config.ScopeID(20), store.ErrGroupNotFound, "", ""}, {managerGroupSimpleTest, config.ScopeID(1), nil, "DACH Group", "euro"}, {managerGroupSimpleTest, config.ScopeID(1), nil, "DACH Group", "euro"}, } for _, test := range tests { g, err := test.m.Group(test.have) if test.wantErr != nil { assert.Nil(t, g) assert.EqualError(t, test.wantErr, err.Error(), "test %#v", test) } else { assert.NotNil(t, g, "test %#v", test) assert.NoError(t, err, "test %#v", test) assert.Equal(t, test.wantGroupName, g.Data.Name) assert.Equal(t, test.wantWebsiteCode, g.Website.Data.Code.String) } } assert.False(t, managerGroupSimpleTest.IsCacheEmpty()) managerGroupSimpleTest.ClearCache() assert.True(t, managerGroupSimpleTest.IsCacheEmpty()) }
func TestNewServiceGroup(t *testing.T) { var serviceGroupSimpleTest = storemock.MustNewService(scope.Option{}, func(ms *storemock.Storage) { ms.MockGroup = func() (*store.Group, error) { return store.NewGroup( &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, store.SetGroupWebsite(&store.TableWebsite{WebsiteID: 1, Code: dbr.NewNullString("euro"), Name: dbr.NewNullString("Europe"), SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NewNullBool(true)}), ) } ms.MockStore = func() (*store.Store, error) { return store.NewStore( &store.TableStore{StoreID: 1, Code: dbr.NewNullString("de"), WebsiteID: 1, GroupID: 1, Name: "Germany", SortOrder: 10, IsActive: true}, &store.TableWebsite{WebsiteID: 1, Code: dbr.NewNullString("euro"), Name: dbr.NewNullString("Europe"), SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NewNullBool(true)}, &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, ) } }) tests := []struct { m *store.Service have scope.GroupIDer wantErr error wantGroupName string wantWebsiteCode string }{ {serviceGroupSimpleTest, scope.MockID(20), nil, "DACH Group", "euro"}, {serviceGroupSimpleTest, scope.MockID(1), nil, "DACH Group", "euro"}, {serviceGroupSimpleTest, scope.MockID(1), nil, "DACH Group", "euro"}, } for i, test := range tests { g, err := test.m.Group(test.have) if test.wantErr != nil { assert.Nil(t, g, "Index %d", i) assert.EqualError(t, test.wantErr, err.Error(), "test %#v", test) } else { assert.NotNil(t, g, "test %#v", test) assert.NoError(t, err, "test %#v", test) assert.Equal(t, test.wantGroupName, g.Data.Name) assert.Equal(t, test.wantWebsiteCode, g.Website.Data.Code.String) } } assert.False(t, serviceGroupSimpleTest.IsCacheEmpty()) serviceGroupSimpleTest.ClearCache() assert.True(t, serviceGroupSimpleTest.IsCacheEmpty()) }
func TestNewGroupErrorArgument(t *testing.T) { ng, err := store.NewGroup(nil, nil) assert.Nil(t, ng) assert.EqualError(t, store.ErrArgumentCannotBeNil, err.Error()) }