Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLOUDP-267015: [Unit Tests] Migrate test_merge_changelog_2_versions_with_deprecations to foascli #159

Merged
merged 6 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions tools/cli/internal/changelog/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func newChangeTypePriority() map[string]int {
return map[string]int{
changeTypeRemove: 1,
changeTypeRelease: 1,
changeTypeUpdate: 2,
changeTypeDeprecated: 3,
changeTypeUpdate: 3,
changeTypeDeprecated: 2,
}
}

Expand Down Expand Up @@ -163,43 +163,35 @@ func (m *Metadata) newPathsFromChanges(
changes []*outputfilter.OasDiffEntry,
changeType string, entry *Entry,
conf map[string]*outputfilter.OperationConfigs) ([]*Path, error) {
paths := make([]*Path, 0)
deprecatedPaths, err := m.newPathsFromDeprecatedChanges(changes, entry, conf)
deprecatedPaths, err := m.newPathsFromDeprecatedChanges(changes, &entry.Paths, conf)
if err != nil {
return nil, err
}

if len(deprecatedPaths) > 0 {
paths = append(paths, deprecatedPaths...)
}

revisionPaths, err := m.newPathsFromRevisionChanges(changes, changeType, entry, conf)
paths, err := m.newPathsFromRevisionChanges(changes, changeType, &deprecatedPaths, conf)
if err != nil {
return nil, err
}

if len(revisionPaths) > 0 {
paths = append(paths, revisionPaths...)
}
return paths, nil
}

// newPathsFromRevisionChanges creates new paths from revision changes
func (m *Metadata) newPathsFromRevisionChanges(
changes []*outputfilter.OasDiffEntry,
changeType string, entry *Entry,
changeType string, changelogPath *[]*Path,
conf map[string]*outputfilter.OperationConfigs) ([]*Path, error) {
revisionChanges := m.newRevisionChanges(changes)
return newMergedChanges(revisionChanges, changeType, m.Revision.Version, &entry.Paths, conf)
return newMergedChanges(revisionChanges, changeType, m.Revision.Version, changelogPath, conf)
}

// newPathsFromDeprecatedChanges creates new paths from deprecated changes
func (m *Metadata) newPathsFromDeprecatedChanges(
changes []*outputfilter.OasDiffEntry,
entry *Entry,
changelogPath *[]*Path,
conf map[string]*outputfilter.OperationConfigs) ([]*Path, error) {
depreactedChanges := m.newDeprecatedByNewerVersionChanges(changes, conf)
return newMergedChanges(depreactedChanges, changeTypeDeprecated, m.Base.Version, &entry.Paths, conf)
return newMergedChanges(depreactedChanges, changeTypeDeprecated, m.Base.Version, changelogPath, conf)
}

