public async Task Can_add_update_delete_end_to_end() { using (var testDatabase = CosmosSqlTestStore.CreateInitialized(DatabaseName)) { var options = Fixture.CreateOptions(testDatabase); var customer = new Customer { Id = 42, Name = "Theon" }; using (var context = new CustomerContext(options)) { await context.Database.EnsureCreatedAsync(); context.Add(customer); await context.SaveChangesAsync(); } using (var context = new CustomerContext(options)) { var customerFromStore = context.Set <Customer>().Single(); Assert.Equal(42, customerFromStore.Id); Assert.Equal("Theon", customerFromStore.Name); } using (var context = new CustomerContext(options)) { customer.Name = "Theon Greyjoy"; context.Update(customer); await context.SaveChangesAsync(); } using (var context = new CustomerContext(options)) { var customerFromStore = context.Set <Customer>().Single(); Assert.Equal(42, customerFromStore.Id); Assert.Equal("Theon Greyjoy", customerFromStore.Name); } using (var context = new CustomerContext(options)) { context.Remove(customer); await context.SaveChangesAsync(); } using (var context = new CustomerContext(options)) { Assert.Equal(0, context.Set <Customer>().Count()); } } }
public async Task Can_add_update_delete_end_to_end_with_conflicting_id() { using (var testDatabase = CosmosSqlTestStore.CreateInitialized(DatabaseName)) { var options = Fixture.CreateOptions(testDatabase); var entity = new ConflictingId { id = "42", Name = "Theon" }; using (var context = new ConflictingIdContext(options)) { await context.Database.EnsureCreatedAsync(); context.Add(entity); await context.SaveChangesAsync(); } using (var context = new ConflictingIdContext(options)) { var entityFromStore = context.Set <ConflictingId>().Single(); Assert.Equal("42", entityFromStore.id); Assert.Equal("Theon", entityFromStore.Name); } using (var context = new ConflictingIdContext(options)) { entity.Name = "Theon Greyjoy"; context.Update(entity); await context.SaveChangesAsync(); } using (var context = new ConflictingIdContext(options)) { var entityFromStore = context.Set <ConflictingId>().Single(); Assert.Equal("42", entityFromStore.id); Assert.Equal("Theon Greyjoy", entityFromStore.Name); } using (var context = new ConflictingIdContext(options)) { context.Remove(entity); await context.SaveChangesAsync(); } using (var context = new ConflictingIdContext(options)) { Assert.Equal(0, context.Set <ConflictingId>().Count()); } } }
//[InlineData(false)] public async Task EnsureDeleted_returns_false_when_database_does_not_exist(bool async) { using (var testDatabase = CosmosSqlTestStore.Create("EnsureDeleteBlogging")) { using (var context = new BloggingContext(testDatabase)) { var creator = context.GetService <IDatabaseCreator>(); Assert.False(async ? await creator.EnsureDeletedAsync() : creator.EnsureDeleted()); } } }
//[InlineData(false)] public async Task EnsureCreated_returns_true_when_database_exists_but_collections_does_not(bool async) { using (var testDatabase = CosmosSqlTestStore.CreateInitialized("EnsureCreatedTest")) { using (var context = new BloggingContext(testDatabase)) { var creator = context.GetService <IDatabaseCreator>(); Assert.True(async ? await creator.EnsureCreatedAsync() : creator.EnsureCreated()); } } }
//[InlineData(false)] public async Task EnsureCreated_returns_false_when_database_and_collections_exists(bool async) { using (var testDatabase = CosmosSqlTestStore.Create("EnsureCreatedReady")) { testDatabase.Initialize(null, () => new BloggingContext(testDatabase), null); using (var context = new BloggingContext(testDatabase)) { var creator = context.GetService <IDatabaseCreator>(); Assert.False(async ? await creator.EnsureCreatedAsync() : creator.EnsureCreated()); } } }
public void Can_update_unmapped_properties() { using (var testDatabase = CosmosSqlTestStore.CreateInitialized(DatabaseName)) { var options = Fixture.CreateOptions(testDatabase); var customer = new Customer { Id = 42, Name = "Theon" }; using (var context = new ExtraCustomerContext(options)) { context.Database.EnsureCreated(); var entry = context.Add(customer); entry.Property <string>("EMail").CurrentValue = "*****@*****.**"; context.SaveChanges(); } using (var context = new CustomerContext(options)) { var customerFromStore = context.Set <Customer>().Single(); Assert.Equal(42, customerFromStore.Id); Assert.Equal("Theon", customerFromStore.Name); customerFromStore.Name = "Theon Greyjoy"; context.SaveChanges(); } using (var context = new ExtraCustomerContext(options)) { var customerFromStore = context.Set <Customer>().Single(); Assert.Equal(42, customerFromStore.Id); Assert.Equal("Theon Greyjoy", customerFromStore.Name); var entry = context.Entry(customerFromStore); Assert.Equal("*****@*****.**", entry.Property <string>("EMail").CurrentValue); context.Remove(customerFromStore); context.SaveChanges(); } } }
public async Task Using_a_conflicting_incompatible_id_throws() { using (var testDatabase = CosmosSqlTestStore.CreateInitialized(DatabaseName)) { var options = Fixture.CreateOptions(testDatabase); using (var context = new ConflictingIncompatibleIdContext(options)) { await Assert.ThrowsAnyAsync <Exception>(async() => { await context.Database.EnsureCreatedAsync(); context.Add(new ConflictingIncompatibleId { id = 42 }); await context.SaveChangesAsync(); }); } } }
public async Task Can_add_update_delete_detached_entity_end_to_end_async() { using (var testDatabase = CosmosSqlTestStore.CreateInitialized(DatabaseName)) { var options = Fixture.CreateOptions(testDatabase); var customer = new Customer { Id = 42, Name = "Theon" }; string storeId = null; using (var context = new CustomerContext(options)) { await context.Database.EnsureCreatedAsync(); var entry = context.Add(customer); await context.SaveChangesAsync(); context.Add(customer); storeId = entry.Property <string>("id").CurrentValue; } Assert.NotNull(storeId); using (var context = new CustomerContext(options)) { var customerFromStore = context.Set <Customer>().Single(); Assert.Equal(42, customerFromStore.Id); Assert.Equal("Theon", customerFromStore.Name); } using (var context = new CustomerContext(options)) { customer.Name = "Theon Greyjoy"; var entry = context.Entry(customer); entry.Property <string>("id").CurrentValue = storeId; entry.State = EntityState.Modified; await context.SaveChangesAsync(); } using (var context = new CustomerContext(options)) { var customerFromStore = context.Set <Customer>().Single(); Assert.Equal(42, customerFromStore.Id); Assert.Equal("Theon Greyjoy", customerFromStore.Name); } using (var context = new CustomerContext(options)) { var entry = context.Entry(customer); entry.Property <string>("id").CurrentValue = storeId; entry.State = EntityState.Deleted; await context.SaveChangesAsync(); } using (var context = new CustomerContext(options)) { Assert.Equal(0, context.Set <Customer>().Count()); } } }
public BloggingContext(CosmosSqlTestStore testStore) { _connectionUri = testStore.ConnectionUri; _authToken = testStore.AuthToken; _name = testStore.Name; }