Esempio n. 1
0
        public async Task Can_Create_Update_And_Delete_Todo_Items()
        {
            // Arrange
            Skip.IfNot(
                RuntimeInformation.IsOSPlatform(OSPlatform.Windows),
                "This test can only be run on Windows.");

            var now   = new DateTimeOffset(2018, 08, 12, 10, 41, 0, TimeSpan.Zero);
            var clock = new FakeClock(Instant.FromDateTimeOffset(now));

            var options = new SqlLocalDbOptions()
            {
                AutomaticallyDeleteInstanceFiles = true,
                StopOptions = StopInstanceOptions.NoWait,
                StopTimeout = TimeSpan.FromSeconds(1),
            };

            using var localDB = new SqlLocalDbApi(options, LoggerFactory);
            using TemporarySqlLocalDbInstance instance = localDB.CreateTemporaryInstance(deleteFiles: true);

            var builder = new DbContextOptionsBuilder <TodoContext>()
                          .UseSqlServer(instance.ConnectionString);

            using var context = new TodoContext(builder.Options);
            await context.Database.MigrateAsync();

            var target = new TodoRepository(clock, context);

            // Act - Verify the repository is empty
            IList <TodoItem> items = await target.GetItemsAsync();

            // Assert
            Assert.NotNull(items);
            Assert.Empty(items);

            // Arrange - Add a new item
            string text = "Buy cheese";

            // Act
            TodoItem item = await target.AddItemAsync(text);

            // Assert
            Assert.NotNull(item);
            Assert.NotEqual(Guid.Empty, item.Id);
            Assert.Equal(text, item.Text);
            Assert.Equal(now, item.CreatedAt);
            Assert.Null(item.CompletedAt);

            // Arrange - Mark the item as completed
            Guid id = item.Id;

            // Act
            bool?completeResult = await target.CompleteItemAsync(id);

            // Assert
            Assert.True(completeResult);

            // Act - Verify the repository has one item that is completed
            items = await target.GetItemsAsync();

            // Assert
            Assert.NotNull(items);
            Assert.NotEmpty(items);
            Assert.Equal(1, items.Count);

            item = items[0];
            Assert.NotNull(item);
            Assert.NotEqual(Guid.Empty, item.Id);
            Assert.Equal(text, item.Text);
            Assert.Equal(now, item.CreatedAt);
            Assert.Equal(now, item.CompletedAt);

            // Act - Delete the item
            bool deleteResult = await target.DeleteItemAsync(id);

            // Assert
            Assert.True(deleteResult);

            // Act - Verify the repository is empty again
            items = await target.GetItemsAsync();

            // Assert
            Assert.NotNull(items);
            Assert.Empty(items);
        }