Skip to content

Commit 1fe5c7c

Browse files
committed
Add tests to verify default behavior
1 parent 5fd9e63 commit 1fe5c7c

File tree

2 files changed

+104
-72
lines changed

2 files changed

+104
-72
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace ServiceControl.Audit.Persistence.Tests;
2+
3+
using System;
4+
using System.Threading.Tasks;
5+
using global::ServiceControl.Audit.Persistence.RavenDB;
6+
using NUnit.Framework;
7+
using Raven.Client.Documents.Indexes;
8+
using Raven.Client.Documents.Operations.Indexes;
9+
10+
[TestFixture]
11+
class IndexSetupTests : PersistenceTestFixture
12+
{
13+
[Test]
14+
public async Task Corax_should_the_defaul_search_engine_type()
15+
{
16+
var indexes = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexesOperation(0, int.MaxValue));
17+
18+
foreach (var index in indexes)
19+
{
20+
var indexStats = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexStatisticsOperation(DatabaseSetup.MessagesViewIndexWithFulltextSearchName));
21+
Assert.That(indexStats.SearchEngineType, Is.EqualTo(SearchEngineType.Corax), $"{index.Name} is not using Corax");
22+
}
23+
}
24+
25+
[Test]
26+
public async Task Free_text_search_index_should_be_used_by_default()
27+
{
28+
var freeTextIndex = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexOperation(DatabaseSetup.MessagesViewIndexWithFulltextSearchName));
29+
var nonFreeTextIndex = await configuration.DocumentStore.Maintenance.SendAsync(new GetIndexOperation(DatabaseSetup.MessagesViewIndexName));
30+
31+
Assert.That(nonFreeTextIndex, Is.Null);
32+
Assert.That(freeTextIndex, Is.Not.Null);
33+
}
34+
}
Lines changed: 70 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
namespace ServiceControl.Audit.Persistence.Tests
1+
namespace ServiceControl.Audit.Persistence.Tests;
2+
3+
using System;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using NUnit.Framework;
7+
using Persistence.RavenDB;
8+
using Raven.Client.Documents.Indexes;
9+
using Raven.Client.Documents.Operations.Indexes;
10+
using SagaAudit;
11+
12+
[TestFixture]
13+
class SagaDetailsIndexTests : PersistenceTestFixture
214
{
3-
using System;
4-
using System.Threading;
5-
using System.Threading.Tasks;
6-
using NUnit.Framework;
7-
using Raven.Client.Documents.Indexes;
8-
using Raven.Client.Documents.Operations.Indexes;
9-
using ServiceControl.SagaAudit;
10-
11-
[TestFixture]
12-
class SagaDetailsIndexTests : PersistenceTestFixture
15+
[Test]
16+
public async Task Deletes_index_that_does_not_have_cap_of_50000()
1317
{
14-
[Test]
15-
public async Task Deletes_index_that_does_not_have_cap_of_50000()
18+
await configuration.DocumentStore.Maintenance.SendAsync(new DeleteIndexOperation(DatabaseSetup.SagaDetailsIndexName));
19+
20+
var indexWithout50000capDefinition = new IndexDefinition
1621
{
17-
await configuration.DocumentStore.Maintenance.SendAsync(new DeleteIndexOperation("SagaDetailsIndex"));
18-
19-
var indexWithout50000capDefinition = new IndexDefinition
20-
{
21-
Name = "SagaDetailsIndex",
22-
Maps =
23-
[
24-
@"from doc in docs
22+
Name = DatabaseSetup.SagaDetailsIndexName,
23+
Maps =
24+
[
25+
@"from doc in docs
2526
select new
2627
{
2728
doc.SagaId,
@@ -41,8 +42,8 @@ public async Task Deletes_index_that_does_not_have_cap_of_50000()
4142
}
4243
}
4344
}"
44-
],
45-
Reduce = @"from result in results
45+
],
46+
Reduce = @"from result in results
4647
group result by result.SagaId
4748
into g
4849
let first = g.First()
@@ -55,69 +56,66 @@ into g
5556
.OrderByDescending(x => x.FinishTime)
5657
.ToList()
5758
}"
58-
};
59+
};
5960

60-
var putIndexesOp = new PutIndexesOperation(indexWithout50000capDefinition);
61+
var putIndexesOp = new PutIndexesOperation(indexWithout50000capDefinition);
6162

62-
await configuration.DocumentStore.Maintenance.SendAsync(putIndexesOp);
63+
await configuration.DocumentStore.Maintenance.SendAsync(putIndexesOp);
6364

64-
var sagaDetailsIndexOperation = new GetIndexOperation("SagaDetailsIndex");
65-
var sagaDetailsIndexDefinition = await configuration.DocumentStore.Maintenance.SendAsync(sagaDetailsIndexOperation);
65+
var sagaDetailsIndexOperation = new GetIndexOperation(DatabaseSetup.SagaDetailsIndexName);
66+
var sagaDetailsIndexDefinition = await configuration.DocumentStore.Maintenance.SendAsync(sagaDetailsIndexOperation);
6667

67-
Assert.That(sagaDetailsIndexDefinition, Is.Not.Null);
68+
Assert.That(sagaDetailsIndexDefinition, Is.Not.Null);
6869

