func TestStoreSlice(t *testing.T) { storeSlice := store.StoreSlice{ 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: "admin", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Admin", Valid: true}}, SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: false, Valid: true}}}, &store.TableGroup{GroupID: 1, WebsiteID: 0, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0}, ), nil, store.NewStore( &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.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}}}, &store.TableGroup{GroupID: 3, WebsiteID: 2, Name: "Australia", RootCategoryID: 2, DefaultStoreID: 5}, ), } assert.True(t, storeSlice.Len() == 3) assert.EqualValues(t, utils.Int64Slice{1, 5}, storeSlice.IDs()) assert.EqualValues(t, utils.StringSlice{"de", "au"}, storeSlice.Codes()) assert.EqualValues(t, 5, storeSlice.LastItem().Data().StoreID) assert.Nil(t, (store.StoreSlice{}).LastItem()) storeSlice2 := storeSlice.Filter(func(s *store.Store) bool { return s.Website().Data().WebsiteID == 2 }) assert.True(t, storeSlice2.Len() == 1) assert.Equal(t, "au", storeSlice2[0].Data().Code.String) assert.EqualValues(t, utils.Int64Slice{5}, storeSlice2.IDs()) assert.EqualValues(t, utils.StringSlice{"au"}, storeSlice2.Codes()) assert.Nil(t, (store.StoreSlice{}).IDs()) assert.Nil(t, (store.StoreSlice{}).Codes()) }
func TestNewServiceWebsites(t *testing.T) { serviceWebsites := storemock.MustNewService(scope.Option{}, func(ms *storemock.Storage) { ms.MockWebsiteSlice = func() (store.WebsiteSlice, error) { return store.WebsiteSlice{}, nil } 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 wantErr error wantNil bool }{ {serviceWebsites, nil, false}, {serviceWebsites, nil, false}, {storemock.MustNewService(scope.Option{}, func(ms *storemock.Storage) { ms.MockWebsiteSlice = func() (store.WebsiteSlice, error) { return nil, nil } 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}, ) } }), nil, true}, } for _, test := range tests { haveWS, haveErr := test.m.Websites() if test.wantErr != nil { assert.Error(t, haveErr, "%#v", test) assert.Nil(t, haveWS, "%#v", test) } else { assert.NoError(t, haveErr, "%#v", test) if test.wantNil { assert.Nil(t, haveWS, "%#v", test) } else { assert.NotNil(t, haveWS, "%#v", test) } } } assert.False(t, serviceWebsites.IsCacheEmpty()) serviceWebsites.ClearCache() assert.True(t, serviceWebsites.IsCacheEmpty()) }
func TestNewManagerStoreInit(t *testing.T) { tms := getTestManager(func(ms *mockStorage) { 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 { haveManager *store.Manager haveID config.ScopeIDer wantErr error }{ {tms, config.ScopeID(1), nil}, {tms, config.ScopeID(1), store.ErrAppStoreSet}, {tms, nil, store.ErrAppStoreSet}, {tms, nil, store.ErrAppStoreSet}, } for _, test := range tests { haveErr := test.haveManager.Init(test.haveID, config.ScopeStoreID) if test.wantErr != nil { assert.Error(t, haveErr) assert.EqualError(t, test.wantErr, haveErr.Error()) } else { assert.NoError(t, haveErr) } s, err := test.haveManager.Store() assert.NotNil(t, s) assert.NoError(t, err) } }
func TestNewStore(t *testing.T) { tests := []struct { w *store.TableWebsite g *store.TableGroup s *store.TableStore }{ { w: &store.TableWebsite{WebsiteID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "admin", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Admin", Valid: true}}, SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: false, Valid: true}}}, g: &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0}, s: &store.TableStore{StoreID: 1, Code: dbr.NullString{NullString: sql.NullString{String: "de", Valid: true}}, WebsiteID: 1, GroupID: 1, Name: "Germany", SortOrder: 10, IsActive: true}, }, { w: &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}}}, g: &store.TableGroup{GroupID: 3, WebsiteID: 2, Name: "Australia", RootCategoryID: 2, DefaultStoreID: 5}, s: &store.TableStore{StoreID: 5, Code: dbr.NullString{NullString: sql.NullString{String: "au", Valid: true}}, WebsiteID: 2, GroupID: 3, Name: "Australia", SortOrder: 10, IsActive: true}, }, } for _, test := range tests { s := store.NewStore(test.s, test.w, test.g) assert.NotNil(t, s) assert.EqualValues(t, test.w.WebsiteID, s.Website().Data().WebsiteID) assert.EqualValues(t, test.g.GroupID, s.Group().Data().GroupID) assert.EqualValues(t, test.s.Code, s.Data().Code) assert.Nil(t, s.Group().Website()) gStores, gErr := s.Group().Stores() assert.Nil(t, gStores) assert.EqualError(t, store.ErrGroupStoresNotAvailable, gErr.Error()) assert.EqualValues(t, test.s.StoreID, s.ScopeID()) } }
func TestToJSON(t *testing.T) { s, err := 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("admin"), Name: dbr.NewNullString("Admin"), SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NewNullBool(false)}, &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0}, ) assert.NoError(t, err) var buf bytes.Buffer assert.NoError(t, s.ToJSON(&buf)) tsd := TestToJSONStore{} assert.NoError(t, json.Unmarshal(buf.Bytes(), &tsd)) want := TestToJSONStore{Code: "de", GroupID: 1, IsActive: true, Name: "Germany", SortOrder: 10, StoreID: 1, WebsiteID: 1} assert.Equal(t, want, tsd) var ds store.TableStore assert.NoError(t, json.NewDecoder(&buf).Decode(&ds)) assert.Equal(t, "de", ds.Code.String) assert.Equal(t, "Germany", ds.Name) assert.Equal(t, int64(1), ds.WebsiteID) }
func TestNewServiceGroups(t *testing.T) { serviceGroups := storemock.MustNewService(scope.Option{}, func(ms *storemock.Storage) { ms.MockGroupSlice = func() (store.GroupSlice, error) { return store.GroupSlice{}, nil } 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}, ) } }) // call it twice to test internal caching ss, err := serviceGroups.Groups() assert.NotNil(t, ss) assert.NoError(t, err) assert.Len(t, ss, 0) ss, err = serviceGroups.Groups() assert.NotNil(t, ss) assert.NoError(t, err) assert.Len(t, ss, 0) assert.False(t, serviceGroups.IsCacheEmpty()) serviceGroups.ClearCache() assert.True(t, serviceGroups.IsCacheEmpty()) }
func TestClaim(t *testing.T) { s := 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: "admin", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Admin", Valid: true}}, SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: false, Valid: true}}}, &store.TableGroup{GroupID: 1, WebsiteID: 0, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0}, ) token := jwt.New(jwt.SigningMethodHS256) s.AddClaim(token.Claims) so, err := store.StoreCodeFromClaim(token.Claims) assert.NoError(t, err) assert.EqualValues(t, "de", so.StoreCode()) so, err = store.StoreCodeFromClaim(nil) assert.EqualError(t, store.ErrStoreNotFound, err.Error()) assert.Nil(t, so.Website) assert.Nil(t, so.Group) assert.Nil(t, so.Store) token2 := jwt.New(jwt.SigningMethodHS256) token2.Claims[store.CookieName] = "Invalid Cod€" so, err = store.StoreCodeFromClaim(token2.Claims) assert.EqualError(t, store.ErrStoreCodeInvalid, err.Error()) assert.Nil(t, so.Website) assert.Nil(t, so.Group) assert.Nil(t, so.Store) }
func TestNewStore(t *testing.T) { tests := []struct { w *store.TableWebsite g *store.TableGroup s *store.TableStore }{ { w: &store.TableWebsite{WebsiteID: 1, Code: dbr.NewNullString("admin"), Name: dbr.NewNullString("Admin"), SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NewNullBool(false)}, g: &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0}, s: &store.TableStore{StoreID: 1, Code: dbr.NewNullString("de"), WebsiteID: 1, GroupID: 1, Name: "Germany", SortOrder: 10, IsActive: true}, }, { w: &store.TableWebsite{WebsiteID: 2, Code: dbr.NewNullString("oz"), Name: dbr.NewNullString("OZ"), SortOrder: 20, DefaultGroupID: 3, IsDefault: dbr.NewNullBool(false)}, g: &store.TableGroup{GroupID: 3, WebsiteID: 2, Name: "Australia", RootCategoryID: 2, DefaultStoreID: 5}, s: &store.TableStore{StoreID: 5, Code: dbr.NewNullString("au"), WebsiteID: 2, GroupID: 3, Name: "Australia", SortOrder: 10, IsActive: true}, }, } for _, test := range tests { s, err := store.NewStore(test.s, test.w, test.g) assert.NoError(t, err) assert.NotNil(t, s) assert.EqualValues(t, test.w.WebsiteID, s.Website.Data.WebsiteID) assert.EqualValues(t, test.g.GroupID, s.Group.Data.GroupID) assert.EqualValues(t, test.s.Code, s.Data.Code) assert.NotNil(t, s.Group.Website) assert.NotEmpty(t, s.Group.Website.WebsiteID()) assert.Nil(t, s.Group.Stores) assert.EqualValues(t, test.s.StoreID, s.StoreID()) assert.EqualValues(t, test.s.GroupID, s.GroupID()) assert.EqualValues(t, test.s.WebsiteID, s.WebsiteID()) } }
func TestNewManagerDefaultStoreView(t *testing.T) { managerDefaultStore := getTestManager(func(ms *mockStorage) { ms.dsv = 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 } }) // call it twice to test internal caching s, err := managerDefaultStore.DefaultStoreView() assert.NotNil(t, s) assert.NoError(t, err) assert.NotEmpty(t, s.Data.Code.String) s, err = managerDefaultStore.DefaultStoreView() assert.NotNil(t, s) assert.NoError(t, err) assert.NotEmpty(t, s.Data.Code.String) assert.False(t, managerDefaultStore.IsCacheEmpty()) managerDefaultStore.ClearCache() assert.True(t, managerDefaultStore.IsCacheEmpty()) }
func TestToJSON(t *testing.T) { s := 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: "admin", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Admin", Valid: true}}, SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: false, Valid: true}}}, &store.TableGroup{GroupID: 1, WebsiteID: 0, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0}, ) var buf bytes.Buffer assert.NoError(t, s.ToJSON(&buf)) tsd := TestToJSONStore{} json.Unmarshal(buf.Bytes(), &tsd) want := TestToJSONStore{Code: "de", GroupID: 1, IsActive: true, Name: "Germany", SortOrder: 10, StoreID: 1, WebsiteID: 1} assert.Equal(t, want, tsd) var ds store.TableStore dec := json.NewDecoder(&buf) dec.Decode(&ds) assert.Equal(t, "de", ds.Code.String) assert.Equal(t, "Germany", ds.Name) assert.Equal(t, int64(1), ds.WebsiteID) }
func TestNewManagerStores(t *testing.T) { managerStores := getTestManager(func(ms *mockStorage) { ms.ss = func() (store.StoreSlice, error) { return store.StoreSlice{ 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}, ), store.NewStore( &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.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}, ), store.NewStore( &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}}}, &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2}, ), }, nil } }) // call it twice to test internal caching ss, err := managerStores.Stores() assert.NotNil(t, ss) assert.NoError(t, err) assert.Equal(t, "at", ss[1].Data.Code.String) ss, err = managerStores.Stores() assert.NotNil(t, ss) assert.NoError(t, err) assert.NotEmpty(t, ss[2].Data.Code.String) assert.False(t, managerStores.IsCacheEmpty()) managerStores.ClearCache() assert.True(t, managerStores.IsCacheEmpty()) ss, err = getTestManager(func(ms *mockStorage) { ms.ss = func() (store.StoreSlice, error) { return nil, nil } }).Stores() assert.Nil(t, ss) assert.NoError(t, err) }
func TestNewStoreErrorIncorrectWebsite(t *testing.T) { s, err := store.NewStore( &store.TableStore{StoreID: 1, Code: dbr.NewNullString("de"), WebsiteID: 1, GroupID: 1, Name: "Germany", SortOrder: 10, IsActive: true}, &store.TableWebsite{WebsiteID: 2, Code: dbr.NewNullString("euro"), Name: dbr.NewNullString("Europe"), SortOrder: 0, DefaultGroupID: 1, IsDefault: dbr.NewNullBool(true)}, &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "UK Group", RootCategoryID: 2, DefaultStoreID: 4}, ) assert.Nil(t, s) assert.EqualError(t, store.ErrStoreIncorrectWebsite, err.Error()) }
func TestMarshalJSON(t *testing.T) { s := 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: "admin", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Admin", Valid: true}}, SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: false, Valid: true}}}, &store.TableGroup{GroupID: 1, WebsiteID: 0, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0}, ) jdata, err := json.Marshal(s) assert.NoError(t, err) assert.Equal(t, []byte(`{"StoreID":1,"Code":"de","WebsiteID":1,"GroupID":1,"Name":"Germany","SortOrder":10,"IsActive":true}`), jdata) }
func TestNewStorePanicArgsNil(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.NewStore(nil, nil, nil) }
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 TestClaim(t *testing.T) { s := 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: "admin", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Admin", Valid: true}}, SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: false, Valid: true}}}, &store.TableGroup{GroupID: 1, WebsiteID: 0, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0}, ) token := jwt.New(jwt.SigningMethodHS256) s.AddClaim(token) sCode := store.GetCodeFromClaim(token) assert.EqualValues(t, config.ScopeCode("de"), sCode) assert.Nil(t, store.GetCodeFromClaim(nil)) token2 := jwt.New(jwt.SigningMethodHS256) token2.Claims[store.CookieName] = "Invalid Cod€" sCode2 := store.GetCodeFromClaim(token2) assert.Nil(t, sCode2) }
func TestNewServiceWebsite(t *testing.T) { var serviceWebsite = storemock.MustNewService(scope.Option{}, func(ms *storemock.Storage) { ms.MockWebsite = func() (*store.Website, error) { return store.NewWebsite( &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.WebsiteIDer wantErr error wantWebsiteCode string }{ {serviceWebsite, scope.MockID(1), nil, "euro"}, {serviceWebsite, scope.MockID(1), nil, "euro"}, {serviceWebsite, scope.MockCode("notImportant"), nil, "euro"}, {serviceWebsite, scope.MockCode("notImportant"), nil, "euro"}, } for _, test := range tests { haveW, haveErr := test.m.Website(test.have) if test.wantErr != nil { assert.Error(t, haveErr, "%#v", test) assert.Nil(t, haveW, "%#v", test) } else { assert.NoError(t, haveErr, "%#v", test) assert.NotNil(t, haveW, "%#v", test) assert.Equal(t, test.wantWebsiteCode, haveW.Data.Code.String) } } assert.False(t, serviceWebsite.IsCacheEmpty()) serviceWebsite.ClearCache() assert.True(t, serviceWebsite.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 TestNewStorePanicIncorrectWebsite(t *testing.T) { defer func() { if r := recover(); r != nil { if err, ok := r.(error); ok { assert.EqualError(t, store.ErrStoreIncorrectWebsite, err.Error()) } else { t.Errorf("Failed to convert to type error: %#v", err) } } else { t.Error("Cannot find panic") } }() _ = 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: 2, 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: "UK Group", RootCategoryID: 2, DefaultStoreID: 4}, ) }
func TestToJSON(t *testing.T) { s := 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: "admin", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Admin", Valid: true}}, SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: false, Valid: true}}}, &store.TableGroup{GroupID: 1, WebsiteID: 0, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0}, ) var buf bytes.Buffer assert.NoError(t, s.ToJSON(&buf)) assert.Equal(t, `{"StoreID":1,"Code":"de","WebsiteID":1,"GroupID":1,"Name":"Germany","SortOrder":10,"IsActive":true}`, buf.String()) var ds store.TableStore dec := json.NewDecoder(&buf) dec.Decode(&ds) assert.Equal(t, "de", ds.Code.String) assert.Equal(t, "Germany", ds.Name) assert.Equal(t, int64(1), ds.WebsiteID) }
func TestContextReaderSuccess(t *testing.T) { ctx := storemock.NewContextService(scope.Option{}, func(ms *storemock.Storage) { ms.MockStore = func() (*store.Store, error) { return store.NewStore( &store.TableStore{StoreID: 6, 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: 6}, ) } }, ) haveMr, s, err := store.FromContextReader(ctx) assert.NoError(t, err) assert.Exactly(t, int64(6), s.StoreID()) s2, err2 := haveMr.Store() assert.NoError(t, err2) assert.Exactly(t, int64(6), s2.StoreID()) }
func init() { middlewareConfigReader = config.NewMockGetter( config.WithMockValues(config.MockPV{ scope.StrDefault.FQPathInt64(0, backend.Backend.WebURLRedirectToBase.String()): 1, scope.StrStores.FQPathInt64(1, backend.Backend.WebSecureUseInFrontend.String()): true, scope.StrStores.FQPathInt64(1, backend.Backend.WebUnsecureBaseURL.String()): "http://www.corestore.io/", scope.StrStores.FQPathInt64(1, backend.Backend.WebSecureBaseURL.String()): "https://www.corestore.io/", }), ) middlewareCtxStoreService = storemock.WithContextMustService( scope.Option{}, func(ms *storemock.Storage) { 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}, store.WithStoreConfig(middlewareConfigReader), ) } }, ) }
func init() { middlewareConfigReader = config.NewMockReader( config.WithMockValues(config.MockPV{ config.MockPathScopeDefault(store.PathRedirectToBase): 1, config.MockPathScopeStore(1, store.PathSecureInFrontend): true, config.MockPathScopeStore(1, store.PathUnsecureBaseURL): "http://www.corestore.io/", config.MockPathScopeStore(1, store.PathSecureBaseURL): "https://www.corestore.io/", }), ) middlewareCtxStoreService = storemock.NewContextService( scope.Option{}, func(ms *storemock.Storage) { 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}, store.SetStoreConfig(middlewareConfigReader), ) } }, ) }
func TestStoreBaseURLandPath(t *testing.T) { s, err := 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("admin"), Name: dbr.NewNullString("Admin"), SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NewNullBool(false)}, &store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "Default", RootCategoryID: 0, DefaultStoreID: 1}, ) assert.NoError(t, err) if s == nil { t.Fail() } tests := []struct { haveR config.Reader haveUT config.URLType haveIsSecure bool wantBaseUrl string wantPath string }{ { config.NewMockReader(config.WithMockString( func(path string) (string, error) { switch path { case config.MockPathScopeDefault(store.PathSecureBaseURL): return "https://corestore.io", nil case config.MockPathScopeDefault(store.PathUnsecureBaseURL): return "http://corestore.io", nil } return "", config.ErrKeyNotFound }, )), config.URLTypeWeb, true, "https://corestore.io/", "/", }, { config.NewMockReader(config.WithMockString( func(path string) (string, error) { switch path { case config.MockPathScopeDefault(store.PathSecureBaseURL): return "https://myplatform.io/customer1", nil case config.MockPathScopeDefault(store.PathUnsecureBaseURL): return "http://myplatform.io/customer1", nil } return "", config.ErrKeyNotFound }, )), config.URLTypeWeb, false, "http://myplatform.io/customer1/", "/customer1/", }, { config.NewMockReader(config.WithMockString( func(path string) (string, error) { switch path { case config.MockPathScopeDefault(store.PathSecureBaseURL): return store.PlaceholderBaseURL, nil case config.MockPathScopeDefault(store.PathUnsecureBaseURL): return store.PlaceholderBaseURL, nil case config.MockPathScopeDefault(config.PathCSBaseURL): return config.CSBaseURL, nil } return "", config.ErrKeyNotFound }, )), config.URLTypeWeb, false, config.CSBaseURL, "/", }, } for i, test := range tests { s.ApplyOptions(store.SetStoreConfig(test.haveR)) assert.NotNil(t, s.Config, "Index %d", i) baseURL, err := s.BaseURL(test.haveUT, test.haveIsSecure) assert.NoError(t, err) assert.EqualValues(t, test.wantBaseUrl, baseURL.String()) assert.EqualValues(t, test.wantPath, s.Path()) _, err = s.BaseURL(config.URLTypeAbsent, false) assert.EqualError(t, err, config.ErrURLCacheCleared.Error()) } }
package store_test import ( "bytes" "database/sql" "encoding/json" "testing" "github.com/corestoreio/csfw/storage/dbr" "github.com/corestoreio/csfw/store" ) var benchmarkJSON []byte var benchmarkJSONStore = 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: "admin", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Admin", Valid: true}}, SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: false, Valid: true}}}, &store.TableGroup{GroupID: 1, WebsiteID: 0, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0}, ) // BenchmarkJSONMarshal-4 300000 4343 ns/op 1032 B/op 12 allocs/op func BenchmarkJSONMarshal(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { var err error benchmarkJSON, err = json.Marshal(benchmarkJSONStore) if err != nil { b.Error(err) } } }
// }, nil)) //} func getTestManager(opts ...func(ms *mockStorage)) *store.Manager { ms := &mockStorage{} for _, opt := range opts { opt(ms) } return store.NewManager(store.SetManagerStorage(ms)) } var managerStoreSimpleTest = getTestManager(func(ms *mockStorage) { 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 } }) func TestNewManagerStore(t *testing.T) { assert.True(t, managerStoreSimpleTest.IsCacheEmpty()) for j := 0; j < 3; j++ { s, err := managerStoreSimpleTest.Store(config.ScopeCode("notNil")) assert.NoError(t, err) assert.NotNil(t, s) assert.EqualValues(t, "de", s.Data.Code.String) } assert.False(t, managerStoreSimpleTest.IsCacheEmpty()) managerStoreSimpleTest.ClearCache()
func TestNewStoreErrorArgsNil(t *testing.T) { s, err := store.NewStore(nil, nil, nil) assert.Nil(t, s) assert.EqualError(t, store.ErrArgumentCannotBeNil, err.Error()) }
func TestStoreBaseUrlandPath(t *testing.T) { s := 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: "admin", Valid: true}}, Name: dbr.NullString{NullString: sql.NullString{String: "Admin", Valid: true}}, SortOrder: 0, DefaultGroupID: 0, IsDefault: dbr.NullBool{NullBool: sql.NullBool{Bool: false, Valid: true}}}, &store.TableGroup{GroupID: 1, WebsiteID: 0, Name: "Default", RootCategoryID: 0, DefaultStoreID: 0}, ) tests := []struct { haveR config.Reader haveUT config.URLType haveIsSecure bool wantBaseUrl string wantPath string }{ { config.NewMockReader(func(path string) string { switch path { case config.ScopeRangeDefault + "/0/" + store.PathSecureBaseURL: return "https://corestore.io" case config.ScopeRangeDefault + "/0/" + store.PathUnsecureBaseURL: return "http://corestore.io" } return "" }, nil), config.URLTypeWeb, true, "https://corestore.io/", "/", }, { config.NewMockReader(func(path string) string { switch path { case config.ScopeRangeDefault + "/0/" + store.PathSecureBaseURL: return "https://myplatform.io/customer1" case config.ScopeRangeDefault + "/0/" + store.PathUnsecureBaseURL: return "http://myplatform.io/customer1" } return "" }, nil), config.URLTypeWeb, false, "http://myplatform.io/customer1/", "/customer1/", }, { config.NewMockReader(func(path string) string { switch path { case config.ScopeRangeDefault + "/0/" + store.PathSecureBaseURL: return store.PlaceholderBaseURL case config.ScopeRangeDefault + "/0/" + store.PathUnsecureBaseURL: return store.PlaceholderBaseURL case config.ScopeRangeDefault + "/0/" + config.PathCSBaseURL: return config.CSBaseURL } return "" }, nil), config.URLTypeWeb, false, config.CSBaseURL, "/", }, } for _, test := range tests { s.ApplyOptions(store.SetStoreConfig(test.haveR)) assert.EqualValues(t, test.wantBaseUrl, s.BaseURL(test.haveUT, test.haveIsSecure)) assert.EqualValues(t, test.wantPath, s.Path()) } }
// case store.PathSecureBaseURL: // return store.PlaceholderBaseURL // case store.PathUnsecureBaseURL: // return store.PlaceholderBaseURL // case config.PathCSBaseURL: // return "http://cs.io/" // } // return "" // }, nil)) //} var serviceStoreSimpleTest = storemock.MustNewService(scope.Option{}, func(ms *storemock.Storage) { 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}, ) } }) func TestNewServiceStore(t *testing.T) { assert.False(t, serviceStoreSimpleTest.IsCacheEmpty()) for j := 0; j < 3; j++ { s, err := serviceStoreSimpleTest.Store(scope.MockCode("notNil")) assert.NoError(t, err) assert.NotNil(t, s) assert.EqualValues(t, "de", s.Data.Code.String) } assert.False(t, serviceStoreSimpleTest.IsCacheEmpty()) serviceStoreSimpleTest.ClearCache()