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

Refactored if statement tests to table tests to increase readability … #22564

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
123 changes: 36 additions & 87 deletions sdk/data/azcosmos/cosmos_container_properties_test.go
Expand Up @@ -66,92 +66,41 @@ func TestContainerPropertiesSerialization(t *testing.T) {
t.Fatal(err, string(jsonString))
}

if properties.ID != otherProperties.ID {
t.Errorf("Expected Id to be %s, but got %s", properties.ID, otherProperties.ID)
}

if *properties.ETag != *otherProperties.ETag {
t.Errorf("Expected ETag to be %s, but got %s", *properties.ETag, *otherProperties.ETag)
}

if properties.SelfLink != otherProperties.SelfLink {
t.Errorf("Expected SelfLink to be %s, but got %s", properties.SelfLink, otherProperties.SelfLink)
}

if properties.ResourceID != otherProperties.ResourceID {
t.Errorf("Expected ResourceId to be %s, but got %s", properties.ResourceID, otherProperties.ResourceID)
}

if properties.LastModified != otherProperties.LastModified {
t.Errorf("Expected LastModified.Time to be %v, but got %v", properties.LastModified, otherProperties.LastModified)
}

if otherProperties.AnalyticalStoreTimeToLiveInSeconds != nil {
t.Errorf("Expected AnalyticalStoreTimeToLiveInSeconds to be nil, but got %d", *otherProperties.AnalyticalStoreTimeToLiveInSeconds)
}

if otherProperties.DefaultTimeToLive != nil {
t.Errorf("Expected DefaultTimeToLive to be nil, but got %d", *otherProperties.DefaultTimeToLive)
}

if properties.PartitionKeyDefinition.Paths[0] != otherProperties.PartitionKeyDefinition.Paths[0] {
t.Errorf("Expected PartitionKeyDefinition.Paths[0] to be %s, but got %s", properties.PartitionKeyDefinition.Paths[0], otherProperties.PartitionKeyDefinition.Paths[0])
}

if properties.PartitionKeyDefinition.Version != otherProperties.PartitionKeyDefinition.Version {
t.Errorf("Expected PartitionKeyDefinition.Version to be %d, but got %d", properties.PartitionKeyDefinition.Version, otherProperties.PartitionKeyDefinition.Version)
}

if otherProperties.IndexingPolicy == nil {
t.Errorf("Expected IndexingPolicy to be not nil, but got nil")
}

if otherProperties.IndexingPolicy.Automatic != properties.IndexingPolicy.Automatic {
t.Errorf("Expected IndexingPolicy.Automatic to be %t, but got %t", properties.IndexingPolicy.Automatic, otherProperties.IndexingPolicy.Automatic)
}

if otherProperties.IndexingPolicy.IndexingMode != properties.IndexingPolicy.IndexingMode {
t.Errorf("Expected IndexingPolicy.IndexingMode to be %v, but got %v", properties.IndexingPolicy.IndexingMode, otherProperties.IndexingPolicy.IndexingMode)
}

if otherProperties.IndexingPolicy.IncludedPaths[0].Path != properties.IndexingPolicy.IncludedPaths[0].Path {
t.Errorf("Expected IndexingPolicy.IncludedPaths[0].Path to be %s, but got %s", properties.IndexingPolicy.IncludedPaths[0].Path, otherProperties.IndexingPolicy.IncludedPaths[0].Path)
}

if otherProperties.IndexingPolicy.ExcludedPaths[0].Path != properties.IndexingPolicy.ExcludedPaths[0].Path {
t.Errorf("Expected IndexingPolicy.ExcludedPaths[0].Path to be %s, but got %s", properties.IndexingPolicy.ExcludedPaths[0].Path, otherProperties.IndexingPolicy.ExcludedPaths[0].Path)
}

if otherProperties.IndexingPolicy.SpatialIndexes[0].Path != properties.IndexingPolicy.SpatialIndexes[0].Path {
t.Errorf("Expected IndexingPolicy.SpatialIndexes[0].Path to be %s, but got %s", properties.IndexingPolicy.SpatialIndexes[0].Path, otherProperties.IndexingPolicy.SpatialIndexes[0].Path)
}

if otherProperties.IndexingPolicy.SpatialIndexes[0].SpatialTypes[0] != properties.IndexingPolicy.SpatialIndexes[0].SpatialTypes[0] {
t.Errorf("Expected IndexingPolicy.SpatialIndexes[0].SpatialTypes[0] to be %v, but got %v", properties.IndexingPolicy.SpatialIndexes[0].SpatialTypes[0], otherProperties.IndexingPolicy.SpatialIndexes[0].SpatialTypes[0])
}

if otherProperties.IndexingPolicy.CompositeIndexes[0][0].Path != properties.IndexingPolicy.CompositeIndexes[0][0].Path {
t.Errorf("Expected IndexingPolicy.CompositeIndexes[0][0].Path to be %s, but got %s", properties.IndexingPolicy.CompositeIndexes[0][0].Path, otherProperties.IndexingPolicy.CompositeIndexes[0][0].Path)
}

if otherProperties.UniqueKeyPolicy == nil {
t.Errorf("Expected UniqueKeyPolicy to be not nil, but got nil")
}

