8 Commits

8 changed files with 171 additions and 114 deletions

1
.gitignore vendored
View File

@@ -4,6 +4,7 @@
## Get latest from `dotnet new gitignore`
**/python/
CREDENTIALS*
test_data_path/
# dotenv files
.env

View File

@@ -5,27 +5,32 @@ 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 pyModJsonStructs;
internal TestPlugin() : base() {
internal dynamic pyModJsonData;
private const string absPath = @"A:\Arbeitsaufgaben\Delta-Barth\cs-wrapper\dopt.DeltaBarth";
internal TestPlugin() : base(absPath) {
using (Py.GIL())
{
pyModJsonStructs = Py.Import("delta_barth._csharp.json_types");
pyModJsonData = Py.Import("delta_barth._csharp.json_types");
}
}
}
[TestClass]
public class JsonStructsTest
public class DataObjectsTest
{
static void PrettyPrint(object toSerialise)
{
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()
@@ -34,11 +39,12 @@ namespace dopt.DeltaBarth.Tests
string pyJson;
using (Py.GIL())
{
pyJson = (string)plugin.pyModJsonStructs.status_err();
pyJson = (string)plugin.pyModJsonData.status_err();
}
var parsed = JsonSerializer.Deserialize<JsonStructs.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("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);
@@ -51,9 +57,10 @@ namespace dopt.DeltaBarth.Tests
string pyJson;
using (Py.GIL())
{
pyJson = (string)plugin.pyModJsonStructs.delta_barth_api_error();
pyJson = (string)plugin.pyModJsonData.delta_barth_api_error();
}
var parsed = JsonSerializer.Deserialize<JsonStructs.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("test message", parsed.message);
Assert.AreEqual("test code", parsed.code);
@@ -71,9 +78,10 @@ namespace dopt.DeltaBarth.Tests
string pyJson;
using (Py.GIL())
{
pyJson = (string)plugin.pyModJsonStructs.api_credentials();
pyJson = (string)plugin.pyModJsonData.api_credentials();
}
var parsed = JsonSerializer.Deserialize<JsonStructs.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("pass", parsed.password);
Assert.AreEqual("test1", parsed.database);
@@ -88,9 +96,10 @@ namespace dopt.DeltaBarth.Tests
string pyJson;
using (Py.GIL())
{
pyJson = (string)plugin.pyModJsonStructs.sales_prognosis_result();
pyJson = (string)plugin.pyModJsonData.sales_prognosis_result();
}
var parsed = JsonSerializer.Deserialize<JsonStructs.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(12, parsed.monat);
Assert.AreEqual(3000.3456m, parsed.vorhersage);
@@ -104,14 +113,15 @@ namespace dopt.DeltaBarth.Tests
string pyJson;
using (Py.GIL())
{
pyJson = (string)plugin.pyModJsonStructs.sales_prognosis_results();
pyJson = (string)plugin.pyModJsonData.sales_prognosis_results();
}
var parsed = JsonSerializer.Deserialize<JsonStructs.UmsatzPrognoseErgebnisse>(pyJson);
var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseErgebnisse>(pyJson);
Assert.IsNotNull(parsed);
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<JsonStructs.UmsatzPrognoseEinzelergebnis> arr = ImmutableArray.Create(e1, e2, e3);
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++)
{
@@ -127,15 +137,16 @@ namespace dopt.DeltaBarth.Tests
string pyJson;
using (Py.GIL())
{
pyJson = (string)plugin.pyModJsonStructs.sales_prognosis_results_export();
pyJson = (string)plugin.pyModJsonData.sales_prognosis_results_export();
}
var parsed = JsonSerializer.Deserialize<JsonStructs.UmsatzPrognoseAusgabe>(pyJson);
var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseAusgabe>(pyJson);
if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); }
// 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<JsonStructs.UmsatzPrognoseEinzelergebnis> arr = ImmutableArray.Create(e1, e2, e3);
var data = new JsonStructs.UmsatzPrognoseErgebnisse { daten = arr };
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);

View File