func (m *Metadata) newOasDiffEntries() ([]*outputfilter.OasDiffEntry, error) {
Expand Down Expand Up @@ -244,7 +236,7 @@ func newMergedChanges(changes []*outputfilter.OasDiffEntry,
changeType, version string, changelogPath *[]*Path,
operationConfig map[string]*outputfilter.OperationConfigs) ([]*Path, error) {
if len(changes) == 0 {
return []*Path{}, nil
return *changelogPath, nil
}

for _, change := range changes {
Expand All @@ -259,7 +251,7 @@ func newMergedChanges(changes []*outputfilter.OasDiffEntry,
pathEntry.OperationID = operationdID
pathEntry.Tag = conf.Tag()

pathEntryVersion := newEntryVersion(pathEntry.Versions, version)
pathEntryVersion := newEntryVersion(&pathEntry.Versions, version)
pathEntryVersion.StabilityLevel = stabilityLevelStable
pathEntryVersion.ChangeType = newChangeType(pathEntryVersion.ChangeType, changeType, change.ID)

Expand All @@ -271,7 +263,6 @@ func newMergedChanges(changes []*outputfilter.OasDiffEntry,
}

pathEntryVersion.Changes = append(pathEntryVersion.Changes, versionChange)
pathEntry.Versions = append(pathEntry.Versions, pathEntryVersion)
}

return *changelogPath, nil
Expand Down Expand Up @@ -307,7 +298,8 @@ func (m *Metadata) newDeprecatedByNewerVersionChanges(
continue
}

newChanges = append(newChanges, newDeprecatedChangeEntry(change, m.Base.Version, m.Revision.Version, operationConfig))
newChanges = append(newChanges,
newDeprecatedChangeEntry(change, m.Base.Version, m.Revision.Version, operationConfig))
}

return newChanges
Expand All @@ -332,7 +324,12 @@ func newDeprecatedChangeEntry(
Text: fmt.Sprintf(
"New resource added {%s}. Resource version {%s} and marked for removal on %s",
revisionVersion, baseVersionSunset, baseVersion),
Level: change.Level,
Level: change.Level,
Path: change.Path,
HideFromChangelog: change.HideFromChangelog,
Date: change.Date,
Source: change.Source,
Section: change.Section,
}
}

Expand All @@ -350,16 +347,18 @@ func newChangeType(currentChangeType, newChangeType, changeCode string) string {
return currentChangeType
}

func newEntryVersion(versions []*Version, specVersion string) *Version {
for _, version := range versions {
func newEntryVersion(versions *[]*Version, specVersion string) *Version {
for _, version := range *versions {
if version.Version == specVersion {
return version
}
}

return &Version{
newVersion := []*Version{{
Version: specVersion,
}
}}
*versions = append(newVersion, *versions...)
return (*versions)[0]
}

// newPathEntry returns the index and the path entry if it already exists in the changelog
Expand Down
137 changes: 131 additions & 6 deletions tools/cli/internal/changelog/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestMergeChangelogTwoVersionsNoDeprecations(t *testing.T) {
changelog, err = changelogMetadataSecondVersion.mergeChangelog(changeTypeSecondVersion, changesSecondVersion, endpointsConfig)
require.NoError(t, err)

assert.Len(t, changelog, 2, fmt.Sprintf("merged changelog should have 2 entries, got %d", len(changelog)))
require.Len(t, changelog, 2, fmt.Sprintf("merged changelog should have 2 entries, got %d", len(changelog)))
assert.Equal(t, changelog[0].Date, runDate, "merged changelog should have entries for the run date")
assert.Equal(t, changelog[1].Date, lastChangelogRunDate, fmt.Sprintf("merged changelog should have entries for the %s", lastChangelogRunDate))

Expand All @@ -196,17 +196,17 @@ func TestMergeChangelogTwoVersionsNoDeprecations(t *testing.T) {
assert.Equal(t, "Multi-Cloud Clusters", paths[0].Tag)

versions := paths[0].Versions
assert.Len(t, versions, 2, "latest changelog entry includes both versions and the versions are ordered DES")
require.Len(t, versions, 2, "latest changelog entry includes both versions and the versions are ordered DES")

firstVersionEntry := versions[0]
assert.Len(t, firstVersionEntry.Changes, 1)
require.Len(t, firstVersionEntry.Changes, 1)
assert.Equal(t, firstVersionEntry.Version, secondVersion)
assert.Equal(t, changeTypeRelease, firstVersionEntry.ChangeType)
assert.Equal(t, "request-property-removed", firstVersionEntry.Changes[0].Code)
assert.False(t, firstVersionEntry.Changes[0].BackwardCompatible)

secondVersionEntry := versions[1]
assert.Len(t, secondVersionEntry.Changes, 1)
require.Len(t, secondVersionEntry.Changes, 1)
assert.Equal(t, secondVersionEntry.Version, firstVersion)
assert.Equal(t, changeTypeUpdate, secondVersionEntry.ChangeType)
assert.Equal(t, "request-property-added", secondVersionEntry.Changes[0].Code)
Expand Down Expand Up @@ -310,7 +310,7 @@ func TestNewChangeType(t *testing.T) {
currentChangeType: changeTypeUpdate,
newChangeType: changeTypeDeprecated,
changeCode: "endpoint-deprecated",
expectedResult: changeTypeUpdate,
expectedResult: changeTypeDeprecated,
},
{
currentChangeType: changeTypeRelease,
Expand Down Expand Up @@ -340,7 +340,7 @@ func TestNewChangeType(t *testing.T) {

for _, tc := range testCases {
result := newChangeType(tc.currentChangeType, tc.newChangeType, tc.changeCode)
assert.Equal(t, tc.expectedResult, result, "newChangeType should return the expected result")
assert.Equal(t, tc.expectedResult, result)
}
}
func TestSortChangelog(t *testing.T) {
Expand Down Expand Up @@ -462,3 +462,128 @@ func TestSortChangelog(t *testing.T) {
// sortChangelog should sort the changelog by date DESC, path + httpMethod ASC, version DESC
assert.Equal(t, expectedChangelog, sortChangelog(changelog))
}

func TestMergeChangelogTwoVersionsWithDeprecations(t *testing.T) {
// arrange
baseChangelog, err := NewChangelogEntries("../../test/data/changelog/changelog.json")
require.NoError(t, err)

lastChangelogRunDate := baseChangelog[0].Date

firstVersion := "2023-02-01"
secondVersion := "2023-02-02"
changeTypeFirstVersion := changeTypeUpdate
changeTypeSecondVersion := changeTypeRelease

runDate := "2023-06-15"
sunset := "2024-02-02"
endpointsConfig := map[string]*outputfilter.OperationConfigs{
"createCluster": {
Base: &outputfilter.OperationConfig{
Path: "/api/atlas/v2/groups/{groupId}/clusters",
HTTPMethod: "POST",
Tag: "Multi-Cloud Clusters",
Sunset: sunset,
ManualChangelogEntries: make(map[string]interface{}),
},
Revision: &outputfilter.OperationConfig{
Path: "/api/atlas/v2/groups/{groupId}/clusters",
HTTPMethod: "POST",
Tag: "Multi-Cloud Clusters",
Sunset: "",
ManualChangelogEntries: make(map[string]interface{}),
},
},
}

changesFirstVersion := []*outputfilter.OasDiffEntry{
{
ID: "request-property-added",
Text: "added 'replicationSpecs.regionConfigs' request property",
Level: 1,
Operation: "POST",
OperationID: "createCluster",
Path: "/api/atlas/v2/groups/{groupId}/clusters",
},
}

changesSecondVersion := []*outputfilter.OasDiffEntry{
{
ID: "endpoint-reactivated",
Text: "endpoint reactivated",
Level: 1,
Operation: "POST",
OperationID: "createCluster",
Path: "/api/atlas/v2/groups/{groupId}/clusters",
},
{
ID: "request-property-removed",
Text: "removed the request properties: 'mongoURIWithOptions', 'providerBackupEnabled'",
Level: 3,
Operation: "POST",
OperationID: "createCluster",
Path: "/api/atlas/v2/groups/{groupId}/clusters",
},
}

changelogMetadataFirstVersion := &Metadata{
Base: &load.SpecInfo{
Version: firstVersion,
},
Revision: &load.SpecInfo{
Version: firstVersion,
},
BaseChangelog: baseChangelog,
RunDate: runDate,
}

changelog, err := changelogMetadataFirstVersion.mergeChangelog(changeTypeFirstVersion, changesFirstVersion, endpointsConfig)
require.NoError(t, err)

changelogMetadataSecondVersion := &Metadata{
Base: &load.SpecInfo{
Version: firstVersion,
},
Revision: &load.SpecInfo{
Version: secondVersion,
},
BaseChangelog: changelog,
RunDate: runDate,
}

changelog, err = changelogMetadataSecondVersion.mergeChangelog(changeTypeSecondVersion, changesSecondVersion, endpointsConfig)
require.NoError(t, err)

require.Len(t, changelog, 2, fmt.Sprintf("merged changelog should have 2 entries, got %d", len(changelog)))
assert.Equal(t, changelog[0].Date, runDate)
assert.Equal(t, changelog[1].Date, lastChangelogRunDate)

paths := changelog[0].Paths
require.Len(t, paths, 1)

assert.Equal(t, paths[0].URI, changesSecondVersion[0].Path)
assert.Equal(t, paths[0].HTTPMethod, changesSecondVersion[0].Operation)
assert.Equal(t, paths[0].OperationID, changesSecondVersion[0].OperationID)
assert.Equal(t, "Multi-Cloud Clusters", paths[0].Tag)

versions := paths[0].Versions
require.Len(t, versions, 2)

firstVersionEntry := versions[1]
require.Len(t, firstVersionEntry.Changes, 2)
assert.Equal(t, firstVersion, firstVersionEntry.Version)
assert.Equal(t, changeTypeDeprecated, firstVersionEntry.ChangeType)

assert.Equal(t, "request-property-added", firstVersionEntry.Changes[0].Code)
assert.True(t, firstVersionEntry.Changes[0].BackwardCompatible)
assert.Equal(t, "endpoint-deprecated", firstVersionEntry.Changes[1].Code)
assert.True(t, firstVersionEntry.Changes[1].BackwardCompatible)

secondVersionEntry := versions[0]
require.Len(t, secondVersionEntry.Changes, 1)
assert.Equal(t, secondVersionEntry.Version, secondVersion)
assert.Equal(t, changeTypeRelease, secondVersionEntry.ChangeType)

assert.Equal(t, "request-property-removed", secondVersionEntry.Changes[0].Code)
assert.False(t, secondVersionEntry.Changes[0].BackwardCompatible)
}
Loading