Skip to content

Commit

Permalink
Eliminating most usages of sync SaveChanges in tests
Browse files Browse the repository at this point in the history
all tests passing after the big changes

adjusting code samples in docs

Finishing up removing SaveChanges()

Yet another round of SaveChanges to SaveChangesAsync

Another round of SaveChanges retirement in the tests

More elimination of SaveChanges() in test code

More sync to async test conversions
  • Loading branch information
jeremydmiller committed Oct 23, 2024
1 parent e20e002 commit 0278bbe
Show file tree
Hide file tree
Showing 239 changed files with 2,642 additions and 2,722 deletions.
2 changes: 1 addition & 1 deletion docs/configuration/multitenancy.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var store = DocumentStore.For(opts =>
opts.TenantIdStyle = TenantIdStyle.ForceUpperCase;
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/MultiTenancy.cs#L13-L27' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_tenant_id_style' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/MultiTenancy.cs#L14-L28' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_tenant_id_style' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Static Database to Tenant Mapping
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration/storeoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static DocumentStore For(Action<StoreOptions> configure)
return new DocumentStore(options);
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten/DocumentStore.cs#L520-L530' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_documentstore.for' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten/DocumentStore.cs#L525-L535' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_documentstore.for' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

The major parts of `StoreOptions` are shown in the class diagram below:
Expand Down
20 changes: 9 additions & 11 deletions docs/documents/concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class CoffeeShop: Shop
public ICollection<Guid> Employees { get; set; } = new List<Guid>();
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Concurrency/optimistic_concurrency.cs#L833-L843' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_useoptimisticconcurrencyattribute' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Concurrency/optimistic_concurrency.cs#L826-L836' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_useoptimisticconcurrencyattribute' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or by using Marten's configuration API to do it programmatically:
Expand All @@ -62,13 +62,13 @@ To demonstrate the failure case, consider the following  acceptance test from M
<a id='snippet-sample_update_with_stale_version_standard'></a>
```cs
[Fact]
public void update_with_stale_version_standard()
public async Task update_with_stale_version_standard()
{
var doc1 = new CoffeeShop();
using (var session = theStore.LightweightSession())
{
session.Store(doc1);
session.SaveChanges();
await session.SaveChangesAsync();
}

var session1 = theStore.DirtyTrackedSession();
Expand All @@ -83,11 +83,11 @@ public void update_with_stale_version_standard()
session2Copy.Name = "Dominican Joe's";

// Should go through just fine
session2.SaveChanges();
await session2.SaveChangesAsync();

var ex = Exception<ConcurrencyException>.ShouldBeThrownBy(() =>
var ex = await Should.ThrowAsync<ConcurrencyException>(async () =>
{
session1.SaveChanges();
await session1.SaveChangesAsync();
});

ex.Message.ShouldBe($"Optimistic concurrency check failed for {typeof(Shop).FullName} #{doc1.Id}");
Expand All @@ -98,13 +98,11 @@ public void update_with_stale_version_standard()
session2.Dispose();
}

using (var query = theStore.QuerySession())
{
query.Load<CoffeeShop>(doc1.Id).Name.ShouldBe("Dominican Joe's");
}
await using var query = theStore.QuerySession();
query.Load<CoffeeShop>(doc1.Id).Name.ShouldBe("Dominican Joe's");
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Concurrency/optimistic_concurrency.cs#L127-L171' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_update_with_stale_version_standard' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Concurrency/optimistic_concurrency.cs#L127-L169' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_update_with_stale_version_standard' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Marten is throwing an `AggregateException` for the entire batch of changes.
Expand Down
60 changes: 30 additions & 30 deletions docs/documents/deletes.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Marten also provides the ability to delete any documents of a certain type meeti
```cs
theSession.DeleteWhere<Target>(x => x.Double == 578);

theSession.SaveChanges();
await theSession.SaveChangesAsync();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Deleting/delete_many_documents_by_query.cs#L30-L34' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_deletewhere' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
Expand All @@ -79,17 +79,17 @@ var company1 = new Company { Name = "ECorp" };

session.StoreObjects(new object[] { user1, issue1, company1 });

session.SaveChanges();
await session.SaveChangesAsync();

// Delete a mix of documents types
using (var documentSession = theStore.LightweightSession())
{
documentSession.DeleteObjects(new object[] { user1, company1 });

documentSession.SaveChanges();
await documentSession.SaveChangesAsync();
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Deleting/deleting_multiple_documents.cs#L59-L78' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_deleteobjects' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Deleting/deleting_multiple_documents.cs#L60-L79' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_deleteobjects' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Soft Deletes
Expand Down Expand Up @@ -179,7 +179,7 @@ in this acceptance test from the Marten codebase:
<a id='snippet-sample_query_soft_deleted_docs'></a>
```cs
[Fact]
public void query_soft_deleted_docs()
public async Task query_soft_deleted_docs()
{
var user1 = new User { UserName = "foo" };
var user2 = new User { UserName = "bar" };
Expand All @@ -188,11 +188,11 @@ public void query_soft_deleted_docs()

using var session = theStore.LightweightSession();
session.Store(user1, user2, user3, user4);
session.SaveChanges();
await session.SaveChangesAsync();

// Deleting 'bar' and 'baz'
session.DeleteWhere<User>(x => x.UserName.StartsWith("b"));
session.SaveChanges();
await session.SaveChangesAsync();

// no where clause, deleted docs should be filtered out
session.Query<User>().OrderBy(x => x.UserName).Select(x => x.UserName)
Expand All @@ -207,7 +207,7 @@ public void query_soft_deleted_docs()
<a id='snippet-sample_query_soft_deleted_docs-1'></a>
```cs
[Fact]
public void query_soft_deleted_docs()
public async Task query_soft_deleted_docs()
{
var user1 = new User { UserName = "foo" };
var user2 = new User { UserName = "bar" };
Expand All @@ -216,11 +216,11 @@ public void query_soft_deleted_docs()

using var session = theStore.LightweightSession();
session.Store(user1, user2, user3, user4);
session.SaveChanges();
await session.SaveChangesAsync();

// Deleting 'bar' and 'baz'
session.DeleteWhere<User>(x => x.UserName.StartsWith("b"));
session.SaveChanges();
await session.SaveChangesAsync();

// no where clause, deleted docs should be filtered out
session.Query<User>().OrderBy(x => x.UserName).Select(x => x.UserName)
Expand Down Expand Up @@ -249,7 +249,7 @@ as shown in this acceptance tests:
<a id='snippet-sample_query_maybe_soft_deleted_docs'></a>
```cs
[Fact]
public void query_maybe_soft_deleted_docs()
public async Task query_maybe_soft_deleted_docs()
{
var user1 = new User { UserName = "foo" };
var user2 = new User { UserName = "bar" };
Expand All @@ -258,10 +258,10 @@ public void query_maybe_soft_deleted_docs()

using var session = theStore.LightweightSession();
session.Store(user1, user2, user3, user4);
session.SaveChanges();
await session.SaveChangesAsync();

session.DeleteWhere<User>(x => x.UserName.StartsWith("b"));
session.SaveChanges();
await session.SaveChangesAsync();

// no where clause, all documents are returned
session.Query<User>().Where(x => x.MaybeDeleted()).OrderBy(x => x.UserName).Select(x => x.UserName)
Expand All @@ -279,7 +279,7 @@ public void query_maybe_soft_deleted_docs()
<a id='snippet-sample_query_maybe_soft_deleted_docs-1'></a>
```cs
[Fact]
public void query_maybe_soft_deleted_docs()
public async Task query_maybe_soft_deleted_docs()
{
var user1 = new User { UserName = "foo" };
var user2 = new User { UserName = "bar" };
Expand All @@ -288,10 +288,10 @@ public void query_maybe_soft_deleted_docs()

using var session = theStore.LightweightSession();
session.Store(user1, user2, user3, user4);
session.SaveChanges();
await session.SaveChangesAsync();

session.DeleteWhere<User>(x => x.UserName.StartsWith("b"));
session.SaveChanges();
await session.SaveChangesAsync();

// no where clause, all documents are returned
session.Query<User>().Where(x => x.MaybeDeleted()).OrderBy(x => x.UserName).Select(x => x.UserName)
Expand Down Expand Up @@ -363,7 +363,7 @@ as shown below:
<a id='snippet-sample_query_is_soft_deleted_docs'></a>
```cs
[Fact]
public void query_is_soft_deleted_docs()
public async Task query_is_soft_deleted_docs()
{
var user1 = new User { UserName = "foo" };
var user2 = new User { UserName = "bar" };
Expand All @@ -372,10 +372,10 @@ public void query_is_soft_deleted_docs()

using var session = theStore.LightweightSession();
session.Store(user1, user2, user3, user4);
session.SaveChanges();
await session.SaveChangesAsync();

session.DeleteWhere<User>(x => x.UserName.StartsWith("b"));
session.SaveChanges();
await session.SaveChangesAsync();

// no where clause
session.Query<User>().Where(x => x.IsDeleted()).OrderBy(x => x.UserName).Select(x => x.UserName)
Expand All @@ -393,7 +393,7 @@ public void query_is_soft_deleted_docs()
<a id='snippet-sample_query_is_soft_deleted_docs-1'></a>
```cs
[Fact]
public void query_is_soft_deleted_docs()
public async Task query_is_soft_deleted_docs()
{
var user1 = new User { UserName = "foo" };
var user2 = new User { UserName = "bar" };
Expand All @@ -402,10 +402,10 @@ public void query_is_soft_deleted_docs()

using var session = theStore.LightweightSession();
session.Store(user1, user2, user3, user4);
session.SaveChanges();
await session.SaveChangesAsync();

session.DeleteWhere<User>(x => x.UserName.StartsWith("b"));
session.SaveChanges();
await session.SaveChangesAsync();

// no where clause
session.Query<User>().Where(x => x.IsDeleted()).OrderBy(x => x.UserName).Select(x => x.UserName)
Expand All @@ -431,7 +431,7 @@ and the counterpart `DeletedSince(DateTimeOffset)` as show below:
<a id='snippet-sample_query_soft_deleted_since'></a>
```cs
[Fact]
public void query_is_soft_deleted_since_docs()
public async Task query_is_soft_deleted_since_docs()
{
var user1 = new User { UserName = "foo" };
var user2 = new User { UserName = "bar" };
Expand All @@ -440,14 +440,14 @@ public void query_is_soft_deleted_since_docs()

using var session = theStore.LightweightSession();
session.Store(user1, user2, user3, user4);
session.SaveChanges();
await session.SaveChangesAsync();

session.Delete(user3);
session.SaveChanges();
await session.SaveChangesAsync();

var epoch = session.MetadataFor(user3).DeletedAt;
session.Delete(user4);
session.SaveChanges();
await session.SaveChangesAsync();

session.Query<User>().Where(x => x.DeletedSince(epoch.Value)).Select(x => x.UserName)
.ToList().ShouldHaveTheSameElementsAs("jack");
Expand All @@ -457,7 +457,7 @@ public void query_is_soft_deleted_since_docs()
<a id='snippet-sample_query_soft_deleted_since-1'></a>
```cs
[Fact]
public void query_is_soft_deleted_since_docs()
public async Task query_is_soft_deleted_since_docs()
{
var user1 = new User { UserName = "foo" };
var user2 = new User { UserName = "bar" };
Expand All @@ -466,14 +466,14 @@ public void query_is_soft_deleted_since_docs()

using var session = theStore.LightweightSession();
session.Store(user1, user2, user3, user4);
session.SaveChanges();
await session.SaveChangesAsync();

session.Delete(user3);
session.SaveChanges();
await session.SaveChangesAsync();

var epoch = session.MetadataFor(user3).DeletedAt;
session.Delete(user4);
session.SaveChanges();
await session.SaveChangesAsync();

session.Query<User>().Where(x => x.DeletedSince(epoch.Value)).Select(x => x.UserName)
.ToList().ShouldHaveTheSameElementsAs("jack");
Expand Down
22 changes: 11 additions & 11 deletions docs/documents/hierarchies.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ using (var session = store.QuerySession())
session.Query<SuperUser>().ToList();
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/BatchedQuerying/batched_querying_acceptance_Tests.cs#L64-L87' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_configure-hierarchy-of-types' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/BatchedQuerying/batched_querying_acceptance_Tests.cs#L74-L97' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_configure-hierarchy-of-types' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

With the configuration above, you can now query by `User` and get `AdminUser` and `SuperUser` documents as part of the results,
Expand Down Expand Up @@ -153,48 +153,48 @@ Now you can query the "complex" hierarchy in the following ways:
<a id='snippet-sample_query-subclass-hierarchy'></a>
```cs
[Fact]
public void get_all_subclasses_of_a_subclass()
public async Task get_all_subclasses_of_a_subclass()
{
var smurf = new Smurf {Ability = "Follow the herd"};
var papa = new PapaSmurf {Ability = "Lead"};
var brainy = new BrainySmurf {Ability = "Invent"};
theSession.Store(smurf, papa, brainy);

theSession.SaveChanges();
await theSession.SaveChangesAsync();

theSession.Query<Smurf>().Count().ShouldBe(3);
}

[Fact]
public void get_all_subclasses_of_a_subclass2()
public async Task get_all_subclasses_of_a_subclass2()
{
var smurf = new Smurf {Ability = "Follow the herd"};
var papa = new PapaSmurf {Ability = "Lead"};
var brainy = new BrainySmurf {Ability = "Invent"};
theSession.Store(smurf, papa, brainy);

theSession.SaveChanges();
await theSession.SaveChangesAsync();

theSession.Logger = new TestOutputMartenLogger(_output);

theSession.Query<PapaSmurf>().Count().ShouldBe(2);
}

[Fact]
public void get_all_subclasses_of_a_subclass_with_where()
public async Task get_all_subclasses_of_a_subclass_with_where()
{
var smurf = new Smurf {Ability = "Follow the herd"};
var papa = new PapaSmurf {Ability = "Lead"};
var brainy = new BrainySmurf {Ability = "Invent"};
theSession.Store(smurf, papa, brainy);

theSession.SaveChanges();
await theSession.SaveChangesAsync();

theSession.Query<PapaSmurf>().Count(s => s.Ability == "Invent").ShouldBe(1);
}

[Fact]
public void get_all_subclasses_of_a_subclass_with_where_with_camel_casing()
public async Task get_all_subclasses_of_a_subclass_with_where_with_camel_casing()
{
StoreOptions(_ =>
{
Expand All @@ -221,21 +221,21 @@ public void get_all_subclasses_of_a_subclass_with_where_with_camel_casing()
var brainy = new BrainySmurf {Ability = "Invent"};
theSession.Store(smurf, papa, brainy);

theSession.SaveChanges();
await theSession.SaveChangesAsync();

theSession.Query<PapaSmurf>().Count(s => s.Ability == "Invent").ShouldBe(1);
}

[Fact]
public void get_all_subclasses_of_an_interface()
public async Task get_all_subclasses_of_an_interface()
{
var smurf = new Smurf {Ability = "Follow the herd"};
var papa = new PapaSmurf {Ability = "Lead"};
var papy = new PapySmurf {Ability = "Lead"};
var brainy = new BrainySmurf {Ability = "Invent"};
theSession.Store(smurf, papa, brainy, papy);

theSession.SaveChanges();
await theSession.SaveChangesAsync();

theSession.Query<IPapaSmurf>().Count().ShouldBe(3);
}
Expand Down
Loading

0 comments on commit 0278bbe

Please sign in to comment.