69-
await Persistence.RavenDB.DatabaseSetup.DeleteLegacySagaDetailsIndex(configuration.DocumentStore, CancellationToken.None);
70+
await DatabaseSetup.DeleteLegacySagaDetailsIndex(configuration.DocumentStore, CancellationToken.None);
7071

71-
sagaDetailsIndexDefinition = await configuration.DocumentStore.Maintenance.SendAsync(sagaDetailsIndexOperation);
72+
sagaDetailsIndexDefinition = await configuration.DocumentStore.Maintenance.SendAsync(sagaDetailsIndexOperation);
7273

73-
Assert.That(sagaDetailsIndexDefinition, Is.Null);
74-
}
74+
Assert.That(sagaDetailsIndexDefinition, Is.Null);
75+
}
7576

76-
[Test]
77-
public async Task Does_not_delete_index_that_does_have_cap_of_50000()
78-
{
79-
await Persistence.RavenDB.DatabaseSetup.DeleteLegacySagaDetailsIndex(configuration.DocumentStore, CancellationToken.None);
77+
[Test]
78+
public async Task Does_not_delete_index_that_does_have_cap_of_50000()
79+
{
80+
await DatabaseSetup.DeleteLegacySagaDetailsIndex(configuration.DocumentStore, CancellationToken.None);
8081

81-
var sagaDetailsIndexOperation = new GetIndexOperation("SagaDetailsIndex");
82-
var sagaDetailsIndexDefinition = await configuration.DocumentStore.Maintenance.SendAsync(sagaDetailsIndexOperation);
82+
var sagaDetailsIndexOperation = new GetIndexOperation(DatabaseSetup.SagaDetailsIndexName);
83+
var sagaDetailsIndexDefinition = await configuration.DocumentStore.Maintenance.SendAsync(sagaDetailsIndexOperation);
8384

84-
Assert.That(sagaDetailsIndexDefinition, Is.Not.Null);
85-
}
85+
Assert.That(sagaDetailsIndexDefinition, Is.Not.Null);
86+
}
87+
88+
[Test]
89+
public async Task Should_only_reduce_the_last_50000_saga_state_changes()
90+
{
91+
var sagaType = "MySagaType";
92+
var sagaState = "some-saga-state";
8693

87-
[Test]
88-
public async Task Should_only_reduce_the_last_50000_saga_state_changes()
94+
await IngestSagaAudits(new SagaSnapshot
8995
{
90-
var sagaType = "MySagaType";
91-
var sagaState = "some-saga-state";
92-
93-
await IngestSagaAudits(new SagaSnapshot
94-
{
95-
SagaId = Guid.NewGuid(),
96-
SagaType = sagaType,
97-
Status = SagaStateChangeStatus.New,
98-
StateAfterChange = sagaState
99-
});
100-
101-
await configuration.CompleteDBOperation();
102-
103-
using (var session = configuration.DocumentStore.OpenAsyncSession())
104-
{
105-
var sagaDetailsIndexOperation = new GetIndexOperation("SagaDetailsIndex");
106-
var sagaDetailsIndexDefinition = await configuration.DocumentStore.Maintenance.SendAsync(sagaDetailsIndexOperation);
107-
108-
Assert.That(sagaDetailsIndexDefinition.Reduce, Does.Contain("Take(50000)"), "The SagaDetails index definition does not contain a .Take(50000) to limit the number of saga state changes that are reduced by the map/reduce");
109-
}
110-
}
96+
SagaId = Guid.NewGuid(),
97+
SagaType = sagaType,
98+
Status = SagaStateChangeStatus.New,
99+
StateAfterChange = sagaState
100+
});
101+
102+
await configuration.CompleteDBOperation();
103+
104+
var sagaDetailsIndexOperation = new GetIndexOperation(DatabaseSetup.SagaDetailsIndexName);
105+
var sagaDetailsIndexDefinition = await configuration.DocumentStore.Maintenance.SendAsync(sagaDetailsIndexOperation);
111106

112-
async Task IngestSagaAudits(params SagaSnapshot[] snapshots)
107+
Assert.That(sagaDetailsIndexDefinition.Reduce, Does.Contain("Take(50000)"), "The SagaDetails index definition does not contain a .Take(50000) to limit the number of saga state changes that are reduced by the map/reduce");
108+
}
109+
110+
async Task IngestSagaAudits(params SagaSnapshot[] snapshots)
111+
{
112+
var unitOfWork = await StartAuditUnitOfWork(snapshots.Length);
113+
foreach (var snapshot in snapshots)
113114
{
114-
var unitOfWork = await StartAuditUnitOfWork(snapshots.Length);
115-
foreach (var snapshot in snapshots)
116-
{
117-
await unitOfWork.RecordSagaSnapshot(snapshot);
118-
}
119-
await unitOfWork.DisposeAsync();
120-
await configuration.CompleteDBOperation();
115+
await unitOfWork.RecordSagaSnapshot(snapshot);
121116
}
117+
118+
await unitOfWork.DisposeAsync();
119+
await configuration.CompleteDBOperation();
122120
}
123121
}

0 commit comments

Comments
 (0)