Exemple #1
0
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(scope.Option{Group: scope.MockID(1)})
	assert.EqualError(t, store.ErrGroupDefaultStoreNotFound, err.Error(), "Incorrect DefaultStore for a Group")

	err = getTestManager().Init(scope.Option{Group: scope.MockID(21)})
	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(scope.Option{Group: scope.MockID(1)})
	assert.NoError(t, err)
	g, err := tm3.Group()
	assert.NoError(t, err)
	assert.NotNil(t, g)
	assert.Equal(t, int64(2), g.Data.DefaultStoreID)
}
Exemple #2
0
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      scope.StoreIDer
		wantErr     error
	}{
		{tms, scope.MockID(1), nil},
		{tms, scope.MockID(1), store.ErrAppStoreSet},
		{tms, nil, store.ErrAppStoreSet},
		{tms, nil, store.ErrAppStoreSet},
	}

	for _, test := range tests {
		haveErr := test.haveManager.Init(scope.Option{Store: test.haveID})
		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)
	}
}
Exemple #3
0
// Store creates a new Store which contains the the store, its group and website
// according to the interface definition.
func (st *Storage) Store(r scope.StoreIDer) (*Store, error) {
	s, err := st.store(r)
	if err != nil {
		return nil, errgo.Mask(err)
	}
	w, err := st.website(scope.MockID(s.WebsiteID))
	if err != nil {
		return nil, errgo.Mask(err)
	}
	g, err := st.group(scope.MockID(s.GroupID))
	if err != nil {
		return nil, errgo.Mask(err)
	}
	ns, err := NewStore(s, w, g, SetStoreConfig(st.cr))
	if err != nil {
		return nil, errgo.Mask(err)
	}
	if _, err := ns.Website.ApplyOptions(SetWebsiteGroupsStores(st.groups, st.stores)); err != nil {
		return nil, errgo.Mask(err)
	}
	if _, err := ns.Group.ApplyOptions(SetGroupStores(st.stores, w)); err != nil {
		return nil, errgo.Mask(err)
	}
	return ns, nil
}
Exemple #4
0
func TestNewManagerGetRequestStore_ScopeGroup(t *testing.T) {
	testOption := scope.Option{Group: scope.MockID(1)}
	testScope := scope.GroupID

	if haveStore, haveErr := storeManagerRequestStore.GetRequestStore(testOption, 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.ErrIDNotFoundTableGroupSlice, storeManagerRequestStore.Init(scope.Option{Group: scope.MockID(123)}).Error())
	if err := storeManagerRequestStore.Init(testOption); err != nil {
		t.Error(err)
		t.Fail()
	}
	assert.EqualError(t, store.ErrAppStoreSet, storeManagerRequestStore.Init(testOption).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 g, err := storeManagerRequestStore.Group(); err == nil {
		assert.EqualValues(t, 1, g.Data.GroupID)
	} else {
		assert.EqualError(t, err, store.ErrStoreNotFound.Error())
		t.Fail()
	}

	// we're testing here against Group ID = 1
	tests := []testNewManagerGetRequestStore{
		{scope.Option{Group: scope.MockID(232)}, "", store.ErrIDNotFoundTableGroupSlice},
		{scope.Option{Store: scope.MockID(232)}, "", store.ErrIDNotFoundTableStoreSlice},
		{scope.Option{}, "", store.ErrUnsupportedScope},
		{scope.Option{Store: scope.MockCode("\U0001f631")}, "", store.ErrIDNotFoundTableStoreSlice},

		{scope.Option{Store: scope.MockID(6)}, "nz", store.ErrStoreChangeNotAllowed},
		{scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive},

		{scope.Option{Store: scope.MockCode("de")}, "de", nil},
		{scope.Option{Store: scope.MockID(2)}, "at", nil},

		{scope.Option{Store: scope.MockID(2)}, "at", nil},
		{scope.Option{Store: scope.MockCode("au")}, "au", store.ErrStoreChangeNotAllowed},
		{scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive},

		{scope.Option{Group: scope.MockCode("ch")}, "", store.ErrIDNotFoundTableGroupSlice},
		{scope.Option{Group: scope.MockID(2)}, "", store.ErrStoreChangeNotAllowed},
		{scope.Option{Group: scope.MockID(1)}, "at", nil},

		{scope.Option{Website: scope.MockCode("xxxx")}, "", store.ErrIDNotFoundTableWebsiteSlice},
		{scope.Option{Website: scope.MockID(2)}, "", store.ErrStoreChangeNotAllowed},
		{scope.Option{Website: scope.MockID(1)}, "at", nil},
	}
	runNewManagerGetRequestStore(t, scope.GroupID, tests)
}
Exemple #5
0
// DefaultStoreView traverses through the websites to find the default website and gets
// the default group which has the default store id assigned to. Only one website can be the default one.
func (st *Storage) DefaultStoreView() (*Store, error) {
	for _, website := range st.websites {
		if website.IsDefault.Bool && website.IsDefault.Valid {
			g, err := st.group(scope.MockID(website.DefaultGroupID))
			if err != nil {
				return nil, errgo.Mask(err)
			}
			return st.Store(scope.MockID(g.DefaultStoreID))
		}
	}
	return nil, ErrStoreNotFound
}
Exemple #6
0
func TestNewServiceRequestedStore_ScopeGroup(t *testing.T) {
	initScope := scope.Option{Group: scope.MockID(1)}

	sm := getInitializedStoreService(initScope)
	if haveStore, haveErr := sm.RequestedStore(initScope); haveErr != nil {
		t.Fatal(haveErr)
	} else {
		assert.NoError(t, haveErr)
		assert.Exactly(t, int64(2), haveStore.StoreID())
	}

	if s, err := sm.Store(); err == nil {
		assert.EqualValues(t, "at", s.Data.Code.String)
	} else {
		assert.EqualError(t, err, store.ErrStoreNotFound.Error())
		t.Fail()
	}

	if g, err := sm.Group(); err == nil {
		assert.EqualValues(t, 1, g.Data.GroupID)
	} else {
		assert.EqualError(t, err, store.ErrStoreNotFound.Error())
		t.Fail()
	}

	//	// we're testing here against Group ID = 1
	tests := []testNewServiceRequestedStore{
		{scope.Option{Group: scope.MockID(232)}, "", store.ErrIDNotFoundTableGroupSlice},

		{scope.Option{Store: scope.MockID(232)}, "", store.ErrIDNotFoundTableStoreSlice},
		{scope.Option{Store: scope.MockCode("\U0001f631")}, "", store.ErrIDNotFoundTableStoreSlice},

		{scope.Option{Store: scope.MockID(6)}, "nz", store.ErrStoreChangeNotAllowed},
		{scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive},

		{scope.Option{Store: scope.MockCode("de")}, "de", nil},
		{scope.Option{Store: scope.MockID(2)}, "at", nil},

		{scope.Option{Store: scope.MockID(2)}, "at", nil},
		{scope.Option{Store: scope.MockCode("au")}, "au", store.ErrStoreChangeNotAllowed},
		{scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive},

		{scope.Option{Group: scope.MockCode("ch")}, "", store.ErrIDNotFoundTableGroupSlice},
		{scope.Option{Group: scope.MockID(2)}, "", store.ErrStoreChangeNotAllowed},
		{scope.Option{Group: scope.MockID(1)}, "at", nil},

		{scope.Option{Website: scope.MockCode("xxxx")}, "", store.ErrIDNotFoundTableWebsiteSlice},
		{scope.Option{Website: scope.MockID(2)}, "", store.ErrStoreChangeNotAllowed},
		{scope.Option{Website: scope.MockID(1)}, "at", nil},
	}
	runTestsRequestedStore(t, sm, tests)
}
Exemple #7
0
func TestApplyID(t *testing.T) {
	tests := []struct {
		wantWebsiteID scope.WebsiteIDer
		wantGroupID   scope.GroupIDer
		wantStoreID   scope.StoreIDer

		haveID int64
		s      scope.Scope
		err    error
	}{
		{scope.MockID(1), nil, nil, 1, scope.WebsiteID, nil},
		{nil, scope.MockID(3), nil, 3, scope.GroupID, nil},
		{nil, nil, scope.MockID(2), 2, scope.StoreID, nil},
		{nil, nil, nil, 4, scope.AbsentID, scope.ErrUnsupportedScope},
	}

	for _, test := range tests {
		so, err := scope.SetByID(test.haveID, test.s)
		assert.NotNil(t, so)
		if test.err != nil {
			assert.EqualError(t, err, test.err.Error())
			assert.Nil(t, so.Website)
			assert.Nil(t, so.Group)
			assert.Nil(t, so.Store)
		} else {
			assert.NoError(t, err)
			assert.Equal(t, test.s, so.Scope())
			assert.Equal(t, "", so.StoreCode())
			assert.Equal(t, "", so.WebsiteCode())

			if test.wantWebsiteID != nil {
				assert.Equal(t, test.wantWebsiteID.WebsiteID(), so.Website.WebsiteID())
			} else {
				assert.Nil(t, test.wantWebsiteID)
			}

			if test.wantGroupID != nil {
				assert.Equal(t, test.wantGroupID.GroupID(), so.Group.GroupID())
			} else {
				assert.Nil(t, test.wantGroupID)
			}

			if test.wantStoreID != nil {
				assert.Equal(t, test.wantStoreID.StoreID(), so.Store.StoreID())
			} else {
				assert.Nil(t, test.wantStoreID)
			}

		}
	}
}
Exemple #8
0
func TestNewManagerStore(t *testing.T) {
	assert.True(t, managerStoreSimpleTest.IsCacheEmpty())
	for j := 0; j < 3; j++ {
		s, err := managerStoreSimpleTest.Store(scope.MockCode("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    scope.StoreIDer
		wantErr error
	}{
		{scope.MockCode("nilSlices"), store.ErrStoreNotFound},
		{scope.MockID(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())
}
Exemple #9
0
func TestMustNewService(t *testing.T) {
	defer func() {
		if r := recover(); r != nil {
			assert.EqualError(t, r.(error), store.ErrStoreNotFound.Error())
		} else {
			t.Fatal("Expecting a Panic")
		}
	}()
	tests := []struct {
		have    scope.StoreIDer
		wantErr error
	}{
		{scope.MockCode("nilSlices"), store.ErrStoreNotFound},
		{scope.MockID(2), store.ErrStoreNotFound},
		{nil, store.ErrStoreNotFound},
	}
	serviceEmpty := storemock.MustNewService(scope.Option{})
	for _, test := range tests {
		s, err := serviceEmpty.Store(test.have)
		assert.Nil(t, s)
		assert.EqualError(t, test.wantErr, err.Error())
	}
	assert.True(t, serviceStoreSimpleTest.IsCacheEmpty())

}
Exemple #10
0
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            scope.GroupIDer
		wantErr         error
		wantGroupName   string
		wantWebsiteCode string
	}{
		{managerGroupSimpleTest, nil, store.ErrAppStoreNotSet, "", ""},
		{getTestManager(), scope.MockID(20), store.ErrGroupNotFound, "", ""},
		{managerGroupSimpleTest, scope.MockID(1), nil, "DACH Group", "euro"},
		{managerGroupSimpleTest, scope.MockID(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())
}
Exemple #11
0
func TestApplyWebsite(t *testing.T) {

	so := scope.Option{Website: scope.MockID(3)}
	assert.NotNil(t, so)
	assert.Equal(t, int64(3), so.Website.WebsiteID())
	assert.Nil(t, so.Group)
	assert.Nil(t, so.Store)
}
Exemple #12
0
func TestApplyStore(t *testing.T) {
	so := scope.Option{Store: scope.MockID(3)}
	assert.NotNil(t, so)
	assert.Equal(t, int64(3), so.Store.StoreID())
	assert.Nil(t, so.Website)
	assert.Nil(t, so.Group)
	assert.Exactly(t, scope.StoreID.String(), so.String())
}
Exemple #13
0
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())

}
Exemple #14
0
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())
}
Exemple #15
0
func TestNewManagerGetRequestStore_ScopeStore(t *testing.T) {

	testCode := scope.MockCode("de")
	testScope := scope.StoreID

	if haveStore, haveErr := storeManagerRequestStore.GetRequestStore(scope.Option{Store: scope.MockID(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())
	}

	// init with scope store
	if err := storeManagerRequestStore.Init(scope.Option{Store: testCode}); err != nil {
		t.Error(err)
		t.Fail()
	}
	assert.EqualError(t, store.ErrAppStoreSet, storeManagerRequestStore.Init(scope.Option{Store: testCode}).Error())

	if s, err := storeManagerRequestStore.Store(); err == nil {
		assert.EqualValues(t, "de", s.Data.Code.String)
	} else {
		assert.EqualError(t, err, store.ErrStoreNotFound.Error())
		t.Fail()
	}

	tests := []testNewManagerGetRequestStore{
		{scope.Option{Store: scope.MockID(232)}, "", store.ErrIDNotFoundTableStoreSlice},
		{scope.Option{}, "", store.ErrUnsupportedScope},
		{scope.Option{Store: scope.MockCode("\U0001f631")}, "", store.ErrIDNotFoundTableStoreSlice},

		{scope.Option{Store: scope.MockID(6)}, "nz", nil},
		{scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive},

		{scope.Option{Store: scope.MockCode("nz")}, "nz", nil},
		{scope.Option{Store: scope.MockCode("de")}, "de", nil},
		{scope.Option{Store: scope.MockID(2)}, "at", nil},

		{scope.Option{Store: scope.MockID(2)}, "at", nil},
		{scope.Option{Store: scope.MockCode("au")}, "au", nil},
		{scope.Option{Store: scope.MockCode("ch")}, "", store.ErrStoreNotActive},
	}
	runNewManagerGetRequestStore(t, testScope, tests)
}
Exemple #16
0
func TestApplyGroup(t *testing.T) {

	so := scope.Option{Group: scope.MockID(3)}
	assert.NotNil(t, so)

	assert.Equal(t, int64(3), so.Group.GroupID())
	assert.Nil(t, so.Website)
	assert.Nil(t, so.Store)
}
Exemple #17
0
func TestNewManagerReInit(t *testing.T) {

	t.Skip(TODO_Better_Test_Data)

	// quick implement, use mock of dbr.SessionRunner and remove connection
	dbc := csdb.MustConnectTest()
	defer dbc.Close()
	dbrSess := dbc.NewSession()

	storeManager := store.NewManager(store.NewStorage(nil /* trick it*/))
	if err := storeManager.ReInit(dbrSess); err != nil {
		t.Fatal(err)
	}

	tests := []struct {
		have    scope.StoreIDer
		wantErr error
	}{
		{scope.MockCode("dede"), nil},
		{scope.MockCode("czcz"), store.ErrIDNotFoundTableStoreSlice},
		{scope.MockID(1), nil},
		{scope.MockID(100), store.ErrStoreNotFound},
		{mockIDCode{1, "dede"}, nil},
		{mockIDCode{2, "czfr"}, 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, "No Err; for test: %#v", test)
			assert.NotNil(t, s)
			//			assert.NotEmpty(t, s.Data.Code.String, "%#v", s.Data)
		} else {
			assert.Error(t, err, "Err for test: %#v", test)
			assert.EqualError(t, test.wantErr, err.Error(), "EqualErr for test: %#v", test)
			assert.Nil(t, s)
		}
	}
	assert.False(t, storeManager.IsCacheEmpty())
	storeManager.ClearCache()
	assert.True(t, storeManager.IsCacheEmpty())
}
Exemple #18
0
// Stores creates a new store slice. Can return an error when the website or
// the group cannot be found.
func (st *Storage) Stores() (StoreSlice, error) {
	stores := make(StoreSlice, len(st.stores), len(st.stores))
	for i, s := range st.stores {
		var err error
		if stores[i], err = st.Store(scope.MockID(s.StoreID)); err != nil {
			return nil, errgo.Mask(err)
		}
	}
	return stores, nil
}
Exemple #19
0
// Groups creates a new group slice containing its website all related stores.
// May panic when a website pointer is nil.
func (st *Storage) Groups() (GroupSlice, error) {
	groups := make(GroupSlice, len(st.groups), len(st.groups))
	for i, g := range st.groups {
		w, err := st.website(scope.MockID(g.WebsiteID))
		if err != nil {
			return nil, errgo.Mask(err)
		}
		groups[i] = NewGroup(g, SetGroupConfig(st.cr), SetGroupWebsite(w)).SetStores(st.stores, nil)
	}
	return groups, nil
}
Exemple #20
0
func TestStorageWebsite(t *testing.T) {

	tests := []struct {
		have      scope.WebsiteIDer
		err       error
		wantWCode string
	}{
		{nil, store.ErrWebsiteNotFound, ""},
		{scope.MockID(2015), store.ErrIDNotFoundTableWebsiteSlice, ""},
		{scope.MockID(1), nil, "euro"},
		{scope.MockCode("asia"), store.ErrIDNotFoundTableWebsiteSlice, ""},
		{scope.MockCode("oz"), nil, "oz"},
		{mockIDCode{1, "oz"}, nil, "oz"},
		{mockIDCode{1, "ozzz"}, store.ErrIDNotFoundTableWebsiteSlice, ""},
	}
	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(scope.MockCode("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)

	assert.NotNil(t, w.Groups)
	assert.EqualValues(t, utils.Int64Slice{1, 2}, w.Groups.IDs())

	assert.NotNil(t, w.Stores)
	assert.EqualValues(t, utils.StringSlice{"de", "uk", "at", "ch"}, w.Stores.Codes())
}
Exemple #21
0
func TestStorageStore(t *testing.T) {

	tests := []struct {
		have     scope.StoreIDer
		err      error
		wantCode string
	}{
		{nil, store.ErrStoreNotFound, ""},
		{scope.MockID(2015), store.ErrIDNotFoundTableStoreSlice, ""},
		{scope.MockID(1), nil, "de"},
		{scope.MockCode("asia"), store.ErrIDNotFoundTableStoreSlice, ""},
		{scope.MockCode("nz"), nil, "nz"},
		{mockIDCode{4, "nz"}, nil, "nz"},
		{mockIDCode{4, "auuuuu"}, store.ErrIDNotFoundTableStoreSlice, ""},
	}
	for i, test := range tests {
		s, err := testStorage.Store(test.have)
		if test.err != nil {
			assert.Nil(t, s, "%#v", test)
			assert.EqualError(t, err, test.err.Error(), "Index: %d", i)
		} 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(scope.MockCode("at"))
	assert.NoError(t, err)
	assert.NotNil(t, s)

	assert.EqualValues(t, "DACH Group", s.Group.Data.Name)

	assert.EqualValues(t, "euro", s.Website.Data.Code.String)
	wg, err := s.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)
}
Exemple #22
0
func TestWithInitStoreByToken(t *testing.T) {

	var newReq = func(i int) *http.Request {
		req, err := http.NewRequest(httputils.MethodGet, fmt.Sprintf("https://corestore.io/store/list/%d", i), nil)
		if err != nil {
			t.Fatal(err)
		}
		return req
	}

	tests := []struct {
		ctx           context.Context
		wantStoreCode string
		wantErr       error
	}{
		{store.NewContextReader(context.Background(), nil), "de", store.ErrContextServiceNotFound},
		{store.NewContextReader(context.Background(), getInitializedStoreService(scope.Option{Store: scope.MockCode("de")})), "de", ctxjwt.ErrContextJWTNotFound},
		{newStoreServiceWithTokenCtx(scope.Option{Store: scope.MockCode("de")}, "de"), "de", nil},
		{newStoreServiceWithTokenCtx(scope.Option{Store: scope.MockCode("at")}, "ch"), "at", store.ErrStoreNotActive},
		{newStoreServiceWithTokenCtx(scope.Option{Store: scope.MockCode("de")}, "at"), "at", nil},
		{newStoreServiceWithTokenCtx(scope.Option{Store: scope.MockCode("de")}, "a$t"), "de", store.ErrStoreCodeInvalid},
		{newStoreServiceWithTokenCtx(scope.Option{Store: scope.MockCode("at")}, ""), "at", store.ErrStoreCodeInvalid},

		{newStoreServiceWithTokenCtx(scope.Option{Group: scope.MockID(1)}, "de"), "de", nil},
		{newStoreServiceWithTokenCtx(scope.Option{Group: scope.MockID(1)}, "ch"), "at", store.ErrStoreNotActive},
		{newStoreServiceWithTokenCtx(scope.Option{Group: scope.MockID(1)}, " ch"), "at", store.ErrStoreCodeInvalid},
		{newStoreServiceWithTokenCtx(scope.Option{Group: scope.MockID(1)}, "uk"), "at", store.ErrStoreChangeNotAllowed},

		{newStoreServiceWithTokenCtx(scope.Option{Website: scope.MockID(2)}, "uk"), "au", store.ErrStoreChangeNotAllowed},
		{newStoreServiceWithTokenCtx(scope.Option{Website: scope.MockID(2)}, "nz"), "nz", nil},
		{newStoreServiceWithTokenCtx(scope.Option{Website: scope.MockID(2)}, "n z"), "au", store.ErrStoreCodeInvalid},
		{newStoreServiceWithTokenCtx(scope.Option{Website: scope.MockID(2)}, "au"), "au", nil},
		{newStoreServiceWithTokenCtx(scope.Option{Website: scope.MockID(2)}, ""), "au", store.ErrStoreCodeInvalid},
	}
	for i, test := range tests {

		mw := store.WithInitStoreByToken()(finalInitStoreHandler(t, test.wantStoreCode))
		rec := httptest.NewRecorder()
		surfErr := mw.ServeHTTPContext(test.ctx, rec, newReq(i))
		if test.wantErr != nil {
			assert.EqualError(t, surfErr, test.wantErr.Error(), "Index %d", i)
			continue
		}
		assert.NoError(t, surfErr, "Index %d", i)
	}
}
Exemple #23
0
// Group creates a new Group which contains all related stores and its website according to the
// interface definition.
func (st *Storage) Group(id scope.GroupIDer) (*Group, error) {
	g, err := st.group(id)
	if err != nil {
		return nil, err
	}

	w, err := st.website(scope.MockID(g.WebsiteID))
	if err != nil {
		return nil, err
	}
	return NewGroup(g, SetGroupWebsite(w), SetGroupConfig(st.cr)).SetStores(st.stores, nil), nil
}
Exemple #24
0
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            scope.WebsiteIDer
		wantErr         error
		wantWebsiteCode string
	}{
		{managerWebsite, nil, store.ErrAppStoreNotSet, ""},
		{getTestManager(), scope.MockID(20), store.ErrGroupNotFound, ""},
		{managerWebsite, scope.MockID(1), nil, "euro"},
		{managerWebsite, scope.MockID(1), nil, "euro"},
		{managerWebsite, scope.MockCode("notImportant"), nil, "euro"},
		{managerWebsite, 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, managerWebsite.IsCacheEmpty())
	managerWebsite.ClearCache()
	assert.True(t, managerWebsite.IsCacheEmpty())

}
Exemple #25
0
// MBA mid 2012 CPU: Intel Core i5-3427U CPU @ 1.80GHz
// BenchmarkStorageGroupGetDefaultStore	 1000000	      1916 ns/op	     464 B/op	      14 allocs/op
func BenchmarkStorageGroupGetDefaultStore(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var err error
		benchmarkStorageGroup, err = testStorage.Group(scope.MockID(3))
		if err != nil {
			b.Error(err)
		}

		benchmarkStorageGroupDefaultStore, err = benchmarkStorageGroup.DefaultStore()
		if err != nil {
			b.Error(err)
		}
	}
}
Exemple #26
0
// Group creates a new Group which contains all related stores and its website according to the
// interface definition.
func (st *Storage) Group(id scope.GroupIDer) (*Group, error) {
	g, err := st.group(id)
	if err != nil {
		return nil, errgo.Mask(err)
	}

	w, err := st.website(scope.MockID(g.WebsiteID))
	if err != nil {
		if PkgLog.IsDebug() {
			PkgLog.Debug("store.Storage.Group.website", "err", err, "websiteID", g.WebsiteID, "groupID", id.GroupID())
		}
		return nil, errgo.Mask(err)
	}
	return NewGroup(g, SetGroupConfig(st.cr), SetGroupWebsite(w), SetGroupStores(st.stores, nil))
}
Exemple #27
0
func TestStorageGroup(t *testing.T) {

	tests := []struct {
		id       scope.GroupIDer
		err      error
		wantName string
	}{
		{nil, store.ErrGroupNotFound, ""},
		{scope.MockID(2015), store.ErrIDNotFoundTableGroupSlice, ""},
		{scope.MockID(1), nil, "DACH Group"},
	}
	for _, test := range tests {
		g, err := testStorage.Group(test.id)
		if test.err != nil {
			assert.Nil(t, g)
			assert.EqualError(t, err, test.err.Error())
		} else {
			assert.NotNil(t, g)
			assert.NoError(t, err)
			assert.Equal(t, test.wantName, g.Data.Name)
		}
	}

	g, err := testStorage.Group(scope.MockID(3))
	assert.NoError(t, err)
	assert.NotNil(t, g)

	dStore, err := g.DefaultStore()
	assert.NoError(t, err)
	assert.EqualValues(t, "au", dStore.Data.Code.String)

	assert.EqualValues(t, "oz", g.Website.Data.Code.String)

	assert.NotNil(t, g.Stores)
	assert.EqualValues(t, utils.StringSlice{"au", "nz"}, g.Stores.Codes())
}
Exemple #28
0
func TestWithInitStoreByToken_EqualPointers(t *testing.T) {
	// this Test is related to Benchmark_WithInitStoreByToken
	// The returned pointers from store.FromContextReader must be the
	// same for each request with the same request pattern.

	ctx := newStoreServiceWithTokenCtx(scope.Option{Website: scope.MockID(2)}, "nz")
	rec := httptest.NewRecorder()
	req, err := http.NewRequest(httputils.MethodGet, "https://corestore.io/store/list", nil)
	if err != nil {
		t.Fatal(err)
	}

	var equalStorePointer *store.Store
	mw := store.WithInitStoreByToken()(ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
		_, haveReqStore, err := store.FromContextReader(ctx)
		if err != nil {
			return err
		}

		if equalStorePointer == nil {
			equalStorePointer = haveReqStore
		}

		if "nz" != haveReqStore.StoreCode() {
			t.Errorf("Have: %s\nWant: nz", haveReqStore.StoreCode())
		}

		wantP := reflect.ValueOf(equalStorePointer)
		haveP := reflect.ValueOf(haveReqStore)

		if wantP.Pointer() != haveP.Pointer() {
			t.Errorf("Expecting equal pointers for each request.\nWant: %p\nHave: %p", equalStorePointer, haveReqStore)
		}

		return nil
	}))

	for i := 0; i < 10; i++ {
		if err := mw.ServeHTTPContext(ctx, rec, req); err != nil {
			t.Error(err)
		}
	}
}
Exemple #29
0
func TestStorageGroupNoWebsite(t *testing.T) {
	var tst = store.NewStorage(
		store.SetStorageWebsites(
			&store.TableWebsite{WebsiteID: 21, 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: 3, WebsiteID: 2, Name: "Australia", RootCategoryID: 2, DefaultStoreID: 5},
		),
		store.SetStorageStores(
			&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: 6, Code: dbr.NullString{NullString: sql.NullString{String: "nz", Valid: true}}, WebsiteID: 2, GroupID: 3, Name: "Kiwi", SortOrder: 30, IsActive: true},
		),
	)
	g, err := tst.Group(scope.MockID(3))
	assert.Nil(t, g)
	assert.EqualError(t, store.ErrIDNotFoundTableWebsiteSlice, err.Error())

	gs, err := tst.Groups()
	assert.Nil(t, gs)
	assert.EqualError(t, store.ErrIDNotFoundTableWebsiteSlice, err.Error())
}
Exemple #30
0
// Groups creates a new group slice containing its website all related stores.
// May panic when a website pointer is nil.
func (st *Storage) Groups() (GroupSlice, error) {
	groups := make(GroupSlice, len(st.groups), len(st.groups))
	for i, g := range st.groups {
		w, err := st.website(scope.MockID(g.WebsiteID))
		if err != nil {
			if PkgLog.IsDebug() {
				PkgLog.Debug("store.Storage.Groups.website", "err", err, "g", g, "websiteID", g.WebsiteID)
			}
			return nil, errgo.Mask(err)
		}

		groups[i], err = NewGroup(g, SetGroupConfig(st.cr), SetGroupWebsite(w), SetGroupStores(st.stores, nil))
		if err != nil {
			if PkgLog.IsDebug() {
				PkgLog.Debug("store.Storage.Groups.NewGroup", "err", err, "g", g, "websiteID", g.WebsiteID)
			}
			return nil, errgo.Mask(err)
		}
	}
	return groups, nil
}