using Python.Runtime; using System.Diagnostics; using System.Text.Json; namespace dopt.DeltaBarth.Tests { using System.Collections.Immutable; internal class TestPlugin : DeltaBarth.Plugin { internal dynamic pyModJsonStructs; internal TestPlugin() : base() { using (Py.GIL()) { pyModJsonStructs = Py.Import("delta_barth._csharp.json_types"); } } } [TestClass] public class JsonStructsTest { static void PrettyPrint(object toSerialise) { string prettyJson = JsonSerializer.Serialize(toSerialise, new JsonSerializerOptions { WriteIndented = true }); Console.WriteLine($"Parsed struct is: {prettyJson}"); } [TestMethod] public void Parse_Error_Status_Test() { var plugin = new TestPlugin(); string pyJson; using (Py.GIL()) { pyJson = (string)plugin.pyModJsonStructs.status_err(); } var parsed = JsonSerializer.Deserialize(pyJson); Assert.AreEqual(102, parsed.code); Assert.AreEqual("internal error occurred", parsed.description); Assert.AreEqual("caused by test", parsed.message); Assert.IsNull(parsed.apiServerError); PrettyPrint(parsed); plugin.Shutdown(); } [TestMethod] public void Parse_ApiError_Test() { var plugin = new TestPlugin(); string pyJson; using (Py.GIL()) { pyJson = (string)plugin.pyModJsonStructs.delta_barth_api_error(); } var parsed = JsonSerializer.Deserialize(pyJson); Assert.AreEqual(401, parsed.status_code); Assert.AreEqual("test message", parsed.message); Assert.AreEqual("test code", parsed.code); Assert.IsNull(parsed.hints); Assert.IsNull(parsed.type); Assert.IsNull(parsed.title); Assert.IsNull(parsed.traceId); PrettyPrint(parsed); plugin.Shutdown(); } [TestMethod] public void Parse_Credentials_Test() { var plugin = new TestPlugin(); string pyJson; using (Py.GIL()) { pyJson = (string)plugin.pyModJsonStructs.api_credentials(); } var parsed = JsonSerializer.Deserialize(pyJson); Assert.AreEqual("user", parsed.username); Assert.AreEqual("pass", parsed.password); Assert.AreEqual("test1", parsed.database); Assert.AreEqual("mandant1", parsed.mandant); PrettyPrint(parsed); plugin.Shutdown(); } [TestMethod] public void Parse_SalesPrognosisResult_Test() { var plugin = new TestPlugin(); string pyJson; using (Py.GIL()) { pyJson = (string)plugin.pyModJsonStructs.sales_prognosis_result(); } var parsed = JsonSerializer.Deserialize(pyJson); Assert.AreEqual(2023, parsed.jahr); Assert.AreEqual(12, parsed.monat); Assert.AreEqual(3000.3456m, parsed.vorhersage); PrettyPrint(parsed); plugin.Shutdown(); } [TestMethod] public void Parse_SalesPrognosisResults_Test() { var plugin = new TestPlugin(); string pyJson; using (Py.GIL()) { pyJson = (string)plugin.pyModJsonStructs.sales_prognosis_results(); } var parsed = JsonSerializer.Deserialize(pyJson); Assert.AreEqual(3, parsed.daten.Length); var e1 = new JsonStructs.UmsatzPrognoseEinzelergebnis { jahr = 2023, monat = 12, vorhersage = 3000.3456m }; var e2 = new JsonStructs.UmsatzPrognoseEinzelergebnis { jahr = 2024, monat = 1, vorhersage = 3300.685m }; var e3 = new JsonStructs.UmsatzPrognoseEinzelergebnis { jahr = 2024, monat = 2, vorhersage = 3700.548m }; ImmutableArray arr = ImmutableArray.Create(e1, e2, e3); for (int i = 0; i < parsed.daten.Length; i++) { Assert.AreEqual(arr[i], parsed.daten[i]); } PrettyPrint(parsed); plugin.Shutdown(); } [TestMethod] public void Parse_SalesPrognosisExport_Test() { var plugin = new TestPlugin(); string pyJson; using (Py.GIL()) { pyJson = (string)plugin.pyModJsonStructs.sales_prognosis_results_export(); } var parsed = JsonSerializer.Deserialize(pyJson); // result var e1 = new JsonStructs.UmsatzPrognoseEinzelergebnis { jahr = 2023, monat = 12, vorhersage = 3000.3456m }; var e2 = new JsonStructs.UmsatzPrognoseEinzelergebnis { jahr = 2024, monat = 1, vorhersage = 3300.685m }; var e3 = new JsonStructs.UmsatzPrognoseEinzelergebnis { jahr = 2024, monat = 2, vorhersage = 3700.548m }; ImmutableArray arr = ImmutableArray.Create(e1, e2, e3); var data = new JsonStructs.UmsatzPrognoseErgebnisse { daten = arr }; // check status Assert.AreEqual(0, parsed.status.code); Assert.AreEqual("Erfolg", parsed.status.description); Assert.AreEqual("", parsed.status.message); Assert.IsNull(parsed.status.apiServerError); // check result for (int i = 0; i < parsed.response.daten.Length; i++) { Assert.AreEqual(data.daten[i], parsed.response.daten[i]); } PrettyPrint(parsed); plugin.Shutdown(); } } }