if otherProperties.UniqueKeyPolicy.UniqueKeys[0].Paths[0] != properties.UniqueKeyPolicy.UniqueKeys[0].Paths[0] {
t.Errorf("Expected UniqueKeyPolicy.UniqueKeys[0].Paths[0] to be %s, but got %s", properties.UniqueKeyPolicy.UniqueKeys[0].Paths[0], otherProperties.UniqueKeyPolicy.UniqueKeys[0].Paths[0])
}

if otherProperties.ConflictResolutionPolicy == nil {
t.Errorf("Expected ConflictResolutionPolicy to be not nil, but got nil")
}

if otherProperties.ConflictResolutionPolicy.Mode != properties.ConflictResolutionPolicy.Mode {
t.Errorf("Expected ConflictResolutionPolicy.Mode to be %v, but got %v", properties.ConflictResolutionPolicy.Mode, otherProperties.ConflictResolutionPolicy.Mode)
}

if otherProperties.ConflictResolutionPolicy.ResolutionPath != properties.ConflictResolutionPolicy.ResolutionPath {
t.Errorf("Expected ConflictResolutionPolicy.ResolutionPath to be %s, but got %s", properties.ConflictResolutionPolicy.ResolutionPath, otherProperties.ConflictResolutionPolicy.ResolutionPath)
tests := []struct {
name string
got interface{}
expected interface{}
}{
{"ID", otherProperties.ID, properties.ID},
{"ETag", *otherProperties.ETag, *properties.ETag},
{"SelfLink", otherProperties.SelfLink, properties.SelfLink},
{"ResourceID", otherProperties.ResourceID, properties.ResourceID},
{"LastModified", otherProperties.LastModified, properties.LastModified},
{"AnalyticalStoreTimtoLiveInSeconds", otherProperties.AnalyticalStoreTimeToLiveInSeconds, properties.AnalyticalStoreTimeToLiveInSeconds},
{"DefaultTimeToLive", otherProperties.DefaultTimeToLive, properties.DefaultTimeToLive},
{"PartitionKeyDefinitionPaths", otherProperties.PartitionKeyDefinition.Paths[0], properties.PartitionKeyDefinition.Paths[0]},
{"PartitionKeyDefinitionVersion", otherProperties.PartitionKeyDefinition.Version, properties.PartitionKeyDefinition.Version},
{"IndexingPolicy", otherProperties.IndexingPolicy != nil, properties.IndexingPolicy != nil},
{"IndexingPolicyAutomatic", otherProperties.IndexingPolicy.Automatic, properties.IndexingPolicy.Automatic},
{"IndexingPolicyIndexingMode", otherProperties.IndexingPolicy.IndexingMode, properties.IndexingPolicy.IndexingMode},
{"IndexingPolicyIncludedPaths", otherProperties.IndexingPolicy.IncludedPaths[0], properties.IndexingPolicy.IncludedPaths[0]},
{"IndexingPolicyExcludedPaths", otherProperties.IndexingPolicy.ExcludedPaths[0], properties.IndexingPolicy.ExcludedPaths[0]},
{"IndexingPolicySpatialIndexesPath", otherProperties.IndexingPolicy.SpatialIndexes[0].Path, properties.IndexingPolicy.SpatialIndexes[0].Path},
{"IndexingPolicySpatialIndexesSpatialTypes", otherProperties.IndexingPolicy.SpatialIndexes[0].SpatialTypes[0], properties.IndexingPolicy.SpatialIndexes[0].SpatialTypes[0]},
{"IndexingPolicyCompositeIndexesPath", otherProperties.IndexingPolicy.CompositeIndexes[0][0].Path, properties.IndexingPolicy.CompositeIndexes[0][0].Path},
{"UniqueKeyPolicy", otherProperties.UniqueKeyPolicy != nil, properties.UniqueKeyPolicy != nil},
{"UniqueKeyPolicyUniqueKeys", otherProperties.UniqueKeyPolicy.UniqueKeys[0].Paths[0], properties.UniqueKeyPolicy.UniqueKeys[0].Paths[0]},
{"ConflictResolutionPolicy", otherProperties.ConflictResolutionPolicy != nil, properties.ConflictResolutionPolicy !=nil},
{"ConflictResolutionPolicyMode", otherProperties.ConflictResolutionPolicy.Mode, properties.ConflictResolutionPolicy.Mode},
{"ConflictResolutionPolicyResolutionPath", otherProperties.ConflictResolutionPolicy.ResolutionPath, properties.ConflictResolutionPolicy.ResolutionPath},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally find this refactoring a bit less clear than the ifs. Having to understand the usage of t.Run with the delegate seems a bit more complicated than understanding a list of if conditions.

But I would see value if these ifs are unbounded/can keep growing, so it would reduce the volume of code required. But the reality is that these properties will not really change frequently so the chances of a developer having to keep adding checks here is very low to offset the increase in reading complexity.

Finally, the same pattern (ifs) are used throughout the other files. If we want to think a common pattern, I guess we would need to refactor all the other files too?

if tt.got != tt.expected {
t.Errorf("Expected %s to be %v, but got %v", tt.name, tt.expected, tt.got)
}
})
}
}

Expand All @@ -171,4 +120,4 @@ func TestContainerPropertiesSerializationWithTTL(t *testing.T) {
if *properties.AnalyticalStoreTimeToLiveInSeconds != 20 {
t.Errorf("Expected properties.AnalyticalStoreTimeToLiveInSeconds to be %d, but got %d", 20, properties.AnalyticalStoreTimeToLiveInSeconds)
}
}
}