public async Task SetAsyncAddsNewItem()
        {
            // arrange
            using var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
            var item = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());

            // act
            var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
            var result     = await repository.SetAsync(item).ConfigureAwait(false);

            // assert
            using var connection = new SqlConnection(_database.ConnectionString);
            var saved = await connection.QuerySingleOrDefaultAsync <CoursePath>("SELECT [Key], [Name], [Slug], [Version] FROM [dbo].[CoursePath] WHERE [Key] = @Key", new { item.Key }).ConfigureAwait(false);

            // assert - result equals input except version
            Assert.Equal(result.Key, item.Key);
            Assert.Equal(result.Name, item.Name);
            Assert.Equal(result.Slug, item.Slug);
            Assert.NotEqual(result.Version, item.Version);

            // assert - saved equals input except version
            Assert.Equal(saved.Key, item.Key);
            Assert.Equal(saved.Name, item.Name);
            Assert.Equal(saved.Slug, item.Slug);
            Assert.NotEqual(saved.Version, item.Version);

            // assert - result version equals saved version
            Assert.Equal(saved.Version, result.Version);
        }
        public async Task GetAllAsyncReturnsExistingItems()
        {
            // arrange
            using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var items = new[]
                {
                    new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid()),
                    new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid()),
                    new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid())
                };

                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    await connection.ExecuteAsync("DELETE FROM [dbo].[CoursePath]").ConfigureAwait(false);

                    foreach (var item in items)
                    {
                        await connection.ExecuteAsync("INSERT INTO [dbo].[CoursePath] ([Key], [Name], [Slug], [Version]) VALUES (@Key, @Name, @Slug, @Version)", item).ConfigureAwait(false);
                    }
                }

                // act
                var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
                var result     = await repository.GetAllAsync().ConfigureAwait(false);

                // assert
                Assert.True(items.OrderBy(x => x.Key).SequenceEqual(result.OrderBy(x => x.Key)));
            }
        }
        public async Task GetAsyncReturnsNullOnMissingItem()
        {
            // arrange
            using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var id = Guid.NewGuid();

                // act
                var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
                var result     = await repository.GetAsync(id).ConfigureAwait(false);

                // assert
                Assert.Null(result);
            }
        }
        public async Task ClearAsyncIgnoresMissingItem()
        {
            // arrange
            using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                // arrange
                var item       = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());
                var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);

                // act
                await repository.ClearAsync(item.Key, item.Version).ConfigureAwait(false);

                // assert
                Assert.True(true);
            }
        }
        public async Task SetAsyncThrowsOnExistingId()
        {
            // arrange
            using var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
            var item = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());

            await using (var connection = new SqlConnection(_database.ConnectionString))
            {
                await connection.ExecuteAsync("INSERT INTO [dbo].[CoursePath] ([Key], [Name], [Slug], [Version]) VALUES (@Key, @Name, @Slug, @Version)", item).ConfigureAwait(false);
            }

            // act
            var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
            var ex         = await Assert.ThrowsAsync <KeyAlreadyExistsException>(() => repository.SetAsync(item)).ConfigureAwait(false);

            // assert
            Assert.Equal(item.Key, ex.Key);
        }
        public async Task GetAllAsyncReturnsEmptyOnMissingItems()
        {
            // arrange
            using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    await connection.ExecuteAsync("DELETE FROM [dbo].[CoursePath]").ConfigureAwait(false);
                }

                // act
                var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
                var result     = await repository.GetAllAsync().ConfigureAwait(false);

                // assert
                Assert.NotNull(result);
                Assert.Empty(result);
            }
        }
        public async Task ClearAsyncThrowsOnVersionMismatch()
        {
            // arrange
            using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var existing = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());
                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    await connection.ExecuteAsync("INSERT INTO [dbo].[CoursePath] ([Key], [Name], [Slug], [Version]) VALUES (@Key, @Name, @Slug, @Version)", existing).ConfigureAwait(false);
                }

                // act
                var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
                var item       = existing.WithVersion(Guid.NewGuid());
                var ex         = await Assert.ThrowsAsync <ConcurrencyException>(() => repository.ClearAsync(item.Key, item.Version)).ConfigureAwait(false);

                // assert
                Assert.Equal(existing.Version, ex.StoredVersion);
                Assert.Equal(item.Version, ex.CurrentVersion);
            }
        }
        public async Task GetAsyncReturnsExistingItem()
        {
            // arrange
            using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var item = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());
                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    await connection.ExecuteAsync("INSERT INTO [dbo].[CoursePath] ([Key], [Name], [Slug], [Version]) VALUES (@Key, @Name, @Slug, @Version)", item).ConfigureAwait(false);
                }

                // act
                var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
                var result     = await repository.GetAsync(item.Key).ConfigureAwait(false);

                // assert
                Assert.Equal(item.Key, result.Key);
                Assert.Equal(item.Name, result.Name);
                Assert.Equal(item.Slug, result.Slug);
                Assert.Equal(item.Version, result.Version);
            }
        }
        public async Task SetAsyncUpdatesExistingItem()
        {
            // arrange
            using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var item = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());
                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    await connection.ExecuteAsync("INSERT INTO [dbo].[CoursePath] ([Key], [Name], [Slug], [Version]) VALUES (@Key, @Name, @Slug, @Version)", item).ConfigureAwait(false);
                }

                // act
                var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
                var current    = await repository.GetAsync(item.Key).ConfigureAwait(false);

                var proposed = new CoursePath(current.Key, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), current.Version);
                var saved    = await repository.SetAsync(proposed).ConfigureAwait(false);

                // assert
                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    var actual = await connection.QuerySingleOrDefaultAsync <CoursePath>("SELECT [Key], [Name], [Slug], [Version] FROM [dbo].[CoursePath] WHERE [Key] = @Key", new { item.Key }).ConfigureAwait(false);

                    // assert - current equals item
                    Assert.Equal(item, current);

                    // assert - actual equals saved
                    Assert.Equal(saved, actual);

                    // assert - saved equals proposed plus new version
                    Assert.Equal(proposed.Key, saved.Key);
                    Assert.Equal(proposed.Name, saved.Name);
                    Assert.Equal(proposed.Slug, saved.Slug);
                    Assert.NotEqual(proposed.Version, saved.Version);
                }
            }
        }
        public async Task ClearAsyncRemovesExistingItem()
        {
            // arrange
            using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var item = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());
                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    await connection.ExecuteAsync("INSERT INTO [dbo].[CoursePath] ([Key], [Name], [Slug], [Version]) VALUES (@Key, @Name, @Slug, @Version)", item).ConfigureAwait(false);
                }

                // act
                var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
                await repository.ClearAsync(item.Key, item.Version).ConfigureAwait(false);

                // assert
                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    var saved = await connection.QuerySingleOrDefaultAsync <CoursePath>("SELECT [Key], [Name], [Slug], [Version] FROM [dbo].[CoursePath] WHERE [Key] = @Key", new { item.Key }).ConfigureAwait(false);

                    Assert.Null(saved);
                }
            }
        }