public async Task Save_inserts_Memento_entity_correctly()
        {
            // Arrange
            var sourceId = Guid.NewGuid();
            var memento  = new FakeUserMemento();

            var serializer = new JsonMessageSerializer();

            var sut = new SqlMementoStore(
                () => new MementoStoreDbContext(_dbContextOptions),
                serializer);

            // Act
            await sut.Save <FakeUser>(sourceId, memento, default);

            // Assert
            using (var db = new MementoStoreDbContext(_dbContextOptions))
            {
                Memento actual = await db
                                 .Mementoes
                                 .AsNoTracking()
                                 .Where(m => m.AggregateId == sourceId)
                                 .SingleOrDefaultAsync();

                actual.Should().NotBeNull();
                object restored = serializer.Deserialize(actual.MementoJson);
                restored.Should().BeOfType <FakeUserMemento>();
                restored.ShouldBeEquivalentTo(memento);
            }
        }
        public async Task Delete_deletes_Memento_entity()
        {
            // Arrange
            var sourceId = Guid.NewGuid();
            var memento  = new FakeUserMemento();

            var sut = new SqlMementoStore(
                () => new MementoStoreDbContext(_dbContextOptions),
                new JsonMessageSerializer());

            await sut.Save <FakeUser>(sourceId, memento, default);

            // Act
            await sut.Delete <FakeUser>(sourceId, default);

            // Assert
            using (var db = new MementoStoreDbContext(_dbContextOptions))
            {
                bool actual = await db
                              .Mementoes
                              .Where(m => m.AggregateId == sourceId)
                              .AnyAsync();

                actual.Should().BeFalse();
            }
        }
Esempio n. 3
0
        public async Task Save_inserts_Memento_entity_correctly(
            Guid sourceId,
            FakeUserMemento memento)
        {
            await sut.Save <FakeUser>(sourceId, memento, CancellationToken.None);

            using (var db = new DataContext())
            {
                Memento actual = await db
                                 .Mementoes
                                 .AsNoTracking()
                                 .Where(m => m.AggregateId == sourceId)
                                 .SingleOrDefaultAsync();

                actual.Should().NotBeNull();
                object restored = serializer.Deserialize(actual.MementoJson);
                restored.Should().BeOfType <FakeUserMemento>();
                restored.ShouldBeEquivalentTo(memento);
            }
        }
        public async Task Save_updates_Memento_entity_if_already_exists()
        {
            // Arrange
            var sourceId = Guid.NewGuid();

            var oldMemento = new FakeUserMemento();
            var newMemento = new FakeUserMemento();

            var serializer = new JsonMessageSerializer();

            long sequence = 0;

            using (var db = new MementoStoreDbContext(_dbContextOptions))
            {
                var memento = new Memento
                {
                    AggregateId = sourceId,
                    MementoJson = serializer.Serialize(oldMemento),
                };
                db.Mementoes.Add(memento);
                await db.SaveChangesAsync();

                sequence = memento.SequenceId;
            }

            var sut = new SqlMementoStore(
                () => new MementoStoreDbContext(_dbContextOptions),
                serializer);

            // Act
            await sut.Save <FakeUser>(sourceId, newMemento, default);

            // Assert
            using (var db = new MementoStoreDbContext(_dbContextOptions))
            {
                Memento actual = await db
                                 .Mementoes
                                 .Where(m => m.SequenceId == sequence)
                                 .SingleOrDefaultAsync();

                actual.Should().NotBeNull();
                actual.AggregateId.Should().Be(sourceId);
                object restored = serializer.Deserialize(actual.MementoJson);
                restored.Should().BeOfType <FakeUserMemento>();
                restored.ShouldBeEquivalentTo(newMemento);
            }
        }
        public async Task Find_returns_memento_correctly()
        {
            // Arrange
            var sourceId = Guid.NewGuid();
            var memento  = new FakeUserMemento();
            var sut      = new SqlMementoStore(
                () => new MementoStoreDbContext(_dbContextOptions),
                new JsonMessageSerializer());
            await sut.Save <FakeUser>(sourceId, memento, default);

            // Act
            IMemento actual = await sut.Find <FakeUser>(sourceId, default);

            // Assert
            actual.Should().BeOfType <FakeUserMemento>();
            actual.ShouldBeEquivalentTo(memento);
        }