From e44a4b495862e9496d68b2ba8500a0999dbaee79 Mon Sep 17 00:00:00 2001 From: foefl Date: Thu, 3 Apr 2025 11:39:17 +0200 Subject: [PATCH] change to record classes instead of structs --- dopt.DeltaBarth.Tests/DataObjectsTest.cs | 14 ++++++-- dopt.DeltaBarth/DataObjects.cs | 42 ++++++++++++------------ dopt.DeltaBarth/Plugin.cs | 12 +++++-- dopt.DeltaBarth/dopt.DeltaBarth.csproj | 2 +- 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/dopt.DeltaBarth.Tests/DataObjectsTest.cs b/dopt.DeltaBarth.Tests/DataObjectsTest.cs index fa1fbbd..d99a0e6 100644 --- a/dopt.DeltaBarth.Tests/DataObjectsTest.cs +++ b/dopt.DeltaBarth.Tests/DataObjectsTest.cs @@ -5,6 +5,9 @@ using System.Text.Json; namespace dopt.DeltaBarth.Tests { using System.Collections.Immutable; + using System.Text.Encodings.Web; + using System.Text.Unicode; + internal class TestPlugin : DeltaBarth.Plugin { internal dynamic pyModJsonData; @@ -24,9 +27,10 @@ namespace dopt.DeltaBarth.Tests { string prettyJson = JsonSerializer.Serialize(toSerialise, new JsonSerializerOptions { + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, WriteIndented = true }); - Console.WriteLine($"Parsed struct is: {prettyJson}"); + Console.WriteLine($"Parsed data is: {prettyJson}"); } [TestMethod] public void Parse_Error_Status_Test() @@ -38,8 +42,9 @@ namespace dopt.DeltaBarth.Tests pyJson = (string)plugin.pyModJsonData.status_err(); } var parsed = JsonSerializer.Deserialize(pyJson); + if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); } Assert.AreEqual(102, parsed.code); - Assert.AreEqual("internal error occurred", parsed.description); + Assert.IsTrue(parsed.description.Contains("internal error occurred")); Assert.AreEqual("caused by test", parsed.message); Assert.IsNull(parsed.apiServerError); PrettyPrint(parsed); @@ -55,6 +60,7 @@ namespace dopt.DeltaBarth.Tests pyJson = (string)plugin.pyModJsonData.delta_barth_api_error(); } var parsed = JsonSerializer.Deserialize(pyJson); + if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); } Assert.AreEqual(401, parsed.status_code); Assert.AreEqual("test message", parsed.message); Assert.AreEqual("test code", parsed.code); @@ -75,6 +81,7 @@ namespace dopt.DeltaBarth.Tests pyJson = (string)plugin.pyModJsonData.api_credentials(); } var parsed = JsonSerializer.Deserialize(pyJson); + if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); } Assert.AreEqual("user", parsed.username); Assert.AreEqual("pass", parsed.password); Assert.AreEqual("test1", parsed.database); @@ -92,6 +99,7 @@ namespace dopt.DeltaBarth.Tests pyJson = (string)plugin.pyModJsonData.sales_prognosis_result(); } var parsed = JsonSerializer.Deserialize(pyJson); + if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); } Assert.AreEqual(2023, parsed.jahr); Assert.AreEqual(12, parsed.monat); Assert.AreEqual(3000.3456m, parsed.vorhersage); @@ -108,6 +116,7 @@ namespace dopt.DeltaBarth.Tests pyJson = (string)plugin.pyModJsonData.sales_prognosis_results(); } var parsed = JsonSerializer.Deserialize(pyJson); + Assert.IsNotNull(parsed); Assert.AreEqual(3, parsed.daten.Length); var e1 = new DataObjects.UmsatzPrognoseEinzelergebnis { jahr = 2023, monat = 12, vorhersage = 3000.3456m }; var e2 = new DataObjects.UmsatzPrognoseEinzelergebnis { jahr = 2024, monat = 1, vorhersage = 3300.685m }; @@ -131,6 +140,7 @@ namespace dopt.DeltaBarth.Tests pyJson = (string)plugin.pyModJsonData.sales_prognosis_results_export(); } var parsed = JsonSerializer.Deserialize(pyJson); + if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); } // result var e1 = new DataObjects.UmsatzPrognoseEinzelergebnis { jahr = 2023, monat = 12, vorhersage = 3000.3456m }; var e2 = new DataObjects.UmsatzPrognoseEinzelergebnis { jahr = 2024, monat = 1, vorhersage = 3300.685m }; diff --git a/dopt.DeltaBarth/DataObjects.cs b/dopt.DeltaBarth/DataObjects.cs index 3cacf84..ec8dc0c 100644 --- a/dopt.DeltaBarth/DataObjects.cs +++ b/dopt.DeltaBarth/DataObjects.cs @@ -7,44 +7,44 @@ using System.Collections.Immutable; namespace dopt.DeltaBarth.DataObjects { - public readonly struct ApiServerError + public record class ApiServerError { - public int status_code { get; init; } - public string message { get; init; } + public required int status_code { get; init; } + public required string message { get; init; } public string? code { get; init; } public string? hints { get; init; } public string? type { get; init; } public string? title { get; init; } public string? traceId { get; init; } } - public readonly struct Status + public record class Status { - public int code { get; init; } - public string description { get; init; } - public string message { get; init; } + public required int code { get; init; } + public required string description { get; init; } + public required string message { get; init; } public ApiServerError? apiServerError { get; init; } } - public readonly struct Credentials + public record class Credentials { - public string username { get; init; } - public string password { get; init; } - public string database { get; init; } - public string mandant { get; init; } + public required string username { get; init; } + public required string password { get; init; } + public required string database { get; init; } + public required string mandant { get; init; } } - public readonly struct UmsatzPrognoseEinzelergebnis + public record class UmsatzPrognoseEinzelergebnis { - public int jahr { get; init; } - public int monat { get; init; } - public decimal vorhersage { get; init; } + public required int jahr { get; init; } + public required int monat { get; init; } + public required decimal vorhersage { get; init; } } - public readonly struct UmsatzPrognoseErgebnisse + public record class UmsatzPrognoseErgebnisse { - public ImmutableArray daten { get; init; } + public required ImmutableArray daten { get; init; } } - public readonly struct UmsatzPrognoseAusgabe + public record class UmsatzPrognoseAusgabe { - public UmsatzPrognoseErgebnisse response { get; init; } - public Status status { get; init; } + public required UmsatzPrognoseErgebnisse response { get; init; } + public required Status status { get; init; } } } diff --git a/dopt.DeltaBarth/Plugin.cs b/dopt.DeltaBarth/Plugin.cs index 45312d0..1a736ea 100644 --- a/dopt.DeltaBarth/Plugin.cs +++ b/dopt.DeltaBarth/Plugin.cs @@ -3,6 +3,11 @@ using System.Text.Json; namespace dopt.DeltaBarth { + public class PythonParsingException : Exception + { + public PythonParsingException() { } + public PythonParsingException(string message) : base(message) { } + } public class Plugin : SharpPython.BasePlugin { protected dynamic pyModManagement; @@ -38,6 +43,7 @@ namespace dopt.DeltaBarth pyJson = pyModPipeline.pipeline_sales_forecast_dummy(firmaId, buchungsDatum); } var parsed = JsonSerializer.Deserialize(pyJson); + if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); } return parsed; } @@ -50,6 +56,7 @@ namespace dopt.DeltaBarth pyJson = pyModPipeline.pipeline_sales_forecast(firmaId, buchungsDatum); } var parsed = JsonSerializer.Deserialize(pyJson); + if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); } return parsed; } @@ -90,9 +97,10 @@ namespace dopt.DeltaBarth pyJson = (string)pyModManagement.get_credentials(); } - DataObjects.Credentials credentials = JsonSerializer.Deserialize(pyJson); + DataObjects.Credentials? parsed = JsonSerializer.Deserialize(pyJson); + if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); } - return credentials; + return parsed; } } } diff --git a/dopt.DeltaBarth/dopt.DeltaBarth.csproj b/dopt.DeltaBarth/dopt.DeltaBarth.csproj index 829f3bf..3f7b75b 100644 --- a/dopt.DeltaBarth/dopt.DeltaBarth.csproj +++ b/dopt.DeltaBarth/dopt.DeltaBarth.csproj @@ -6,7 +6,7 @@ enable x64 x64 - 0.3.0 + 0.3.1