public void 操作真實資料庫_預設EmployeeDbContext() { //arrange DefaultDbContextManager.Now = new DateTime(1900, 1, 1); DefaultDbContextManager.SetPhysicalDatabase <EmployeeDbContext>(); var builder = Host.CreateDefaultBuilder() .ConfigureServices(services => { services.AddSingleton <EmployeeRepository>(); }); var host = builder.Build(); var repository = host.Services.GetService <EmployeeRepository>(); //act var count = repository.NewAsync(new NewRequest { Account = "yao", Password = "******", Name = "余小章", Age = 18, }, "TestUser").Result; //assert Assert.AreEqual(2, count); using var db = new TestEmployeeDbContext(TestDbConnectionString1); var actual = db.Employees .Include(p => p.Identity) .AsNoTracking() .FirstOrDefault(); Assert.AreEqual(actual.Name, "余小章"); Assert.AreEqual(actual.Age, 18); Assert.AreEqual(actual.Identity.Account, "yao"); Assert.AreEqual(actual.Identity.Password, "123456"); }
public static void AssemblyCleanup() { //刪除測試資料庫 Console.WriteLine("AssemblyCleanup"); using var db = new TestEmployeeDbContext(TestDbConnectionString); db.Database.EnsureDeleted(); }
private static void DeleteTestDataRow() { using var db = new TestEmployeeDbContext(TestDbConnectionString); if (db.Database.CanConnect() == false) { return; } var deleteCommand = GetDeleteAllRecordCommand(); db.Database.ExecuteSqlRaw(deleteCommand); }
public static void AssemblyInitialize(TestContext context) { //刪除測試資料庫 Console.WriteLine("AssemblyInitialize"); using var db1 = new TestEmployeeDbContext(TestDbConnectionString1); db1.Database.EnsureDeleted(); using var db2 = new TestEmployeeDbContext(TestDbConnectionString2); db2.Database.EnsureDeleted(); // //建立測試資料庫 // db.Database.Migrate(); }
public void 操作真實資料庫_手動取得Repository執行個體() { //arrange DefaultDbContextManager.Now = new DateTime(1900, 1, 1); DefaultDbContextManager.SetPhysicalDatabase <EmployeeDbContext>(); var repository = new EmployeeRepository(); // var id = Guid.NewGuid(); repository.NewAsync(new NewRequest { Account = "yao", Password = "******", Name = "余小章", Age = 18, Remark = "測試案例,持續航向偉大航道" }, "TestUser").Wait(); using var db = new EmployeeDbContext(s_employeeContextOptions); var id = db.Employees.FirstOrDefault(p => p.Name == "余小章").Id; //act var count = repository.InsertLogAsync(new InsertOrderRequest { Employee_Id = id, Product_Id = "A001", Product_Name = "羅技滑鼠", Remark = "測試案例,持續航向偉大航道" }, "TestUser").Result; //assert Assert.AreEqual(1, count); using var db1 = new TestEmployeeDbContext(TestDbConnectionString); var actual = db1.OrderHistories .AsNoTracking() .FirstOrDefault(); Assert.AreEqual("A001", actual.Product_Id); Assert.AreEqual("羅技滑鼠", actual.Product_Name); }