change to record classes instead of structs #9

Merged
foefl merged 1 commits from records into main 2025-04-03 09:41:55 +00:00
4 changed files with 44 additions and 26 deletions

View File

@ -5,6 +5,9 @@ using System.Text.Json;
namespace dopt.DeltaBarth.Tests namespace dopt.DeltaBarth.Tests
{ {
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Text.Encodings.Web;
using System.Text.Unicode;
internal class TestPlugin : DeltaBarth.Plugin internal class TestPlugin : DeltaBarth.Plugin
{ {
internal dynamic pyModJsonData; internal dynamic pyModJsonData;
@ -24,9 +27,10 @@ namespace dopt.DeltaBarth.Tests
{ {
string prettyJson = JsonSerializer.Serialize(toSerialise, new JsonSerializerOptions string prettyJson = JsonSerializer.Serialize(toSerialise, new JsonSerializerOptions
{ {
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
WriteIndented = true WriteIndented = true
}); });
Console.WriteLine($"Parsed struct is: {prettyJson}"); Console.WriteLine($"Parsed data is: {prettyJson}");
} }
[TestMethod] [TestMethod]
public void Parse_Error_Status_Test() public void Parse_Error_Status_Test()
@ -38,8 +42,9 @@ namespace dopt.DeltaBarth.Tests
pyJson = (string)plugin.pyModJsonData.status_err(); pyJson = (string)plugin.pyModJsonData.status_err();
} }
var parsed = JsonSerializer.Deserialize<DataObjects.Status>(pyJson); var parsed = JsonSerializer.Deserialize<DataObjects.Status>(pyJson);
if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); }
Assert.AreEqual(102, parsed.code); 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.AreEqual("caused by test", parsed.message);
Assert.IsNull(parsed.apiServerError); Assert.IsNull(parsed.apiServerError);
PrettyPrint(parsed); PrettyPrint(parsed);
@ -55,6 +60,7 @@ namespace dopt.DeltaBarth.Tests
pyJson = (string)plugin.pyModJsonData.delta_barth_api_error(); pyJson = (string)plugin.pyModJsonData.delta_barth_api_error();
} }
var parsed = JsonSerializer.Deserialize<DataObjects.ApiServerError>(pyJson); var parsed = JsonSerializer.Deserialize<DataObjects.ApiServerError>(pyJson);
if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); }
Assert.AreEqual(401, parsed.status_code); Assert.AreEqual(401, parsed.status_code);
Assert.AreEqual("test message", parsed.message); Assert.AreEqual("test message", parsed.message);
Assert.AreEqual("test code", parsed.code); Assert.AreEqual("test code", parsed.code);
@ -75,6 +81,7 @@ namespace dopt.DeltaBarth.Tests
pyJson = (string)plugin.pyModJsonData.api_credentials(); pyJson = (string)plugin.pyModJsonData.api_credentials();
} }
var parsed = JsonSerializer.Deserialize<DataObjects.Credentials>(pyJson); var parsed = JsonSerializer.Deserialize<DataObjects.Credentials>(pyJson);
if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); }
Assert.AreEqual("user", parsed.username); Assert.AreEqual("user", parsed.username);
Assert.AreEqual("pass", parsed.password); Assert.AreEqual("pass", parsed.password);
Assert.AreEqual("test1", parsed.database); Assert.AreEqual("test1", parsed.database);
@ -92,6 +99,7 @@ namespace dopt.DeltaBarth.Tests
pyJson = (string)plugin.pyModJsonData.sales_prognosis_result(); pyJson = (string)plugin.pyModJsonData.sales_prognosis_result();
} }
var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseEinzelergebnis>(pyJson); var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseEinzelergebnis>(pyJson);
if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); }
Assert.AreEqual(2023, parsed.jahr); Assert.AreEqual(2023, parsed.jahr);
Assert.AreEqual(12, parsed.monat); Assert.AreEqual(12, parsed.monat);
Assert.AreEqual(3000.3456m, parsed.vorhersage); Assert.AreEqual(3000.3456m, parsed.vorhersage);
@ -108,6 +116,7 @@ namespace dopt.DeltaBarth.Tests
pyJson = (string)plugin.pyModJsonData.sales_prognosis_results(); pyJson = (string)plugin.pyModJsonData.sales_prognosis_results();
} }
var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseErgebnisse>(pyJson); var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseErgebnisse>(pyJson);
Assert.IsNotNull(parsed);
Assert.AreEqual(3, parsed.daten.Length); Assert.AreEqual(3, parsed.daten.Length);
var e1 = new DataObjects.UmsatzPrognoseEinzelergebnis { jahr = 2023, monat = 12, vorhersage = 3000.3456m }; var e1 = new DataObjects.UmsatzPrognoseEinzelergebnis { jahr = 2023, monat = 12, vorhersage = 3000.3456m };
var e2 = new DataObjects.UmsatzPrognoseEinzelergebnis { jahr = 2024, monat = 1, vorhersage = 3300.685m }; 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(); pyJson = (string)plugin.pyModJsonData.sales_prognosis_results_export();
} }
var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseAusgabe>(pyJson); var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseAusgabe>(pyJson);
if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); }
// result // result
var e1 = new DataObjects.UmsatzPrognoseEinzelergebnis { jahr = 2023, monat = 12, vorhersage = 3000.3456m }; var e1 = new DataObjects.UmsatzPrognoseEinzelergebnis { jahr = 2023, monat = 12, vorhersage = 3000.3456m };
var e2 = new DataObjects.UmsatzPrognoseEinzelergebnis { jahr = 2024, monat = 1, vorhersage = 3300.685m }; var e2 = new DataObjects.UmsatzPrognoseEinzelergebnis { jahr = 2024, monat = 1, vorhersage = 3300.685m };

