Esempio n. 1
0
// NewStore creates a new Store. Returns an error if the first three arguments
// are nil. Returns an error if integrity checks fail. config.Reader will be
// also set to Group and Website.
func NewStore(ts *TableStore, tw *TableWebsite, tg *TableGroup, opts ...StoreOption) (s *Store, err error) {
	if ts == nil || tw == nil || tg == nil {
		return nil, ErrArgumentCannotBeNil
	}
	if ts.WebsiteID != tw.WebsiteID {
		return nil, ErrStoreIncorrectWebsite
	}
	if tg.WebsiteID != tw.WebsiteID {
		return nil, ErrStoreIncorrectWebsite
	}
	if ts.GroupID != tg.GroupID {
		return nil, ErrStoreIncorrectGroup
	}

	var nw *Website
	if nw, err = NewWebsite(tw); err != nil {
		if PkgLog.IsDebug() {
			PkgLog.Debug("store.NewStore.NewWebsite", "err", err, "tw", tw)
		}
		return
	}

	var ng *Group
	if ng, err = NewGroup(tg, SetGroupWebsite(tw)); err != nil {
		if PkgLog.IsDebug() {
			PkgLog.Debug("store.NewStore.NewGroup", "err", err, "tg", tg, "tw", tw)
		}
		return
	}

	s = &Store{
		cr:      config.DefaultService,
		Data:    ts,
		Website: nw,
		Group:   ng,
		urlcache: &struct {
			secure   *config.URLCache
			unsecure *config.URLCache
		}{
			secure:   config.NewURLCache(),
			unsecure: config.NewURLCache(),
		},
	}
	s.ApplyOptions(opts...)
	if _, err = s.Website.ApplyOptions(SetWebsiteConfig(s.cr)); err != nil {
		if PkgLog.IsDebug() {
			PkgLog.Debug("store.Website.ApplyOptions", "err", err, "tg", tg, "tw", tw)
		}
		return
	}
	if _, err = s.Group.ApplyOptions(SetGroupConfig(s.cr)); err != nil {
		if PkgLog.IsDebug() {
			PkgLog.Debug("store.Group.ApplyOptions", "err", err, "tg", tg, "tw", tw)
		}
		return
	}
	return s, nil
}
Esempio n. 2
0
func TestURLCache(t *testing.T) {
	tests := []struct {
		haveType  config.URLType
		url       string
		wantError error
	}{
		{config.URLTypeStatic, "", config.ErrURLEmpty},
		{config.URLTypeWeb, "http://corestore.io/", nil},
		{config.URLTypeStatic, "://corestore.io/", errors.New("parse ://corestore.io/: missing protocol scheme")},
		{config.URLType(254), "https://corestore.io/catalog", errors.New("Unknown Index 254")},
	}
	for i, test := range tests {
		uc := config.NewURLCache()

		if test.wantError != nil {
			pu, err := uc.Set(test.haveType, test.url)
			assert.Nil(t, pu, "Index %d", i)
			assert.EqualError(t, err, test.wantError.Error(), "Index %d", i)
			assert.Nil(t, uc.Get(test.haveType))
			continue
		}

		pu, err := uc.Set(test.haveType, test.url) // pu = parsed URL
		assert.NoError(t, err, "Index %d", i)
		assert.Exactly(t, test.url, pu.String(), "Index %d", i)

		puCache := uc.Get(test.haveType)
		assert.Exactly(t, test.url, puCache.String(), "Index %d", i)

		assert.EqualError(t, uc.Clear(), config.ErrURLCacheCleared.Error())
		assert.Nil(t, uc.Get(test.haveType), "Index %d", i)
	}
}