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?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No 

## Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Test
- [ ] 💻 CI/Deployment
- [x] 🧹 Tech Debt/Cleanup

## Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
*https://github.com/alcionai/corso/pull/2407

## Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
This commit is contained in:
Abin Simon 2023-02-09 22:52:23 +05:30 committed by GitHub
parent 057e0e97c0
commit 47d0eeb700
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 72 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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
}

View File

@ -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)
})
}
}