forked from VMelnalksnis/PaperlessDotNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentClientTests.cs
165 lines (131 loc) · 5.71 KB
/
DocumentClientTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2022 Valters Melnalksnis
// Licensed under the Apache License 2.0.
// See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NodaTime;
using VMelnalksnis.PaperlessDotNet.Documents;
using VMelnalksnis.PaperlessDotNet.Tags;
namespace VMelnalksnis.PaperlessDotNet.Tests.Integration.Documents;
public sealed class DocumentClientTests(PaperlessFixture paperlessFixture) : PaperlessTests(paperlessFixture)
{
[Test]
[Order(1)]
public async Task GetAll_ShouldReturnExpected()
{
var documents = await Client.Documents.GetAll().ToListAsync();
documents.Should().BeEmpty();
}
[Test]
public async Task GetAll_PageSizeShouldNotChangeResult()
{
var documents = await Client.Documents.GetAll().ToListAsync();
var pageSizeDocuments = await Client.Documents.GetAll(1).ToListAsync();
documents.Should().BeEquivalentTo(pageSizeDocuments);
}
[Test]
public async Task Create()
{
const string documentName = "Lorem Ipsum.txt";
var correspondent = await Client.Correspondents.Create(new("Foo"));
var tags = new List<Tag>();
foreach (var tag in new List<TagCreation> { new("Receipt"), new("Bill") })
{
tags.Add(await Client.Tags.Create(tag));
}
await using var documentStream = typeof(DocumentClientTests).GetResource(documentName);
var documentCreation = new DocumentCreation(documentStream, documentName)
{
Created = Clock.GetCurrentInstant(),
Title = "Lorem Ipsum",
CorrespondentId = correspondent.Id,
ArchiveSerialNumber = 1,
TagIds = tags.Select(tag => tag.Id).ToArray(),
};
var result = await Client.Documents.Create(documentCreation);
if (PaperlessVersion < new Version(2, 0))
{
result.Should().BeOfType<ImportStarted>();
return;
}
var id = result.Should().BeOfType<DocumentCreated>().Subject.Id;
var document = (await Client.Documents.Get(id))!;
var documents = await Client.Documents.GetAll().ToListAsync();
using var scope = new AssertionScope();
var currentTime = SystemClock.Instance.GetCurrentInstant();
var content = await typeof(DocumentClientTests).ReadResource(documentName);
documents.Should().ContainSingle(d => d.Id == id).Which.Should().BeEquivalentTo(document);
document.Should().NotBeNull();
document.OriginalFileName.Should().Be(documentName);
document.Created.ToInstant().Should().Be(documentCreation.Created.Value);
document.Added.ToInstant().Should().BeInRange(currentTime - Duration.FromSeconds(10), currentTime);
document.Modified.ToInstant().Should().BeInRange(currentTime - Duration.FromSeconds(10), currentTime);
document.Title.Should().Be(documentCreation.Title);
document.ArchiveSerialNumber.Should().Be(documentCreation.ArchiveSerialNumber);
document.CorrespondentId.Should().Be(documentCreation.CorrespondentId);
document.TagIds.Should().BeEquivalentTo(tags.Select(tag => tag.Id));
#if NET6_0_OR_GREATER
document.Content.ReplaceLineEndings().Should().BeEquivalentTo(content);
#else
document.Content.Replace("\n", Environment.NewLine).Replace("\r\n", Environment.NewLine).Should().Be(content);
#endif
var update = new DocumentUpdate { Title = $"{document.Title}1" };
var updatedDocument = await Client.Documents.Update(id, update);
updatedDocument.Title.Should().Be(update.Title);
await Client.Correspondents.Delete(correspondent.Id);
foreach (var tag in tags)
{
await Client.Tags.Delete(tag.Id);
}
}
[Test]
public async Task CustomFields()
{
if (PaperlessVersion < new Version(2, 0))
{
Assert.Ignore($"Paperless v{PaperlessVersion} does not support custom fields");
}
const string documentName = "Lorem Ipsum 2.txt";
await Client.Documents.CreateCustomField(new("field1", CustomFieldType.String));
await Client.Documents.CreateCustomField(new("field2", CustomFieldType.Url));
await Client.Documents.CreateCustomField(new("field3", CustomFieldType.Date));
await Client.Documents.CreateCustomField(new("field4", CustomFieldType.Boolean));
await Client.Documents.CreateCustomField(new("field5", CustomFieldType.Integer));
await Client.Documents.CreateCustomField(new("field6", CustomFieldType.Float));
await Client.Documents.CreateCustomField(new("field7", CustomFieldType.Monetary));
await Client.Documents.CreateCustomField(new("field8", CustomFieldType.DocumentLink));
var customFields = await Client.Documents.GetCustomFields().ToListAsync();
customFields.Should().HaveCount(8);
var paginatedCustomFields = await Client.Documents.GetCustomFields(1).ToListAsync();
paginatedCustomFields.Should().BeEquivalentTo(customFields);
await using var documentStream = typeof(DocumentClientTests).GetResource(documentName);
var documentCreation = new DocumentCreation(documentStream, documentName);
var result = await Client.Documents.Create(documentCreation);
SerializerOptions.CustomFields.Clear();
var id = result.Should().BeOfType<DocumentCreated>().Subject.Id;
var document = (await Client.Documents.Get<CustomFields>(id))!;
document.CustomFields.Should().BeNull("cannot create document with custom fields");
var update = new DocumentUpdate<CustomFields>
{
CustomFields = new()
{
Field1 = "foo",
Field2 = new("https://example.com/"),
Field3 = new LocalDate(2024, 01, 19),
Field4 = true,
Field5 = 12,
Field6 = 12.34567f,
Field7 = 12.34f,
Field8 = [id],
},
};
SerializerOptions.CustomFields.Clear();
document = await Client.Documents.Update(id, update);
document.CustomFields.Should().BeEquivalentTo(update.CustomFields);
SerializerOptions.CustomFields.Clear();
var documents = await Client.Documents.GetAll<CustomFields>().ToListAsync();
documents.Should().ContainSingle(d => d.Id == id).Which.Should().BeEquivalentTo(document);
}
}