process-data-standard-format changes for better unit testing

This commit is contained in:
Mike Phares 2025-04-30 09:53:54 -07:00
parent f0ec303ddb
commit c5e45a7afd
3 changed files with 50 additions and 58 deletions

View File

@ -22,19 +22,26 @@ public class FileRead : Shared.FileRead, IFileRead
internal class PreWith internal class PreWith
{ {
internal string MatchingFile { get; private set; }
internal string CheckFile { get; private set; }
internal string ErrFile { get; private set; } internal string ErrFile { get; private set; }
internal string CheckFile { get; private set; }
internal string MatchingFile { get; private set; }
internal string CheckDirectory { get; private set; } internal string CheckDirectory { get; private set; }
internal string NoWaitDirectory { get; private set; } internal string NoWaitDirectory { get; private set; }
internal bool MesEntityMatchesProcess { get; private set; }
internal PreWith(string matchingFile, string checkFile, string errFile, string checkDirectory, string noWaitDirectory) internal PreWith(string checkDirectory,
string checkFile,
string errFile,
string matchingFile,
string noWaitDirectory,
bool mesEntityMatchesProcess)
{ {
MatchingFile = matchingFile;
CheckFile = checkFile;
ErrFile = errFile; ErrFile = errFile;
CheckFile = checkFile;
MatchingFile = matchingFile;
CheckDirectory = checkDirectory; CheckDirectory = checkDirectory;
NoWaitDirectory = noWaitDirectory; NoWaitDirectory = noWaitDirectory;
MesEntityMatchesProcess = mesEntityMatchesProcess;
} }
} }
@ -205,7 +212,7 @@ public class FileRead : Shared.FileRead, IFileRead
return result; return result;
} }
private static ReadOnlyCollection<PreWith> GetPreWithCollection(ReadOnlyCollection<Pre> preCollection) private static ReadOnlyCollection<PreWith> GetPreWithCollection(ReadOnlyCollection<Pre> preCollection, bool mesEntityMatchesProcess)
{ {
List<PreWith> results = new(); List<PreWith> results = new();
string errFile; string errFile;
@ -221,7 +228,12 @@ public class FileRead : Shared.FileRead, IFileRead
if (!Directory.Exists(checkDirectory)) if (!Directory.Exists(checkDirectory))
_ = Directory.CreateDirectory(checkDirectory); _ = Directory.CreateDirectory(checkDirectory);
noWaitDirectory = Path.Combine(checkDirectory, "NoWaitDirectory"); noWaitDirectory = Path.Combine(checkDirectory, "NoWaitDirectory");
preWith = new(pre.MatchingFile, pre.CheckFile, errFile, checkDirectory, noWaitDirectory); preWith = new(checkDirectory: checkDirectory,
checkFile: pre.CheckFile,
errFile: errFile,
matchingFile: pre.MatchingFile,
noWaitDirectory: noWaitDirectory,
mesEntityMatchesProcess: mesEntityMatchesProcess);
results.Add(preWith); results.Add(preWith);
} }
return results.AsReadOnly(); return results.AsReadOnly();
@ -345,16 +357,9 @@ public class FileRead : Shared.FileRead, IFileRead
private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime) private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime)
{ {
Tuple<string, Test[], JsonElement[], List<FileInfo>> results = new(string.Empty, Array.Empty<Test>(), Array.Empty<JsonElement>(), new List<FileInfo>()); Tuple<string, Test[], JsonElement[], List<FileInfo>> results = new(string.Empty, Array.Empty<Test>(), Array.Empty<JsonElement>(), new List<FileInfo>());
ProcessDataStandardFormat? processDataStandardFormat = ProcessDataStandardFormat.GetProcessDataStandardFormat(reportFullPath, _ProcessDataStandardFormatMapping); ProcessDataStandardFormat processDataStandardFormat = ProcessDataStandardFormat.GetProcessDataStandardFormat(reportFullPath, _ProcessDataStandardFormatMapping);
if (processDataStandardFormat is not null)
_Logistics = new Logistics(reportFullPath, processDataStandardFormat); _Logistics = new Logistics(reportFullPath, processDataStandardFormat);
else if (!_IsEAFHosted)
{
processDataStandardFormat = ProcessDataStandardFormat.GetProcessDataStandardFormat(reportFullPath);
_Logistics = new Logistics(reportFullPath, processDataStandardFormat);
processDataStandardFormat = null;
}
if (!_IsEAFHosted && processDataStandardFormat is not null)
ProcessDataStandardFormat.Write(".pdsf", processDataStandardFormat, wsResults: null); ProcessDataStandardFormat.Write(".pdsf", processDataStandardFormat, wsResults: null);
SetFileParameterLotIDToLogisticsMID(); SetFileParameterLotIDToLogisticsMID();
int numberLength = 2; int numberLength = 2;
@ -364,11 +369,17 @@ public class FileRead : Shared.FileRead, IFileRead
ReadOnlyCollection<string> matchingFiles = GetMatchingFiles(ticks, reportFullPath, searchDirectories); ReadOnlyCollection<string> matchingFiles = GetMatchingFiles(ticks, reportFullPath, searchDirectories);
if (matchingFiles.Count != searchDirectories.Count) if (matchingFiles.Count != searchDirectories.Count)
throw new Exception($"Didn't find all files after {_BreakAfterSeconds} second(s)!"); throw new Exception($"Didn't find all files after {_BreakAfterSeconds} second(s)!");
if (_IsEAFHosted)
{
try try
{ CreatePointerFile(numberLength, parentParentDirectory, matchingFiles); } { CreatePointerFile(numberLength, parentParentDirectory, matchingFiles); }
catch (Exception) { } catch (Exception) { }
}
JsonElement[] jsonElements = ProcessDataStandardFormat.GetArray(processDataStandardFormat);
List<RsM.Description> descriptions = RsM.ProcessData.GetDescriptions(jsonElements);
bool mesEntityMatchesProcess = descriptions.Count > 0 && descriptions[0].MesEntity == descriptions[0].Reactor;
ReadOnlyCollection<Pre> preCollection = GetPreCollection(numberLength, parentParentDirectory, matchingFiles); ReadOnlyCollection<Pre> preCollection = GetPreCollection(numberLength, parentParentDirectory, matchingFiles);
ReadOnlyCollection<PreWith> preWithCollection = GetPreWithCollection(preCollection); ReadOnlyCollection<PreWith> preWithCollection = GetPreWithCollection(preCollection, mesEntityMatchesProcess);
MoveCollection(dateTime, processDataStandardFormat, preWithCollection); MoveCollection(dateTime, processDataStandardFormat, preWithCollection);
return results; return results;
} }

