From 9b4e4e57b5671d9d04eb023f13e7919345aed44b Mon Sep 17 00:00:00 2001 From: Valters Melnalksnis Date: Thu, 28 Nov 2024 09:29:52 +0200 Subject: [PATCH] test: add example of custom fields not working --- .../Documents/DocumentClientTests.cs | 14 ++- .../Example.cs | 112 ++++++++++++++++++ .../MinimalExampleTests.cs | 4 + .../PaperlessSetup.cs | 2 + .../PaperlessTests.cs | 2 + 5 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs diff --git a/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Documents/DocumentClientTests.cs b/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Documents/DocumentClientTests.cs index 742805e..e3b4229 100644 --- a/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Documents/DocumentClientTests.cs +++ b/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Documents/DocumentClientTests.cs @@ -10,6 +10,9 @@ using System.Net.Mime; using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; + using NodaTime; using VMelnalksnis.PaperlessDotNet.Documents; @@ -196,9 +199,10 @@ public async Task CustomFields() await Client.Documents.CreateCustomField(new("field6", CustomFieldType.Float)); await Client.Documents.CreateCustomField(new("field7", CustomFieldType.Monetary)); await Client.Documents.CreateCustomField(new("field8", CustomFieldType.DocumentLink)); + await Client.Documents.CreateCustomField(new("pfad", CustomFieldType.String)); var customFields = await Client.Documents.GetCustomFields().ToListAsync(); - customFields.Should().HaveCount(8); + customFields.Should().HaveCount(9); var paginatedCustomFields = await Client.Documents.GetCustomFields(1).ToListAsync(); paginatedCustomFields.Should().BeEquivalentTo(customFields); @@ -213,6 +217,14 @@ public async Task CustomFields() var id = result.Should().BeOfType().Subject.Id; var document = (await Client.Documents.Get(id))!; + var options = Services.GetRequiredService>().Value; + var client = new PaperlessNgxClient.PaperlessNgxClient(options.BaseAddress.AbsoluteUri, options.Token); + var documents2 = await client.GetDocuments(); + foreach (var document2 in documents2 ?? []) + { + await client.UpdateCustomFieldPath(document2, Guid.NewGuid().ToString()); + } + document.CustomFields.Should().BeNull("cannot create document with custom fields"); var update = new DocumentUpdate diff --git a/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs b/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs new file mode 100644 index 0000000..50bafc6 --- /dev/null +++ b/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Net.Mime; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Console; + +using NodaTime; +using NodaTime.Testing; + +using VMelnalksnis.PaperlessDotNet; +using VMelnalksnis.PaperlessDotNet.DependencyInjection; +using VMelnalksnis.PaperlessDotNet.Documents; +using VMelnalksnis.PaperlessDotNet.Serialization; + +namespace PaperlessNgxClient; + +internal sealed class PaperlessNgxClient : IAsyncDisposable +{ + private readonly IPaperlessClient _paperlessClient; + private readonly ServiceProvider _serviceProvider; + + public PaperlessNgxClient(string url, string token) + { + _serviceProvider = GetServiceProvider(url, token); + _serviceProvider.GetRequiredService(); + var serviceScope = _serviceProvider.CreateAsyncScope(); + _paperlessClient = serviceScope.ServiceProvider.GetRequiredService(); + + var httpClient = new HttpClient { BaseAddress = new(url) }; + httpClient.DefaultRequestHeaders.Add("Accept", $"{MediaTypeNames.Application.Json}; version=2"); + httpClient.DefaultRequestHeaders.Authorization = new("Token", token); + } + + public async Task> UpdateCustomFieldPath(Document document, string path) + { + var docUpdate = new DocumentUpdate + { + CustomFields = new() + { + Pfad = path, + }, + }; + return await _paperlessClient.Documents.Update(document.Id, docUpdate); + } + + public async Task>?> GetDocuments() + { + try + { + return await _paperlessClient.Documents.GetAll().ToListAsync(); + } + catch (Exception ex) + { + Console.WriteLine(ex); + return null; + } + } + + /// + public ValueTask DisposeAsync() => _serviceProvider.DisposeAsync(); + + private static ServiceProvider GetServiceProvider(string url, string token) + { + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(new List> + { + new("Paperless:BaseAddress", url), + new("Paperless:Token", token), + }) + .Build(); + + var serviceCollection = new ServiceCollection(); + serviceCollection + .AddSingleton(DateTimeZoneProviders.Tzdb) + .AddSingleton(new FakeClock(Instant.FromUtc(2024, 01, 17, 18, 8, 23), Duration.Zero)) + .AddPaperlessDotNet( + configuration, + options => + { + options.Options.Converters.Add(new CustomFieldsConverter(options)); + options.Options.TypeInfoResolverChain.Add(TestSerializerContext.Default); + }) + .ConfigureHttpClient(client => client.Timeout = TimeSpan.FromSeconds(20)); + + serviceCollection.AddLogging(builder => builder + .SetMinimumLevel(LogLevel.Trace) + .AddSimpleConsole(options => + { + options.ColorBehavior = LoggerColorBehavior.Enabled; + options.IncludeScopes = true; + })); + + return serviceCollection.BuildServiceProvider(true); + } +} + +internal sealed class CustomFields +{ + public string? Pfad { get; set; } +} + +/// +[JsonSerializable(typeof(PaginatedList>))] +[JsonSerializable(typeof(DocumentUpdate))] +internal sealed partial class TestSerializerContext : JsonSerializerContext; diff --git a/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/MinimalExampleTests.cs b/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/MinimalExampleTests.cs index 0e2f903..67ad293 100644 --- a/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/MinimalExampleTests.cs +++ b/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/MinimalExampleTests.cs @@ -2,11 +2,15 @@ // Licensed under the Apache License 2.0. // See LICENSE file in the project root for full license information. +using System; using System.Linq; using System.Net.Http; using System.Net.Mime; using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; + using NodaTime; using VMelnalksnis.PaperlessDotNet.Correspondents; diff --git a/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/PaperlessSetup.cs b/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/PaperlessSetup.cs index ddc042c..072ad89 100644 --- a/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/PaperlessSetup.cs +++ b/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/PaperlessSetup.cs @@ -19,6 +19,8 @@ public static class PaperlessSetup new("1.9.2", "1.9.2 with timezone", builder => builder.WithEnvironment("PAPERLESS_TIME_ZONE", "America/Chicago")), new("2.3.3"), new("2.3.3", "2.3.3 with timezone", builder => builder.WithEnvironment("PAPERLESS_TIME_ZONE", "America/Chicago")), + new("2.8.6"), + new("2.8.6", "2.8.6 with timezone", builder => builder.WithEnvironment("PAPERLESS_TIME_ZONE", "America/Chicago")), ]; [OneTimeSetUp] diff --git a/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/PaperlessTests.cs b/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/PaperlessTests.cs index 3800271..65996ce 100644 --- a/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/PaperlessTests.cs +++ b/tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/PaperlessTests.cs @@ -36,6 +36,8 @@ protected PaperlessTests(PaperlessFixture paperlessFixture) protected IPaperlessClient Client { get; private set; } = null!; + protected IServiceProvider Services => _serviceScope.ServiceProvider; + [SetUp] public void SetUp() {