Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from DataConverter to AsyncDataImporter #2

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 113 additions & 54 deletions idee5.Globalization.Test/ImportResoucesTests.cs
Original file line number Diff line number Diff line change
@@ -1,69 +1,128 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Text;
using System.Threading.Tasks;

using idee5.Common.Data;
using idee5.Globalization.Commands;
using idee5.Globalization.Models;
using idee5.Globalization.Queries;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace idee5.Globalization.Test {
[TestClass]
public class ImportResoucesTests : WithSQLiteBase {
[TestMethod]
public async Task CanImportResources() {
// Arrange
var resourcesToImport = new Resource[] {
// update existing resource (value changed)
new() { Id = "Maybe", ResourceSet = Constants.CommonTerms, BinFile = null, Textfile = null, Comment = null, Customer = "idee5", Industry = "IT", Language = "de-CH", Value = "Villücht (Branche + Kunde)" },
// insert simple resource
new() { Id = "xyz", ResourceSet = Constants.CommonTerms, BinFile = null, Textfile = null, Comment = null, Customer = "", Industry = "", Language = "de-CH", Value = "xyz" },
// insert textfile
new() { Id = "textfile", ResourceSet = Constants.CommonTerms, BinFile = null, Textfile = null, Comment = null, Customer = "", Industry = "", Language = "", Value = "app.config" },
// insert binfile
new() { Id = "binfile", ResourceSet = Constants.CommonTerms, BinFile = null, Textfile = null, Comment = null, Customer = "", Industry = "", Language = "", Value = "New.png" },
// same as existing resource
new() { Id = "Maybe", ResourceSet = Constants.CommonTerms, BinFile = null, Textfile = null, Comment = null, Customer = "", Industry = "", Language = "", Value = "Maybee" }
};
var cmd = new ImportResourcesCommand([]) {
Resources = resourcesToImport
};
var handler = new ImportResourcesCommandHandler(resourceUnitOfWork);
var startCount = await context.Resources.CountAsync().ConfigureAwait(false);
// Act
await handler.HandleAsync(cmd).ConfigureAwait(false);
var endCount = await context.Resources.CountAsync().ConfigureAwait(false);
// Assert
Assert.AreEqual(3, endCount - startCount);
Resource? maybeCH = await resourceUnitOfWork.ResourceRepository.GetSingleAsync(r => r.Id == "Maybe" && r.ResourceSet == Constants.CommonTerms && r.Customer == "idee5" && r.Language == "de-CH" && r.Industry == "IT").ConfigureAwait(false);
Assert.AreEqual("Villücht (Branche + Kunde)", maybeCH?.Value);
}
namespace idee5.Globalization.Test;
[TestClass]
public class ImportResoucesTests : WithSQLiteBase {
[TestMethod]
public async Task CanImportResources() {
// Arrange
var resourcesToImport = new Resource[] {
// update existing resource (value changed)
new() { Id = "Maybe", ResourceSet = Constants.CommonTerms, BinFile = null, Textfile = null, Comment = null, Customer = "idee5", Industry = "IT", Language = "de-CH", Value = "Villücht (Branche + Kunde)" },
// insert simple resource
new() { Id = "xyz", ResourceSet = Constants.CommonTerms, BinFile = null, Textfile = null, Comment = null, Customer = "", Industry = "", Language = "de-CH", Value = "xyz" },
// insert textfile
new() { Id = "textfile", ResourceSet = Constants.CommonTerms, BinFile = null, Textfile = null, Comment = null, Customer = "", Industry = "", Language = "", Value = "app.config" },
// insert binfile
new() { Id = "binfile", ResourceSet = Constants.CommonTerms, BinFile = null, Textfile = null, Comment = null, Customer = "", Industry = "", Language = "", Value = "New.png" },
// same as existing resource
new() { Id = "Maybe", ResourceSet = Constants.CommonTerms, BinFile = null, Textfile = null, Comment = null, Customer = "", Industry = "", Language = "", Value = "Maybee" }
};
var cmd = new ImportResourcesCommand([]) {
Resources = resourcesToImport
};
var handler = new ImportResourcesCommandHandler(resourceUnitOfWork);
var startCount = await context.Resources.CountAsync().ConfigureAwait(false);
// Act
await handler.HandleAsync(cmd).ConfigureAwait(false);
var endCount = await context.Resources.CountAsync().ConfigureAwait(false);
// Assert
Assert.AreEqual(3, endCount - startCount);
Resource? maybeCH = await resourceUnitOfWork.ResourceRepository.GetSingleAsync(r => r.Id == "Maybe" && r.ResourceSet == Constants.CommonTerms && r.Customer == "idee5" && r.Language == "de-CH" && r.Industry == "IT").ConfigureAwait(false);
Assert.AreEqual("Villücht (Branche + Kunde)", maybeCH?.Value);
}

[TestMethod]
public async Task CanImportResxFile() {
// Arrange
var handler = new CreateOrUpdateResourceCommandHandler(resourceUnitOfWork);
var recursiveAnnotationsValidator = new RecursiveAnnotationsValidator();
var validationReporter = new ConsoleValidationReporter();
var inputHandler = new ResxFileInputHandler(new NullLogger<ResxFileInputHandler>());
var outputHandler = new DataAnnotationValidationCommandHandlerAsync<CreateOrUpdateResourceCommand>(recursiveAnnotationsValidator, validationReporter, handler);

var importer = new AsyncDataImporter<ResxFileInputHandlerQuery, CreateOrUpdateResourceCommand, NoCleanupCommand>(inputHandler, outputHandler, new NoCleanupCommandHandler());
var query = new ResxFileInputHandlerQuery("", "", null, null, null) {
Path = "ImportTest.resx",
ResourceSet = "testset",
TargetLanguage = "de"
};

// This is needed to support the text file encoding in our resx
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// Act
await importer.ExecuteAsync(query, new NoCleanupCommand()).ConfigureAwait(false);

// Assert
List<Resource> res = await resourceUnitOfWork.ResourceRepository.GetAsync(r => r.ResourceSet == "testset").ConfigureAwait(false);
Assert.AreEqual(5, res.Count);
Assert.IsNotNull(res.SingleOrDefault(r => r.Id == "CommentedText")?.Comment);
Assert.IsNotNull(res.SingleOrDefault(r => r.Id == "Icon1")?.BinFile);
Assert.IsNotNull(res.SingleOrDefault(r => r.Id == "NewTextFile")?.Textfile);
}
[TestMethod]
public async Task DoesNotThrowIfResxNotFound() {
// Arrange
var handler = new CreateOrUpdateResourceCommandHandler(resourceUnitOfWork);
var recursiveAnnotationsValidator = new RecursiveAnnotationsValidator();
var validationReporter = new ConsoleValidationReporter();
using ILoggerFactory loggerFactory = LoggerFactory.Create(o => o.AddConsole());
var logger = loggerFactory.CreateLogger<ResxFileInputHandler>();
var inputHandler = new ResxFileInputHandler(logger);
var outputHandler = new DataAnnotationValidationCommandHandlerAsync<CreateOrUpdateResourceCommand>(recursiveAnnotationsValidator, validationReporter, handler);

var importer = new AsyncDataImporter<ResxFileInputHandlerQuery, CreateOrUpdateResourceCommand, NoCleanupCommand>(inputHandler, outputHandler, new NoCleanupCommandHandler());
var query = new ResxFileInputHandlerQuery("", "", null, null, null) {
Path = "DoesNotExist.resx",
ResourceSet = "testset",
TargetLanguage = "de"
};

// Act
await importer.ExecuteAsync(query, new NoCleanupCommand()).ConfigureAwait(false);

// Assert
int count = await resourceUnitOfWork.ResourceRepository.CountAsync(r => r.ResourceSet == "testset").ConfigureAwait(false);
Assert.AreEqual(0, count);
}
[TestMethod]
public async Task DoesNoThrowOnUnavailableFileLink() {
// Arrange
var handler = new CreateOrUpdateResourceCommandHandler(resourceUnitOfWork);
var recursiveAnnotationsValidator = new RecursiveAnnotationsValidator();
var validationReporter = new ConsoleValidationReporter();
using ILoggerFactory loggerFactory = LoggerFactory.Create(o => o.AddConsole());
var logger = loggerFactory.CreateLogger<ResxFileInputHandler>();
var inputHandler = new ResxFileInputHandler(logger);
var outputHandler = new DataAnnotationValidationCommandHandlerAsync<CreateOrUpdateResourceCommand>(recursiveAnnotationsValidator, validationReporter, handler);

[TestMethod]
public async Task CanImportResxFileAsync() {
// Arrange
var handler = new ImportResourcesCommandHandler(resourceUnitOfWork);
var recursiveAnnotationsValidator = new RecursiveAnnotationsValidator();
var validationReporter = new ConsoleValidationReporter();
var inputHandler = new ResxFileInputHandler();
var outputHandler = new DataAnnotationValidationCommandHandlerAsync<ImportResourcesCommand>(recursiveAnnotationsValidator, validationReporter, handler);
var converter = new DataConverterAsync<ResxFileInputHandlerQuery, ImportResourcesCommand>(inputHandler, outputHandler);

// TODO: var converter = new AsyncDataImporter<ResxFileInputHandlerQuery, ImportResourcesCommand, NoCleanupCommand>(inputHandler, outputHandler,new NoCleanupCommandHandler());
var query = new ResxFileInputHandlerQuery("", "", null, null, null) {
Path = "ImportTes.resx",
ResourceSet = "testset",
TargetLanguage = "de"
};
var importer = new AsyncDataImporter<ResxFileInputHandlerQuery, CreateOrUpdateResourceCommand, NoCleanupCommand>(inputHandler, outputHandler, new NoCleanupCommandHandler());
var query = new ResxFileInputHandlerQuery("", "", null, null, null) {
Path = "WrongFileLink.resx",
ResourceSet = "testset",
TargetLanguage = "de"
};

// Act
await converter.ExecuteAsync(query, CancellationToken.None).ConfigureAwait(false);
// Act
await importer.ExecuteAsync(query, new NoCleanupCommand()).ConfigureAwait(false);

// Assert
var count = await resourceUnitOfWork.ResourceRepository.CountAsync(r => r.ResourceSet == "testset").ConfigureAwait(false);
Assert.AreNotEqual(5, count);
}
// Assert
int count = await resourceUnitOfWork.ResourceRepository.CountAsync(r => r.ResourceSet == "testset").ConfigureAwait(false);
Assert.AreEqual(1, count);
}
}
124 changes: 124 additions & 0 deletions idee5.Globalization.Test/WrongFileLink.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="Icon1" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>DoesNotExist.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
32 changes: 30 additions & 2 deletions idee5.Globalization.Test/idee5.Globalization.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,47 @@
<Version>1.0.0-beta-68</Version>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Remove="ImportTest.resx" />
<EmbeddedResource Remove="WrongFileLink.resx" />
</ItemGroup>
<ItemGroup>
<None Include="WrongFileLink.resx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<SubType>Designer</SubType>
</None>
<None Include="ImportTest.resx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="idee5.Common.Data" version="2.2.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="NSpecifications" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.3" />
<PackageReference Include="System.Resources.Extensions" Version="8.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\idee5.Globalization.EFCore\idee5.Globalization.EFCore.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Resources\Icon1.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\NewImg.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\NewTextFile.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<!-- "fix" the wrong assembly name in .net core https://github.com/dotnet/corefx/issues/22101#issuecomment-371018696 -->
<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
<Copy SourceFiles="app.config" DestinationFiles="$(OutDir)\testhost.dll.config" />
Expand Down
2 changes: 1 addition & 1 deletion idee5.Globalization.Web/idee5.Globalization.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.2" />
<PackageReference Include="NSwag.AspNetCore" Version="14.0.3" />
<PackageReference Include="NSwag.AspNetCore" Version="14.0.7" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="idee5.AspNetCore" Version="1.1.1" />
<PackageReference Include="idee5.AspNetCore" Version="1.1.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading