From 4c670a4299ce9527f08b3fde16eff7b2c77a0a3a Mon Sep 17 00:00:00 2001 From: jvdvleuten Date: Thu, 28 Apr 2016 12:24:51 +0200 Subject: [PATCH 1/2] Add delete_all_event_data and delete_all_event_data_with_different_schema_names tests and fixed DeleteAllEventData when using another schema name for events. --- .../Schema/DocumentCleanerTests.cs | 49 +++++++++++++++++-- src/Marten/Schema/DocumentCleaner.cs | 4 +- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/Marten.Testing/Schema/DocumentCleanerTests.cs b/src/Marten.Testing/Schema/DocumentCleanerTests.cs index c061e709bc..7024115958 100644 --- a/src/Marten.Testing/Schema/DocumentCleanerTests.cs +++ b/src/Marten.Testing/Schema/DocumentCleanerTests.cs @@ -1,24 +1,26 @@ -using System.Diagnostics; +using System; using System.Linq; using Baseline; -using Marten.Generation; +using Marten.Events; using Marten.Schema; using Marten.Services; using Marten.Testing.Documents; +using Marten.Testing.Events; using Marten.Testing.Fixtures; using Shouldly; -using StructureMap; using Xunit; +using Issue = Marten.Testing.Documents.Issue; namespace Marten.Testing.Schema { public class DocumentCleanerTests : DocumentSessionFixture { - private readonly DocumentCleaner theCleaner; + private readonly Lazy _theCleaner; + private DocumentCleaner theCleaner => _theCleaner.Value; public DocumentCleanerTests() { - theCleaner = theStore.Advanced.Clean.As(); + _theCleaner = new Lazy(() => theStore.Advanced.Clean.As()); } [Fact] @@ -126,6 +128,43 @@ public void completely_remove_everything() ShouldBeEmpty(schema.SchemaFunctionNames()); } + [Fact] + public void delete_all_event_data() + { + var streamId = Guid.NewGuid(); + theSession.Events.StartStream(streamId, new QuestStarted()); + + theSession.SaveChanges(); + + theCleaner.DeleteAllEventData(); + + theSession.Events.Query().ShouldBeEmpty(); + theSession.Events.FetchStream(streamId).ShouldBeEmpty(); + + } + + [Fact] + public void delete_all_event_data_with_different_schema_names() + { + StoreOptions(_ => + { + _.DatabaseSchemaName = "test"; + _.Events.DatabaseSchemaName = "events"; + }); + + var streamId = Guid.NewGuid(); + + theSession.Events.StartStream(streamId, new QuestStarted()); + + theSession.SaveChanges(); + + theCleaner.DeleteAllEventData(); + + // Todo: theSession.Events.Query<>() uses the wrong schema name when using Event type. + //theSession.Events.Query().ShouldBeEmpty(); + theSession.Events.FetchStream(streamId).ShouldBeEmpty(); + } + private static void ShouldBeEmpty(T[] documentTables) { var stillInDatabase = string.Join(",", documentTables); diff --git a/src/Marten/Schema/DocumentCleaner.cs b/src/Marten/Schema/DocumentCleaner.cs index 5249df86c1..1709321b1c 100644 --- a/src/Marten/Schema/DocumentCleaner.cs +++ b/src/Marten/Schema/DocumentCleaner.cs @@ -89,8 +89,8 @@ public void DeleteAllEventData() { using (var connection = new ManagedConnection(_factory, CommandRunnerMode.ReadOnly)) { - connection.Execute($"truncate table {_schema.StoreOptions.DatabaseSchemaName}.mt_events cascade;" + - $"truncate table {_schema.StoreOptions.DatabaseSchemaName}.mt_streams cascade"); + connection.Execute($"truncate table {_schema.Events.DatabaseSchemaName}.mt_events cascade;" + + $"truncate table {_schema.Events.DatabaseSchemaName}.mt_streams cascade"); } } } From 00aa417318beb756bca375194556be2f9b08c93d Mon Sep 17 00:00:00 2001 From: jvdvleuten Date: Thu, 28 Apr 2016 12:32:28 +0200 Subject: [PATCH 2/2] Failing test for can_query_against_event_type_with_different_schema_name --- .../query_against_event_documents_Tests.cs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Marten.Testing/Events/query_against_event_documents_Tests.cs b/src/Marten.Testing/Events/query_against_event_documents_Tests.cs index 8e17978e5a..62bb7947cf 100644 --- a/src/Marten.Testing/Events/query_against_event_documents_Tests.cs +++ b/src/Marten.Testing/Events/query_against_event_documents_Tests.cs @@ -39,5 +39,28 @@ public void will_not_blow_up_if_searching_for_events_before_event_store_is_warme { theSession.Events.Query().Any().ShouldBeFalse(); } + + [Fact] + public void can_query_against_event_type_with_different_schema_name() + { + StoreOptions(_ => + { + _.DatabaseSchemaName = "test"; + _.Events.DatabaseSchemaName = "events"; + }); + + theSession.Events.StartStream(joined1, departed1); + theSession.Events.StartStream(joined2, departed2); + + theSession.SaveChanges(); + + theSession.Events.Query().Count().ShouldBe(2); + theSession.Events.Query().ToArray().SelectMany(x => x.Members).Distinct() + .OrderBy(x => x) + .ShouldHaveTheSameElementsAs("Egwene", "Matt", "Nynaeve", "Perrin", "Rand", "Thom"); + + theSession.Events.Query().Where(x => x.Members.Contains("Matt")) + .Single().Id.ShouldBe(departed2.Id); + } } } \ No newline at end of file