View File

@ -7,44 +7,44 @@ using System.Collections.Immutable;
namespace dopt.DeltaBarth.DataObjects namespace dopt.DeltaBarth.DataObjects
{ {
public readonly struct ApiServerError public record class ApiServerError
{ {
public int status_code { get; init; } public required int status_code { get; init; }
public string message { get; init; } public required string message { get; init; }
public string? code { get; init; } public string? code { get; init; }
public string? hints { get; init; } public string? hints { get; init; }
public string? type { get; init; } public string? type { get; init; }
public string? title { get; init; } public string? title { get; init; }
public string? traceId { get; init; } public string? traceId { get; init; }
} }
public readonly struct Status public record class Status
{ {
public int code { get; init; } public required int code { get; init; }
public string description { get; init; } public required string description { get; init; }
public string message { get; init; } public required string message { get; init; }
public ApiServerError? apiServerError { get; init; } public ApiServerError? apiServerError { get; init; }
} }
public readonly struct Credentials public record class Credentials
{ {
public string username { get; init; } public required string username { get; init; }
public string password { get; init; } public required string password { get; init; }
public string database { get; init; } public required string database { get; init; }
public string mandant { get; init; } public required string mandant { get; init; }
} }
public readonly struct UmsatzPrognoseEinzelergebnis public record class UmsatzPrognoseEinzelergebnis
{ {
public int jahr { get; init; } public required int jahr { get; init; }
public int monat { get; init; } public required int monat { get; init; }
public decimal vorhersage { get; init; } public required decimal vorhersage { get; init; }
} }
public readonly struct UmsatzPrognoseErgebnisse public record class UmsatzPrognoseErgebnisse
{ {
public ImmutableArray<UmsatzPrognoseEinzelergebnis> daten { get; init; } public required ImmutableArray<UmsatzPrognoseEinzelergebnis> daten { get; init; }
} }
public readonly struct UmsatzPrognoseAusgabe public record class UmsatzPrognoseAusgabe
{ {
public UmsatzPrognoseErgebnisse response { get; init; } public required UmsatzPrognoseErgebnisse response { get; init; }
public Status status { get; init; } public required Status status { get; init; }
} }
} }

View File

@ -3,6 +3,11 @@ using System.Text.Json;
namespace dopt.DeltaBarth namespace dopt.DeltaBarth
{ {
public class PythonParsingException : Exception
{
public PythonParsingException() { }
public PythonParsingException(string message) : base(message) { }
}
public class Plugin : SharpPython.BasePlugin public class Plugin : SharpPython.BasePlugin
{ {
protected dynamic pyModManagement; protected dynamic pyModManagement;
@ -38,6 +43,7 @@ namespace dopt.DeltaBarth
pyJson = pyModPipeline.pipeline_sales_forecast_dummy(firmaId, buchungsDatum); pyJson = pyModPipeline.pipeline_sales_forecast_dummy(firmaId, buchungsDatum);
} }
var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseAusgabe>(pyJson); var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseAusgabe>(pyJson);
if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); }
return parsed; return parsed;
} }
@ -50,6 +56,7 @@ namespace dopt.DeltaBarth
pyJson = pyModPipeline.pipeline_sales_forecast(firmaId, buchungsDatum); pyJson = pyModPipeline.pipeline_sales_forecast(firmaId, buchungsDatum);
} }
var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseAusgabe>(pyJson); var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseAusgabe>(pyJson);
if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); }
return parsed; return parsed;
} }
@ -90,9 +97,10 @@ namespace dopt.DeltaBarth
pyJson = (string)pyModManagement.get_credentials(); pyJson = (string)pyModManagement.get_credentials();
} }
DataObjects.Credentials credentials = JsonSerializer.Deserialize<DataObjects.Credentials>(pyJson); DataObjects.Credentials? parsed = JsonSerializer.Deserialize<DataObjects.Credentials>(pyJson);
if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); }
return credentials; return parsed;
} }
} }
} }

View File

@ -6,7 +6,7 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<PlatformTarget>x64</PlatformTarget> <PlatformTarget>x64</PlatformTarget>
<Platforms>x64</Platforms> <Platforms>x64</Platforms>
<Version>0.3.0</Version> <Version>0.3.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>