private static async Task Create_creates_physical_database_but_not_tables_test(bool async) { using (var testDatabase = await TestDatabase.Scratch(createDatabase: false)) { var creator = GetDataStoreCreator(testDatabase); Assert.False(creator.Exists()); if (async) { await creator.CreateAsync(); } else { creator.Create(); } Assert.True(creator.Exists()); if (testDatabase.Connection.State != ConnectionState.Open) { await testDatabase.Connection.OpenAsync(); } Assert.Equal(0, (await testDatabase.QueryAsync <string>("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES")).Count()); } }
private static async Task EnsureCreated_can_create_physical_database_and_schema_test(bool async) { using (var testDatabase = await TestDatabase.Scratch(createDatabase: false)) { await RunDatabaseCreationTest(testDatabase, async); } }
private static async Task EnsuredDeleted_noop_when_database_doesnt_exist_test(bool async) { using (var testDatabase = await TestDatabase.Scratch(createDatabase: false)) { using (var context = new BloggingContext(testDatabase)) { Assert.False(async ? await context.Database.AsRelational().ExistsAsync() : context.Database.AsRelational().Exists()); if (async) { Assert.False(await context.Database.AsRelational().EnsureDeletedAsync()); } else { Assert.False(context.Database.AsRelational().EnsureDeleted()); } Assert.Equal(ConnectionState.Closed, context.Database.AsRelational().Connection.DbConnection.State); Assert.False(async ? await context.Database.AsRelational().ExistsAsync() : context.Database.AsRelational().Exists()); Assert.Equal(ConnectionState.Closed, context.Database.AsRelational().Connection.DbConnection.State); } } }
private static async Task EnsureDeleted_will_delete_database_test(bool async) { using (var testDatabase = await TestDatabase.Scratch(createDatabase: true)) { testDatabase.Connection.Close(); using (var context = new BloggingContext(testDatabase)) { Assert.True(async ? await context.Database.ExistsAsync() : context.Database.Exists()); if (async) { Assert.True(await context.Database.EnsureDeletedAsync()); } else { Assert.True(context.Database.EnsureDeleted()); } Assert.Equal(ConnectionState.Closed, ((RelationalConnection)context.Database.Connection).DbConnection.State); Assert.False(async ? await context.Database.ExistsAsync() : context.Database.Exists()); Assert.Equal(ConnectionState.Closed, ((RelationalConnection)context.Database.Connection).DbConnection.State); } } }
private static async Task EnsureCreated_can_create_schema_in_existing_database_test(bool async) { using (var testDatabase = await TestDatabase.Scratch()) { await RunDatabaseCreationTest(testDatabase, async); } }
private static async Task Exists_returns_true_when_database_exists_test(bool async) { using (var testDatabase = await TestDatabase.Scratch(createDatabase: true)) { var creator = GetDataStoreCreator(testDatabase); Assert.True(async ? await creator.ExistsAsync() : creator.Exists()); } }
private static async Task HasTables_returns_false_when_database_exists_but_has_no_tables_test(bool async) { using (var testDatabase = await TestDatabase.Scratch(createDatabase: true)) { var creator = GetDataStoreCreator(testDatabase); Assert.False(async ? await creator.HasTablesAsync() : creator.HasTables()); } }
private static async Task HasTables_returns_true_when_database_exists_and_has_any_tables_test(bool async) { using (var testDatabase = await TestDatabase.Scratch(createDatabase: true)) { await testDatabase.ExecuteNonQueryAsync("CREATE TABLE SomeTable (Id uniqueidentifier)"); var creator = GetDataStoreCreator(testDatabase); Assert.True(async ? await creator.HasTablesAsync() : creator.HasTables()); } }
private static async Task Exists_returns_true_when_database_exists_test(bool async) { using (var testDatabase = await TestDatabase.Scratch(createDatabase: true)) { using (var context = new BloggingContext(testDatabase)) { Assert.True(async ? await context.Database.ExistsAsync() : context.Database.Exists()); Assert.Equal(ConnectionState.Closed, ((RelationalConnection)context.Database.Connection).DbConnection.State); } } }
private static async Task Delete_throws_when_database_doesnt_exist_test(bool async) { using (var testDatabase = await TestDatabase.Scratch(createDatabase: false)) { var creator = GetDataStoreCreator(testDatabase); Assert.Equal( 3701, // Database does not exist async ? (await Assert.ThrowsAsync <SqlException>(() => creator.DeleteAsync())).Number : Assert.Throws <SqlException>(() => creator.Delete()).Number); } }
private static async Task HasTables_throws_when_database_doesnt_exist_test(bool async) { using (var testDatabase = await TestDatabase.Scratch(createDatabase: false)) { var creator = GetDataStoreCreator(testDatabase); Assert.Equal( 4060, // Login failed error number async ? (await Assert.ThrowsAsync <SqlException>(() => creator.HasTablesAsync())).Number : Assert.Throws <SqlException>(() => creator.HasTables()).Number); } }
private static async Task Create_throws_if_database_already_exists_test(bool async) { using (var testDatabase = await TestDatabase.Scratch(createDatabase: true)) { var creator = GetDataStoreCreator(testDatabase); Assert.Equal( 1801, // Database with given name already exists async ? (await Assert.ThrowsAsync <SqlException>(() => creator.CreateAsync())).Number : Assert.Throws <SqlException>(() => creator.Create()).Number); } }
private static async Task CreateTables_creates_schema_in_existing_database_test(bool async) { using (var testDatabase = await TestDatabase.Scratch()) { var serviceCollection = new ServiceCollection(); serviceCollection.AddEntityFramework().AddSqlServer(); var serviceProvider = serviceCollection.BuildServiceProvider(); var configuration = new DbContextOptions() .UseSqlServer(testDatabase.Connection.ConnectionString) .BuildConfiguration(); using (var context = new BloggingContext(serviceProvider, configuration)) { var creator = context.Configuration.DataStoreCreator; if (async) { await creator.CreateTablesAsync(context.Model); } else { creator.CreateTables(context.Model); } if (testDatabase.Connection.State != ConnectionState.Open) { await testDatabase.Connection.OpenAsync(); } var tables = await testDatabase.QueryAsync <string>("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES"); Assert.Equal(1, tables.Count()); Assert.Equal("Blog", tables.Single()); var columns = await testDatabase.QueryAsync <string>("SELECT TABLE_NAME + '.' + COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS"); Assert.Equal(2, columns.Count()); Assert.True(columns.Any(c => c == "Blog.Id")); Assert.True(columns.Any(c => c == "Blog.Name")); } } }
private static async Task EnsuredCreated_is_noop_when_database_exists_and_has_schema_test(bool async) { using (var testDatabase = await TestDatabase.Scratch(createDatabase: false)) { using (var context = new BloggingContext(testDatabase)) { context.Database.EnsureCreated(); if (async) { Assert.False(await context.Database.EnsureCreatedAsync()); } else { Assert.False(context.Database.EnsureCreated()); } Assert.Equal(ConnectionState.Closed, ((RelationalConnection)context.Database.Connection).DbConnection.State); } } }
private static async Task Delete_will_delete_database_test(bool async) { using (var testDatabase = await TestDatabase.Scratch(createDatabase: true)) { testDatabase.Connection.Close(); var creator = GetDataStoreCreator(testDatabase); Assert.True(async ? await creator.ExistsAsync() : creator.Exists()); if (async) { await creator.DeleteAsync(); } else { creator.Delete(); } Assert.False(async ? await creator.ExistsAsync() : creator.Exists()); } }
private async Task RoundTripChanges <TBlog>() where TBlog : class, IBlog, new() { using (var testDatabase = await TestDatabase.Scratch()) { var options = new DbContextOptions().UseSqlServer(testDatabase.Connection.ConnectionString); int blog1Id; int blog2Id; int blog3Id; using (var context = new BloggingContext <TBlog>(options)) { var blogs = await CreateBlogDatabase <TBlog>(context); blog1Id = blogs[0].Id; blog2Id = blogs[1].Id; Assert.NotEqual(0, blog1Id); Assert.NotEqual(0, blog2Id); Assert.NotEqual(blog1Id, blog2Id); } using (var context = new BloggingContext <TBlog>(options)) { var blogs = context.Blogs.ToList(); Assert.Equal(2, blogs.Count); var blog1 = blogs.Single(b => b.Name == "Blog1"); Assert.Equal(blog1Id, blog1.Id); Assert.Equal("Blog1", blog1.Name); Assert.True(blog1.George); Assert.Equal(new Guid("0456AEF1-B7FC-47AA-8102-975D6BA3A9BF"), blog1.TheGu); Assert.Equal(new DateTime(1973, 9, 3, 0, 10, 33, 777), blog1.NotFigTime); Assert.Equal(64, blog1.ToEat); Assert.Equal(0.123456789, blog1.OrNothing); Assert.Equal(777, blog1.Fuse); Assert.Equal(9876543210, blog1.WayRound); Assert.Equal(0.12345f, blog1.Away); Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, blog1.AndChew); blog1.Name = "New Name"; var blog2 = blogs.Single(b => b.Name == "Blog2"); Assert.Equal(blog2Id, blog2.Id); blog2.Name = null; blog2.NotFigTime = new DateTime(); blog2.AndChew = null; var blog3 = context.Add(new TBlog()); await context.SaveChangesAsync(); blog3Id = blog3.Id; Assert.NotEqual(0, blog3Id); } using (var context = new BloggingContext <TBlog>(options)) { var blogs = context.Blogs.ToList(); Assert.Equal(3, blogs.Count); Assert.Equal("New Name", blogs.Single(b => b.Id == blog1Id).Name); var blog2 = blogs.Single(b => b.Id == blog2Id); Assert.Null(blog2.Name); Assert.Equal(blog2.NotFigTime, new DateTime()); Assert.Null(blog2.AndChew); var blog3 = blogs.Single(b => b.Id == blog3Id); Assert.Null(blog3.Name); Assert.Equal(blog3.NotFigTime, new DateTime()); Assert.Null(blog3.AndChew); } } }
public async Task Can_save_changes() { using (var testDatabase = await TestDatabase.Scratch()) { var options = new DbContextOptions().UseSqlServer(testDatabase.Connection.ConnectionString); using (var db = new BloggingContext(options)) { await CreateBlogDatabase <Blog>(db); } using (var db = new BloggingContext(options)) { var toUpdate = db.Blogs.Single(b => b.Name == "Blog1"); toUpdate.Name = "Blog is Updated"; var toDelete = db.Blogs.Single(b => b.Name == "Blog2"); toDelete.Name = "Blog to delete"; db.ChangeTracker.Entry(toUpdate).State = EntityState.Modified; db.ChangeTracker.Entry(toDelete).State = EntityState.Deleted; var toAdd = db.Blogs.Add(new Blog() { Name = "Blog to Insert", George = true, TheGu = new Guid("0456AEF1-B7FC-47AA-8102-975D6BA3A9BF"), NotFigTime = new DateTime(1973, 9, 3, 0, 10, 33, 777), ToEat = 64, OrNothing = 0.123456789, Fuse = 777, WayRound = 9876543210, Away = 0.12345f, AndChew = new byte[16] }); await db.SaveChangesAsync(); Assert.NotEqual(0, toAdd.Id); Assert.Equal(EntityState.Unchanged, db.ChangeTracker.Entry(toUpdate).State); Assert.Equal(EntityState.Unchanged, db.ChangeTracker.Entry(toAdd).State); Assert.DoesNotContain(toDelete, db.ChangeTracker.Entries().Select(e => e.Entity)); var rows = await testDatabase.ExecuteScalarAsync <int>( @"SELECT Count(*) FROM [dbo].[Blog] WHERE Id = 1 AND Name = 'Blog is Updated'", CancellationToken.None); Assert.Equal(1, rows); rows = await testDatabase.ExecuteScalarAsync <int>( @"SELECT Count(*) FROM [dbo].[Blog] WHERE Id = 2", CancellationToken.None); Assert.Equal(0, rows); rows = await testDatabase.ExecuteScalarAsync <int>( @"SELECT Count(*) FROM [dbo].[Blog] WHERE Id = 3 AND Name = 'Blog to Insert'", CancellationToken.None); Assert.Equal(1, rows); } } }