@@ -4,6 +4,7 @@ using static Microsoft.ApplicationInsights.MetricDimensionNames.TelemetryContext
namespace dopt.DeltaBarth.Tests
{
using System.Text.Encodings.Web;
using Microsoft.Extensions.Configuration;
internal class Config
{
@@ -37,7 +38,9 @@ namespace dopt.DeltaBarth.Tests
}
public class TPlugin : DeltaBarth.Plugin
{
public new JsonStructs.Credentials GetCredentials()
private const string absPath = @"A:\Arbeitsaufgaben\Delta-Barth\cs-wrapper\dopt.DeltaBarth";
public TPlugin() : base(absPath) { }
public new DataObjects.Credentials GetCredentials()
{
return base.GetCredentials();
}
@@ -45,15 +48,25 @@ namespace dopt.DeltaBarth.Tests
{
return base.GetBaseApiUrl();
}
public new string GetDataPath()
{
return base.GetDataPath();
}
public new void Setup(string datenPfad, string basisApiUrl)
{
base.Setup(datenPfad, basisApiUrl);
}
}
[TestClass]
public sealed class PluginTest
{
private const string baseDataPath = @"A:\Arbeitsaufgaben\Delta-Barth\cs-wrapper\dopt.DeltaBarth\test_data_path";
internal Config config = new Config();
static void PrettyPrint(object toSerialise)
{
string prettyJson = JsonSerializer.Serialize(toSerialise, new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
WriteIndented = true
});
Console.WriteLine($"Parsed struct is: {prettyJson}");
@@ -79,13 +92,19 @@ namespace dopt.DeltaBarth.Tests
test.Dispose();
}
[TestMethod]
public void Set_and_Obtain_BaseApiUrl_Test()
public void SetupSession_Test()
{
var test = new TPlugin();
string apiUrlSet = "http://10.2.22.21:8080/api/";
test.SetzeBasisApiUrl(apiUrlSet);
test.Setup(baseDataPath, apiUrlSet);
// data path
var dataPathGet = test.GetDataPath();
Console.WriteLine($"Result for data path from Python session was: {dataPathGet}");
bool pathElementsContained = dataPathGet.Contains("test_data_path");
Assert.IsTrue(pathElementsContained);
// API URL
var apiUrlGet = test.GetBaseApiUrl();
Console.WriteLine($"Result from Python session was: {apiUrlGet}");
Console.WriteLine($"Result for API URL from Python session was: {apiUrlGet}");
Assert.AreEqual(apiUrlSet, apiUrlGet);
test.Dispose();
}
@@ -94,7 +113,7 @@ namespace dopt.DeltaBarth.Tests
{
var test = new TPlugin();
string apiUrlSet = "http://10.2.22.21:8080/api/", user = "user", password = "password", database = "DB1", mandant = "mandant1";
test.Startup(apiUrlSet, user, password, database, mandant);
test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant);
var apiUrlGet = test.GetBaseApiUrl();
var creds = test.GetCredentials();
Console.WriteLine($"Result from Python session was: API-URL={apiUrlGet}, creds={creds}");
@@ -111,7 +130,7 @@ namespace dopt.DeltaBarth.Tests
{
var test = new TPlugin();
string apiUrlSet = "http://10.2.22.21:8080/api/", user = "user", password = "password", database = "DB1", mandant = "mandant1";
test.Startup(apiUrlSet, user, password, database, mandant);
test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant);
var res = test.UmsatzprognoseDummy(null, null);
PrettyPrint(res);
test.Dispose();
@@ -121,7 +140,7 @@ namespace dopt.DeltaBarth.Tests
{
var test = new TPlugin();
string apiUrlSet = "http://10.2.22.21:8080/api/", user = "user", password = "password", database = "DB1", mandant = "mandant1";
test.Startup(apiUrlSet, user, password, database, mandant);
test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant);
var comp_id = 1000;
var res = test.UmsatzprognoseDummy(comp_id, null);
PrettyPrint(res);
@@ -132,22 +151,33 @@ namespace dopt.DeltaBarth.Tests
{
var test = new TPlugin();
string apiUrlSet = "http://10.2.22.21:8080/api/", user = "user", password = "password", database = "DB1", mandant = "mandant1";
test.Startup(apiUrlSet, user, password, database, mandant);
test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant);
var date = new DateTime(2023, 1, 1, 12, 45, 30);
var res = test.UmsatzprognoseDummy(null, date);
PrettyPrint(res);
test.Dispose();
}
[TestMethod]
public void Umsatzprognose_NoConnectionNeeded_Test()
{
var test = new TPlugin();
string apiUrlSet = config.apiUrl, user = config.username, password = config.password, database = config.database, mandant = config.mandant;
test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant);
var date = new DateTime(2030, 1, 1, 12, 45, 30);
var res = test.Umsatzprognose(null, date);
PrettyPrint(res);
test.Dispose();
}
[TestMethod]
[TestCategory("ActiveAPI")]
public void Umsatzprognose_WithCompanyIdWithoutDate_Test()
{
var test = new TPlugin();
string apiUrlSet = config.apiUrl, user = config.username, password = config.password, database = config.database, mandant = config.mandant;
test.Startup(apiUrlSet, user, password, database, mandant);
test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant);
var comp_id = 1024;
var res = test.Umsatzprognose(comp_id, null);
Assert.AreEqual(4, res.status.code);
PrettyPrint(res);
test.Dispose();
}
@@ -157,9 +187,10 @@ namespace dopt.DeltaBarth.Tests
{
var test = new TPlugin();
string apiUrlSet = config.apiUrl, user = config.username, password = config.password, database = config.database, mandant = config.mandant;
test.Startup(apiUrlSet, user, password, database, mandant);
test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant);
var date = new DateTime(2030, 1, 1, 12, 45, 30);
var res = test.Umsatzprognose(null, date);
Assert.AreEqual(3, res.status.code);
PrettyPrint(res);
test.Dispose();
}
@@ -169,9 +200,10 @@ namespace dopt.DeltaBarth.Tests
{
var test = new TPlugin();
string apiUrlSet = config.apiUrl, user = config.username, password = config.password, database = config.database, mandant = config.mandant;
test.Startup(apiUrlSet, user, password, database, mandant);
test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant);
var date = new DateTime(2015, 1, 1, 12, 45, 30);
var res = test.Umsatzprognose(null, date);
Assert.AreEqual(4, res.status.code);
PrettyPrint(res);
test.Dispose();
}

