1 Commits

Author SHA1 Message Date
Claire Kuang 213221e7d5 improved comparer and reporting
build and deploy Speckle functions / publish-automate-function-version (push) Has been cancelled
2024-01-31 20:58:13 +00:00
@@ -89,9 +89,9 @@ static class AutomateFunction
Console.WriteLine($"Found {testCommitObjects.Count()} objects in release version"); Console.WriteLine($"Found {testCommitObjects.Count()} objects in release version");
// compare objects // compare objects
int addedCount = 0;
int modifiedCount = 0;
int unchangedCount = 0; int unchangedCount = 0;
var addedList = new List<Tuple<string, string>>();
var modifiedList = new List<Tuple<string, string, string>>();
foreach (Base testObject in testCommitObjects) foreach (Base testObject in testCommitObjects)
{ {
if ( if (
@@ -109,7 +109,6 @@ static class AutomateFunction
// if ids are different, find property differences // if ids are different, find property differences
else else
{ {
modifiedCount++;
var diffDictionary = new Dictionary<string, string>(); var diffDictionary = new Dictionary<string, string>();
Dictionary<string, object?> releaseObjectPropDict = Dictionary<string, object?> releaseObjectPropDict =
releaseObject.GetMembers(); releaseObject.GetMembers();
@@ -158,14 +157,17 @@ static class AutomateFunction
var sb = new System.Text.StringBuilder(); var sb = new System.Text.StringBuilder();
foreach (var entry in diffDictionary) foreach (var entry in diffDictionary)
{ {
sb.AppendLine($"{entry.Key}: {entry.Value}"); sb.AppendLine($"{entry.Key}: {entry.Value}. ");
} }
automationContext.AttachWarningToObjects( modifiedList.Add(
MODIFIED, new Tuple<string, string, string>(
new List<string>() { testObject.id }, testObject.id,
sb.ToString() testObject.speckle_type,
sb.ToString()
)
); );
//automationContext.AttachWarningToObjects( MODIFIED, new List<string>() { testObject.id }, sb.ToString());
} }
} }
@@ -173,40 +175,88 @@ static class AutomateFunction
} }
else else
{ {
addedCount++; //automationContext.AttachInfoToObjects(ADDED, new List<string>() { testObject.id });
automationContext.AttachInfoToObjects( addedList.Add(
ADDED, new Tuple<string, string>(testObject.id, testObject.speckle_type)
new List<string>() { testObject.id }
); );
} }
} }
// if there are any remaining release commit objects, this indicates missing objects in the test run. // if there are any remaining release commit objects, this indicates missing objects in the test run.
// mark run as failed and list missing object details. var deletedList = new List<Tuple<string, string>>();
if (releaseCommitObjectsDict.Keys.Count > 0) foreach (var entry in releaseCommitObjectsDict)
{
deletedList.Add(new Tuple<string, string>(entry.Key, entry.Value.speckle_type));
}
// mark run failed if there are any added, modified, or deleted objects and report
if (addedList.Count + deletedList.Count + modifiedList.Count > 0)
{ {
automationContext.MarkRunFailed( automationContext.MarkRunFailed(
$"Missing {releaseCommitObjectsDict.Keys.Count} objects compared to the release commit." $"Run failed due to {addedList.Count} ADDED, {modifiedList.Count} MODIFIED, and {deletedList.Count} DELETED objects compared to the release commit."
); );
foreach (var missingObject in releaseCommitObjectsDict) addedList.ForEach(
{ o => Console.WriteLine($"ADDED object: id( {o.Item1} ), type( {o.Item2} )")
Console.WriteLine( );
$"Missing object info: id( {missingObject.Value.id} ), applicationId( {missingObject.Key} ), type( {missingObject.Value.speckle_type} )" deletedList.ForEach(
); o => Console.WriteLine($"DELETED object: id( {o.Item1} ), type( {o.Item2} )")
} );
modifiedList.ForEach(
o =>
Console.WriteLine(
$"MODIFIED object: id( {o.Item1} ), type( {o.Item2} ), changed props:( {o.Item3} )"
)
);
} }
// mark run as succeeded, noting any changed objects and added objects
else else
{ {
automationContext.MarkRunSuccess( automationContext.MarkRunSuccess(
$"Run passed with {addedCount} new objects, {modifiedCount} objects, and {unchangedCount} unchanged objects." $"Run passed with {unchangedCount} unchanged objects."
); );
} }
} }
public static bool Equals<T>(T a, T b) public static bool Equals<T>(T a, T b)
{ {
return EqualityComparer<T>.Default.Equals(a, b); switch (a)
{
case Base o:
return b is Base bBase ? o.id == bBase.id : false;
case List<object> aList:
if (b is List<object> bList && aList.Count == bList.Count)
{
for (int i = 0; i < aList.Count; i++)
{
if (!Equals(aList[i], bList[i]))
{
return false;
}
}
return true;
}
return false;
case Dictionary<string, object> aDictionary:
if (
b is Dictionary<string, object> bDictionary
&& aDictionary.Count == bDictionary.Count
)
{
foreach (var entry in aDictionary)
{
if (
!bDictionary.ContainsKey(entry.Key)
|| !Equals(entry.Value, bDictionary[entry.Key])
)
{
return false;
}
}
return true;
}
return false;
default:
return EqualityComparer<T>.Default.Equals(a, b);
}
} }
} }