View File

@ -61,8 +61,8 @@ internal class ProcessDataStandardFormat
internal static string Archive(bool addSpaces = true, char separator = ' ') => internal static string Archive(bool addSpaces = true, char separator = ' ') =>
GetString(SearchFor.Archive, addSpaces, separator); GetString(SearchFor.Archive, addSpaces, separator);
internal static ProcessDataStandardFormat GetEmpty() => internal static ProcessDataStandardFormat GetEmpty(Logistics logistics) =>
new(new(Array.Empty<string>()), new(Array.Empty<string>()), new(Array.Empty<string>()), new(Array.Empty<string>()), null, new(new string[] { "LOGISTICS_1" }), null); new(new(Array.Empty<string>()), new(Array.Empty<string>()), new(Array.Empty<string>()), new(Array.Empty<string>()), null, new(logistics.Logistics1), null);
internal static List<string> PDSFToFixedWidth(string reportFullPath) internal static List<string> PDSFToFixedWidth(string reportFullPath)
{ {
@ -214,25 +214,26 @@ internal class ProcessDataStandardFormat
return results.AsReadOnly(); return results.AsReadOnly();
} }
internal static ProcessDataStandardFormat? GetProcessDataStandardFormat(string reportFullPath, ProcessDataStandardFormatMapping pdsfMapping) internal static ProcessDataStandardFormat GetProcessDataStandardFormat(string reportFullPath, ProcessDataStandardFormatMapping pdsfMapping)
{ {
ProcessDataStandardFormat? result; ProcessDataStandardFormat result;
const int columnsLine = 6; const int columnsLine = 6;
FileInfo fileInfo = new(reportFullPath); FileInfo fileInfo = new(reportFullPath);
ProcessDataStandardFormat processDataStandardFormat = GetProcessDataStandardFormat(fileInfo.LastWriteTime, pdsfMapping.NewColumnNames.Count, columnsLine, fileInfo.FullName, lines: null); ProcessDataStandardFormat processDataStandardFormat = GetProcessDataStandardFormat(fileInfo.LastWriteTime, columnsLine, fileInfo.FullName, lines: null);
JsonElement[]? jsonElements = GetArray(pdsfMapping.NewColumnNames.Count, processDataStandardFormat, lookForNumbers: false); JsonElement[]? jsonElements = pdsfMapping.OldColumnNames.Count != pdsfMapping.ColumnIndices.Count ? null : GetFullArray(processDataStandardFormat);
if (jsonElements is null || jsonElements.Length == 0 || pdsfMapping.OldColumnNames.Count != pdsfMapping.ColumnIndices.Count) JsonProperty[]? jsonProperties = jsonElements is null || jsonElements.Length == 0 ? null : jsonElements[0].EnumerateObject().ToArray();
result = null; if (jsonElements is null || jsonProperties is null || jsonProperties.Length != pdsfMapping.NewColumnNames.Count)
result = processDataStandardFormat;
else else
{ {
result = GetProcessDataStandardFormat(pdsfMapping, jsonElements, processDataStandardFormat); result = GetProcessDataStandardFormat(pdsfMapping, jsonElements, processDataStandardFormat);
if (result.Sequence is null || result.Columns.Count == 0 || result.Body.Count == 0 || result.Logistics.Count == 0) if (result.Sequence is null || result.Columns.Count == 0 || result.Body.Count == 0 || result.Logistics.Count == 0)
result = null; result = processDataStandardFormat;
} }
return result; return result;
} }
private static ProcessDataStandardFormat GetProcessDataStandardFormat(DateTime lastWriteTime, int expectedColumns, int columnsLine, string path, string[]? lines) private static ProcessDataStandardFormat GetProcessDataStandardFormat(DateTime lastWriteTime, int columnsLine, string path, string[]? lines)
{ {
ProcessDataStandardFormat result; ProcessDataStandardFormat result;
long sequence; long sequence;
@ -248,8 +249,6 @@ internal class ProcessDataStandardFormat
else else
{ {
segments = lines[columnsLine].Split('\t'); segments = lines[columnsLine].Split('\t');
if (segments.Length != expectedColumns)
segments = Array.Empty<string>();
for (int i = 0; i < columnsLine; i++) for (int i = 0; i < columnsLine; i++)
header.Add(lines[i]); header.Add(lines[i]);
} }
@ -285,7 +284,7 @@ internal class ProcessDataStandardFormat
return result; return result;
} }
private static JsonElement[]? GetArray(int expectedColumns, ProcessDataStandardFormat processDataStandardFormat, bool lookForNumbers) private static JsonElement[]? GetFullArray(ProcessDataStandardFormat processDataStandardFormat)
{ {
JsonElement[]? results; JsonElement[]? results;
if (processDataStandardFormat.Body.Count == 0 || !processDataStandardFormat.Body[0].Contains('\t')) if (processDataStandardFormat.Body.Count == 0 || !processDataStandardFormat.Body[0].Contains('\t'))
@ -293,37 +292,19 @@ internal class ProcessDataStandardFormat
else else
{ {
string value; string value;
string[] segments; List<string> segments;
List<string> lines = new(); List<string> lines = new();
StringBuilder stringBuilder = new(); StringBuilder stringBuilder = new();
foreach (string bodyLine in processDataStandardFormat.Body) foreach (string bodyLine in processDataStandardFormat.Body)
{ {
_ = stringBuilder.Clear(); _ = stringBuilder.Clear();
_ = stringBuilder.Append('{'); _ = stringBuilder.Append('{');
segments = bodyLine.Split('\t'); segments = bodyLine.Split('\t').ToList();
if (segments.Length != expectedColumns) for (int c = 0; c < segments.Count; c++)
continue;
if (!lookForNumbers)
{
for (int c = 0; c < segments.Length; c++)
{ {
value = segments[c].Replace("\"", "\\\"").Replace("\\", "\\\\"); value = segments[c].Replace("\"", "\\\"").Replace("\\", "\\\\");
_ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":\"").Append(value).Append("\","); _ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":\"").Append(value).Append("\",");
} }
}
else
{
for (int c = 0; c < segments.Length; c++)
{
value = segments[c].Replace("\"", "\\\"").Replace("\\", "\\\\");
if (string.IsNullOrEmpty(value))
_ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":").Append(value).Append("null,");
else if (value.All(char.IsDigit))
_ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":").Append(value).Append(',');
else
_ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":\"").Append(value).Append("\",");
}
}
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1); _ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
_ = stringBuilder.AppendLine("}"); _ = stringBuilder.AppendLine("}");
lines.Add(stringBuilder.ToString()); lines.Add(stringBuilder.ToString());

View File

@ -1182,7 +1182,7 @@ public class AdaptationTesting : ISMTP
Assert.IsNotNull(extractResult.Item3); Assert.IsNotNull(extractResult.Item3);
Assert.IsNotNull(extractResult.Item4); Assert.IsNotNull(extractResult.Item4);
if (!validatePDSF) if (!validatePDSF)
_ = GetProcessDataStandardFormat(fileRead, logistics, extractResult, ProcessDataStandardFormat.GetEmpty()); _ = GetProcessDataStandardFormat(fileRead, logistics, extractResult, ProcessDataStandardFormat.GetEmpty(logistics));
else else
{ {
Assert.IsTrue(extractResult.Item3.Length > 0, "extractResult Array Length check!"); Assert.IsTrue(extractResult.Item3.Length > 0, "extractResult Array Length check!");