From 4b50e5e18ed642f9ea8703f41dc0470a464c05ac Mon Sep 17 00:00:00 2001 From: John Lenz Date: Mon, 9 Sep 2024 13:21:51 -0500 Subject: [PATCH] mazak: fix a bug when downloading a new program --- server/machines/mazak/sync/MazakSync.cs | 30 ++++++++++++++++ server/test/mazak/SyncSpec.cs | 48 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/server/machines/mazak/sync/MazakSync.cs b/server/machines/mazak/sync/MazakSync.cs index 7b685334..40da5297 100644 --- a/server/machines/mazak/sync/MazakSync.cs +++ b/server/machines/mazak/sync/MazakSync.cs @@ -126,6 +126,36 @@ public IEnumerable CheckNewJobs(IRepository db, NewJobs jobs) { ProgramRevision lookupProg(string prog, long? rev) { + // while checking jobs, a newly downloaded program might not yet + // be in the database. Therefore, we need to check if the program + // is going to be included as part of the download + NewProgramContent? newlyAdded = jobs.Programs?.FirstOrDefault(p => + { + if (rev.HasValue && rev.Value != 0) + { + return p.ProgramName == prog && p.Revision == rev.Value; + } + else + { + // 0 revision just means most recent + return p.ProgramName == prog; + } + }); + + if (newlyAdded != null) + { + return new ProgramRevision() + { + ProgramName = prog, + Comment = newlyAdded.Comment, + CellControllerProgramName = "", // not yet in the cell controller + // This revision isn't correct since a 0 or negative revision will be + // assigned as part of the transaction adding the jobs and programs + // to the database. + Revision = newlyAdded.Revision, + }; + } + if (rev.HasValue) { return db.LoadProgram(prog, rev.Value); diff --git a/server/test/mazak/SyncSpec.cs b/server/test/mazak/SyncSpec.cs index 85e200c7..b3f34829 100644 --- a/server/test/mazak/SyncSpec.cs +++ b/server/test/mazak/SyncSpec.cs @@ -183,6 +183,54 @@ public void CheckFailMissingProgram() ); } + [Fact] + public void CheckSucceedIfProgramInDownload() + { + var jsonSettings = new JsonSerializerOptions(); + FMSInsightWebHost.JsonSettings(jsonSettings); + _read + .LoadAllData() + .Returns( + new MazakAllData() + { + MainPrograms = + [ + new MazakProgramRow() { MainProgram = "1001", Comment = "" }, + // no 1002 + new MazakProgramRow() { MainProgram = "1003", Comment = "" }, + new MazakProgramRow() { MainProgram = "1004", Comment = "" }, + ], + Fixtures = [], + Pallets = [], + Parts = [], + Schedules = [], + } + ); + + var newJ = JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine("..", "..", "..", "sample-newjobs", "fixtures-queues.json")), + jsonSettings + ); + + newJ = newJ with + { + Programs = + [ + new NewProgramContent() + { + ProgramName = "1002", + Comment = "program 1002", + ProgramContent = "content for 1002", + Revision = -1, + }, + ], + }; + + using var db = repo.OpenConnection(); + + _sync.CheckNewJobs(db, newJ).Should().BeEmpty(); + } + [Fact] public void MissingQueue() {