From 47d0eeb700834ae41eb9d2af864581a4f3bd259d Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Thu, 9 Feb 2023 22:52:23 +0530 Subject: [PATCH] Move stateOf to data (#2455) ## Description stateOf needs to be used in more places. Move it to a common location so as to expose it. ## Does this PR need a docs update or release note? - [ ] :white_check_mark: Yes, it's included - [ ] :clock1: Yes, but in a later PR - [x] :no_entry: No ## Type of change - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Test - [ ] :computer: CI/Deployment - [x] :broom: Tech Debt/Cleanup ## Issue(s) *https://github.com/alcionai/corso/pull/2407 ## Test Plan - [ ] :muscle: Manual - [x] :zap: Unit test - [ ] :green_heart: E2E --- .../exchange/exchange_data_collection.go | 18 +---- .../exchange/exchange_data_collection_test.go | 55 ---------------- src/internal/data/data_collection.go | 18 +++++ src/internal/data/data_collection_test.go | 66 +++++++++++++++++++ 4 files changed, 85 insertions(+), 72 deletions(-) create mode 100644 src/internal/data/data_collection_test.go diff --git a/src/internal/connector/exchange/exchange_data_collection.go b/src/internal/connector/exchange/exchange_data_collection.go index ecb85521b..07ce33b52 100644 --- a/src/internal/connector/exchange/exchange_data_collection.go +++ b/src/internal/connector/exchange/exchange_data_collection.go @@ -107,7 +107,7 @@ func NewCollection( added: make(map[string]struct{}, 0), removed: make(map[string]struct{}, 0), prevPath: prev, - state: stateOf(prev, curr), + state: data.StateOf(prev, curr), statusUpdater: statusUpdater, user: user, items: items, @@ -116,22 +116,6 @@ func NewCollection( return collection } -func stateOf(prev, curr path.Path) data.CollectionState { - if curr == nil || len(curr.String()) == 0 { - return data.DeletedState - } - - if prev == nil || len(prev.String()) == 0 { - return data.NewState - } - - if curr.Folder() != prev.Folder() { - return data.MovedState - } - - return data.NotMovedState -} - // Items utility function to asynchronously execute process to fill data channel with // M365 exchange objects and returns the data channel func (col *Collection) Items() <-chan data.Stream { diff --git a/src/internal/connector/exchange/exchange_data_collection_test.go b/src/internal/connector/exchange/exchange_data_collection_test.go index e45f3d80c..b327c70c8 100644 --- a/src/internal/connector/exchange/exchange_data_collection_test.go +++ b/src/internal/connector/exchange/exchange_data_collection_test.go @@ -12,10 +12,8 @@ import ( "github.com/alcionai/corso/src/internal/common" "github.com/alcionai/corso/src/internal/connector/graph" - "github.com/alcionai/corso/src/internal/data" "github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/pkg/backup/details" - "github.com/alcionai/corso/src/pkg/control" "github.com/alcionai/corso/src/pkg/path" ) @@ -118,59 +116,6 @@ func (suite *ExchangeDataCollectionSuite) TestExchangeDataCollection_NewExchange suite.Equal(fullPath, edc.FullPath()) } -func (suite *ExchangeDataCollectionSuite) TestNewCollection_state() { - fooP, err := path.Builder{}. - Append("foo"). - ToDataLayerExchangePathForCategory("t", "u", path.EmailCategory, false) - require.NoError(suite.T(), err) - barP, err := path.Builder{}. - Append("bar"). - ToDataLayerExchangePathForCategory("t", "u", path.EmailCategory, false) - require.NoError(suite.T(), err) - - table := []struct { - name string - prev path.Path - curr path.Path - expect data.CollectionState - }{ - { - name: "new", - curr: fooP, - expect: data.NewState, - }, - { - name: "not moved", - prev: fooP, - curr: fooP, - expect: data.NotMovedState, - }, - { - name: "moved", - prev: fooP, - curr: barP, - expect: data.MovedState, - }, - { - name: "deleted", - prev: fooP, - expect: data.DeletedState, - }, - } - for _, test := range table { - suite.T().Run(test.name, func(t *testing.T) { - c := NewCollection( - "u", - test.curr, test.prev, - 0, - &mockItemer{}, nil, - control.Options{}, - false) - assert.Equal(t, test.expect, c.State()) - }) - } -} - func (suite *ExchangeDataCollectionSuite) TestGetItemWithRetries() { table := []struct { name string diff --git a/src/internal/data/data_collection.go b/src/internal/data/data_collection.go index beeffd3d7..764a7b886 100644 --- a/src/internal/data/data_collection.go +++ b/src/internal/data/data_collection.go @@ -112,3 +112,21 @@ type StreamSize interface { type StreamModTime interface { ModTime() time.Time } + +// StateOf lets us figure out the state of the collection from the +// previous and current path +func StateOf(prev, curr path.Path) CollectionState { + if curr == nil || len(curr.String()) == 0 { + return DeletedState + } + + if prev == nil || len(prev.String()) == 0 { + return NewState + } + + if curr.Folder() != prev.Folder() { + return MovedState + } + + return NotMovedState +} diff --git a/src/internal/data/data_collection_test.go b/src/internal/data/data_collection_test.go new file mode 100644 index 000000000..9ca093032 --- /dev/null +++ b/src/internal/data/data_collection_test.go @@ -0,0 +1,66 @@ +package data + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + "github.com/alcionai/corso/src/pkg/path" +) + +type DataCollectionSuite struct { + suite.Suite +} + +func TestDataCollectionSuite(t *testing.T) { + suite.Run(t, new(DataCollectionSuite)) +} + +func (suite *DataCollectionSuite) TestStateOf() { + fooP, err := path.Builder{}. + Append("foo"). + ToDataLayerExchangePathForCategory("t", "u", path.EmailCategory, false) + require.NoError(suite.T(), err) + barP, err := path.Builder{}. + Append("bar"). + ToDataLayerExchangePathForCategory("t", "u", path.EmailCategory, false) + require.NoError(suite.T(), err) + + table := []struct { + name string + prev path.Path + curr path.Path + expect CollectionState + }{ + { + name: "new", + curr: fooP, + expect: NewState, + }, + { + name: "not moved", + prev: fooP, + curr: fooP, + expect: NotMovedState, + }, + { + name: "moved", + prev: fooP, + curr: barP, + expect: MovedState, + }, + { + name: "deleted", + prev: fooP, + expect: DeletedState, + }, + } + for _, test := range table { + suite.T().Run(test.name, func(t *testing.T) { + state := StateOf(test.prev, test.curr) + assert.Equal(t, test.expect, state) + }) + } +}