View File

@@ -23,10 +23,6 @@
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.14.2" />
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="1.6.3" />
<PackageReference Include="MSTest" Version="3.8.3" />
<Content Include="..\python\**\*">
<Link>python\%(RecursiveDir)/%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\CREDENTIALS.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Immutable;
namespace dopt.DeltaBarth.DataObjects
{
public record class ApiServerError
{
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 record class Status
{
public required int code { get; init; }
public required string description { get; init; }
public required string message { get; init; }
public ApiServerError? apiServerError { get; init; }
}
public record class Credentials
{
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 record class UmsatzPrognoseEinzelergebnis
{
public required int jahr { get; init; }
public required int monat { get; init; }
public required decimal vorhersage { get; init; }
}
public record class UmsatzPrognoseErgebnisse
{
public required ImmutableArray<UmsatzPrognoseEinzelergebnis> daten { get; init; }
}
public record class UmsatzPrognoseAusgabe
{
public required UmsatzPrognoseErgebnisse response { get; init; }
public required Status status { get; init; }
}
}

View File

@@ -1,50 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Immutable;
namespace dopt.DeltaBarth.JsonStructs
{
public readonly struct ApiServerError
{
public int status_code { get; init; }
public 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 int code { get; init; }
public string description { get; init; }
public string message { get; init; }
public ApiServerError? apiServerError { get; init; }
}
public readonly struct Credentials
{
public string username { get; init; }
public string password { get; init; }
public string database { get; init; }
public string mandant { get; init; }
}
public readonly struct UmsatzPrognoseEinzelergebnis
{
public int jahr { get; init; }
public int monat { get; init; }
public decimal vorhersage { get; init; }
}
public readonly struct UmsatzPrognoseErgebnisse
{
public ImmutableArray<UmsatzPrognoseEinzelergebnis> daten { get; init; }
}
public readonly struct UmsatzPrognoseAusgabe
{
public UmsatzPrognoseErgebnisse response { get; init; }
public Status status { get; init; }
}
}

View File

@@ -3,11 +3,16 @@ 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;
protected dynamic pyModPipeline;
public Plugin() : base(verbose: false)
public Plugin(string runtimePath) : base(runtimePath: runtimePath, verbose: false)
{
base.Initialise();
using (Py.GIL())
@@ -16,17 +21,12 @@ namespace dopt.DeltaBarth
pyModPipeline = Py.Import("delta_barth.pipelines");
}
}
public void Startup(string basisApiUrl, string nutzername, string passwort, string datenbank, string mandant)
public void Startup(string datenPfad, string basisApiUrl, string nutzername, string passwort, string datenbank, string mandant)
{
AssertNotDisposed();
SetzeBasisApiUrl(basisApiUrl);
Setup(datenPfad, basisApiUrl);
SetzeNutzerdaten(nutzername, passwort, datenbank, mandant);
}
public void SetzeBasisApiUrl(string basisApiUrl)
{
AssertNotDisposed();
pyModManagement.set_base_url(basisApiUrl);
}
public void SetzeNutzerdaten(string nutzername, string passwort, string datenbank, string mandant)
{
AssertNotDisposed();
@@ -34,7 +34,7 @@ namespace dopt.DeltaBarth
pyModManagement.set_credentials(nutzername, passwort, datenbank, mandant);
}
}
public JsonStructs.UmsatzPrognoseAusgabe UmsatzprognoseDummy(int? firmaId, DateTime? buchungsDatum)
public DataObjects.UmsatzPrognoseAusgabe UmsatzprognoseDummy(int? firmaId, DateTime? buchungsDatum)
{
AssertNotDisposed();
string pyJson;
@@ -42,11 +42,12 @@ namespace dopt.DeltaBarth
{
pyJson = pyModPipeline.pipeline_sales_forecast_dummy(firmaId, buchungsDatum);
}
var parsed = JsonSerializer.Deserialize<JsonStructs.UmsatzPrognoseAusgabe>(pyJson);
var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseAusgabe>(pyJson);
if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); }
return parsed;
}
public JsonStructs.UmsatzPrognoseAusgabe Umsatzprognose(int? firmaId, DateTime? buchungsDatum)
public DataObjects.UmsatzPrognoseAusgabe Umsatzprognose(int? firmaId, DateTime? buchungsDatum)
{
AssertNotDisposed();
string pyJson;
@@ -54,10 +55,19 @@ namespace dopt.DeltaBarth
{
pyJson = pyModPipeline.pipeline_sales_forecast(firmaId, buchungsDatum);
}
var parsed = JsonSerializer.Deserialize<JsonStructs.UmsatzPrognoseAusgabe>(pyJson);
var parsed = JsonSerializer.Deserialize<DataObjects.UmsatzPrognoseAusgabe>(pyJson);
if (parsed == null) { throw new PythonParsingException("Could not correctly parse object from Python"); }
return parsed;
}
protected void Setup(string datenPfad, string basisApiUrl)
{
AssertNotDisposed();
using (Py.GIL())
{
pyModManagement.setup(datenPfad, basisApiUrl);
}
}
protected string GetBaseApiUrl()
{
AssertNotDisposed();
@@ -68,7 +78,17 @@ namespace dopt.DeltaBarth
}
return pyJson;
}
protected JsonStructs.Credentials GetCredentials()
protected string GetDataPath()
{
AssertNotDisposed();
string pyJson;
using (Py.GIL())
{
pyJson = (string)pyModManagement.get_data_path();
}
return pyJson;
}
protected DataObjects.Credentials GetCredentials()
{
AssertNotDisposed();
string pyJson;
@@ -77,9 +97,10 @@ namespace dopt.DeltaBarth
pyJson = (string)pyModManagement.get_credentials();
}
JsonStructs.Credentials credentials = JsonSerializer.Deserialize<JsonStructs.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,15 +6,11 @@
<Nullable>enable</Nullable>
<PlatformTarget>x64</PlatformTarget>
<Platforms>x64</Platforms>
<Version>0.1.0-alpha1</Version>
<Version>0.3.2</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="dopt.SharpPython" Version="0.3.0" />
<Content Include="..\python\**\*">
<Link>python\%(RecursiveDir)/%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<PackageReference Include="dopt.SharpPython" Version="0.4.0" />
</ItemGroup>
</Project>