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 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 SetAsyncThrowsOnExistingSlug() { // arrange using var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled); var existing = 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)", existing).ConfigureAwait(false); } // act var item = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), existing.Slug, Guid.NewGuid()); var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions); var ex = await Assert.ThrowsAsync <SlugAlreadyExistsException>(() => repository.SetAsync(item)).ConfigureAwait(false); // assert Assert.Equal(existing.Slug, ex.Slug); }