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 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); } } }