165 lines
6.9 KiB
C#
165 lines
6.9 KiB
C#
using Python.Runtime;
|
|
using System.Diagnostics;
|
|
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;
|
|
private const string absPath = @"A:\Arbeitsaufgaben\Delta-Barth\cs-wrapper\dopt.DeltaBarth";
|
|
internal TestPlugin() : base(absPath) {
|
|
using (Py.GIL())
|
|
{
|
|
pyModJsonData = Py.Import("delta_barth._csharp.json_types");
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestClass]
|
|
public class DataObjectsTest
|
|
{
|
|
static void PrettyPrint(object toSerialise)
|
|
{
|
|
string prettyJson = JsonSerializer.Serialize(toSerialise, new JsonSerializerOptions
|
|
{
|
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
|
WriteIndented = true
|
|
});
|
|
Console.WriteLine($"Parsed data is: {prettyJson}");
|
|
}
|
|
[TestMethod]
|
|
public void Parse_Error_Status_Test()
|
|
{
|
|
var plugin = new TestPlugin();
|
|
string pyJson;
|
|
using (Py.GIL())
|
|
{
|
|
pyJson = (string)plugin.pyModJsonData.status_err();
|
|
}
|
|
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.IsTrue(parsed.description.Contains("internal error occurred"));
|
|
Assert.AreEqual("caused by test", parsed.message);
|
|
Assert.IsNull(parsed.apiServerError);
|
|
PrettyPrint(parsed);
|
|
plugin.Dispose();
|
|
}
|
|
[TestMethod]
|
|
public void Parse_ApiError_Test()
|
|
{
|
|
var plugin = new TestPlugin();
|
|
string pyJson;
|
|
using (Py.GIL())
|
|
{
|
|
pyJson = (string)plugin.pyModJsonData.delta_barth_api_error();
|
|
}
|
|
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("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.Dispose();
|
|
}
|
|
[TestMethod]
|
|
public void Parse_Credentials_Test()
|
|
{
|
|
var plugin = new TestPlugin();
|
|
string pyJson;
|
|
using (Py.GIL())
|
|
{
|
|
pyJson = (string)plugin.pyModJsonData.api_credentials();
|
|
}
|
|
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("pass", parsed.password);
|
|
Assert.AreEqual("test1", parsed.database);
|
|
Assert.AreEqual("mandant1", parsed.mandant);
|
|
PrettyPrint(parsed);
|
|
plugin.Dispose();
|
|
}
|
|
[TestMethod]
|
|
public void Parse_SalesPrognosisResult_Test()
|
|
{
|
|
var plugin = new TestPlugin();
|
|
string pyJson;
|
|
using (Py.GIL())
|
|
{
|
|
pyJson = (string)plugin.pyModJsonData.sales_prognosis_result();
|
|
}
|
|
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(12, parsed.monat);
|
|
Assert.AreEqual(3000.3456m, parsed.vorhersage);
|
|
PrettyPrint(parsed);
|
|
plugin.Dispose();
|
|
}
|
|
[TestMethod]
|
|
public void Parse_SalesPrognosisResults_Test()
|
|
{
|
|
var plugin = new TestPlugin();
|
|
string pyJson;
|
|
using (Py.GIL())
|
|
{
|
|
pyJson = (string)plugin.pyModJsonData.sales_prognosis_results();
|
|
}
|
|
var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseErgebnisse>(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 };
|
|
var e3 = new DataObjects.UmsatzPrognoseEinzelergebnis { jahr = 2024, monat = 2, vorhersage = 3700.548m };
|
|
ImmutableArray<DataObjects.UmsatzPrognoseEinzelergebnis> 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.Dispose();
|
|
}
|
|
[TestMethod]
|
|
public void Parse_SalesPrognosisExport_Test()
|
|
{
|
|
var plugin = new TestPlugin();
|
|
string pyJson;
|
|
using (Py.GIL())
|
|
{
|
|
pyJson = (string)plugin.pyModJsonData.sales_prognosis_results_export();
|
|
}
|
|
var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseAusgabe>(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 };
|
|
var e3 = new DataObjects.UmsatzPrognoseEinzelergebnis { jahr = 2024, monat = 2, vorhersage = 3700.548m };
|
|
ImmutableArray<DataObjects.UmsatzPrognoseEinzelergebnis> arr = ImmutableArray.Create(e1, e2, e3);
|
|
var data = new DataObjects.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.Dispose();
|
|
}
|
|
}
|
|
}
|