Skip to content

Commit

Permalink
Replaced old custom Exception.ShouldThrow**** with Shouldly
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Oct 21, 2024
1 parent 5c00cbd commit 4be70f5
Show file tree
Hide file tree
Showing 28 changed files with 84 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Marten.Testing.Harness;
using Shouldly;
using Weasel.Core;
using Weasel.Core.Migrations;
using Xunit;
Expand All @@ -25,10 +26,10 @@ public async Task should_be_validating_the_new_doc_does_not_exist()
cfg.AutoCreateSchemaObjects = AutoCreate.None;
});

await Exception<DatabaseValidationException>.ShouldBeThrownByAsync(() =>
await Should.ThrowAsync<DatabaseValidationException>(async () =>
{
return theStore.Storage.Database.AssertDatabaseMatchesConfigurationAsync();
await theStore.Storage.Database.AssertDatabaseMatchesConfigurationAsync();
});
}

}
}
10 changes: 5 additions & 5 deletions src/DaemonTests/event_projection_scenario_tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async Task sad_path_test_with_inline_projection()
opts.Projections.Add(new UserProjection(), ProjectionLifecycle.Inline);
});

await Exception<ProjectionScenarioException>.ShouldBeThrownByAsync(async () =>
await Should.ThrowAsync<ProjectionScenarioException>(async () =>
{
await theStore.Advanced.EventProjectionScenario(scenario =>
{
Expand Down Expand Up @@ -121,7 +121,7 @@ public async Task sad_path_test_with_inline_projection_with_document_assertion()
opts.Projections.Add(new UserProjection(), ProjectionLifecycle.Inline);
});

await Exception<ProjectionScenarioException>.ShouldBeThrownByAsync(async () =>
await Should.ThrowAsync<ProjectionScenarioException>(async () =>
{
await theStore.Advanced.EventProjectionScenario(scenario =>
{
Expand Down Expand Up @@ -186,7 +186,7 @@ public async Task sad_path_test_with_inline_projection_async()
opts.Projections.Add(new UserProjection(), ProjectionLifecycle.Async);
});

await Exception<ProjectionScenarioException>.ShouldBeThrownByAsync(async () =>
await Should.ThrowAsync<ProjectionScenarioException>(async () =>
{
await theStore.Advanced.EventProjectionScenario(scenario =>
{
Expand Down Expand Up @@ -221,7 +221,7 @@ public async Task sad_path_test_with_inline_projection_with_document_assertion_a
opts.Projections.Add(new UserProjection(), ProjectionLifecycle.Async);
});

await Exception<ProjectionScenarioException>.ShouldBeThrownByAsync(async () =>
await Should.ThrowAsync<ProjectionScenarioException>(async () =>
{
await theStore.Advanced.EventProjectionScenario(scenario =>
{
Expand Down Expand Up @@ -278,4 +278,4 @@ public void Project(DeleteUser deletion, IDocumentOperations operations)
}
}

#endregion
#endregion
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Marten.Services;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using Shouldly;
using Xunit;

namespace DocumentDbTests.Bugs;
Expand All @@ -13,7 +14,7 @@ public class Bug_648_defensive_programming_checks_on_bad_id_type: IntegrationCon
[Fact]
public void try_to_load_a_guid_identified_type_with_wrong_type()
{
Exception<DocumentIdTypeMismatchException>.ShouldBeThrownBy(() =>
Should.Throw<DocumentIdTypeMismatchException>(() =>
{
theSession.Load<Target>(111);
});
Expand All @@ -22,16 +23,16 @@ public void try_to_load_a_guid_identified_type_with_wrong_type()
[Fact]
public Task try_to_load_a_guid_identified_type_with_wrong_type_async()
{
return Exception<DocumentIdTypeMismatchException>.ShouldBeThrownByAsync(() =>
return Should.ThrowAsync<DocumentIdTypeMismatchException>(async () =>
{
return theSession.LoadAsync<Target>(111);
await theSession.LoadAsync<Target>(111);
});
}

[Fact]
public void bad_id_to_load_many()
{
Exception<DocumentIdTypeMismatchException>.ShouldBeThrownBy(() =>
Should.Throw<DocumentIdTypeMismatchException>(() =>
{
theSession.LoadMany<Target>(111, 121);
});
Expand All @@ -40,13 +41,13 @@ public void bad_id_to_load_many()
[Fact]
public Task try_to_loadmany_a_guid_identified_type_with_wrong_type_async()
{
return Exception<DocumentIdTypeMismatchException>.ShouldBeThrownByAsync(() =>
return Should.ThrowAsync<DocumentIdTypeMismatchException>(async () =>
{
return theSession.LoadManyAsync<Target>(111, 222);
await theSession.LoadManyAsync<Target>(111, 222);
});
}

public Bug_648_defensive_programming_checks_on_bad_id_type(DefaultStoreFixture fixture) : base(fixture)
{
}
}
}
3 changes: 2 additions & 1 deletion src/DocumentDbTests/Bugs/Bug_953_too_long_index_names.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Marten.Exceptions;
using Marten.Testing.Harness;
using Shouldly;
using Weasel.Postgresql;
using Xunit;

Expand All @@ -24,7 +25,7 @@ public void can_ensure_storage_with_index_id_greater_than_63_bytes()
_.NameDataLength = 64;
});

Exception<PostgresqlIdentifierTooLongException>.ShouldBeThrownBy(() =>
Should.Throw<PostgresqlIdentifierTooLongException>(() =>
{
theStore.Tenancy.Default.Database.EnsureStorageExists(typeof(LongEnoughNameToCauseIdTruncation));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public async Task update_with_stale_version_throws_exception_async()

session2.Update(doc2);

var ex = await Exception<ConcurrencyException>.ShouldBeThrownByAsync(async () =>
var ex = await Should.ThrowAsync<ConcurrencyException>(async () =>
{
await session2.SaveChangesAsync();
});
Expand Down
8 changes: 4 additions & 4 deletions src/DocumentDbTests/Concurrency/optimistic_concurrency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public async Task update_with_stale_version_standard_async()
// Should go through just fine
await session2.SaveChangesAsync();

var ex = await Exception<ConcurrencyException>.ShouldBeThrownByAsync(async () =>
var ex = await Should.ThrowAsync<ConcurrencyException>(async () =>
{
await session1.SaveChangesAsync();
});
Expand Down Expand Up @@ -474,7 +474,7 @@ public async Task update_multiple_docs_at_a_time_sad_path_async()
await other.SaveChangesAsync();
}

var ex = await Exception<AggregateException>.ShouldBeThrownByAsync(async () =>
var ex = await Should.ThrowAsync<AggregateException>(async () =>
{
await session.SaveChangesAsync();
});
Expand Down Expand Up @@ -589,7 +589,7 @@ public async Task store_with_the_right_version_sad_path_async()
// Some random version that won't match
session.UpdateExpectedVersion(doc1, Guid.NewGuid());

await Exception<ConcurrencyException>.ShouldBeThrownByAsync(async () =>
await Should.ThrowAsync<ConcurrencyException>(async () =>
{
await session.SaveChangesAsync();
});
Expand Down Expand Up @@ -797,7 +797,7 @@ public async Task update_with_stale_version_async()
// Should go through just fine
await session2.SaveChangesAsync();

var ex = await Exception<ConcurrencyException>.ShouldBeThrownByAsync(async () =>
var ex = await Should.ThrowAsync<ConcurrencyException>(async () =>
{
await session1.SaveChangesAsync();
});
Expand Down
4 changes: 2 additions & 2 deletions src/DocumentDbTests/Configuration/DocumentMappingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ public void to_upsert_with_subclasses()
[Fact]
public void trying_to_replace_the_hilo_settings_when_not_using_hilo_for_the_sequence_throws()
{
Exception<InvalidOperationException>.ShouldBeThrownBy(
Should.Throw<InvalidOperationException>(
() => { DocumentMapping.For<StringId>().HiloSettings = new HiloSettings(); });
}

Expand Down Expand Up @@ -622,7 +622,7 @@ public void uses_ConfigureMarten_method_to_alter_mapping_upon_construction_with_
[Fact]
public void trying_to_index_deleted_at_when_not_soft_deleted_document_throws()
{
Exception<InvalidOperationException>.ShouldBeThrownBy(() => DocumentMapping.For<IntId>().AddDeletedAtIndex());
Should.Throw<InvalidOperationException>(() => DocumentMapping.For<IntId>().AddDeletedAtIndex());
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/DocumentDbTests/ForeignKeys/foreign_keys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public async Task order_inserts()
[Fact]
public void throws_exception_on_cyclic_dependency()
{
Exception<InvalidOperationException>.ShouldBeThrownBy(() =>
Should.Throw<InvalidOperationException>(() =>
{
StoreOptions(_ =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void version_member_set_by_attribute()
[Fact]
public void wrong_version_member()
{
Exception<ArgumentOutOfRangeException>.ShouldBeThrownBy(() =>
Should.Throw<ArgumentOutOfRangeException>(() =>
{
DocumentMapping.For<WrongVersionTypedDoc>();
});
Expand Down Expand Up @@ -86,4 +86,4 @@ public class WrongVersionTypedDoc

[Version]
public string Version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Marten.Services;
using Marten.Storage;
using Marten.Testing.Harness;
using Shouldly;
using Xunit;

namespace DocumentDbTests.MultiTenancy;
Expand All @@ -16,7 +17,7 @@ public void get_exception_when_creating_session_with_default_tenant_usage_disabl
_.Advanced.DefaultTenantUsageEnabled = false;
});

Exception<DefaultTenantUsageDisabledException>.ShouldBeThrownBy(() =>
Should.Throw<DefaultTenantUsageDisabledException>(() =>
{
using (var session = theStore.LightweightSession()) { }
});
Expand All @@ -30,7 +31,7 @@ public void get_exception_when_creating_query_session_with_default_tenant_usage_
_.Advanced.DefaultTenantUsageEnabled = false;
});

Exception<DefaultTenantUsageDisabledException>.ShouldBeThrownBy(() =>
Should.Throw<DefaultTenantUsageDisabledException>(() =>
{
using (var session = theStore.LightweightSession()) { }
});
Expand All @@ -44,7 +45,7 @@ public void get_exception_when_creating_session_with_default_tenant_and_default_
_.Advanced.DefaultTenantUsageEnabled = false;
});

Exception<DefaultTenantUsageDisabledException>.ShouldBeThrownBy(() =>
Should.Throw<DefaultTenantUsageDisabledException>(() =>
{
using (var session = theStore.LightweightSession(Tenancy.DefaultTenantId)) { }
});
Expand All @@ -58,7 +59,7 @@ public void get_exception_when_creating_query_session_with_default_tenant_and_de
_.Advanced.DefaultTenantUsageEnabled = false;
});

Exception<DefaultTenantUsageDisabledException>.ShouldBeThrownBy(() =>
Should.Throw<DefaultTenantUsageDisabledException>(() =>
{
using (var session = theStore.LightweightSession(Tenancy.DefaultTenantId)) { }
});
Expand All @@ -72,7 +73,7 @@ public void get_exception_when_creating_session_with_default_tenant_session_opti
_.Advanced.DefaultTenantUsageEnabled = false;
});

Exception<DefaultTenantUsageDisabledException>.ShouldBeThrownBy(() =>
Should.Throw<DefaultTenantUsageDisabledException>(() =>
{
var sessionOptions = new SessionOptions {TenantId = Tenancy.DefaultTenantId};
using (var session = theStore.LightweightSession(sessionOptions)) { }
Expand All @@ -87,7 +88,7 @@ public void get_exception_when_creating_query_session_with_default_tenant_sessio
_.Advanced.DefaultTenantUsageEnabled = false;
});

Exception<DefaultTenantUsageDisabledException>.ShouldBeThrownBy(() =>
Should.Throw<DefaultTenantUsageDisabledException>(() =>
{
var sessionOptions = new SessionOptions {TenantId = Tenancy.DefaultTenantId};
using (var session = theStore.QuerySession(sessionOptions)) { }
Expand Down
14 changes: 7 additions & 7 deletions src/DocumentDbTests/Reading/Json/streaming_json_results.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public async Task first_throws_when_none_returned()
};
theSession.Store(user1,user2);
await theSession.SaveChangesAsync();
var ex = await Exception<InvalidOperationException>.ShouldBeThrownByAsync(async () => await theSession.Query<SimpleUser>().Where(x => x.Number != 5).ToJsonFirst());
var ex = await Should.ThrowAsync<InvalidOperationException>(async () => await theSession.Query<SimpleUser>().Where(x => x.Number != 5).ToJsonFirst());
ex.Message.ShouldBe("Sequence contains no elements");
}

Expand Down Expand Up @@ -421,7 +421,7 @@ public async Task first_async_throws_when_none_returned()
};
theSession.Store(user1,user2);
await theSession.SaveChangesAsync();
var ex = await Exception<InvalidOperationException>.ShouldBeThrownByAsync(() =>
var ex = await Should.ThrowAsync<InvalidOperationException>(() =>
theSession.Query<SimpleUser>().Where(x => x.Number != 5).ToJsonFirst());
ex.Message.ShouldBe("Sequence contains no elements");
}
Expand Down Expand Up @@ -694,7 +694,7 @@ public async Task single_throws_when_none_found()
};
theSession.Store(user1, user2);
await theSession.SaveChangesAsync();
var ex = await Exception<InvalidOperationException>.ShouldBeThrownByAsync(async () => await theSession.Query<SimpleUser>().Where(x => x.Number != 5).ToJsonSingle());
var ex = await Should.ThrowAsync<InvalidOperationException>(async () => await theSession.Query<SimpleUser>().Where(x => x.Number != 5).ToJsonSingle());
ex.Message.ShouldBe("Sequence contains no elements");
}

Expand All @@ -717,7 +717,7 @@ public async Task single_throws_when_more_than_one_found()
};
theSession.Store(user1, user2);
await theSession.SaveChangesAsync();
var ex = await Exception<InvalidOperationException>.ShouldBeThrownByAsync(async () => await theSession.Query<SimpleUser>().Where(x => x.Number == 5).ToJsonSingle());
var ex = await Should.ThrowAsync<InvalidOperationException>(async () => await theSession.Query<SimpleUser>().Where(x => x.Number == 5).ToJsonSingle());
ex.Message.ShouldBe("Sequence contains more than one element");
}

Expand Down Expand Up @@ -780,7 +780,7 @@ public async Task single_async_throws_when_none_returned()
};
theSession.Store(user1, user2);
await theSession.SaveChangesAsync();
var ex = await Exception<InvalidOperationException>.ShouldBeThrownByAsync(() =>
var ex = await Should.ThrowAsync<InvalidOperationException>(() =>
theSession.Query<SimpleUser>().Where(x => x.Number != 5).ToJsonSingle());
ex.Message.ShouldBe("Sequence contains no elements");
}
Expand All @@ -804,7 +804,7 @@ public async Task single_async_throws_when_more_than_one_returned()
};
theSession.Store(user1, user2);
await theSession.SaveChangesAsync();
var ex = await Exception<InvalidOperationException>.ShouldBeThrownByAsync(() =>
var ex = await Should.ThrowAsync<InvalidOperationException>(() =>
theSession.Query<SimpleUser>().Where(x => x.Number == 5).ToJsonSingle());
ex.Message.ShouldBe("Sequence contains more than one element");
}
Expand Down Expand Up @@ -936,7 +936,7 @@ public async Task single_or_default_throws_when_more_than_one_found()
};
theSession.Store(user1, user2);
await theSession.SaveChangesAsync();
var ex = await Exception<InvalidOperationException>.ShouldBeThrownByAsync(async () => await theSession.Query<SimpleUser>().Where(x => x.Number == 5).ToJsonSingleOrDefault());
var ex = await Should.ThrowAsync<InvalidOperationException>(async () => await theSession.Query<SimpleUser>().Where(x => x.Number == 5).ToJsonSingleOrDefault());
ex.Message.ShouldBe("Sequence contains more than one element");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void given_document_with_same_id_is_already_added_then_exception_should_o
var session = OpenSession(tracking);
session.Store(user1);

Exception<InvalidOperationException>.ShouldBeThrownBy(() => session.Store(user2))
Should.Throw<InvalidOperationException>(() => session.Store(user2))
.Message.ShouldBe("Document 'Marten.Testing.Documents.User' with same Id already added to the session.");
}

Expand Down
7 changes: 4 additions & 3 deletions src/DocumentDbTests/SessionMechanics/session_timeouts.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Marten.Services;
using Marten.Testing.Harness;
using Shouldly;
using Xunit;

namespace DocumentDbTests.SessionMechanics;
Expand All @@ -10,17 +11,17 @@ public class session_timeouts : IntegrationContext
[Fact]
public void should_respect_command_timeout_options()
{
var ex = Exception<ArgumentOutOfRangeException>.ShouldBeThrownBy(() =>
var ex = Should.Throw<ArgumentOutOfRangeException>(() =>
{
var session = theStore.QuerySession(new SessionOptions() {Timeout = -1});
});

ex.Message.ShouldContain("CommandTimeout can't be less than zero");
ex.Message.ShouldContain("CommandTimeout can't be less than zero", Case.Insensitive);
}



public session_timeouts(DefaultStoreFixture fixture) : base(fixture)
{
}
}
}
Loading

0 comments on commit 4be70f5

Please sign in to comment.