本文整理汇总了Golang中github.com/corestoreio/csfw/config.ScopeCode函数的典型用法代码示例。如果您正苦于以下问题:Golang ScopeCode函数的具体用法?Golang ScopeCode怎么用?Golang ScopeCode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ScopeCode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestNewManagerStore
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()
assert.True(t, managerStoreSimpleTest.IsCacheEmpty())
tests := []struct {
have config.ScopeIDer
wantErr error
}{
{config.ScopeCode("nilSlices"), store.ErrStoreNotFound},
{config.ScopeID(2), store.ErrStoreNotFound},
{nil, store.ErrAppStoreNotSet},
}
managerEmpty := getTestManager()
for _, test := range tests {
s, err := managerEmpty.Store(test.have)
assert.Nil(t, s)
assert.EqualError(t, test.wantErr, err.Error())
}
assert.True(t, managerStoreSimpleTest.IsCacheEmpty())
}
示例2: TestNewManagerGetRequestStore_ScopeWebsite
func TestNewManagerGetRequestStore_ScopeWebsite(t *testing.T) {
testCode := config.ScopeID(1)
testScope := config.ScopeWebsiteID
if haveStore, haveErr := storeManagerRequestStore.GetRequestStore(config.ScopeID(1), testScope); haveErr == nil {
t.Error("appStore should not be set!")
t.Fail()
} else {
assert.Nil(t, haveStore)
assert.EqualError(t, store.ErrAppStoreNotSet, haveErr.Error())
}
assert.EqualError(t, store.ErrUnsupportedScopeGroup, storeManagerRequestStore.Init(config.ScopeID(123), config.ScopeDefaultID).Error())
assert.EqualError(t, store.ErrWebsiteNotFound, storeManagerRequestStore.Init(config.ScopeID(123), testScope).Error())
if err := storeManagerRequestStore.Init(testCode, testScope); err != nil {
t.Error(err)
t.Fail()
}
assert.EqualError(t, store.ErrAppStoreSet, storeManagerRequestStore.Init(testCode, testScope).Error())
if s, err := storeManagerRequestStore.Store(); err == nil {
assert.EqualValues(t, "at", s.Data.Code.String)
} else {
assert.EqualError(t, err, store.ErrStoreNotFound.Error())
t.Fail()
}
if w, err := storeManagerRequestStore.Website(); err == nil {
assert.EqualValues(t, "euro", w.Data.Code.String)
} else {
assert.EqualError(t, err, store.ErrStoreNotFound.Error())
t.Fail()
}
tests := []testNewManagerGetRequestStore{
{config.ScopeID(232), "", store.ErrStoreNotFound},
{nil, "", store.ErrStoreNotFound},
{config.ScopeCode("\U0001f631"), "", store.ErrStoreNotFound},
{config.ScopeID(6), "nz", store.ErrStoreChangeNotAllowed},
{config.ScopeCode("ch"), "", store.ErrStoreNotActive},
{config.ScopeCode("de"), "de", nil},
{config.ScopeID(2), "at", nil},
{config.ScopeID(2), "at", nil},
{config.ScopeCode("au"), "au", store.ErrStoreChangeNotAllowed},
{config.ScopeCode("ch"), "", store.ErrStoreNotActive},
}
runNewManagerGetRequestStore(t, testScope, tests)
}
示例3: InitByRequest
// InitByRequest returns a new Store read from a cookie or HTTP request param.
// The internal appStore must be set before hand.
// 1. check cookie store, always a string and the store code
// 2. check for ___store variable, always a string and the store code
// 3. May return nil,nil if nothing is set.
// This function must be used within an HTTP handler.
// The returned new Store must be used in the HTTP context and overrides the appStore.
// @see \Magento\Store\Model\StorageFactory::_reinitStores
func (sm *Manager) InitByRequest(res http.ResponseWriter, req *http.Request, scopeType config.ScopeGroup) (*Store, error) {
if sm.appStore == nil {
// that means you must call Init() before executing this function.
return nil, ErrAppStoreNotSet
}
var reqStore *Store
if keks := GetCodeFromCookie(req); keks != nil {
reqStore, _ = sm.GetRequestStore(keks, scopeType) // ignore errors
}
if reqStoreCode := req.URL.Query().Get(HTTPRequestParamStore); reqStoreCode != "" {
var err error
// @todo reqStoreCode if number ... cast to int64 because then group id if ScopeGroup is group.
if reqStore, err = sm.GetRequestStore(config.ScopeCode(reqStoreCode), scopeType); err != nil {
return nil, errgo.Mask(err)
}
// also delete and re-set a new cookie
if reqStore != nil && reqStore.Data().Code.String == reqStoreCode {
wds, err := reqStore.Website().DefaultStore()
if err != nil {
return nil, errgo.Mask(err)
}
if wds.Data().Code.String == reqStoreCode {
reqStore.DeleteCookie(res) // cookie not needed anymore
} else {
reqStore.SetCookie(res) // make sure we force set the new store
}
}
}
return reqStore, nil // can be nil,nil
}
示例4: TestNewManagerReInit
func TestNewManagerReInit(t *testing.T) {
numCPU := runtime.NumCPU()
prevCPU := runtime.GOMAXPROCS(numCPU)
t.Logf("GOMAXPROCS was: %d now: %d", prevCPU, numCPU)
defer runtime.GOMAXPROCS(prevCPU)
// quick implement, use mock of dbr.SessionRunner and remove connection
db := csdb.MustConnectTest()
defer db.Close()
dbrSess := dbr.NewConnection(db, nil).NewSession(nil)
storeManager := store.NewManager(store.NewStorageOption(nil /* trick it*/))
if err := storeManager.ReInit(dbrSess); err != nil {
t.Fatal(err)
}
tests := []struct {
have config.ScopeIDer
wantErr error
}{
{config.ScopeCode("de"), nil},
{config.ScopeCode("cz"), store.ErrStoreNotFound},
{config.ScopeCode("de"), nil},
{config.ScopeID(1), nil},
{config.ScopeID(100), store.ErrStoreNotFound},
{mockIDCode{1, "de"}, nil},
{mockIDCode{2, "cz"}, store.ErrStoreNotFound},
{mockIDCode{2, ""}, nil},
{nil, store.ErrAppStoreNotSet}, // if set returns default store
}
for _, test := range tests {
s, err := storeManager.Store(test.have)
if test.wantErr == nil {
assert.NoError(t, err, "For test: %#v", test)
assert.NotNil(t, s)
// assert.NotEmpty(t, s.Data().Code.String, "%#v", s.Data())
} else {
assert.Error(t, err, "For test: %#v", test)
assert.EqualError(t, test.wantErr, err.Error(), "For test: %#v", test)
assert.Nil(t, s)
}
}
assert.False(t, storeManager.IsCacheEmpty())
storeManager.ClearCache()
assert.True(t, storeManager.IsCacheEmpty())
}
示例5: GetCodeFromCookie
// GetCookie returns from a Request the value of the store cookie or nil.
func GetCodeFromCookie(req *http.Request) config.ScopeIDer {
if req == nil {
return nil
}
if keks, err := req.Cookie(CookieName); nil == err && nil == ValidateStoreCode(keks.Value) {
return config.ScopeCode(keks.Value)
}
return nil
}
示例6: TestStorageWebsite
func TestStorageWebsite(t *testing.T) {
tests := []struct {
have config.ScopeIDer
err error
wantWCode string
}{
{nil, store.ErrWebsiteNotFound, ""},
{config.ScopeID(2015), store.ErrWebsiteNotFound, ""},
{config.ScopeID(1), nil, "euro"},
{config.ScopeCode("asia"), store.ErrWebsiteNotFound, ""},
{config.ScopeCode("oz"), nil, "oz"},
{mockIDCode{1, "oz"}, nil, "oz"},
{mockIDCode{1, "ozzz"}, store.ErrWebsiteNotFound, ""},
}
for _, test := range tests {
w, err := testStorage.Website(test.have)
if test.err != nil {
assert.Nil(t, w)
assert.EqualError(t, test.err, err.Error())
} else {
assert.NotNil(t, w, "Test: %#v", test)
assert.NoError(t, err, "Test: %#v", test)
assert.Equal(t, test.wantWCode, w.Data().Code.String, "Test: %#v", test)
}
}
w, err := testStorage.Website(config.ScopeCode("euro"))
assert.NoError(t, err)
assert.NotNil(t, w)
dGroup, err := w.DefaultGroup()
assert.NoError(t, err)
assert.EqualValues(t, "DACH Group", dGroup.Data().Name)
groups, err := w.Groups()
assert.NoError(t, err)
assert.EqualValues(t, utils.Int64Slice{1, 2}, groups.IDs())
stores, err := w.Stores()
assert.NoError(t, err)
assert.EqualValues(t, utils.StringSlice{"de", "uk", "at", "ch"}, stores.Codes())
}
示例7: GetCodeFromClaim
// GetClaim returns a valid store code from a JSON web token or nil
func GetCodeFromClaim(t *jwt.Token) config.ScopeIDer {
if t == nil {
return nil
}
c, ok := t.Claims[CookieName]
if cs, okcs := c.(string); okcs && ok && nil == ValidateStoreCode(cs) {
return config.ScopeCode(cs)
}
return nil
}
示例8: TestNewManagerWebsiteInit
func TestNewManagerWebsiteInit(t *testing.T) {
err := getTestManager(func(ms *mockStorage) {
ms.w = func() (*store.Website, error) {
return store.NewWebsite(
&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.ScopeCode("euro"), config.ScopeWebsiteID)
assert.EqualError(t, store.ErrWebsiteDefaultGroupNotFound, err.Error())
managerWebsite := getTestManager(func(ms *mockStorage) {
ms.w = func() (*store.Website, error) {
return store.NewWebsite(
&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}}},
).SetGroupsStores(
store.TableGroupSlice{
&store.TableGroup{GroupID: 1, WebsiteID: 1, Name: "DACH Group", RootCategoryID: 2, DefaultStoreID: 2},
},
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: 1, Code: dbr.NullString{NullString: sql.NullString{String: "de", Valid: true}}, WebsiteID: 1, GroupID: 1, Name: "Germany", 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: 3, Code: dbr.NullString{NullString: sql.NullString{String: "ch", Valid: true}}, WebsiteID: 1, GroupID: 1, Name: "Schweiz", SortOrder: 30, IsActive: true},
},
), nil
}
})
w1, err := managerWebsite.Website()
assert.EqualError(t, store.ErrAppStoreNotSet, err.Error())
assert.Nil(t, w1)
err = managerWebsite.Init(config.ScopeCode("euro"), config.ScopeWebsiteID)
assert.NoError(t, err)
w2, err := managerWebsite.Website()
assert.NoError(t, err)
assert.EqualValues(t, "euro", w2.Data.Code.String)
err3 := getTestManager(func(ms *mockStorage) {}).Init(config.ScopeCode("euronen"), config.ScopeWebsiteID)
assert.Error(t, err3, "config.ScopeCode(euro), config.ScopeWebsite: %#v => %s", err3, err3)
assert.EqualError(t, store.ErrWebsiteNotFound, err3.Error())
}
示例9: TestStorageStore
func TestStorageStore(t *testing.T) {
tests := []struct {
have config.ScopeIDer
err error
wantCode string
}{
{nil, store.ErrStoreNotFound, ""},
{config.ScopeID(2015), store.ErrStoreNotFound, ""},
{config.ScopeID(1), nil, "de"},
{config.ScopeCode("asia"), store.ErrStoreNotFound, ""},
{config.ScopeCode("nz"), nil, "nz"},
{mockIDCode{4, "nz"}, nil, "nz"},
{mockIDCode{4, "auuuuu"}, store.ErrStoreNotFound, ""},
}
for _, test := range tests {
s, err := testStorage.Store(test.have)
if test.err != nil {
assert.Nil(t, s, "%#v", test)
assert.EqualError(t, test.err, err.Error())
} else {
assert.NotNil(t, s, "%#v", test)
assert.NoError(t, err, "%#v", test)
assert.Equal(t, test.wantCode, s.Data().Code.String)
}
}
s, err := testStorage.Store(config.ScopeCode("at"))
assert.NoError(t, err)
assert.NotNil(t, s)
assert.EqualValues(t, "DACH Group", s.Group().Data().Name)
website := s.Website()
assert.EqualValues(t, "euro", website.Data().Code.String)
wg, err := website.DefaultGroup()
assert.NotNil(t, wg)
assert.EqualValues(t, "DACH Group", wg.Data().Name)
wgs, err := wg.DefaultStore()
assert.NoError(t, err)
assert.EqualValues(t, "at", wgs.Data().Code.String)
}
示例10: BenchmarkManagerGetStore
// BenchmarkManagerGetStore 5000000 355 ns/op 24 B/op 2 allocs/op
func BenchmarkManagerGetStore(b *testing.B) {
for i := 0; i < b.N; i++ {
var err error
benchmarkManagerStore, err = managerStoreSimpleTest.Store(config.ScopeCode("de"))
if err != nil {
b.Error(err)
}
if benchmarkManagerStore == nil {
b.Error("benchmarkManagerStore is nil")
}
}
}
示例11: TestStorageStoreErrors
func TestStorageStoreErrors(t *testing.T) {
var nsw = store.NewStorage(
store.SetStorageWebsites(),
store.SetStorageGroups(),
store.SetStorageStores(
&store.TableStore{StoreID: 4, Code: dbr.NullString{NullString: sql.NullString{String: "au", Valid: true}}, WebsiteID: 2, GroupID: 3, Name: "Australia", SortOrder: 10, 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},
),
)
stw, err := nsw.Store(config.ScopeCode("nz"))
assert.Nil(t, stw)
assert.EqualError(t, store.ErrWebsiteNotFound, err.Error())
stws, err := nsw.Stores()
assert.Nil(t, stws)
assert.EqualError(t, store.ErrWebsiteNotFound, err.Error())
var nsg = store.NewStorage(
store.SetStorageWebsites(
&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.SetStorageGroups(
&store.TableGroup{GroupID: 13, WebsiteID: 12, Name: "Australia", RootCategoryID: 2, DefaultStoreID: 4},
),
store.SetStorageStores(
&store.TableStore{StoreID: 4, Code: dbr.NullString{NullString: sql.NullString{String: "au", Valid: true}}, WebsiteID: 2, GroupID: 3, Name: "Australia", SortOrder: 10, 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},
),
)
stg, err := nsg.Store(config.ScopeCode("nz"))
assert.Nil(t, stg)
assert.EqualError(t, store.ErrGroupNotFound, err.Error())
stgs, err := nsg.Stores()
assert.Nil(t, stgs)
assert.EqualError(t, store.ErrGroupNotFound, err.Error())
}
示例12: TestNewManagerWebsite
func TestNewManagerWebsite(t *testing.T) {
var managerWebsite = getTestManager(func(ms *mockStorage) {
ms.w = func() (*store.Website, error) {
return store.NewWebsite(
&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
}
})
tests := []struct {
m *store.Manager
have config.ScopeIDer
wantErr error
wantWebsiteCode string
}{
{managerWebsite, nil, store.ErrAppStoreNotSet, ""},
{getTestManager(), config.ScopeID(20), store.ErrGroupNotFound, ""},
{managerWebsite, config.ScopeID(1), nil, "euro"},
{managerWebsite, config.ScopeID(1), nil, "euro"},
{managerWebsite, config.ScopeCode("notImportant"), nil, "euro"},
{managerWebsite, config.ScopeCode("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, managerWebsite.IsCacheEmpty())
managerWebsite.ClearCache()
assert.True(t, managerWebsite.IsCacheEmpty())
}
示例13: BenchmarkStorageStoreGetWebsite
// MBA mid 2012 CPU: Intel Core i5-3427U CPU @ 1.80GHz
// BenchmarkStorageStoreGetWebsite 2000000 656 ns/op 176 B/op 6 allocs/op
func BenchmarkStorageStoreGetWebsite(b *testing.B) {
for i := 0; i < b.N; i++ {
var err error
benchmarkStorageStore, err = testStorage.Store(config.ScopeCode("de"))
if err != nil {
b.Error(err)
}
benchmarkStorageStoreWebsite = benchmarkStorageStore.Website()
if benchmarkStorageStoreWebsite == nil {
b.Error("benchmarkStorageStoreWebsite is nil")
}
}
}
示例14: BenchmarkStorageWebsiteGetDefaultGroup
// MBA mid 2012 CPU: Intel Core i5-3427U CPU @ 1.80GHz
// BenchmarkStorageWebsiteGetDefaultGroup 200000 6081 ns/op 1712 B/op 45 allocs/op
func BenchmarkStorageWebsiteGetDefaultGroup(b *testing.B) {
for i := 0; i < b.N; i++ {
var err error
benchmarkStorageWebsite, err = testStorage.Website(config.ScopeCode("euro"))
if err != nil {
b.Error(err)
}
benchmarkStorageWebsiteDefaultGroup, err = benchmarkStorageWebsite.DefaultGroup()
if err != nil {
b.Error(err)
}
}
}
示例15: TestClaim
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)
}