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

Add example of custom fields not working #269

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -213,6 +217,14 @@ public async Task CustomFields()
var id = result.Should().BeOfType<DocumentCreated>().Subject.Id;
var document = (await Client.Documents.Get<CustomFields>(id))!;

var options = Services.GetRequiredService<IOptions<PaperlessOptions>>().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<CustomFields>
Expand Down
112 changes: 112 additions & 0 deletions tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;

Check failure on line 1 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Run tests

The file header is missing or not located at the top of the file. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1633.md)

Check failure on line 1 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Run tests

The file header is missing or not located at the top of the file. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1633.md)

Check failure on line 1 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Run tests

The file header is missing or not located at the top of the file. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1633.md)

Check failure on line 1 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Resharper

The file header is missing or not located at the top of the file. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1633.md)

Check failure on line 1 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Resharper

The file header is missing or not located at the top of the file. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1633.md)

Check failure on line 1 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Resharper

The file header is missing or not located at the top of the file. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1633.md)
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

Check failure on line 24 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Run tests

Check failure on line 24 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Run tests

Check failure on line 24 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Run tests

Check failure on line 24 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Resharper

Check failure on line 24 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Resharper

Check failure on line 24 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Resharper

{
private readonly IPaperlessClient _paperlessClient;
private readonly ServiceProvider _serviceProvider;

public PaperlessNgxClient(string url, string token)
{
_serviceProvider = GetServiceProvider(url, token);
_serviceProvider.GetRequiredService<PaperlessJsonSerializerOptions>();
var serviceScope = _serviceProvider.CreateAsyncScope();
_paperlessClient = serviceScope.ServiceProvider.GetRequiredService<IPaperlessClient>();

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<Document<CustomFields>> UpdateCustomFieldPath(Document document, string path)
{
var docUpdate = new DocumentUpdate<CustomFields>
{
CustomFields = new()
{
Pfad = path,
},
};
return await _paperlessClient.Documents.Update(document.Id, docUpdate);
}

public async Task<List<Document<CustomFields>>?> GetDocuments()
{
try
{
return await _paperlessClient.Documents.GetAll<CustomFields>().ToListAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex);
return null;
}
}

/// <inheritdoc />
public ValueTask DisposeAsync() => _serviceProvider.DisposeAsync();

private static ServiceProvider GetServiceProvider(string url, string token)
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new List<KeyValuePair<string, string?>>
{
new("Paperless:BaseAddress", url),
new("Paperless:Token", token),
})
.Build();

var serviceCollection = new ServiceCollection();
serviceCollection
.AddSingleton(DateTimeZoneProviders.Tzdb)
.AddSingleton<IClock>(new FakeClock(Instant.FromUtc(2024, 01, 17, 18, 8, 23), Duration.Zero))
.AddPaperlessDotNet(
configuration,
options =>
{
options.Options.Converters.Add(new CustomFieldsConverter<CustomFields>(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

Check failure on line 104 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Run tests

Check failure on line 104 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Run tests

Check failure on line 104 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Resharper

Check failure on line 104 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Resharper

{
public string? Pfad { get; set; }
}

/// <inheritdoc cref="System.Text.Json.Serialization.JsonSerializerContext" />
[JsonSerializable(typeof(PaginatedList<Document<CustomFields>>))]
[JsonSerializable(typeof(DocumentUpdate<CustomFields>))]
internal sealed partial class TestSerializerContext : JsonSerializerContext;

Check failure on line 112 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Run tests

Check failure on line 112 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Run tests

Check failure on line 112 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Resharper

Check failure on line 112 in tests/VMelnalksnis.PaperlessDotNet.Tests.Integration/Example.cs

View workflow job for this annotation

GitHub Actions / Resharper

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ protected PaperlessTests(PaperlessFixture paperlessFixture)

protected IPaperlessClient Client { get; private set; } = null!;

protected IServiceProvider Services => _serviceScope.ServiceProvider;

[SetUp]
public void SetUp()
{
Expand Down
Loading